Skip to content

Commit

Permalink
rustc: filter out empty linker args
Browse files Browse the repository at this point in the history
This is inspired by a mystifying linker failure when using `pkg-config` to
generate the linker args: `pkg-config` produces output that ends in a
space, thus resulting in an empty linker argument.

Also added some updates to the concerning error messages that helped
spotting this bug.
  • Loading branch information
Blei committed Dec 1, 2013
1 parent df41115 commit 32688f8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,12 @@ pub mod write {
~"-o", object.as_str().unwrap().to_owned(),
assembly.as_str().unwrap().to_owned()];

debug!("{} {}", cc, args.connect(" "));
debug!("{} '{}'", cc, args.connect("' '"));
let prog = run::process_output(cc, args);

if !prog.status.success() {
sess.err(format!("linking with `{}` failed: {}", cc, prog.status));
sess.note(format!("{} arguments: {}", cc, args.connect(" ")));
sess.note(format!("{} arguments: '{}'", cc, args.connect("' '")));
sess.note(str::from_utf8(prog.error + prog.output));
sess.abort_if_errors();
}
Expand Down Expand Up @@ -1061,7 +1061,7 @@ fn link_natively(sess: Session, dylib: bool, obj_filename: &Path,
let mut cc_args = sess.targ_cfg.target_strs.cc_args.clone();
cc_args.push_all_move(link_args(sess, dylib, obj_filename, out_filename));
if (sess.opts.debugging_opts & session::print_link_args) != 0 {
println!("{} link args: {}", cc_prog, cc_args.connect(" "));
println!("{} link args: '{}'", cc_prog, cc_args.connect("' '"));
}

// May have not found libraries in the right formats.
Expand All @@ -1073,7 +1073,7 @@ fn link_natively(sess: Session, dylib: bool, obj_filename: &Path,

if !prog.status.success() {
sess.err(format!("linking with `{}` failed: {}", cc_prog, prog.status));
sess.note(format!("{} arguments: {}", cc_prog, cc_args.connect(" ")));
sess.note(format!("{} arguments: '{}'", cc_prog, cc_args.connect("' '")));
sess.note(str::from_utf8(prog.error + prog.output));
sess.abort_if_errors();
}
Expand Down
8 changes: 7 additions & 1 deletion src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,13 @@ pub fn build_session_options(binary: @str,
let ar = matches.opt_str("ar");
let linker = matches.opt_str("linker");
let linker_args = matches.opt_strs("link-args").flat_map( |a| {
a.split(' ').map(|arg| arg.to_owned()).collect()
a.split(' ').filter_map(|arg| {
if arg.is_empty() {
None
} else {
Some(arg.to_owned())
}
}).collect()
});

let cfg = parse_cfgspecs(matches.opt_strs("cfg"), demitter);
Expand Down
6 changes: 6 additions & 0 deletions src/test/run-make/prune-link-args/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-include ../tools.mk
# Notice the space in the end, this emulates the output of pkg-config
RUSTC_FLAGS = --link-args "-lc "

all:
$(RUSTC) $(RUSTC_FLAGS) empty.rs
1 change: 1 addition & 0 deletions src/test/run-make/prune-link-args/empty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() { }

5 comments on commit 32688f8

@bors
Copy link
Contributor

@bors bors commented on 32688f8 Dec 1, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 32688f8 Dec 1, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Blei/rust/fix-linker-args = 32688f8 into auto

@bors
Copy link
Contributor

@bors bors commented on 32688f8 Dec 1, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blei/rust/fix-linker-args = 32688f8 merged ok, testing candidate = 9ac4878

@bors
Copy link
Contributor

@bors bors commented on 32688f8 Dec 1, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 32688f8 Dec 1, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 9ac4878

Please sign in to comment.