|
1 | 1 | use std::env;
|
2 | 2 |
|
| 3 | +/// TODO: Better validation of this |
| 4 | +/// |
| 5 | +/// The version is used for providing different behaviour when: |
| 6 | +/// - CGException.cpp getObjCPersonality (GNUStep >= 1.7) |
| 7 | +/// - Clang.cpp Clang::AddObjCRuntimeArgs (GNUStep >= 2.0) |
| 8 | +/// - isLegacyDispatchDefaultForArch (macOS < 10.6, GNUStep < 1.6) |
| 9 | +/// - hasNativeARC (macOS < 10.7, iOS < 5) |
| 10 | +/// - shouldUseARCFunctionsForRetainRelease (macOS < 10.10, iOS < 8) |
| 11 | +/// - shouldUseRuntimeFunctionsForAlloc (macOS < 10.10, iOS < 8) |
| 12 | +/// - shouldUseRuntimeFunctionForCombinedAllocInit (macOS >= 10.14.4, iOS >= 12.2, watchOS >= 5.2) |
| 13 | +/// - hasOptimizedSetter (macOS >= 10.8, iOS >= 6, GNUStep >= 1.7) |
| 14 | +/// - hasSubscripting (macOS < 10.11, iOS < 9) |
| 15 | +/// - hasTerminate (macOS < 10.8, iOS < 5) |
| 16 | +/// - hasARCUnsafeClaimAutoreleasedReturnValue (macOS >= 10.11, iOS >= 9, watchOS >= 2) |
| 17 | +/// - hasEmptyCollections (macOS >= 10.11, iOS >= 9, watchOS >= 2) |
| 18 | +/// - ... (incomplete) |
| 19 | +/// |
| 20 | +/// `macosx-fragile` and `gcc` was not considered in this analysis, made on |
| 21 | +/// clang version 13's source code: |
| 22 | +/// https://github.com/llvm/llvm-project/blob/llvmorg-13.0.0/clang/include/clang/Basic/ObjCRuntime.h |
| 23 | +/// |
| 24 | +/// Anyhow, it's not ultra important, but enables some optimizations if this |
| 25 | +/// is specified. In the future, Rust will hopefully get something similar to |
| 26 | +/// clang's `-mmacosx-version-min`, and then we won't need this for the |
| 27 | +/// Apple runtimes. |
| 28 | +type Version = Option<String>; |
| 29 | + |
| 30 | +// For clang "-fobjc-runtime" support |
| 31 | +enum AppleRuntime { |
| 32 | + MacOS(Version), |
| 33 | + IOS(Version), |
| 34 | + TvOS(Version), |
| 35 | + WatchOS(Version), |
| 36 | + // BridgeOS, |
| 37 | +} |
| 38 | +use AppleRuntime::*; |
| 39 | + |
| 40 | +impl AppleRuntime { |
| 41 | + fn get_default(target_os: &str) -> Option<Self> { |
| 42 | + match target_os { |
| 43 | + "macos" => Some(Self::MacOS(None)), |
| 44 | + "ios" => Some(Self::IOS(None)), |
| 45 | + "watchos" => Some(Self::WatchOS(None)), |
| 46 | + "tvos" => Some(Self::TvOS(None)), |
| 47 | + _ => None, |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +enum Runtime { |
| 53 | + Apple(AppleRuntime), |
| 54 | + GNUStep(Version), |
| 55 | + WinObjc(Version), |
| 56 | + ObjFW(Version), |
| 57 | +} |
| 58 | +use Runtime::*; |
| 59 | + |
3 | 60 | fn main() {
|
4 |
| - // Only rerun if this file changes; the script doesn't depend on our code |
| 61 | + // The script doesn't depend on our code |
5 | 62 | println!("cargo:rerun-if-changed=build.rs");
|
6 |
| - // Link to libobjc |
7 |
| - println!("cargo:rustc-link-lib=dylib=objc"); |
| 63 | + |
| 64 | + println!("cargo:rerun-if-env-changed=OBJC_RUNTIME"); |
| 65 | + |
| 66 | + // Used to figure out when BOOL should be i8 vs. bool |
| 67 | + if env::var("TARGET").unwrap().ends_with("macabi") { |
| 68 | + println!("cargo:rustc-cfg=target_abi_macabi"); |
| 69 | + } |
| 70 | + |
| 71 | + // TODO: Figure out when to enable this |
| 72 | + // println!("cargo:rustc-cfg=libobjc2_strict_apple_compat"); |
8 | 73 |
|
9 | 74 | let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
|
| 75 | + let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); |
| 76 | + |
| 77 | + // OBJC_RUNTIME syntax: `RUNTIME ("-" VERSION)?` |
| 78 | + let runtime = if let Ok(runtime) = env::var("OBJC_RUNTIME") { |
| 79 | + let (runtime, version) = if let Some((runtime, version)) = runtime.split_once('-') { |
| 80 | + (runtime, Some(version.into())) |
| 81 | + } else { |
| 82 | + (&*runtime, None) |
| 83 | + }; |
10 | 84 |
|
11 |
| - // Adds useful #[cfg(apple)] and #[cfg(gnustep)] directives |
12 |
| - if target_vendor == "apple" { |
13 |
| - println!("cargo:rustc-cfg=apple"); |
| 85 | + match runtime { |
| 86 | + "apple" => { |
| 87 | + if version.is_some() { |
| 88 | + panic!("Invalid OBJC_RUNTIME: Version doesn't make sense for the `apple` runtime; use `macos`, `ios`, `tvos` or `watchos`"); |
| 89 | + } |
| 90 | + Apple(AppleRuntime::get_default(&target_os).expect("Invalid OBJC_RUNTIME: Target OS invalid for the `apple` runtime; specify manually with `macos`, `ios`, `tvos` or `watchos`")) |
| 91 | + } |
| 92 | + "gnustep" => GNUStep(version), |
| 93 | + "winobjc" => WinObjc(version), |
| 94 | + "objfw" => ObjFW(version), |
| 95 | + // Support clang "syntax" (`macosx`) |
| 96 | + "macos" | "macosx" => Apple(MacOS(version)), |
| 97 | + "ios" => Apple(IOS(version)), |
| 98 | + "tvos" => Apple(TvOS(version)), |
| 99 | + "watchos" => Apple(WatchOS(version)), |
| 100 | + _ => { |
| 101 | + panic!("Invalid OBJC_RUNTIME: {}", runtime) |
| 102 | + } |
| 103 | + } |
14 | 104 | } else {
|
15 |
| - // TODO: Are there other possibilities than GNUStep? Airyx? Is it |
16 |
| - // possible to use Apple's open source objc4 on other platforms? |
17 |
| - println!("cargo:rustc-cfg=gnustep"); |
18 |
| - // TODO: Should we vendor GNUStep's libobjc2? |
19 |
| - // Using Cargo.toml: |
20 |
| - // [target.'cfg(not(target_vendor = "apple"))'.build-dependencies] |
21 |
| - // cc = "1.0" |
22 |
| - } |
| 105 | + if target_vendor == "apple" { |
| 106 | + Apple(AppleRuntime::get_default(&target_os).unwrap()) |
| 107 | + } else if target_os == "windows" { |
| 108 | + WinObjc(None) |
| 109 | + } else { |
| 110 | + GNUStep(None) |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + // Add `#[cfg(RUNTIME)]` directive |
| 115 | + let runtime_cfg = match runtime { |
| 116 | + Apple(_) => "apple", |
| 117 | + GNUStep(_) => "gnustep", |
| 118 | + WinObjc(_) => "winobjc", |
| 119 | + ObjFW(_) => "objfw", |
| 120 | + }; |
| 121 | + println!("cargo:rustc-cfg={}", runtime_cfg); |
| 122 | + // Allow downstream build scripts to do the same |
| 123 | + println!("cargo:runtime={}", runtime_cfg); // DEP_OBJC_RUNTIME |
| 124 | + |
| 125 | + let clang_runtime = match &runtime { |
| 126 | + Apple(runtime) => { |
| 127 | + let clang_runtime_str = match runtime { |
| 128 | + MacOS(_) => "macosx", |
| 129 | + IOS(_) => "ios", |
| 130 | + WatchOS(_) => "watchos", |
| 131 | + TvOS(_) => "ios", // ?? |
| 132 | + }; |
| 133 | + match runtime { |
| 134 | + MacOS(version) | IOS(version) | WatchOS(version) | TvOS(version) => { |
| 135 | + if let Some(version) = version { |
| 136 | + format!("{}-{}", clang_runtime_str, version) |
| 137 | + } else { |
| 138 | + clang_runtime_str.into() |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + GNUStep(version) => { |
| 144 | + // GNUStep default in clang is 1.6; we require at least 1.7 |
| 145 | + let version = version.as_deref().unwrap_or("1.7"); |
| 146 | + format!("gnustep-{}", version) |
| 147 | + } |
| 148 | + WinObjc(version) => { |
| 149 | + // WinObjc use a small fork of GNUStep version 1.8; so lower |
| 150 | + // versions doesn't make sense. |
| 151 | + let version = version.as_deref().unwrap_or("1.8"); |
| 152 | + format!("gnustep-{}", version) |
| 153 | + } |
| 154 | + ObjFW(version) => { |
| 155 | + // Default in clang |
| 156 | + let _version = version.as_deref().unwrap_or("0.8"); |
| 157 | + todo!() |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + // Add clang arguments |
| 162 | + println!( |
| 163 | + "cargo:clang_args=-fobjc-arc -fobjc-arc-exceptions -fobjc-exceptions -fobjc-weak -fobjc-runtime={}", |
| 164 | + // -fobjc-arc implies -fobjc-link-runtime, so people actually don't |
| 165 | + // even need to specify `-lobjc` (though they probably still should). |
| 166 | + clang_runtime |
| 167 | + ); // DEP_OBJC_CLANG_ARGS |
| 168 | + |
| 169 | + // Add GCC arguments. Not really supported |
| 170 | + match runtime { |
| 171 | + Apple(_) => { |
| 172 | + println!("cargo:gcc_args=-fnext-runtime -fobjc-exceptions -fobjc-abi-version=2") |
| 173 | + } |
| 174 | + _ => println!("cargo:gcc_args=-fgnu-runtime -fobjc-exceptions"), |
| 175 | + } // DEP_OBJC_GCC_ARGS |
| 176 | + |
| 177 | + // Link to libobjc |
| 178 | + println!("cargo:rustc-link-lib=dylib=objc"); |
23 | 179 | }
|
0 commit comments