diff --git a/src/lib.rs b/src/lib.rs index 7d19e135e..1d9a5fb65 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,6 +92,8 @@ pub struct Config { cargo_metadata: bool, pic: Option, static_crt: Option, + shared_flag: Option, + static_flag: Option, } /// Configuration used to represent an invocation of a C compiler. @@ -185,6 +187,8 @@ impl Config { objects: Vec::new(), flags: Vec::new(), files: Vec::new(), + shared_flag: None, + static_flag: None, cpp: false, cpp_link_stdlib: None, cpp_set_stdlib: None, @@ -226,6 +230,24 @@ impl Config { self } + /// Set the `-shared` flag. + /// + /// When enabled, the compiler will produce a shared object which can + /// then be linked with other objects to form an executable. + pub fn shared_flag(&mut self, shared_flag: bool) -> &mut Config { + self.shared_flag = Some(shared_flag); + self + } + + /// Set the `-static` flag. + /// + /// When enabled on systems that support dynamic linking, this prevents + /// linking with the shared libraries. + pub fn static_flag(&mut self, static_flag: bool) -> &mut Config { + self.static_flag = Some(static_flag); + self + } + /// Add a file which will be compiled pub fn file>(&mut self, p: P) -> &mut Config { self.files.push(p.as_ref().to_path_buf()); @@ -618,7 +640,7 @@ impl Config { cmd.args.push("-m64".into()); } - if target.contains("musl") { + if self.static_flag.is_none() && target.contains("musl") { cmd.args.push("-static".into()); } @@ -698,6 +720,13 @@ impl Config { self.ios_flags(&mut cmd); } + if self.static_flag.unwrap_or(false) { + cmd.args.push("-static".into()); + } + if self.shared_flag.unwrap_or(false) { + cmd.args.push("-shared".into()); + } + if self.cpp { match (self.cpp_set_stdlib.as_ref(), cmd.family) { (None, _) => { } diff --git a/tests/test.rs b/tests/test.rs index 8fda3ed64..2be5ecdba 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -169,6 +169,34 @@ fn gnu_compile_assembly() { test.cmd(0).must_have("foo.S"); } +#[test] +fn gnu_shared() { + let test = Test::gnu(); + test.gcc() + .file("foo.c") + .shared_flag(true) + .static_flag(false) + .compile("libfoo.a"); + + test.cmd(0) + .must_have("-shared") + .must_not_have("-static"); +} + +#[test] +fn gnu_static() { + let test = Test::gnu(); + test.gcc() + .file("foo.c") + .shared_flag(false) + .static_flag(true) + .compile("libfoo.a"); + + test.cmd(0) + .must_have("-static") + .must_not_have("-shared"); +} + #[test] fn msvc_smoke() { let test = Test::msvc();