Skip to content

Commit c86e7c4

Browse files
committed
rustbuild: Add helper to abstract hard_link/copy
Also helps provide context if it fails.
1 parent 943ec3b commit c86e7c4

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/bootstrap/build/compile.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::process::Command;
1515

1616
use build_helper::output;
1717

18-
use build::util::{exe, staticlib, libdir, mtime, is_dylib};
18+
use build::util::{exe, staticlib, libdir, mtime, is_dylib, copy};
1919
use build::{Build, Compiler, Mode};
2020

2121
/// Build the standard library.
@@ -32,8 +32,8 @@ pub fn std<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
3232
let libdir = build.sysroot_libdir(compiler, target);
3333
let _ = fs::remove_dir_all(&libdir);
3434
t!(fs::create_dir_all(&libdir));
35-
t!(fs::hard_link(&build.compiler_rt_built.borrow()[target],
36-
libdir.join(staticlib("compiler-rt", target))));
35+
copy(&build.compiler_rt_built.borrow()[target],
36+
&libdir.join(staticlib("compiler-rt", target)));
3737

3838
build_startup_objects(build, target, &libdir);
3939

@@ -77,8 +77,8 @@ pub fn std_link(build: &Build,
7777
if host != compiler.host {
7878
let _ = fs::remove_dir_all(&libdir);
7979
t!(fs::create_dir_all(&libdir));
80-
t!(fs::hard_link(&build.compiler_rt_built.borrow()[target],
81-
libdir.join(staticlib("compiler-rt", target))));
80+
copy(&build.compiler_rt_built.borrow()[target],
81+
&libdir.join(staticlib("compiler-rt", target)));
8282
}
8383
add_to_sysroot(&out_dir, &libdir);
8484

@@ -93,7 +93,7 @@ pub fn std_link(build: &Build,
9393
/// Only required for musl targets that statically link to libc
9494
fn copy_third_party_objects(build: &Build, target: &str, into: &Path) {
9595
for &obj in &["crt1.o", "crti.o", "crtn.o"] {
96-
t!(fs::copy(compiler_file(build.cc(target), obj), into.join(obj)));
96+
copy(&compiler_file(build.cc(target), obj), &into.join(obj));
9797
}
9898
}
9999

@@ -119,7 +119,7 @@ fn build_startup_objects(build: &Build, target: &str, into: &Path) {
119119
}
120120

121121
for obj in ["crt2.o", "dllcrt2.o"].iter() {
122-
t!(fs::copy(compiler_file(build.cc(target), obj), into.join(obj)));
122+
copy(&compiler_file(build.cc(target), obj), &into.join(obj));
123123
}
124124
}
125125

@@ -240,9 +240,10 @@ fn libtest_shim(build: &Build, compiler: &Compiler, target: &str) -> PathBuf {
240240
build.cargo_out(compiler, Mode::Libtest, target).join("libtest_shim.rlib")
241241
}
242242

243-
fn compiler_file(compiler: &Path, file: &str) -> String {
244-
output(Command::new(compiler)
245-
.arg(format!("-print-file-name={}", file))).trim().to_string()
243+
fn compiler_file(compiler: &Path, file: &str) -> PathBuf {
244+
let out = output(Command::new(compiler)
245+
.arg(format!("-print-file-name={}", file)));
246+
PathBuf::from(out.trim())
246247
}
247248

248249
/// Prepare a new compiler from the artifacts in `stage`
@@ -270,7 +271,7 @@ pub fn assemble_rustc(build: &Build, stage: u32, host: &str) {
270271
for f in t!(fs::read_dir(&src_libdir)).map(|f| t!(f)) {
271272
let filename = f.file_name().into_string().unwrap();
272273
if is_dylib(&filename) {
273-
t!(fs::hard_link(&f.path(), sysroot_libdir.join(&filename)));
274+
copy(&f.path(), &sysroot_libdir.join(&filename));
274275
}
275276
}
276277

@@ -282,15 +283,15 @@ pub fn assemble_rustc(build: &Build, stage: u32, host: &str) {
282283
t!(fs::create_dir_all(&bindir));
283284
let compiler = build.compiler_path(&Compiler::new(stage, host));
284285
let _ = fs::remove_file(&compiler);
285-
t!(fs::hard_link(rustc, compiler));
286+
copy(&rustc, &compiler);
286287

287288
// See if rustdoc exists to link it into place
288289
let rustdoc = exe("rustdoc", host);
289290
let rustdoc_src = out_dir.join(&rustdoc);
290291
let rustdoc_dst = bindir.join(&rustdoc);
291292
if fs::metadata(&rustdoc_src).is_ok() {
292293
let _ = fs::remove_file(&rustdoc_dst);
293-
t!(fs::hard_link(&rustdoc_src, &rustdoc_dst));
294+
copy(&rustdoc_src, &rustdoc_dst);
294295
}
295296
}
296297

@@ -329,8 +330,7 @@ fn add_to_sysroot(out_dir: &Path, sysroot_dst: &Path) {
329330
let (_, path) = paths.iter().map(|path| {
330331
(mtime(&path).seconds(), path)
331332
}).max().unwrap();
332-
t!(fs::hard_link(&path,
333-
sysroot_dst.join(path.file_name().unwrap())));
333+
copy(&path, &sysroot_dst.join(path.file_name().unwrap()));
334334
}
335335
}
336336

src/bootstrap/build/util.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ pub fn mtime(path: &Path) -> FileTime {
3030
}).unwrap_or(FileTime::zero())
3131
}
3232

33+
pub fn copy(src: &Path, dst: &Path) {
34+
let res = fs::hard_link(src, dst);
35+
let res = res.or_else(|_| fs::copy(src, dst).map(|_| ()));
36+
if let Err(e) = res {
37+
panic!("failed to copy `{}` to `{}`: {}", src.display(),
38+
dst.display(), e)
39+
}
40+
}
41+
3342
pub fn cp_r(src: &Path, dst: &Path) {
3443
for f in t!(fs::read_dir(src)) {
3544
let f = t!(f);
@@ -42,7 +51,7 @@ pub fn cp_r(src: &Path, dst: &Path) {
4251
cp_r(&path, &dst);
4352
} else {
4453
let _ = fs::remove_file(&dst);
45-
t!(fs::hard_link(&path, dst));
54+
copy(&path, &dst);
4655
}
4756
}
4857
}

0 commit comments

Comments
 (0)