Skip to content

Commit 610b42d

Browse files
committed
Provide cross-compiling guidance when Apple SDK is missing
The SDK, along with a cross-linker, are the two main components required to link pure-Rust applications from non-macOS systems, but this can be hard to figure out.
1 parent fce0e74 commit 610b42d

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,23 @@ codegen_ssa_visual_studio_not_installed = you may need to install Visual Studio
404404
codegen_ssa_xcrun_command_line_tools_insufficient =
405405
when compiling for iOS, tvOS, visionOS or watchOS, you need a full installation of Xcode
406406
407+
codegen_ssa_xcrun_cross_download_sdk =
408+
the SDK can be downloaded and extracted from https://developer.apple.com/download/all/?q=xcode (requires an Apple ID).
409+
410+
The full Xcode bundle should contain the SDK in Xcode.app/Contents/Developer/Platforms/{$sdk_name}.platform/Developer/SDKs/{$sdk_name}.sdk{ $sdk_name ->
411+
[MacOSX] , but downloading just the "Command Line Tools for Xcode" should also be sufficient to obtain the macOS SDK.
412+
*[other] .
413+
}
414+
415+
codegen_ssa_xcrun_cross_env_var =
416+
pass the path to the SDK using the SDKROOT environment variable
417+
418+
codegen_ssa_xcrun_cross_ill_supported_target =
419+
cross-compiling to iOS, tvOS, visionOS or watchOS (in particular the linking step) is ill supported on non-macOS hosts
420+
421+
codegen_ssa_xcrun_cross_linker_not_explicitly_set =
422+
you will also need to use a linker capable of linking Mach-O files, consider using the bundled `lld` with `-Clinker=rust-lld`
423+
407424
codegen_ssa_xcrun_failed_invoking = invoking `{$command_formatted}` to find {$sdk_name}.sdk failed: {$error}
408425
409426
codegen_ssa_xcrun_found_developer_dir = found active developer directory at "{$developer_dir}"

compiler/rustc_codegen_ssa/src/back/apple.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,29 @@ pub(super) fn get_sdk_root(sess: &Session) -> Option<PathBuf> {
172172
let mut diag = sess.dcx().create_err(err);
173173

174174
// Recognize common error cases, and give more Rust-specific error messages for those.
175-
if let Some(developer_dir) = xcode_select_developer_dir() {
176-
diag.arg("developer_dir", &developer_dir);
177-
diag.note(fluent::codegen_ssa_xcrun_found_developer_dir);
178-
if developer_dir.as_os_str().to_string_lossy().contains("CommandLineTools") {
179-
if sdk_name != "MacOSX" {
180-
diag.help(fluent::codegen_ssa_xcrun_command_line_tools_insufficient);
175+
if sess.host.os == "macos" {
176+
if let Some(developer_dir) = xcode_select_developer_dir() {
177+
diag.arg("developer_dir", &developer_dir);
178+
diag.note(fluent::codegen_ssa_xcrun_found_developer_dir);
179+
if developer_dir.as_os_str().to_string_lossy().contains("CommandLineTools") {
180+
if sdk_name != "MacOSX" {
181+
diag.help(fluent::codegen_ssa_xcrun_command_line_tools_insufficient);
182+
}
181183
}
184+
} else {
185+
diag.help(fluent::codegen_ssa_xcrun_no_developer_dir);
182186
}
183187
} else {
184-
diag.help(fluent::codegen_ssa_xcrun_no_developer_dir);
188+
diag.help(fluent::codegen_ssa_xcrun_cross_env_var);
189+
diag.help(fluent::codegen_ssa_xcrun_cross_download_sdk);
190+
191+
if sess.opts.cg.linker.is_none() {
192+
diag.warn(fluent::codegen_ssa_xcrun_cross_linker_not_explicitly_set);
193+
}
194+
195+
if sess.target.os != "macos" {
196+
diag.warn(fluent::codegen_ssa_xcrun_cross_ill_supported_target);
197+
}
185198
}
186199

187200
diag.emit();

0 commit comments

Comments
 (0)