Skip to content

Commit 9631e9f

Browse files
author
Jorge Aparicio
committed
rustc: implement -C link-arg
this flag lets you pass a _single_ argument to the linker but can be used _repeatedly_. For example, instead of using: ``` rustc -C link-args='-l bar' (..) ``` you could write ``` rustc -C link-arg='-l' -C link-arg='bar' (..) ``` This new flag can be used with RUSTFLAGS where `-C link-args` has problems with "nested" spaces: ``` RUSTFLAGS='-C link-args="-Tlayout.ld -nostartfiles"' ``` This passes three arguments to rustc: `-C` `link-args="-Tlayout.ld` and `-nostartfiles"` to `rustc`. That's not what we meant. But this does what we want: ``` RUSTFLAGS='-C link-arg=-Tlayout.ld -C link-arg=-nostartfiles` ``` cc rust-lang/rfcs#1509
1 parent fb62f4d commit 9631e9f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/librustc/session/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ macro_rules! options {
606606
pub const parse_opt_bool: Option<&'static str> =
607607
Some("one of: `y`, `yes`, `on`, `n`, `no`, or `off`");
608608
pub const parse_string: Option<&'static str> = Some("a string");
609+
pub const parse_string_push: Option<&'static str> = Some("a string");
609610
pub const parse_opt_string: Option<&'static str> = Some("a string");
610611
pub const parse_list: Option<&'static str> = Some("a space-separated list of strings");
611612
pub const parse_opt_list: Option<&'static str> = Some("a space-separated list of strings");
@@ -668,6 +669,13 @@ macro_rules! options {
668669
}
669670
}
670671

672+
fn parse_string_push(slot: &mut Vec<String>, v: Option<&str>) -> bool {
673+
match v {
674+
Some(s) => { slot.push(s.to_string()); true },
675+
None => false,
676+
}
677+
}
678+
671679
fn parse_list(slot: &mut Vec<String>, v: Option<&str>)
672680
-> bool {
673681
match v {
@@ -743,6 +751,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
743751
"tool to assemble archives with"),
744752
linker: Option<String> = (None, parse_opt_string, [UNTRACKED],
745753
"system linker to link outputs with"),
754+
link_arg: Vec<String> = (vec![], parse_string_push, [UNTRACKED],
755+
"a single extra argument to pass to the linker (can be used several times)"),
746756
link_args: Option<Vec<String>> = (None, parse_opt_list, [UNTRACKED],
747757
"extra arguments to pass to the linker (space separated)"),
748758
link_dead_code: bool = (false, parse_bool, [UNTRACKED],

src/librustc_trans/back/link.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,8 @@ fn link_args(cmd: &mut Linker,
753753
let empty_vec = Vec::new();
754754
let empty_str = String::new();
755755
let args = sess.opts.cg.link_args.as_ref().unwrap_or(&empty_vec);
756-
let mut args = args.iter().chain(used_link_args.iter());
756+
let more_args = &sess.opts.cg.link_arg;
757+
let mut args = args.iter().chain(more_args.iter()).chain(used_link_args.iter());
757758
let relocation_model = sess.opts.cg.relocation_model.as_ref()
758759
.unwrap_or(&empty_str);
759760
if (t.options.relocation_model == "pic" || *relocation_model == "pic")
@@ -843,6 +844,7 @@ fn link_args(cmd: &mut Linker,
843844
if let Some(ref args) = sess.opts.cg.link_args {
844845
cmd.args(args);
845846
}
847+
cmd.args(&sess.opts.cg.link_arg);
846848
cmd.args(&used_link_args);
847849
}
848850

0 commit comments

Comments
 (0)