Skip to content

Commit 4b09dc6

Browse files
committed
Introduce crt_static target option in config.toml
This controls the value of the crt-static feature used when building the standard library for a target, as well as the compiler itself when that target is the host.
1 parent 0c7a0e9 commit 4b09dc6

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

config.toml.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@
292292
# build native code.
293293
#android-ndk = "/path/to/ndk"
294294

295+
# Force static or dynamic linkage of the standard library for this target. If
296+
# this target is a host for rustc, this will also affect the linkage of the
297+
# compiler itself. This is useful for building rustc on targets that normally
298+
# only use static libraries. If unset, the target's default linkage is used.
299+
#crt-static = false
300+
295301
# The root location of the MUSL installation directory. The library directory
296302
# will also need to contain libunwind.a for an unwinding implementation. Note
297303
# that this option only makes sense for MUSL targets that produce statically

src/bootstrap/bin/rustc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ fn main() {
242242
cmd.arg("-C").arg("target-feature=+crt-static");
243243
}
244244

245+
if let Ok(s) = env::var("RUSTC_CRT_STATIC") {
246+
if s == "true" {
247+
cmd.arg("-C").arg("target-feature=+crt-static");
248+
}
249+
if s == "false" {
250+
cmd.arg("-C").arg("target-feature=-crt-static");
251+
}
252+
}
253+
245254
// Force all crates compiled by this compiler to (a) be unstable and (b)
246255
// allow the `rustc_private` feature to link to other unstable crates
247256
// also in the sysroot.

src/bootstrap/builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ impl<'a> Builder<'a> {
503503
cargo.env("RUSTC_METADATA_SUFFIX", "rustc");
504504
}
505505

506+
if let Some(x) = self.crt_static(target) {
507+
cargo.env("RUSTC_CRT_STATIC", x.to_string());
508+
}
509+
506510
// Enable usage of unstable features
507511
cargo.env("RUSTC_BOOTSTRAP", "1");
508512
self.add_rust_test_threads(&mut cargo);

src/bootstrap/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub struct Target {
143143
pub cc: Option<PathBuf>,
144144
pub cxx: Option<PathBuf>,
145145
pub ndk: Option<PathBuf>,
146+
pub crt_static: Option<bool>,
146147
pub musl_root: Option<PathBuf>,
147148
pub qemu_rootfs: Option<PathBuf>,
148149
}
@@ -275,6 +276,7 @@ struct TomlTarget {
275276
cc: Option<String>,
276277
cxx: Option<String>,
277278
android_ndk: Option<String>,
279+
crt_static: Option<bool>,
278280
musl_root: Option<String>,
279281
qemu_rootfs: Option<String>,
280282
}
@@ -446,6 +448,7 @@ impl Config {
446448
}
447449
target.cxx = cfg.cxx.clone().map(PathBuf::from);
448450
target.cc = cfg.cc.clone().map(PathBuf::from);
451+
target.crt_static = cfg.crt_static.clone();
449452
target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
450453
target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
451454

src/bootstrap/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,12 @@ impl Build {
656656
base
657657
}
658658

659+
/// Returns if this target should statically link the C runtime, if specified
660+
fn crt_static(&self, target: Interned<String>) -> Option<bool> {
661+
self.config.target_config.get(&target)
662+
.and_then(|t| t.crt_static)
663+
}
664+
659665
/// Returns the "musl root" for this `target`, if defined
660666
fn musl_root(&self, target: Interned<String>) -> Option<&Path> {
661667
self.config.target_config.get(&target)

0 commit comments

Comments
 (0)