@@ -21,10 +21,8 @@ use std::env;
21
21
/// clang version 13's source code:
22
22
/// https://github.com/llvm/llvm-project/blob/llvmorg-13.0.0/clang/include/clang/Basic/ObjCRuntime.h
23
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.
24
+ /// In short, it's not ultra important, but enables some optimizations if this
25
+ /// is specified.
28
26
type Version = Option < String > ;
29
27
30
28
// For clang "-fobjc-runtime" support
@@ -37,32 +35,28 @@ enum AppleRuntime {
37
35
}
38
36
use AppleRuntime :: * ;
39
37
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
38
enum Runtime {
53
39
Apple ( AppleRuntime ) ,
54
- GNUStep ( Version ) ,
55
- WinObjc ( Version ) ,
56
- ObjFW ( Version ) ,
40
+ GNUStep ( u8 , u8 ) ,
41
+ WinObjc ,
42
+ #[ allow( dead_code) ]
43
+ ObjFW ( Option < String > ) ,
57
44
}
58
45
use Runtime :: * ;
59
46
47
+ fn get_env ( env : & str ) -> Option < String > {
48
+ println ! ( "cargo:rerun-if-env-changed={}" , env) ;
49
+ match env:: var ( env) {
50
+ Ok ( var) => Some ( var) ,
51
+ Err ( env:: VarError :: NotPresent ) => None ,
52
+ Err ( env:: VarError :: NotUnicode ( var) ) => panic ! ( "Invalid unicode for {}: {:?}" , env, var) ,
53
+ }
54
+ }
55
+
60
56
fn main ( ) {
61
57
// The script doesn't depend on our code
62
58
println ! ( "cargo:rerun-if-changed=build.rs" ) ;
63
59
64
- println ! ( "cargo:rerun-if-env-changed=OBJC_RUNTIME" ) ;
65
-
66
60
// Used to figure out when BOOL should be i8 vs. bool
67
61
if env:: var ( "TARGET" ) . unwrap ( ) . ends_with ( "macabi" ) {
68
62
println ! ( "cargo:rustc-cfg=target_abi_macabi" ) ;
@@ -71,51 +65,36 @@ fn main() {
71
65
// TODO: Figure out when to enable this
72
66
// println!("cargo:rustc-cfg=libobjc2_strict_apple_compat");
73
67
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
- } ;
84
-
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
- }
104
- } else {
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
- }
68
+ let runtime = match get_env ( "RUNTIME_VERSION" ) . as_deref ( ) {
69
+ // Force using GNUStep on all platforms
70
+ Some ( "gnustep-1.7" ) => GNUStep ( 1 , 7 ) ,
71
+ Some ( "gnustep-1.8" ) => GNUStep ( 1 , 8 ) ,
72
+ Some ( "gnustep-1.9" ) => GNUStep ( 1 , 9 ) ,
73
+ Some ( "gnustep-2.0" ) => GNUStep ( 2 , 0 ) ,
74
+ Some ( "gnustep-2.1" ) => GNUStep ( 2 , 1 ) ,
75
+
76
+ // Pick the relevant runtime to use, and find target versions
77
+ None => match & * env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) {
78
+ "macos" => Apple ( MacOS ( Some (
79
+ get_env ( "MACOSX_DEPLOYMENT_TARGET" ) . unwrap_or_else ( || "10.7" . into ( ) ) ,
80
+ ) ) ) ,
81
+ "ios" => Apple ( IOS ( Some (
82
+ get_env ( "IPHONEOS_DEPLOYMENT_TARGET" ) . unwrap_or_else ( || "7.0" . into ( ) ) ,
83
+ ) ) ) ,
84
+ "tvos" => Apple ( TvOS ( get_env ( "TVOS_DEPLOYMENT_TARGET" ) ) ) ,
85
+ "watchos" => Apple ( WatchOS ( get_env ( "WATCHOS_DEPLOYMENT_TARGET" ) ) ) ,
86
+ "windows" => WinObjc ,
87
+ _ => GNUStep ( 1 , 8 ) , // GNUStep's own default
88
+ } ,
89
+
90
+ Some ( runtime) => panic ! ( "Invalid RUNTIME_VERSION: {}" , runtime) ,
112
91
} ;
113
92
114
93
// Add `#[cfg(RUNTIME)]` directive
115
94
let runtime_cfg = match runtime {
116
95
Apple ( _) => "apple" ,
117
- GNUStep ( _) => "gnustep" ,
118
- WinObjc ( _ ) => "winobjc" ,
96
+ GNUStep ( _, _ ) => "gnustep" ,
97
+ WinObjc => "winobjc" ,
119
98
ObjFW ( _) => "objfw" ,
120
99
} ;
121
100
println ! ( "cargo:rustc-cfg={}" , runtime_cfg) ;
@@ -140,17 +119,10 @@ fn main() {
140
119
}
141
120
}
142
121
}
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
- }
122
+ // Default in clang is 1.6
123
+ GNUStep ( major, minor) => format ! ( "gnustep-{}.{}" , major, minor) ,
124
+ // WinObjC's libobjc2 is just a fork of gnustep's from version 1.8
125
+ WinObjc => "gnustep-1.8" . into ( ) ,
154
126
ObjFW ( version) => {
155
127
// Default in clang
156
128
let _version = version. as_deref ( ) . unwrap_or ( "0.8" ) ;
@@ -160,7 +132,7 @@ fn main() {
160
132
161
133
// Add clang arguments
162
134
println ! (
163
- "cargo:clang_args=-fobjc-link-runtime -fobjc- arc -fobjc-arc-exceptions -fobjc-exceptions -fobjc-runtime={}" ,
135
+ "cargo:clang_args=-fobjc-arc -fobjc-arc-exceptions -fobjc-exceptions -fobjc-runtime={}" ,
164
136
// `-fobjc-link-runtime` -> people don't need to specify `-lobjc`.
165
137
166
138
// -fobjc-weak ?
0 commit comments