|
1 | | -use std::{env, path::Path}; |
| 1 | +use std::env; |
2 | 2 |
|
3 | 3 | use crate::spec::{LinkArgs, TargetOptions}; |
4 | 4 |
|
@@ -52,62 +52,18 @@ pub fn macos_llvm_target(arch: &str) -> String { |
52 | 52 | format!("{}-apple-macosx{}.{}.0", arch, major, minor) |
53 | 53 | } |
54 | 54 |
|
55 | | -#[cfg(target_os = "macos")] |
56 | | -pub fn sysroot(sdk: &str) -> Result<Option<String>, String> { |
57 | | - // Like Clang, allow the `SDKROOT` environment variable used by Xcode to define the sysroot. |
58 | | - if let Some(sdk_root) = env::var("SDKROOT").ok() { |
59 | | - let actual_sdk_path = sdk_path(sdk)?; |
60 | | - let sdk_root_p = Path::new(&sdk_root); |
61 | | - // Ignore `SDKROOT` if it's not a valid path. |
62 | | - if !sdk_root_p.is_absolute() || sdk_root_p == Path::new("/") || !sdk_root_p.exists() { |
63 | | - return Ok(Some(actual_sdk_path)); |
| 55 | +pub fn macos_link_env() -> Vec<(String, String)> { |
| 56 | + let mut env = Vec::with_capacity(2); |
| 57 | + // Ignore the `SDKROOT` environment variable if it's clearly set for the wrong platform, which |
| 58 | + // may occur when we're linking a custom build script while targeting iOS for example. |
| 59 | + if let Some(sdkroot) = env::var("SDKROOT").ok() { |
| 60 | + if sdkroot.contains("iPhoneOS.platform") || sdkroot.contains("iPhoneSimulator.platform") { |
| 61 | + env.push(("SDKROOT".to_string(), String::new())) |
64 | 62 | } |
65 | | - // Ignore `SDKROOT` if it's clearly set for the wrong platform, which may occur when we're |
66 | | - // compiling a custom build script while targeting iOS for example. |
67 | | - return Ok(Some(match sdk { |
68 | | - "iphoneos" if sdk_root.contains("iPhoneSimulator.platform") |
69 | | - || sdk_root.contains("MacOSX.platform") => actual_sdk_path, |
70 | | - "iphonesimulator" if sdk_root.contains("iPhoneOS.platform") |
71 | | - || sdk_root.contains("MacOSX.platform") => actual_sdk_path, |
72 | | - "macosx" | "macosx10.15" if sdk_root.contains("iPhoneOS.platform") |
73 | | - || sdk_root.contains("iPhoneSimulator.platform") => actual_sdk_path, |
74 | | - _ => sdk_root, |
75 | | - })) |
76 | | - } |
77 | | - Ok(None) |
78 | | -} |
79 | | - |
80 | | -// `xcrun` is only available on macOS. |
81 | | -#[cfg(not(target_os = "macos"))] |
82 | | -pub fn sysroot(_sdk: &str) -> Result<Option<String>, String> { |
83 | | - if let Some(sdk_root) = env::var("SDKROOT").ok() { |
84 | | - let sdk_root_p = Path::new(&sdk_root); |
85 | | - // Use `SDKROOT` only if it's a valid path. |
86 | | - if sdk_root_p.is_absolute() && sdk_root_p != Path::new("/") && sdk_root_p.exists() { |
87 | | - return Ok(Some(sdk_root)); |
88 | | - } |
89 | | - } |
90 | | - Ok(None) |
91 | | -} |
92 | | - |
93 | | -#[cfg(target_os = "macos")] |
94 | | -fn sdk_path(sdk_name: &str) -> Result<String, String> { |
95 | | - let res = std::process::Command::new("xcrun") |
96 | | - .arg("--show-sdk-path") |
97 | | - .arg("-sdk") |
98 | | - .arg(sdk_name) |
99 | | - .output() |
100 | | - .and_then(|output| { |
101 | | - if output.status.success() { |
102 | | - Ok(String::from_utf8(output.stdout).unwrap()) |
103 | | - } else { |
104 | | - let error = String::from_utf8(output.stderr); |
105 | | - let error = format!("process exit with error: {}", error.unwrap()); |
106 | | - Err(std::io::Error::new(std::io::ErrorKind::Other, &error[..])) |
107 | | - } |
108 | | - }); |
109 | | - match res { |
110 | | - Ok(output) => Ok(output.trim().to_string()), |
111 | | - Err(e) => Err(format!("failed to get {} SDK path: {}", sdk_name, e)), |
112 | 63 | } |
| 64 | + // Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at |
| 65 | + // "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld", |
| 66 | + // although this is apparently ignored when using the linker at "/usr/bin/ld". |
| 67 | + env.push(("IPHONEOS_DEPLOYMENT_TARGET".to_string(), String::new())); |
| 68 | + env |
113 | 69 | } |
0 commit comments