1 //! Sanity checking performed by rustbuild before actually executing anything.
3 //! This module contains the implementation of ensuring that the build
4 //! environment looks reasonable before progressing. This will verify that
5 //! various programs like git and python exist, along with ensuring that all C
6 //! compilers for cross-compiling are found.
8 //! In theory if we get past this phase it's a bug if a build fails, but in
9 //! practice that's likely not true!
11 use std
::collections
::HashMap
;
13 use std
::ffi
::{OsStr, OsString}
;
15 use std
::path
::PathBuf
;
16 use std
::process
::Command
;
18 use crate::cache
::INTERNER
;
19 use crate::config
::Target
;
20 use crate::util
::output
;
24 cache
: HashMap
<OsString
, Option
<PathBuf
>>,
29 pub fn new() -> Self {
30 Self { cache: HashMap::new(), path: env::var_os("PATH").unwrap_or_default() }
33 pub fn maybe_have
<S
: Into
<OsString
>>(&mut self, cmd
: S
) -> Option
<PathBuf
> {
34 let cmd
: OsString
= cmd
.into();
35 let path
= &self.path
;
39 for path
in env
::split_paths(path
) {
40 let target
= path
.join(&cmd
);
41 let mut cmd_exe
= cmd
.clone();
44 if target
.is_file() // some/path/git
45 || path
.join(&cmd_exe
).exists() // some/path/git.exe
46 || target
.join(&cmd_exe
).exists()
47 // some/path/git/git.exe
57 pub fn must_have
<S
: AsRef
<OsStr
>>(&mut self, cmd
: S
) -> PathBuf
{
58 self.maybe_have(&cmd
).unwrap_or_else(|| {
59 panic
!("\n\ncouldn't find required command: {:?}\n\n", cmd
.as_ref());
64 pub fn check(build
: &mut Build
) {
65 let path
= env
::var_os("PATH").unwrap_or_default();
66 // On Windows, quotes are invalid characters for filename paths, and if
67 // one is present as part of the PATH then that can lead to the system
68 // being unable to identify the files properly. See
69 // https://github.com/rust-lang/rust/issues/34959 for more details.
70 if cfg
!(windows
) && path
.to_string_lossy().contains('
\"'
) {
71 panic
!("PATH contains invalid character '\"'");
74 let mut cmd_finder
= Finder
::new();
75 // If we've got a git directory we're gonna need git to update
76 // submodules and learn about various other aspects.
77 if build
.rust_info
.is_git() {
78 cmd_finder
.must_have("git");
81 // We need cmake, but only if we're actually building LLVM or sanitizers.
82 let building_llvm
= build
.config
.rust_codegen_backends
.contains(&INTERNER
.intern_str("llvm"))
91 .map(|config
| config
.llvm_config
.is_none())
94 .any(|build_llvm_ourselves
| build_llvm_ourselves
);
95 let need_cmake
= building_llvm
|| build
.config
.any_sanitizers_enabled();
97 cmd_finder
.must_have("cmake");
100 build
.config
.python
= build
104 .map(|p
| cmd_finder
.must_have(p
))
105 .or_else(|| env
::var_os("BOOTSTRAP_PYTHON").map(PathBuf
::from
)) // set by bootstrap.py
106 .or_else(|| Some(cmd_finder
.must_have("python")));
108 build
.config
.nodejs
= build
112 .map(|p
| cmd_finder
.must_have(p
))
113 .or_else(|| cmd_finder
.maybe_have("node"))
114 .or_else(|| cmd_finder
.maybe_have("nodejs"));
116 build
.config
.npm
= build
120 .map(|p
| cmd_finder
.must_have(p
))
121 .or_else(|| cmd_finder
.maybe_have("npm"));
123 build
.config
.gdb
= build
127 .map(|p
| cmd_finder
.must_have(p
))
128 .or_else(|| cmd_finder
.maybe_have("gdb"));
130 // We're gonna build some custom C code here and there, host triples
131 // also build some C++ shims for LLVM so we need a C++ compiler.
132 for target
in &build
.targets
{
133 // On emscripten we don't actually need the C compiler to just
134 // build the target artifacts, only for testing. For the sake
135 // of easier bot configuration, just skip detection.
136 if target
.contains("emscripten") {
140 // We don't use a C compiler on wasm32
141 if target
.contains("wasm32") {
145 if !build
.config
.dry_run
{
146 cmd_finder
.must_have(build
.cc(*target
));
147 if let Some(ar
) = build
.ar(*target
) {
148 cmd_finder
.must_have(ar
);
153 for host
in &build
.hosts
{
154 if !build
.config
.dry_run
{
155 cmd_finder
.must_have(build
.cxx(*host
).unwrap());
159 if build
.config
.rust_codegen_backends
.contains(&INTERNER
.intern_str("llvm")) {
160 // Externally configured LLVM requires FileCheck to exist
161 let filecheck
= build
.llvm_filecheck(build
.build
);
162 if !filecheck
.starts_with(&build
.out
) && !filecheck
.exists() && build
.config
.codegen_tests
{
163 panic
!("FileCheck executable {:?} does not exist", filecheck
);
167 for target
in &build
.targets
{
172 .or_insert_with(|| Target
::from_triple(&target
.triple
));
174 if target
.contains("-none-") || target
.contains("nvptx") {
175 if build
.no_std(*target
) == Some(false) {
176 panic
!("All the *-none-* and nvptx* targets are no-std targets")
180 // Make sure musl-root is valid
181 if target
.contains("musl") {
182 // If this is a native target (host is also musl) and no musl-root is given,
183 // fall back to the system toolchain in /usr before giving up
184 if build
.musl_root(*target
).is_none() && build
.config
.build
== *target
{
185 let target
= build
.config
.target_config
.entry(*target
).or_default();
186 target
.musl_root
= Some("/usr".into());
188 match build
.musl_libdir(*target
) {
190 if fs
::metadata(libdir
.join("libc.a")).is_err() {
191 panic
!("couldn't find libc.a in musl libdir: {}", libdir
.display());
195 "when targeting MUSL either the rust.musl-root \
196 option or the target.$TARGET.musl-root option must \
197 be specified in config.toml"
202 if need_cmake
&& target
.contains("msvc") {
203 // There are three builds of cmake on windows: MSVC, MinGW, and
204 // Cygwin. The Cygwin build does not have generators for Visual
205 // Studio, so detect that here and error.
206 let out
= output(Command
::new("cmake").arg("--help"));
207 if !out
.contains("Visual Studio") {
210 cmake does not support Visual Studio generators.
212 This is likely due to it being an msys/cygwin build of cmake,
213 rather than the required windows version, built using MinGW
216 If you are building under msys2 try installing the mingw-w64-x86_64-cmake
217 package instead of cmake:
219 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
226 if let Some(ref s
) = build
.config
.ccache
{
227 cmd_finder
.must_have(s
);