Skip to content

Commit d99d7e0

Browse files
committed
Add cargo:rustc-link-arg to pass custom linker arguments
It is useful to produce correct cdylibs on platforms such as Linux and MacOS.
1 parent 53e436d commit d99d7e0

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/cargo/core/compiler/build_context/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ impl TargetConfig {
227227
let mut output = BuildOutput {
228228
library_paths: Vec::new(),
229229
library_links: Vec::new(),
230+
linker_args: Vec::new(),
230231
cfgs: Vec::new(),
231232
env: Vec::new(),
232233
metadata: Vec::new(),
@@ -262,6 +263,12 @@ impl TargetConfig {
262263
.library_paths
263264
.extend(list.iter().map(|v| PathBuf::from(&v.0)));
264265
}
266+
"rustc-link-arg" => {
267+
let args = value.list(k)?;
268+
output
269+
.linker_args
270+
.extend(args.iter().map(|v| v.0.clone()));
271+
}
265272
"rustc-cfg" => {
266273
let list = value.list(k)?;
267274
output.cfgs.extend(list.iter().map(|v| v.0.clone()));

src/cargo/core/compiler/custom_build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub struct BuildOutput {
2121
pub library_paths: Vec<PathBuf>,
2222
/// Names and link kinds of libraries, suitable for the `-l` flag
2323
pub library_links: Vec<String>,
24+
/// Linker arguments suitable to be passed to `-C link-arg=<args>`
25+
pub linker_args: Vec<String>,
2426
/// Various `--cfg` flags to pass to the compiler
2527
pub cfgs: Vec<String>,
2628
/// Additional environment variables to run the compiler with.
@@ -425,6 +427,7 @@ impl BuildOutput {
425427
) -> CargoResult<BuildOutput> {
426428
let mut library_paths = Vec::new();
427429
let mut library_links = Vec::new();
430+
let mut linker_args = Vec::new();
428431
let mut cfgs = Vec::new();
429432
let mut env = Vec::new();
430433
let mut metadata = Vec::new();
@@ -472,6 +475,7 @@ impl BuildOutput {
472475
}
473476
"rustc-link-lib" => library_links.push(value.to_string()),
474477
"rustc-link-search" => library_paths.push(PathBuf::from(value)),
478+
"rustc-link-arg" => linker_args.push(value.to_string()),
475479
"rustc-cfg" => cfgs.push(value.to_string()),
476480
"rustc-env" => env.push(BuildOutput::parse_rustc_env(&value, &whence)?),
477481
"warning" => warnings.push(value.to_string()),
@@ -484,6 +488,7 @@ impl BuildOutput {
484488
Ok(BuildOutput {
485489
library_paths,
486490
library_links,
491+
linker_args,
487492
cfgs,
488493
env,
489494
metadata,

src/cargo/core/compiler/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ fn rustc<'a, 'cfg>(
365365
rustc.arg("-l").arg(name);
366366
}
367367
}
368+
for arg in output.linker_args.iter() {
369+
let link_arg = format!("link-arg={}", arg);
370+
rustc.arg("-C").arg(link_arg);
371+
}
368372
}
369373
}
370374
Ok(())

0 commit comments

Comments
 (0)