From 59eac97869eed3a7aa41a96c92021194ddca99f9 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Perennou Date: Wed, 5 Jun 2019 15:41:44 +0200 Subject: [PATCH 1/6] rustbuild: detect cxx for all targets Replaces #61544 Fixes #59917 We need CXX to build llvm-libunwind which can be enabled for all targets. As we needed it for all hosts anyways, just move the detection so that it is ran for all targets (which contains all hosts) instead. Signed-off-by: Marc-Antoine Perennou --- src/bootstrap/builder.rs | 10 ++++------ src/bootstrap/cc_detect.rs | 32 +++++++++++++++----------------- src/bootstrap/compile.rs | 2 +- src/bootstrap/lib.rs | 9 ++------- src/bootstrap/native.rs | 2 +- src/bootstrap/sanity.rs | 2 +- src/bootstrap/test.rs | 2 +- 7 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 2281a45e014a9..ebe03596b8a1a 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1135,12 +1135,10 @@ impl<'a> Builder<'a> { .env(format!("RANLIB_{}", target), ranlib); } - if let Ok(cxx) = self.cxx(target) { - let cxx = ccacheify(&cxx); - cargo - .env(format!("CXX_{}", target), &cxx) - .env(format!("CXXFLAGS_{}", target), cflags); - } + let cxx = ccacheify(&self.cxx(target)); + cargo + .env(format!("CXX_{}", target), &cxx) + .env(format!("CXXFLAGS_{}", target), cflags); } if (cmd == "build" || cmd == "rustc") diff --git a/src/bootstrap/cc_detect.rs b/src/bootstrap/cc_detect.rs index dfc243b7054ab..7cdc835f2281b 100644 --- a/src/bootstrap/cc_detect.rs +++ b/src/bootstrap/cc_detect.rs @@ -95,29 +95,27 @@ pub fn find(build: &mut Build) { }; build.cc.insert(target, compiler); - build.verbose(&format!("CC_{} = {:?}", &target, build.cc(target))); - build.verbose(&format!("CFLAGS_{} = {:?}", &target, build.cflags(target, GitRepo::Rustc))); - if let Some(ar) = ar { - build.verbose(&format!("AR_{} = {:?}", &target, ar)); - build.ar.insert(target, ar); - } - } + let cflags = build.cflags(target, GitRepo::Rustc); - // For all host triples we need to find a C++ compiler as well - let hosts = build.hosts.iter().cloned().chain(iter::once(build.build)).collect::>(); - for host in hosts.into_iter() { - let mut cfg = cc::Build::new(); - cfg.cargo_metadata(false).opt_level(2).warnings(false).debug(false).cpp(true) - .target(&host).host(&build.build); - let config = build.config.target_config.get(&host); + // If we use llvm-libunwind, we will need a C++ compiler as well for all targets + // We'll need one anyways if the target triple is also a host triple + cfg.cpp(true); if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) { cfg.compiler(cxx); } else { - set_compiler(&mut cfg, Language::CPlusPlus, host, config, build); + set_compiler(&mut cfg, Language::CPlusPlus, target, config, build); } let compiler = cfg.get_compiler(); - build.verbose(&format!("CXX_{} = {:?}", host, compiler.path())); - build.cxx.insert(host, compiler); + build.cxx.insert(target, compiler); + + build.verbose(&format!("CC_{} = {:?}", &target, build.cc(target))); + build.verbose(&format!("CFLAGS_{} = {:?}", &target, cflags)); + build.verbose(&format!("CXX_{} = {:?}", &target, build.cxx(target))); + build.verbose(&format!("CXXFLAGS_{} = {:?}", &target, cflags)); + if let Some(ar) = ar { + build.verbose(&format!("AR_{} = {:?}", &target, ar)); + build.ar.insert(target, ar); + } } } diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index c7fa8e788b573..e2c741a064ffe 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -782,7 +782,7 @@ pub fn build_codegen_backend(builder: &Builder<'_>, !target.contains("windows") && !target.contains("apple") { let file = compiler_file(builder, - builder.cxx(target).unwrap(), + builder.cxx(target), target, "libstdc++.a"); cargo.env("LLVM_STATIC_STDCPP", file); diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index b9d287abb0c7e..42fbecd8e1c72 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -815,13 +815,8 @@ impl Build { } /// Returns the path to the C++ compiler for the target specified. - fn cxx(&self, target: Interned) -> Result<&Path, String> { - match self.cxx.get(&target) { - Some(p) => Ok(p.path()), - None => Err(format!( - "target `{}` is not configured as a host, only as a target", - target)) - } + fn cxx(&self, target: Interned) -> &Path { + self.cxx[&target].path() } /// Returns the path to the linker for the given target if it needs to be overridden. diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index bf3601cb312fd..9df1870751f6a 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -358,7 +358,7 @@ fn configure_cmake(builder: &Builder<'_>, let (cc, cxx) = match builder.config.llvm_clang_cl { Some(ref cl) => (cl.as_ref(), cl.as_ref()), - None => (builder.cc(target), builder.cxx(target).unwrap()), + None => (builder.cc(target), builder.cxx(target)), }; // Handle msvc + ninja + ccache specially (this is what the bots use) diff --git a/src/bootstrap/sanity.rs b/src/bootstrap/sanity.rs index dc65fb9b79706..5bf08a45b2901 100644 --- a/src/bootstrap/sanity.rs +++ b/src/bootstrap/sanity.rs @@ -146,7 +146,7 @@ pub fn check(build: &mut Build) { for host in &build.hosts { if !build.config.dry_run { - cmd_finder.must_have(build.cxx(*host).unwrap()); + cmd_finder.must_have(build.cxx(*host)); } } diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index 74caaae2840c5..095ba146caa6e 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -1211,7 +1211,7 @@ impl Step for Compiletest { cmd.arg("--cc") .arg(builder.cc(target)) .arg("--cxx") - .arg(builder.cxx(target).unwrap()) + .arg(builder.cxx(target)) .arg("--cflags") .arg(builder.cflags(target, GitRepo::Rustc).join(" ")) .arg("--llvm-components") From 00b6015c078fc3ab76d4ca9ab869c6f0e5b8a1f6 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Perennou Date: Thu, 13 Jun 2019 10:04:42 +0200 Subject: [PATCH 2/6] rustbuild: fix default value for cxx Signed-off-by: Marc-Antoine Perennou --- src/bootstrap/cc_detect.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/cc_detect.rs b/src/bootstrap/cc_detect.rs index 7cdc835f2281b..dc7287ee3013b 100644 --- a/src/bootstrap/cc_detect.rs +++ b/src/bootstrap/cc_detect.rs @@ -99,7 +99,9 @@ pub fn find(build: &mut Build) { // If we use llvm-libunwind, we will need a C++ compiler as well for all targets // We'll need one anyways if the target triple is also a host triple - cfg.cpp(true); + let mut cfg = cc::Build::new(); + cfg.cargo_metadata(false).opt_level(2).warnings(false).debug(false).cpp(true) + .target(&target).host(&build.build); if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) { cfg.compiler(cxx); } else { From fdecfe4ca56d5ba47662e7980f8e7335250bc6c1 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Perennou Date: Thu, 13 Jun 2019 16:21:42 +0200 Subject: [PATCH 3/6] rustbuild: don't set cxx if not configured Signed-off-by: Marc-Antoine Perennou --- src/bootstrap/builder.rs | 10 ++++++---- src/bootstrap/cc_detect.rs | 33 +++++++++++++++++++++++---------- src/bootstrap/compile.rs | 2 +- src/bootstrap/lib.rs | 9 +++++++-- src/bootstrap/native.rs | 2 +- src/bootstrap/sanity.rs | 2 +- src/bootstrap/test.rs | 2 +- 7 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index ebe03596b8a1a..2281a45e014a9 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1135,10 +1135,12 @@ impl<'a> Builder<'a> { .env(format!("RANLIB_{}", target), ranlib); } - let cxx = ccacheify(&self.cxx(target)); - cargo - .env(format!("CXX_{}", target), &cxx) - .env(format!("CXXFLAGS_{}", target), cflags); + if let Ok(cxx) = self.cxx(target) { + let cxx = ccacheify(&cxx); + cargo + .env(format!("CXX_{}", target), &cxx) + .env(format!("CXXFLAGS_{}", target), cflags); + } } if (cmd == "build" || cmd == "rustc") diff --git a/src/bootstrap/cc_detect.rs b/src/bootstrap/cc_detect.rs index dc7287ee3013b..e0fef972f74cc 100644 --- a/src/bootstrap/cc_detect.rs +++ b/src/bootstrap/cc_detect.rs @@ -102,18 +102,25 @@ pub fn find(build: &mut Build) { let mut cfg = cc::Build::new(); cfg.cargo_metadata(false).opt_level(2).warnings(false).debug(false).cpp(true) .target(&target).host(&build.build); - if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) { + + let cxx_configured = if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) { cfg.compiler(cxx); + true } else { - set_compiler(&mut cfg, Language::CPlusPlus, target, config, build); + set_compiler(&mut cfg, Language::CPlusPlus, target, config, build) + }; + + if cxx_configured { + let compiler = cfg.get_compiler(); + build.cxx.insert(target, compiler); } - let compiler = cfg.get_compiler(); - build.cxx.insert(target, compiler); build.verbose(&format!("CC_{} = {:?}", &target, build.cc(target))); build.verbose(&format!("CFLAGS_{} = {:?}", &target, cflags)); - build.verbose(&format!("CXX_{} = {:?}", &target, build.cxx(target))); - build.verbose(&format!("CXXFLAGS_{} = {:?}", &target, cflags)); + if let Ok(cxx) = build.cxx(target) { + build.verbose(&format!("CXX_{} = {:?}", &target, cxx)); + build.verbose(&format!("CXXFLAGS_{} = {:?}", &target, cflags)); + } if let Some(ar) = ar { build.verbose(&format!("AR_{} = {:?}", &target, ar)); build.ar.insert(target, ar); @@ -125,7 +132,7 @@ fn set_compiler(cfg: &mut cc::Build, compiler: Language, target: Interned, config: Option<&Target>, - build: &Build) { + build: &Build) -> bool { match &*target { // When compiling for android we may have the NDK configured in the // config.toml in which case we look there. Otherwise the default @@ -138,6 +145,7 @@ fn set_compiler(cfg: &mut cc::Build, .replace("thumbv7", "arm"); let compiler = format!("{}-{}", target, compiler.clang()); cfg.compiler(ndk.join("bin").join(compiler)); + return true; } } @@ -147,32 +155,35 @@ fn set_compiler(cfg: &mut cc::Build, let c = cfg.get_compiler(); let gnu_compiler = compiler.gcc(); if !c.path().ends_with(gnu_compiler) { - return + return false; } let output = output(c.to_command().arg("--version")); let i = match output.find(" 4.") { Some(i) => i, - None => return, + None => return false, }; match output[i + 3..].chars().next().unwrap() { '0' ..= '6' => {} - _ => return, + _ => return false, } let alternative = format!("e{}", gnu_compiler); if Command::new(&alternative).output().is_ok() { cfg.compiler(alternative); + return true; } } "mips-unknown-linux-musl" => { if cfg.get_compiler().path().to_str() == Some("gcc") { cfg.compiler("mips-linux-musl-gcc"); + return true; } } "mipsel-unknown-linux-musl" => { if cfg.get_compiler().path().to_str() == Some("gcc") { cfg.compiler("mipsel-linux-musl-gcc"); + return true; } } @@ -181,12 +192,14 @@ fn set_compiler(cfg: &mut cc::Build, let guess = root.join("bin/musl-gcc"); if guess.exists() { cfg.compiler(guess); + return true; } } } _ => {} } + false } /// The target programming language for a native compiler. diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index e2c741a064ffe..c7fa8e788b573 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -782,7 +782,7 @@ pub fn build_codegen_backend(builder: &Builder<'_>, !target.contains("windows") && !target.contains("apple") { let file = compiler_file(builder, - builder.cxx(target), + builder.cxx(target).unwrap(), target, "libstdc++.a"); cargo.env("LLVM_STATIC_STDCPP", file); diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs index 42fbecd8e1c72..b9d287abb0c7e 100644 --- a/src/bootstrap/lib.rs +++ b/src/bootstrap/lib.rs @@ -815,8 +815,13 @@ impl Build { } /// Returns the path to the C++ compiler for the target specified. - fn cxx(&self, target: Interned) -> &Path { - self.cxx[&target].path() + fn cxx(&self, target: Interned) -> Result<&Path, String> { + match self.cxx.get(&target) { + Some(p) => Ok(p.path()), + None => Err(format!( + "target `{}` is not configured as a host, only as a target", + target)) + } } /// Returns the path to the linker for the given target if it needs to be overridden. diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index 9df1870751f6a..bf3601cb312fd 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -358,7 +358,7 @@ fn configure_cmake(builder: &Builder<'_>, let (cc, cxx) = match builder.config.llvm_clang_cl { Some(ref cl) => (cl.as_ref(), cl.as_ref()), - None => (builder.cc(target), builder.cxx(target)), + None => (builder.cc(target), builder.cxx(target).unwrap()), }; // Handle msvc + ninja + ccache specially (this is what the bots use) diff --git a/src/bootstrap/sanity.rs b/src/bootstrap/sanity.rs index 5bf08a45b2901..dc65fb9b79706 100644 --- a/src/bootstrap/sanity.rs +++ b/src/bootstrap/sanity.rs @@ -146,7 +146,7 @@ pub fn check(build: &mut Build) { for host in &build.hosts { if !build.config.dry_run { - cmd_finder.must_have(build.cxx(*host)); + cmd_finder.must_have(build.cxx(*host).unwrap()); } } diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index 095ba146caa6e..74caaae2840c5 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -1211,7 +1211,7 @@ impl Step for Compiletest { cmd.arg("--cc") .arg(builder.cc(target)) .arg("--cxx") - .arg(builder.cxx(target)) + .arg(builder.cxx(target).unwrap()) .arg("--cflags") .arg(builder.cflags(target, GitRepo::Rustc).join(" ")) .arg("--llvm-components") From c682ac9528822215e57c1ab70b2dd306b6fbbbeb Mon Sep 17 00:00:00 2001 From: Marc-Antoine Perennou Date: Thu, 13 Jun 2019 17:41:03 +0200 Subject: [PATCH 4/6] rustbuild: set cxx for hosts even if not configured Signed-off-by: Marc-Antoine Perennou --- src/bootstrap/cc_detect.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/cc_detect.rs b/src/bootstrap/cc_detect.rs index e0fef972f74cc..71d08ae38efad 100644 --- a/src/bootstrap/cc_detect.rs +++ b/src/bootstrap/cc_detect.rs @@ -110,7 +110,7 @@ pub fn find(build: &mut Build) { set_compiler(&mut cfg, Language::CPlusPlus, target, config, build) }; - if cxx_configured { + if cxx_configured || build.hosts.contains(&target) { let compiler = cfg.get_compiler(); build.cxx.insert(target, compiler); } From 870f13a0b342771746c22f335b4118303626ef23 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Perennou Date: Fri, 21 Jun 2019 13:59:00 +0200 Subject: [PATCH 5/6] rustbuild: only autodetect cxx for hosts Signed-off-by: Marc-Antoine Perennou --- src/bootstrap/cc_detect.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/bootstrap/cc_detect.rs b/src/bootstrap/cc_detect.rs index 71d08ae38efad..3b74da04fea7a 100644 --- a/src/bootstrap/cc_detect.rs +++ b/src/bootstrap/cc_detect.rs @@ -106,11 +106,14 @@ pub fn find(build: &mut Build) { let cxx_configured = if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) { cfg.compiler(cxx); true + } else if build.hosts.contains(&target) { + set_compiler(&mut cfg, Language::CPlusPlus, target, config, build); + true } else { - set_compiler(&mut cfg, Language::CPlusPlus, target, config, build) + false }; - if cxx_configured || build.hosts.contains(&target) { + if cxx_configured { let compiler = cfg.get_compiler(); build.cxx.insert(target, compiler); } @@ -132,7 +135,7 @@ fn set_compiler(cfg: &mut cc::Build, compiler: Language, target: Interned, config: Option<&Target>, - build: &Build) -> bool { + build: &Build) { match &*target { // When compiling for android we may have the NDK configured in the // config.toml in which case we look there. Otherwise the default @@ -145,7 +148,6 @@ fn set_compiler(cfg: &mut cc::Build, .replace("thumbv7", "arm"); let compiler = format!("{}-{}", target, compiler.clang()); cfg.compiler(ndk.join("bin").join(compiler)); - return true; } } @@ -155,35 +157,32 @@ fn set_compiler(cfg: &mut cc::Build, let c = cfg.get_compiler(); let gnu_compiler = compiler.gcc(); if !c.path().ends_with(gnu_compiler) { - return false; + return } let output = output(c.to_command().arg("--version")); let i = match output.find(" 4.") { Some(i) => i, - None => return false, + None => return, }; match output[i + 3..].chars().next().unwrap() { '0' ..= '6' => {} - _ => return false, + _ => return, } let alternative = format!("e{}", gnu_compiler); if Command::new(&alternative).output().is_ok() { cfg.compiler(alternative); - return true; } } "mips-unknown-linux-musl" => { if cfg.get_compiler().path().to_str() == Some("gcc") { cfg.compiler("mips-linux-musl-gcc"); - return true; } } "mipsel-unknown-linux-musl" => { if cfg.get_compiler().path().to_str() == Some("gcc") { cfg.compiler("mipsel-linux-musl-gcc"); - return true; } } @@ -192,14 +191,12 @@ fn set_compiler(cfg: &mut cc::Build, let guess = root.join("bin/musl-gcc"); if guess.exists() { cfg.compiler(guess); - return true; } } } _ => {} } - false } /// The target programming language for a native compiler. From 087cd77d969d9c3cf80f47d4fd8a4379aa48f224 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Perennou Date: Mon, 24 Jun 2019 09:12:24 +0200 Subject: [PATCH 6/6] rustbuild: always set cxx for build host Even if it's not in hosts Signed-off-by: Marc-Antoine Perennou --- src/bootstrap/cc_detect.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/cc_detect.rs b/src/bootstrap/cc_detect.rs index 3b74da04fea7a..400375cd201c4 100644 --- a/src/bootstrap/cc_detect.rs +++ b/src/bootstrap/cc_detect.rs @@ -106,7 +106,7 @@ pub fn find(build: &mut Build) { let cxx_configured = if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) { cfg.compiler(cxx); true - } else if build.hosts.contains(&target) { + } else if build.hosts.contains(&target) || build.build == target { set_compiler(&mut cfg, Language::CPlusPlus, target, config, build); true } else {