Skip to content

Commit c816720

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#36574 - japaric:link-arg, r=alexcrichton
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 r? @alexcrichton cc @Zoxc This needs a test. Any suggestion?
2 parents e3ffde8 + 2f71fa7 commit c816720

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-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
@@ -754,7 +754,8 @@ fn link_args(cmd: &mut Linker,
754754
let empty_vec = Vec::new();
755755
let empty_str = String::new();
756756
let args = sess.opts.cg.link_args.as_ref().unwrap_or(&empty_vec);
757-
let mut args = args.iter().chain(used_link_args.iter());
757+
let more_args = &sess.opts.cg.link_arg;
758+
let mut args = args.iter().chain(more_args.iter()).chain(used_link_args.iter());
758759
let relocation_model = sess.opts.cg.relocation_model.as_ref()
759760
.unwrap_or(&empty_str);
760761
if (t.options.relocation_model == "pic" || *relocation_model == "pic")
@@ -844,6 +845,7 @@ fn link_args(cmd: &mut Linker,
844845
if let Some(ref args) = sess.opts.cg.link_args {
845846
cmd.args(args);
846847
}
848+
cmd.args(&sess.opts.cg.link_arg);
847849
cmd.args(&used_link_args);
848850
}
849851

src/test/run-make/link-arg/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
RUSTC_FLAGS = -C link-arg="-lfoo" -C link-arg="-lbar" -Z print-link-args
3+
4+
all:
5+
$(RUSTC) $(RUSTC_FLAGS) empty.rs | grep lfoo | grep lbar

src/test/run-make/link-arg/empty.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() { }

0 commit comments

Comments
 (0)