@@ -29,9 +29,17 @@ struct SwiftEnv {
29
29
}
30
30
31
31
impl SwiftEnv {
32
- fn new ( minimum_macos_version : & str , minimum_ios_version : Option < & str > ) -> Self {
32
+ fn new (
33
+ minimum_macos_version : & str ,
34
+ minimum_ios_version : Option < & str > ,
35
+ minimum_visionos_version : Option < & str > ,
36
+ ) -> Self {
33
37
let rust_target = RustTarget :: from_env ( ) ;
34
- let target = rust_target. swift_target_triple ( minimum_macos_version, minimum_ios_version) ;
38
+ let target = rust_target. swift_target_triple (
39
+ minimum_macos_version,
40
+ minimum_ios_version,
41
+ minimum_visionos_version,
42
+ ) ;
35
43
36
44
let swift_target_info_str = Command :: new ( "swift" )
37
45
. args ( [ "-target" , & target, "-print-target-info" ] )
@@ -47,13 +55,15 @@ impl SwiftEnv {
47
55
enum RustTargetOS {
48
56
MacOS ,
49
57
IOS ,
58
+ VisionOS ,
50
59
}
51
60
52
61
impl RustTargetOS {
53
62
fn from_env ( ) -> Self {
54
63
match env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) . as_str ( ) {
55
64
"macos" => RustTargetOS :: MacOS ,
56
65
"ios" => RustTargetOS :: IOS ,
66
+ "visionos" => RustTargetOS :: VisionOS ,
57
67
_ => panic ! ( "unexpected target operating system" ) ,
58
68
}
59
69
}
@@ -62,6 +72,7 @@ impl RustTargetOS {
62
72
match self {
63
73
Self :: MacOS => "macosx" ,
64
74
Self :: IOS => "ios" ,
75
+ Self :: VisionOS => "xros" ,
65
76
}
66
77
}
67
78
}
@@ -71,6 +82,7 @@ impl Display for RustTargetOS {
71
82
match self {
72
83
Self :: MacOS => write ! ( f, "macos" ) ,
73
84
Self :: IOS => write ! ( f, "ios" ) ,
85
+ Self :: VisionOS => write ! ( f, "visionos" ) ,
74
86
}
75
87
}
76
88
}
@@ -80,18 +92,23 @@ enum SwiftSDK {
80
92
MacOS ,
81
93
IOS ,
82
94
IOSSimulator ,
95
+ VisionOS ,
96
+ VisionOSSimulator ,
83
97
}
84
98
85
99
impl SwiftSDK {
86
100
fn from_os ( os : & RustTargetOS ) -> Self {
87
101
let target = env:: var ( "TARGET" ) . unwrap ( ) ;
88
102
let simulator = target. ends_with ( "ios-sim" )
103
+ || target. ends_with ( "visionos-sim" )
89
104
|| ( target. starts_with ( "x86_64" ) && target. ends_with ( "ios" ) ) ;
90
105
91
106
match os {
92
107
RustTargetOS :: MacOS => Self :: MacOS ,
93
108
RustTargetOS :: IOS if simulator => Self :: IOSSimulator ,
94
109
RustTargetOS :: IOS => Self :: IOS ,
110
+ RustTargetOS :: VisionOS if simulator => Self :: VisionOSSimulator ,
111
+ RustTargetOS :: VisionOS => Self :: VisionOS ,
95
112
}
96
113
}
97
114
@@ -100,6 +117,8 @@ impl SwiftSDK {
100
117
Self :: MacOS => "osx" ,
101
118
Self :: IOS => "ios" ,
102
119
Self :: IOSSimulator => "iossim" ,
120
+ Self :: VisionOS => "xros" ,
121
+ Self :: VisionOSSimulator => "xrsimulator" ,
103
122
}
104
123
}
105
124
}
@@ -110,6 +129,8 @@ impl Display for SwiftSDK {
110
129
Self :: MacOS => write ! ( f, "macosx" ) ,
111
130
Self :: IOSSimulator => write ! ( f, "iphonesimulator" ) ,
112
131
Self :: IOS => write ! ( f, "iphoneos" ) ,
132
+ Self :: VisionOSSimulator => write ! ( f, "xrsimulator" ) ,
133
+ Self :: VisionOS => write ! ( f, "xros" ) ,
113
134
}
114
135
}
115
136
}
@@ -133,19 +154,23 @@ impl RustTarget {
133
154
& self ,
134
155
minimum_macos_version : & str ,
135
156
minimum_ios_version : Option < & str > ,
157
+ minimum_visionos_version : Option < & str > ,
136
158
) -> String {
137
159
let unversioned = self . unversioned_swift_target_triple ( ) ;
138
160
format ! (
139
161
"{unversioned}{}{}" ,
140
- match ( & self . os, minimum_ios_version ) {
141
- ( RustTargetOS :: MacOS , _ ) => minimum_macos_version,
142
- ( RustTargetOS :: IOS , Some ( version ) ) => version ,
143
- _ => "" ,
162
+ match & self . os {
163
+ RustTargetOS :: MacOS => minimum_macos_version,
164
+ RustTargetOS :: IOS => minimum_ios_version . unwrap ( ) ,
165
+ RustTargetOS :: VisionOS => minimum_visionos_version . unwrap ( ) ,
144
166
} ,
145
167
// simulator suffix
146
- matches!( self . sdk, SwiftSDK :: IOSSimulator )
147
- . then( || "-simulator" . to_string( ) )
148
- . unwrap_or_default( )
168
+ matches!(
169
+ self . sdk,
170
+ SwiftSDK :: IOSSimulator | SwiftSDK :: VisionOSSimulator
171
+ )
172
+ . then( || "-simulator" . to_string( ) )
173
+ . unwrap_or_default( )
149
174
)
150
175
}
151
176
@@ -172,6 +197,7 @@ pub struct SwiftLinker {
172
197
packages : Vec < SwiftPackage > ,
173
198
macos_min_version : String ,
174
199
ios_min_version : Option < String > ,
200
+ visionos_min_version : Option < String > ,
175
201
}
176
202
177
203
impl SwiftLinker {
@@ -183,6 +209,7 @@ impl SwiftLinker {
183
209
packages : vec ! [ ] ,
184
210
macos_min_version : macos_min_version. to_string ( ) ,
185
211
ios_min_version : None ,
212
+ visionos_min_version : None ,
186
213
}
187
214
}
188
215
@@ -195,6 +222,15 @@ impl SwiftLinker {
195
222
self
196
223
}
197
224
225
+ /// Instructs the [`SwiftLinker`] to also compile for visionOS
226
+ /// using the specified minimum visionOS version.
227
+ ///
228
+ /// Minimum visionOS version must be at least 11.
229
+ pub fn with_visionos ( mut self , min_version : & str ) -> Self {
230
+ self . visionos_min_version = Some ( min_version. to_string ( ) ) ;
231
+ self
232
+ }
233
+
198
234
/// Adds a package to be linked against.
199
235
/// `name` should match the `name` field in your `Package.swift`,
200
236
/// and `path` should point to the root of your Swift package relative
@@ -212,7 +248,11 @@ impl SwiftLinker {
212
248
/// This does not (yet) automatically rebuild your Swift files when they are modified,
213
249
/// you'll need to modify/save your `build.rs` file for that.
214
250
pub fn link ( self ) {
215
- let swift_env = SwiftEnv :: new ( & self . macos_min_version , self . ios_min_version . as_deref ( ) ) ;
251
+ let swift_env = SwiftEnv :: new (
252
+ & self . macos_min_version ,
253
+ self . ios_min_version . as_deref ( ) ,
254
+ self . visionos_min_version . as_deref ( ) ,
255
+ ) ;
216
256
217
257
#[ allow( clippy:: uninlined_format_args) ]
218
258
for path in swift_env. paths . runtime_library_paths {
@@ -253,8 +293,11 @@ impl SwiftLinker {
253
293
arch => arch,
254
294
} ;
255
295
256
- let swift_target_triple = rust_target
257
- . swift_target_triple ( & self . macos_min_version , self . ios_min_version . as_deref ( ) ) ;
296
+ let swift_target_triple = rust_target. swift_target_triple (
297
+ & self . macos_min_version ,
298
+ self . ios_min_version . as_deref ( ) ,
299
+ self . visionos_min_version . as_deref ( ) ,
300
+ ) ;
258
301
259
302
command
260
303
// Build the package (duh)
@@ -277,6 +320,8 @@ impl SwiftLinker {
277
320
. args ( [ "-Xcc" , & format ! ( "--target={swift_target_triple}" ) ] )
278
321
. args ( [ "-Xcxx" , & format ! ( "--target={swift_target_triple}" ) ] ) ;
279
322
323
+ println ! ( "Command `{command:?}`" ) ;
324
+
280
325
if !command. status ( ) . unwrap ( ) . success ( ) {
281
326
panic ! ( "Failed to compile swift package {}" , package. name) ;
282
327
}
0 commit comments