From 00ede34fcb4d6de11e49360964565c8e487a678e Mon Sep 17 00:00:00 2001 From: James Miller Date: Mon, 29 Apr 2013 12:57:49 +1200 Subject: [PATCH] Add `--linker` option to pass flags to the linker --- src/librustc/back/link.rs | 3 + src/librustc/driver/driver.rs | 118 ++++++++++++++++++--------------- src/librustc/driver/session.rs | 2 + 3 files changed, 70 insertions(+), 53 deletions(-) diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index 8c442f2d5c9f..aaa425c93348 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -904,6 +904,9 @@ pub fn link_binary(sess: Session, // extern libraries might live, based on the addl_lib_search_paths cc_args.push_all(rpath::get_rpath_flags(sess, &output)); + // Finally add all the linker arguments provided on the command line + cc_args.push_all(sess.opts.linker_args); + debug!("%s link args: %s", cc_prog, str::connect(cc_args, ~" ")); // We run 'cc' here let prog = run::program_output(cc_prog, cc_args); diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index a6c061fee1e8..fbcbd4461ac1 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -645,9 +645,16 @@ pub fn build_session_options(binary: @~str, Some(s) => s }; - let addl_lib_search_paths = - getopts::opt_strs(matches, ~"L") - .map(|s| Path(*s)); + let addl_lib_search_paths = getopts::opt_strs(matches, ~"L").map(|s| Path(*s)); + + let linker_args = getopts::opt_strs(matches, ~"linker").flat_map( |a| { + let mut args = ~[]; + for str::each_split_char(*a, ',') |arg| { + args.push(str::from_slice(arg)); + } + args + }); + let cfg = parse_cfgspecs(getopts::opt_strs(matches, ~"cfg"), demitter); let test = opt_present(matches, ~"test"); let android_cross_path = getopts::opt_maybe_str( @@ -664,6 +671,7 @@ pub fn build_session_options(binary: @~str, jit: jit, output_type: output_type, addl_lib_search_paths: addl_lib_search_paths, + linker_args: linker_args, maybe_sysroot: sysroot_opt, target_triple: target, target_feature: target_feature, @@ -737,62 +745,66 @@ pub fn parse_pretty(sess: Session, name: &str) -> pp_mode { // rustc command line options pub fn optgroups() -> ~[getopts::groups::OptGroup] { ~[ - optflag(~"", ~"bin", ~"Compile an executable crate (default)"), - optflag(~"c", ~"", ~"Compile and assemble, but do not link"), - optmulti(~"", ~"cfg", ~"Configure the compilation - environment", ~"SPEC"), - optflag(~"", ~"emit-llvm", - ~"Produce an LLVM bitcode file"), - optflag(~"h", ~"help",~"Display this message"), - optmulti(~"L", ~"", ~"Add a directory to the library search path", - ~"PATH"), - optflag(~"", ~"lib", ~"Compile a library crate"), - optflag(~"", ~"ls", ~"List the symbols defined by a library crate"), - optflag(~"", ~"no-trans", - ~"Run all passes except translation; no output"), - optflag(~"O", ~"", ~"Equivalent to --opt-level=2"), - optopt(~"o", ~"", ~"Write output to ", ~"FILENAME"), - optopt(~"", ~"opt-level", - ~"Optimize with possible levels 0-3", ~"LEVEL"), - optopt( ~"", ~"out-dir", - ~"Write output to compiler-chosen filename - in ", ~"DIR"), - optflag(~"", ~"parse-only", - ~"Parse only; do not compile, assemble, or link"), - optflagopt(~"", ~"pretty", - ~"Pretty-print the input instead of compiling; + optflag("", "bin", "Compile an executable crate (default)"), + optflag("c", "", "Compile and assemble, but do not link"), + optmulti("", "cfg", "Configure the compilation + environment", "SPEC"), + optflag("", "emit-llvm", + "Produce an LLVM bitcode file"), + optflag("h", "help","Display this message"), + optmulti("L", "", "Add a directory to the library search path", + "PATH"), + optflag("", "lib", "Compile a library crate"), + optmulti("", "linker", "FLAGS is a comma-separated list of flags + passed to the linker", "FLAGS"), + optflag("", "ls", "List the symbols defined by a library crate"), + optflag("", "no-trans", + "Run all passes except translation; no output"), + optflag("O", "", "Equivalent to --opt-level=2"), + optopt("o", "", "Write output to ", "FILENAME"), + optopt("", "opt-level", + "Optimize with possible levels 0-3", "LEVEL"), + optopt( "", "out-dir", + "Write output to compiler-chosen filename + in ", "DIR"), + optflag("", "parse-only", + "Parse only; do not compile, assemble, or link"), + optflagopt("", "pretty", + "Pretty-print the input instead of compiling; valid types are: normal (un-annotated source), expanded (crates expanded), typed (crates expanded, with type annotations), or identified (fully parenthesized, - AST nodes and blocks with IDs)", ~"TYPE"), - optflag(~"S", ~"", ~"Compile only; do not assemble or link"), - optflag(~"", ~"save-temps", - ~"Write intermediate files (.bc, .opt.bc, .o) + AST nodes and blocks with IDs)", "TYPE"), + optflag("", "print-link-args", "Prints all the arguments that would be + passed to the linker."), + optflag("S", "", "Compile only; do not assemble or link"), + optflag("", "save-temps", + "Write intermediate files (.bc, .opt.bc, .o) in addition to normal output"), - optopt(~"", ~"sysroot", - ~"Override the system root", ~"PATH"), - optflag(~"", ~"test", ~"Build a test harness"), - optopt(~"", ~"target", - ~"Target triple cpu-manufacturer-kernel[-os] + optopt("", "sysroot", + "Override the system root", "PATH"), + optflag("", "test", "Build a test harness"), + optopt("", "target", + "Target triple cpu-manufacturer-kernel[-os] to compile for (see chapter 3.4 of http://www.sourceware.org/autobook/ - for detail)", ~"TRIPLE"), - optopt(~"", ~"target-feature", - ~"Target specific attributes (llc -mattr=help - for detail)", ~"FEATURE"), - optopt(~"", ~"android-cross-path", - ~"The path to the Android NDK", "PATH"), - optmulti(~"W", ~"warn", - ~"Set lint warnings", ~"OPT"), - optmulti(~"A", ~"allow", - ~"Set lint allowed", ~"OPT"), - optmulti(~"D", ~"deny", - ~"Set lint denied", ~"OPT"), - optmulti(~"F", ~"forbid", - ~"Set lint forbidden", ~"OPT"), - optmulti(~"Z", ~"", ~"Set internal debugging options", "FLAG"), - optflag( ~"v", ~"version", - ~"Print version info and exit"), + for detail)", "TRIPLE"), + optopt("", "target-feature", + "Target specific attributes (llc -mattr=help + for detail)", "FEATURE"), + optopt("", "android-cross-path", + "The path to the Android NDK", "PATH"), + optmulti("W", "warn", + "Set lint warnings", "OPT"), + optmulti("A", "allow", + "Set lint allowed", "OPT"), + optmulti("D", "deny", + "Set lint denied", "OPT"), + optmulti("F", "forbid", + "Set lint forbidden", "OPT"), + optmulti("Z", "", "Set internal debugging options", "FLAG"), + optflag( "v", "version", + "Print version info and exit"), ] } diff --git a/src/librustc/driver/session.rs b/src/librustc/driver/session.rs index 55c81e6d17b2..94d1da20f926 100644 --- a/src/librustc/driver/session.rs +++ b/src/librustc/driver/session.rs @@ -122,6 +122,7 @@ pub struct options { jit: bool, output_type: back::link::output_type, addl_lib_search_paths: ~[Path], + linker_args: ~[~str], maybe_sysroot: Option, target_triple: ~str, target_feature: ~str, @@ -299,6 +300,7 @@ pub fn basic_options() -> @options { jit: false, output_type: link::output_type_exe, addl_lib_search_paths: ~[], + linker_args:~[], maybe_sysroot: None, target_triple: host_triple(), target_feature: ~"",