Skip to content

Commit 9ceb9bb

Browse files
committed
Move copying of self-contained objects to new function
1 parent 50c0192 commit 9ceb9bb

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

src/bootstrap/compile.rs

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ impl Step for Std {
7474
// Even if we're not building std this stage, the new sysroot must
7575
// still contain the third party objects needed by various targets.
7676
copy_third_party_objects(builder, &compiler, target);
77+
copy_self_contained_objects(builder, &compiler, target);
7778

7879
builder.ensure(StdLink {
7980
compiler: compiler_to_use,
@@ -84,6 +85,7 @@ impl Step for Std {
8485
}
8586

8687
target_deps.extend(copy_third_party_objects(builder, &compiler, target).into_iter());
88+
target_deps.extend(copy_self_contained_objects(builder, &compiler, target));
8789

8890
let mut cargo = builder.cargo(compiler, Mode::Std, target, "build");
8991
std_cargo(builder, target, compiler.stage, &mut cargo);
@@ -109,39 +111,28 @@ impl Step for Std {
109111
}
110112
}
111113

114+
fn copy_and_stamp(
115+
builder: &Builder<'_>,
116+
libdir: &Path,
117+
sourcedir: &Path,
118+
name: &str,
119+
target_deps: &mut Vec<PathBuf>,
120+
) {
121+
let target = libdir.join(name);
122+
builder.copy(&sourcedir.join(name), &target);
123+
124+
target_deps.push((target, dependency_type));
125+
}
126+
112127
/// Copies third party objects needed by various targets.
113128
fn copy_third_party_objects(
114129
builder: &Builder<'_>,
115130
compiler: &Compiler,
116131
target: Interned<String>,
117132
) -> Vec<PathBuf> {
118133
let libdir = builder.sysroot_libdir(*compiler, target);
119-
120134
let mut target_deps = vec![];
121135

122-
let mut copy_and_stamp = |sourcedir: &Path, name: &str| {
123-
let target = libdir.join(name);
124-
builder.copy(&sourcedir.join(name), &target);
125-
target_deps.push(target);
126-
};
127-
128-
// Copies the CRT objects.
129-
//
130-
// rustc historically provides a more self-contained installation for musl targets
131-
// not requiring the presence of a native musl toolchain. For example, it can fall back
132-
// to using gcc from a glibc-targeting toolchain for linking.
133-
// To do that we have to distribute musl startup objects as a part of Rust toolchain
134-
// and link with them manually in the self-contained mode.
135-
if target.contains("musl") {
136-
let srcdir = builder.musl_root(target).unwrap().join("lib");
137-
for &obj in &["crt1.o", "Scrt1.o", "rcrt1.o", "crti.o", "crtn.o"] {
138-
copy_and_stamp(&srcdir, obj);
139-
}
140-
} else if target.ends_with("-wasi") {
141-
let srcdir = builder.wasi_root(target).unwrap().join("lib/wasm32-wasi");
142-
copy_and_stamp(&srcdir, "crt1.o");
143-
}
144-
145136
// Copies libunwind.a compiled to be linked with x86_64-fortanix-unknown-sgx.
146137
//
147138
// This target needs to be linked to Fortanix's port of llvm's libunwind.
@@ -151,7 +142,13 @@ fn copy_third_party_objects(
151142
let src_path_env = "X86_FORTANIX_SGX_LIBS";
152143
let src =
153144
env::var(src_path_env).unwrap_or_else(|_| panic!("{} not found in env", src_path_env));
154-
copy_and_stamp(Path::new(&src), "libunwind.a");
145+
copy_and_stamp(
146+
builder,
147+
&*libdir,
148+
Path::new(&src),
149+
"libunwind.a",
150+
&mut target_deps,
151+
);
155152
}
156153

157154
if builder.config.sanitizers && compiler.stage != 0 {
@@ -163,6 +160,47 @@ fn copy_third_party_objects(
163160
target_deps
164161
}
165162

163+
/// Copies third party objects needed by various targets for self-contained linkage.
164+
fn copy_self_contained_objects(
165+
builder: &Builder<'_>,
166+
compiler: &Compiler,
167+
target: Interned<String>,
168+
) -> Vec<PathBuf> {
169+
let libdir = builder.sysroot_libdir(*compiler, target);
170+
let mut target_deps = vec![];
171+
172+
// Copies the CRT objects.
173+
//
174+
// rustc historically provides a more self-contained installation for musl targets
175+
// not requiring the presence of a native musl toolchain. For example, it can fall back
176+
// to using gcc from a glibc-targeting toolchain for linking.
177+
// To do that we have to distribute musl startup objects as a part of Rust toolchain
178+
// and link with them manually in the self-contained mode.
179+
if target.contains("musl") {
180+
let srcdir = builder.musl_root(target).unwrap().join("lib");
181+
for &obj in &["crt1.o", "Scrt1.o", "rcrt1.o", "crti.o", "crtn.o"] {
182+
copy_and_stamp(
183+
builder,
184+
&libdir_self_contained,
185+
&srcdir,
186+
obj,
187+
&mut target_deps,
188+
);
189+
}
190+
} else if target.ends_with("-wasi") {
191+
let srcdir = builder.wasi_root(target).unwrap().join("lib/wasm32-wasi");
192+
copy_and_stamp(
193+
builder,
194+
&libdir_self_contained,
195+
&srcdir,
196+
"crt1.o",
197+
&mut target_deps,
198+
);
199+
}
200+
201+
target_deps
202+
}
203+
166204
/// Configure cargo to compile the standard library, adding appropriate env vars
167205
/// and such.
168206
pub fn std_cargo(builder: &Builder<'_>, target: Interned<String>, stage: u32, cargo: &mut Cargo) {

0 commit comments

Comments
 (0)