2 #![deny(warnings, rust_2018_idioms, unused_lifetimes)]
5 use std::path::{Path, PathBuf};
6 use std::process::{Command, Stdio};
7 use std::time::{SystemTime, UNIX_EPOCH};
11 /// A helper macro to `unwrap` a result except also print out details like:
13 /// * The file/line of the panic
14 /// * The expression that failed
15 /// * The error itself
17 /// This is currently used judiciously throughout the build system rather than
18 /// using a `Result` with `try!`, but this may change one day...
24 Err(e) => panic!("{} failed with {}", stringify!($e), e),
29 // Because Cargo adds the compiler's dylib path to our library search path, llvm-config may
30 // break: the dylib path for the compiler, as of this writing, contains a copy of the LLVM
31 // shared library, which means that when our freshly built llvm-config goes to load it's
32 // associated LLVM, it actually loads the compiler's LLVM. In particular when building the first
33 // compiler (i.e., in stage 0) that's a problem, as the compiler's LLVM is likely different from
34 // the one we want to use. As such, we restore the environment to what bootstrap saw. This isn't
35 // perfect -- we might actually want to see something from Cargo's added library paths -- but
37 pub fn restore_library_path() {
38 println!("cargo:rerun-if-env-changed=REAL_LIBRARY_PATH_VAR");
39 println!("cargo:rerun-if-env-changed=REAL_LIBRARY_PATH");
40 let key = env::var_os("REAL_LIBRARY_PATH_VAR").expect("REAL_LIBRARY_PATH_VAR");
41 if let Some(env) = env::var_os("REAL_LIBRARY_PATH") {
42 env::set_var(&key, &env);
44 env::remove_var(&key);
48 /// Run the command, printing what we are running.
49 pub fn run_verbose(cmd: &mut Command) {
50 println!("running: {:?}", cmd);
54 pub fn run(cmd: &mut Command) {
56 std::process::exit(1);
60 pub fn try_run(cmd: &mut Command) -> bool {
61 let status = match cmd.status() {
63 Err(e) => fail(&format!(
64 "failed to execute command: {:?}\nerror: {}",
68 if !status.success() {
70 "\n\ncommand did not execute successfully: {:?}\n\
71 expected success, got: {}\n\n",
78 pub fn run_suppressed(cmd: &mut Command) {
79 if !try_run_suppressed(cmd) {
80 std::process::exit(1);
84 pub fn try_run_suppressed(cmd: &mut Command) -> bool {
85 let output = match cmd.output() {
87 Err(e) => fail(&format!(
88 "failed to execute command: {:?}\nerror: {}",
92 if !output.status.success() {
94 "\n\ncommand did not execute successfully: {:?}\n\
95 expected success, got: {}\n\n\
100 String::from_utf8_lossy(&output.stdout),
101 String::from_utf8_lossy(&output.stderr)
104 output.status.success()
107 pub fn gnu_target(target: &str) -> &str {
109 "i686-pc-windows-msvc" => "i686-pc-win32",
110 "x86_64-pc-windows-msvc" => "x86_64-pc-win32",
111 "i686-pc-windows-gnu" => "i686-w64-mingw32",
112 "x86_64-pc-windows-gnu" => "x86_64-w64-mingw32",
117 pub fn make(host: &str) -> PathBuf {
118 if host.contains("dragonfly") || host.contains("freebsd")
119 || host.contains("netbsd") || host.contains("openbsd")
121 PathBuf::from("gmake")
123 PathBuf::from("make")
127 pub fn output(cmd: &mut Command) -> String {
128 let output = match cmd.stderr(Stdio::inherit()).output() {
129 Ok(status) => status,
130 Err(e) => fail(&format!(
131 "failed to execute command: {:?}\nerror: {}",
135 if !output.status.success() {
137 "command did not execute successfully: {:?}\n\
138 expected success, got: {}",
142 String::from_utf8(output.stdout).unwrap()
145 pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
146 let mut stack = dir.read_dir()
149 .filter(|e| &*e.file_name() != ".git")
150 .collect::<Vec<_>>();
151 while let Some(entry) = stack.pop() {
152 let path = entry.path();
153 if entry.file_type().unwrap().is_dir() {
154 stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
156 println!("cargo:rerun-if-changed={}", path.display());
161 /// Returns the last-modified time for `path`, or zero if it doesn't exist.
162 pub fn mtime(path: &Path) -> SystemTime {
164 .and_then(|f| f.modified())
165 .unwrap_or(UNIX_EPOCH)
168 /// Returns `true` if `dst` is up to date given that the file or files in `src`
169 /// are used to generate it.
171 /// Uses last-modified time checks to verify this.
172 pub fn up_to_date(src: &Path, dst: &Path) -> bool {
176 let threshold = mtime(dst);
177 let meta = match fs::metadata(src) {
179 Err(e) => panic!("source {:?} failed to get metadata: {}", src, e),
182 dir_up_to_date(src, threshold)
184 meta.modified().unwrap_or(UNIX_EPOCH) <= threshold
189 pub struct NativeLibBoilerplate {
190 pub src_dir: PathBuf,
191 pub out_dir: PathBuf,
194 impl NativeLibBoilerplate {
195 /// On macOS we don't want to ship the exact filename that compiler-rt builds.
196 /// This conflicts with the system and ours is likely a wildly different
197 /// version, so they can't be substituted.
199 /// As a result, we rename it here but we need to also use
200 /// `install_name_tool` on macOS to rename the commands listed inside of it to
201 /// ensure it's linked against correctly.
202 pub fn fixup_sanitizer_lib_name(&self, sanitizer_name: &str) {
203 if env::var("TARGET").unwrap() != "x86_64-apple-darwin" {
207 let dir = self.out_dir.join("build/lib/darwin");
208 let name = format!("clang_rt.{}_osx_dynamic", sanitizer_name);
209 let src = dir.join(&format!("lib{}.dylib", name));
210 let new_name = format!("lib__rustc__{}.dylib", name);
211 let dst = dir.join(&new_name);
213 println!("{} => {}", src.display(), dst.display());
214 fs::rename(&src, &dst).unwrap();
215 let status = Command::new("install_name_tool")
217 .arg(format!("@rpath/{}", new_name))
220 .expect("failed to execute `install_name_tool`");
221 assert!(status.success());
225 impl Drop for NativeLibBoilerplate {
227 if !thread::panicking() {
228 t!(File::create(self.out_dir.join("rustbuild.timestamp")));
233 // Perform standard preparations for native libraries that are build only once for all stages.
234 // Emit rerun-if-changed and linking attributes for Cargo, check if any source files are
235 // updated, calculate paths used later in actual build with CMake/make or C/C++ compiler.
236 // If Err is returned, then everything is up-to-date and further build actions can be skipped.
237 // Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
238 // of scope, so all the build actions should be completed until then.
239 pub fn native_lib_boilerplate(
244 ) -> Result<NativeLibBoilerplate, ()> {
245 rerun_if_changed_anything_in_dir(src_dir);
247 let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(||
248 env::var_os("OUT_DIR").unwrap());
249 let out_dir = PathBuf::from(out_dir).join(out_name);
250 t!(fs::create_dir_all(&out_dir));
251 if link_name.contains('=') {
252 println!("cargo:rustc-link-lib={}", link_name);
254 println!("cargo:rustc-link-lib=static={}", link_name);
257 "cargo:rustc-link-search=native={}",
258 out_dir.join(search_subdir).display()
261 let timestamp = out_dir.join("rustbuild.timestamp");
262 if !up_to_date(Path::new("build.rs"), ×tamp) || !up_to_date(src_dir, ×tamp) {
263 Ok(NativeLibBoilerplate {
264 src_dir: src_dir.to_path_buf(),
272 pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
273 -> Result<(NativeLibBoilerplate, String), ()>
275 let (link_name, search_path, apple) = match &*env::var("TARGET").unwrap() {
276 "x86_64-unknown-linux-gnu" => (
277 format!("clang_rt.{}-x86_64", sanitizer_name),
281 "x86_64-apple-darwin" => (
282 format!("clang_rt.{}_osx_dynamic", sanitizer_name),
288 let to_link = if apple {
289 format!("dylib=__rustc__{}", link_name)
291 format!("static={}", link_name)
293 // This env var is provided by rustbuild to tell us where `compiler-rt`
295 let dir = env::var_os("RUST_COMPILER_RT_ROOT").unwrap();
296 let lib = native_lib_boilerplate(
305 fn dir_up_to_date(src: &Path, threshold: SystemTime) -> bool {
306 t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
307 let meta = t!(e.metadata());
309 dir_up_to_date(&e.path(), threshold)
311 meta.modified().unwrap_or(UNIX_EPOCH) < threshold
316 fn fail(s: &str) -> ! {
317 println!("\n\n{}\n\n", s);
318 std::process::exit(1);