Skip to content

Commit f4a9110

Browse files
committed
Only warn when invoking xcrun
To allow using zig-cc or similar as the compiler driver.
1 parent 1cc44bf commit f4a9110

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ codegen_ssa_version_script_write_failure = failed to write version script: {$err
401401
402402
codegen_ssa_visual_studio_not_installed = you may need to install Visual Studio build tools with the "C++ build tools" workload
403403
404+
codegen_ssa_xcrun_about =
405+
the SDK is needed by the linker to know where to find symbols in system libraries and for embedding the SDK version in the final object file
406+
404407
codegen_ssa_xcrun_command_line_tools_insufficient =
405408
when compiling for iOS, tvOS, visionOS or watchOS, you need a full installation of Xcode
406409

compiler/rustc_codegen_ssa/src/back/apple.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ pub(super) fn add_version_to_llvm_target(
160160
pub(super) fn get_sdk_root(sess: &Session) -> Option<PathBuf> {
161161
let sdk_name = sdk_name(&sess.target);
162162

163+
// Attempt to invoke `xcrun` to find the SDK.
164+
//
165+
// Note that when cross-compiling from e.g. Linux, the `xcrun` binary may sometimes be provided
166+
// as a shim by a cross-compilation helper tool. It usually isn't, but we still try nonetheless.
163167
match xcrun_show_sdk_path(sdk_name, sess.verbose_internals()) {
164168
Ok((path, stderr)) => {
165169
// Emit extra stderr, such as if `-verbose` was passed, or if `xcrun` emitted a warning.
@@ -169,7 +173,19 @@ pub(super) fn get_sdk_root(sess: &Session) -> Option<PathBuf> {
169173
Some(path)
170174
}
171175
Err(err) => {
172-
let mut diag = sess.dcx().create_err(err);
176+
// Failure to find the SDK is not a hard error, since the user might have specified it
177+
// in a manner unknown to us (moreso if cross-compiling):
178+
// - A compiler driver like `zig cc` which links using an internally bundled SDK.
179+
// - Extra linker arguments (`-Clink-arg=-syslibroot`).
180+
// - A custom linker or custom compiler driver.
181+
//
182+
// Though we still warn, since such cases are uncommon, and it is very hard to debug if
183+
// you do not know the details.
184+
//
185+
// FIXME(madsmtm): Make this a lint, to allow deny warnings to work.
186+
// (Or fix <https://github.com/rust-lang/rust/issues/21204>).
187+
let mut diag = sess.dcx().create_warn(err);
188+
diag.note(fluent::codegen_ssa_xcrun_about);
173189

174190
// Recognize common error cases, and give more Rust-specific error messages for those.
175191
if let Some(developer_dir) = xcode_select_developer_dir() {
@@ -209,6 +225,8 @@ fn xcrun_show_sdk_path(
209225
sdk_name: &'static str,
210226
verbose: bool,
211227
) -> Result<(PathBuf, String), XcrunError> {
228+
// Intentionally invoke the `xcrun` in PATH, since e.g. nixpkgs provide an `xcrun` shim, so we
229+
// don't want to require `/usr/bin/xcrun`.
212230
let mut cmd = Command::new("xcrun");
213231
if verbose {
214232
cmd.arg("--verbose");
@@ -280,7 +298,7 @@ fn stdout_to_path(mut stdout: Vec<u8>) -> PathBuf {
280298
}
281299
#[cfg(unix)]
282300
let path = <OsString as std::os::unix::ffi::OsStringExt>::from_vec(stdout);
283-
#[cfg(not(unix))] // Unimportant, this is only used on macOS
284-
let path = OsString::from(String::from_utf8(stdout).unwrap());
301+
#[cfg(not(unix))] // Not so important, this is mostly used on macOS
302+
let path = OsString::from(String::from_utf8(stdout).expect("stdout must be UTF-8"));
285303
PathBuf::from(path)
286304
}

0 commit comments

Comments
 (0)