Skip to content

Commit 8dee3e9

Browse files
authored
Rollup merge of #131405 - davidtwco:hardcoded-strip-macos, r=jieyouxu,albertlarsan68
bootstrap/codegen_ssa: ship llvm-strip and use it for -Cstrip Fixes #131206. - Includes `llvm-strip` (a symlink to `llvm-objcopy`) in the compiler dist artifact so that it can be used for `-Cstrip` instead of the system tooling. - Uses `llvm-strip` instead of `/usr/bin/strip` for macOS. macOS needs a specific linker and the system one is preferred, hence #130781 but that doesn't work when cross-compiling, so use the `llvm-strip` utility instead. cc #123151
2 parents efa5af9 + f745467 commit 8dee3e9

File tree

6 files changed

+47
-11
lines changed

6 files changed

+47
-11
lines changed

compiler/rustc_codegen_ssa/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ codegen_ssa_L4Bender_exporting_symbols_unimplemented = exporting symbols not imp
22
33
codegen_ssa_add_native_library = failed to add native library {$library_path}: {$error}
44
5+
codegen_ssa_aix_strip_not_used = using host's `strip` binary to cross-compile to AIX which is not guaranteed to work
6+
57
codegen_ssa_apple_deployment_target_invalid =
68
failed to parse deployment target specified in {$env_var}: {$error}
79

compiler/rustc_codegen_ssa/src/back/link.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -1085,9 +1085,7 @@ fn link_natively(
10851085
let strip = sess.opts.cg.strip;
10861086

10871087
if sess.target.is_like_osx {
1088-
// Use system `strip` when running on host macOS.
1089-
// <https://github.com/rust-lang/rust/pull/130781>
1090-
let stripcmd = if cfg!(target_os = "macos") { "/usr/bin/strip" } else { "strip" };
1088+
let stripcmd = "rust-objcopy";
10911089
match (strip, crate_type) {
10921090
(Strip::Debuginfo, _) => {
10931091
strip_symbols_with_external_utility(sess, stripcmd, out_filename, Some("-S"))
@@ -1103,11 +1101,14 @@ fn link_natively(
11031101
}
11041102
}
11051103

1106-
if sess.target.os == "illumos" {
1104+
if sess.target.is_like_solaris {
11071105
// Many illumos systems will have both the native 'strip' utility and
11081106
// the GNU one. Use the native version explicitly and do not rely on
11091107
// what's in the path.
1110-
let stripcmd = "/usr/bin/strip";
1108+
//
1109+
// If cross-compiling and there is not a native version, then use
1110+
// `llvm-strip` and hope.
1111+
let stripcmd = if !sess.host.is_like_solaris { "rust-objcopy" } else { "/usr/bin/strip" };
11111112
match strip {
11121113
// Always preserve the symbol table (-x).
11131114
Strip::Debuginfo => {
@@ -1120,6 +1121,10 @@ fn link_natively(
11201121
}
11211122

11221123
if sess.target.is_like_aix {
1124+
// `llvm-strip` doesn't work for AIX - their strip must be used.
1125+
if !sess.host.is_like_aix {
1126+
sess.dcx().emit_warn(errors::AixStripNotUsed);
1127+
}
11231128
let stripcmd = "/usr/bin/strip";
11241129
match strip {
11251130
Strip::Debuginfo => {
@@ -1147,6 +1152,13 @@ fn strip_symbols_with_external_utility(
11471152
if let Some(option) = option {
11481153
cmd.arg(option);
11491154
}
1155+
1156+
let mut new_path = sess.get_tools_search_paths(false);
1157+
if let Some(path) = env::var_os("PATH") {
1158+
new_path.extend(env::split_paths(&path));
1159+
}
1160+
cmd.env("PATH", env::join_paths(new_path).unwrap());
1161+
11501162
let prog = cmd.arg(out_filename).output();
11511163
match prog {
11521164
Ok(prog) => {

compiler/rustc_codegen_ssa/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1110,3 +1110,7 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for TargetFeatureDisableOrEnable<'_
11101110
diag
11111111
}
11121112
}
1113+
1114+
#[derive(Diagnostic)]
1115+
#[diag(codegen_ssa_aix_strip_not_used)]
1116+
pub(crate) struct AixStripNotUsed;

src/bootstrap/src/core/build_steps/compile.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ impl Step for Std {
219219
.join(compiler.host)
220220
.join("bin");
221221
if src_sysroot_bin.exists() {
222-
let target_sysroot_bin =
223-
builder.sysroot_target_libdir(compiler, target).parent().unwrap().join("bin");
222+
let target_sysroot_bin = builder.sysroot_target_bindir(compiler, target);
224223
t!(fs::create_dir_all(&target_sysroot_bin));
225224
builder.cp_link_r(&src_sysroot_bin, &target_sysroot_bin);
226225
}
@@ -1977,6 +1976,14 @@ impl Step for Assemble {
19771976
}
19781977
}
19791978

1979+
{
1980+
// `llvm-strip` is used by rustc, which is actually just a symlink to `llvm-objcopy`,
1981+
// so copy and rename `llvm-objcopy`.
1982+
let src_exe = exe("llvm-objcopy", target_compiler.host);
1983+
let dst_exe = exe("rust-objcopy", target_compiler.host);
1984+
builder.copy_link(&libdir_bin.join(src_exe), &libdir_bin.join(dst_exe));
1985+
}
1986+
19801987
// In addition to `rust-lld` also install `wasm-component-ld` when
19811988
// LLD is enabled. This is a relatively small binary that primarily
19821989
// delegates to the `rust-lld` binary for linking and then runs

src/bootstrap/src/core/build_steps/dist.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,7 @@ impl Step for Rustc {
459459

460460
// Copy over lld if it's there
461461
if builder.config.lld_enabled {
462-
let src_dir =
463-
builder.sysroot_target_libdir(compiler, host).parent().unwrap().join("bin");
462+
let src_dir = builder.sysroot_target_bindir(compiler, host);
464463
let rust_lld = exe("rust-lld", compiler.host);
465464
builder.copy_link(&src_dir.join(&rust_lld), &dst_dir.join(&rust_lld));
466465
let self_contained_lld_src_dir = src_dir.join("gcc-ld");
@@ -474,9 +473,16 @@ impl Step for Rustc {
474473
);
475474
}
476475
}
476+
477+
{
478+
let src_dir = builder.sysroot_target_bindir(compiler, host);
479+
let llvm_objcopy = exe("llvm-objcopy", compiler.host);
480+
let rust_objcopy = exe("rust-objcopy", compiler.host);
481+
builder.copy_link(&src_dir.join(&llvm_objcopy), &dst_dir.join(&rust_objcopy));
482+
}
483+
477484
if builder.tool_enabled("wasm-component-ld") {
478-
let src_dir =
479-
builder.sysroot_target_libdir(compiler, host).parent().unwrap().join("bin");
485+
let src_dir = builder.sysroot_target_bindir(compiler, host);
480486
let ld = exe("wasm-component-ld", compiler.host);
481487
builder.copy_link(&src_dir.join(&ld), &dst_dir.join(&ld));
482488
}

src/bootstrap/src/core/builder/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,11 @@ impl<'a> Builder<'a> {
11611161
self.ensure(compile::Sysroot::new(compiler))
11621162
}
11631163

1164+
/// Returns the bindir for a compiler's sysroot.
1165+
pub fn sysroot_target_bindir(&self, compiler: Compiler, target: TargetSelection) -> PathBuf {
1166+
self.sysroot_target_libdir(compiler, target).parent().unwrap().join("bin")
1167+
}
1168+
11641169
/// Returns the libdir where the standard library and other artifacts are
11651170
/// found for a compiler's sysroot.
11661171
pub fn sysroot_target_libdir(&self, compiler: Compiler, target: TargetSelection) -> PathBuf {

0 commit comments

Comments
 (0)