From a0911cf95d3cf6bc0fe50ad95c2578771b122e64 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 9 Aug 2018 16:08:12 -0500 Subject: [PATCH] address review comments --- src/librustc_codegen_llvm/back/link.rs | 84 +++++++++++-------------- src/librustc_codegen_llvm/back/write.rs | 19 +++--- 2 files changed, 43 insertions(+), 60 deletions(-) diff --git a/src/librustc_codegen_llvm/back/link.rs b/src/librustc_codegen_llvm/back/link.rs index 0317dead6c0..c31928afb22 100644 --- a/src/librustc_codegen_llvm/back/link.rs +++ b/src/librustc_codegen_llvm/back/link.rs @@ -69,18 +69,13 @@ pub fn get_linker(sess: &Session, linker: &Path, flavor: LinkerFlavor) -> (PathB // This worked historically but is needed manually since #42436 (regression // was tagged as #42791) and some more info can be found on #44443 for // emscripten itself. - let mut cmd = (|| { - if let Some(linker) = linker.to_str() { - if cfg!(windows) && linker.ends_with(".bat") { - return Command::bat_script(linker) - } - } - match flavor { + let mut cmd = match linker.to_str() { + Some(linker) if cfg!(windows) && linker.ends_with(".bat") => Command::bat_script(linker), + _ => match flavor { LinkerFlavor::Lld(f) => Command::lld(linker, f), _ => Command::new(linker), - } - })(); + }; let msvc_tool = windows_registry::find_tool(&sess.opts.target_triple.triple(), "link.exe"); @@ -600,33 +595,26 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary]) { } } -pub fn linker_and_flavor(sess: &Session) -> Result<(PathBuf, LinkerFlavor), ()> { - fn from( +pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) { + fn infer_from( sess: &Session, linker: Option, flavor: Option, - otherwise: F, - ) -> Result<(PathBuf, LinkerFlavor), ()> - where - F: FnOnce() -> Result<(PathBuf, LinkerFlavor), ()> - { + ) -> Option<(PathBuf, LinkerFlavor)> { match (linker, flavor) { - (Some(linker), Some(flavor)) => Ok((linker, flavor)), + (Some(linker), Some(flavor)) => Some((linker, flavor)), // only the linker flavor is known; use the default linker for the selected flavor - (None, Some(flavor)) => Ok((PathBuf::from(match flavor { - LinkerFlavor::Em => "emcc", + (None, Some(flavor)) => Some((PathBuf::from(match flavor { + LinkerFlavor::Em => if cfg!(windows) { "emcc.bat" } else { "emcc" }, LinkerFlavor::Gcc => "gcc", LinkerFlavor::Ld => "ld", LinkerFlavor::Msvc => "link.exe", LinkerFlavor::Lld(_) => "lld", }), flavor)), - // infer the linker flavor from the linker name (Some(linker), None) => { - let stem = linker.file_stem().and_then(|stem| stem.to_str()).ok_or_else(|| { - sess - .struct_err(&format!("couldn't extract file stem from specified linker")) - .emit(); - })?.to_owned(); + let stem = linker.file_stem().and_then(|stem| stem.to_str()).unwrap_or_else(|| { + sess.fatal("couldn't extract file stem from specified linker"); + }).to_owned(); let flavor = if stem == "emcc" { LinkerFlavor::Em @@ -637,32 +625,35 @@ pub fn linker_and_flavor(sess: &Session) -> Result<(PathBuf, LinkerFlavor), ()> } else if stem == "link" || stem == "lld-link" { LinkerFlavor::Msvc } else { - sess - .struct_err(&format!("couldn't infer linker flavor from specified linker")) - .emit(); - return Err(()); + // fall back to the value in the target spec + sess.target.target.linker_flavor }; - Ok((linker, flavor)) + Some((linker, flavor)) }, - (None, None) => otherwise(), + (None, None) => None, } } // linker and linker flavor specified via command line have precedence over what the target // specification specifies - from(sess, sess.opts.cg.linker.clone(), sess.opts.debugging_opts.linker_flavor, || { - from( - sess, - sess.target.target.options.linker.clone().map(PathBuf::from), - Some(sess.target.target.linker_flavor), - || { - sess - .struct_err(&format!("no linker or linker flavor information provided")) - .emit(); - Err(()) - }) - }) + if let Some(ret) = infer_from( + sess, + sess.opts.cg.linker.clone(), + sess.opts.debugging_opts.linker_flavor, + ) { + return ret; + } + + if let Some(ret) = infer_from( + sess, + sess.target.target.options.linker.clone().map(PathBuf::from), + Some(sess.target.target.linker_flavor), + ) { + return ret; + } + + sess.fatal("Not enough information provided to determine how to invoke the linker"); } // Create a dynamic library or executable @@ -675,12 +666,7 @@ fn link_natively(sess: &Session, codegen_results: &CodegenResults, tmpdir: &Path) { info!("preparing {:?} to {:?}", crate_type, out_filename); - let (linker, flavor) = if let Ok((linker, flavor)) = linker_and_flavor(sess) { - (linker, flavor) - } else { - sess.abort_if_errors(); - return; - }; + let (linker, flavor) = linker_and_flavor(sess); // The invocations of cc share some flags across platforms let (pname, mut cmd) = get_linker(sess, &linker, flavor); diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index e5e837d05de..14abbe3f183 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -1503,17 +1503,14 @@ fn start_executing_work(tcx: TyCtxt, let assembler_cmd = if modules_config.no_integrated_as { // HACK: currently we use linker (gcc) as our assembler - if let Ok((linker, flavor)) = link::linker_and_flavor(sess) { - let (name, mut cmd) = get_linker(sess, &linker, flavor); - cmd.args(&sess.target.target.options.asm_args); - Some(Arc::new(AssemblerCommand { - name, - cmd, - })) - } else { - sess.abort_if_errors(); - None - } + let (linker, flavor) = link::linker_and_flavor(sess); + + let (name, mut cmd) = get_linker(sess, &linker, flavor); + cmd.args(&sess.target.target.options.asm_args); + Some(Arc::new(AssemblerCommand { + name, + cmd, + })) } else { None };