Skip to content

Commit 55205ef

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 37a4555 commit 55205ef

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
@@ -223,6 +223,7 @@ impl TargetConfig {
223223
let mut output = BuildOutput {
224224
library_paths: Vec::new(),
225225
library_links: Vec::new(),
226+
linker_args: Vec::new(),
226227
cfgs: Vec::new(),
227228
env: Vec::new(),
228229
metadata: Vec::new(),
@@ -258,6 +259,12 @@ impl TargetConfig {
258259
.library_paths
259260
.extend(list.iter().map(|v| PathBuf::from(&v.0)));
260261
}
262+
"rustc-link-arg" => {
263+
let args = value.list(k)?;
264+
output
265+
.linker_args
266+
.extend(args.iter().map(|v| v.0.clone()));
267+
}
261268
"rustc-cfg" => {
262269
let list = value.list(k)?;
263270
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.
@@ -437,6 +439,7 @@ impl BuildOutput {
437439
) -> CargoResult<BuildOutput> {
438440
let mut library_paths = Vec::new();
439441
let mut library_links = Vec::new();
442+
let mut linker_args = Vec::new();
440443
let mut cfgs = Vec::new();
441444
let mut env = Vec::new();
442445
let mut metadata = Vec::new();
@@ -484,6 +487,7 @@ impl BuildOutput {
484487
}
485488
"rustc-link-lib" => library_links.push(value.to_string()),
486489
"rustc-link-search" => library_paths.push(PathBuf::from(value)),
490+
"rustc-link-arg" => linker_args.push(value.to_string()),
487491
"rustc-cfg" => cfgs.push(value.to_string()),
488492
"rustc-env" => env.push(BuildOutput::parse_rustc_env(&value, &whence)?),
489493
"warning" => warnings.push(value.to_string()),
@@ -496,6 +500,7 @@ impl BuildOutput {
496500
Ok(BuildOutput {
497501
library_paths,
498502
library_links,
503+
linker_args,
499504
cfgs,
500505
env,
501506
metadata,

src/cargo/core/compiler/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ fn rustc<'a, 'cfg>(
367367
rustc.arg("-l").arg(name);
368368
}
369369
}
370+
for arg in output.linker_args.iter() {
371+
let link_arg = format!("link-arg={}", arg);
372+
rustc.arg("-C").arg(link_arg);
373+
}
370374
}
371375
}
372376
Ok(())

0 commit comments

Comments
 (0)