@@ -244,6 +244,27 @@ impl SwiftLinker {
244244 self
245245 }
246246
247+ // Gets the sysroot from cc by looking for the -sysroot flag. Used b/c neither SDKROOT nor
248+ // SYSROOT environment variables are set when running using hermetic bazel.
249+ fn sysroot_from_cc ( & self ) -> Option < String > {
250+ let cc = std:: env:: var ( "CC" ) . ok ( ) ?;
251+ let out = Command :: new ( cc)
252+ . args ( [ "-v" , "-E" , "-" ] )
253+ . stdin ( std:: process:: Stdio :: null ( ) )
254+ . output ( )
255+ . ok ( ) ?;
256+ let s = std:: str:: from_utf8 ( & out. stderr ) . ok ( ) ?;
257+ let mut it = s. split_whitespace ( ) ;
258+ while let Some ( t) = it. next ( ) {
259+ if t == "-isysroot" {
260+ if let Some ( p) = it. next ( ) {
261+ return Some ( p. to_string ( ) ) ;
262+ }
263+ }
264+ }
265+ None
266+ }
267+
247268 /// Links the Swift runtime, then builds and links the provided packages.
248269 /// This does not (yet) automatically rebuild your Swift files when they are modified,
249270 /// you'll need to modify/save your `build.rs` file for that.
@@ -265,25 +286,37 @@ impl SwiftLinker {
265286
266287 link_clang_rt ( & rust_target) ;
267288
268- for package in self . packages {
289+ for package in & self . packages {
269290 let package_path =
270291 Path :: new ( & env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ) . join ( & package. path ) ;
271292 let out_path = Path :: new ( & env:: var ( "OUT_DIR" ) . unwrap ( ) )
272293 . join ( "swift-rs" )
273294 . join ( & package. name ) ;
274295
275- let sdk_path_output = Command :: new ( "xcrun" )
276- . args ( [ "--sdk" , & rust_target. sdk . to_string ( ) , "--show-sdk-path" ] )
277- . output ( )
278- . unwrap ( ) ;
279- if !sdk_path_output. status . success ( ) {
280- panic ! (
281- "Failed to get SDK path with `xcrun --sdk {} --show-sdk-path`" ,
282- rust_target. sdk
283- ) ;
284- }
285-
286- let sdk_path = String :: from_utf8_lossy ( & sdk_path_output. stdout ) ;
296+ // NOTE(paris): Fetch the SDK directly from cc because when running using hermetic
297+ // bazel, we do not have xcrun available in the xcode toolchain (it's an OSX host
298+ // library). So running it will use non-hermetic xcode toolchain and fail on CI machines
299+ // where it is not installed (and give incorrect paths even if it is installed).
300+ let sdk_path = if let Some ( path) = self . sysroot_from_cc ( ) {
301+ path
302+ } else {
303+ let sdk_path_output = Command :: new ( "xcrun" )
304+ . args ( [ "--sdk" , & rust_target. sdk . to_string ( ) , "--show-sdk-path" ] )
305+ . output ( )
306+ . unwrap ( ) ;
307+
308+ if !sdk_path_output. status . success ( ) {
309+ panic ! (
310+ "Failed to get SDK path with `xcrun --sdk {} --show-sdk-path`" ,
311+ rust_target. sdk
312+ ) ;
313+ }
314+
315+ String :: from_utf8 ( sdk_path_output. stdout )
316+ . unwrap ( )
317+ . trim ( )
318+ . to_string ( )
319+ } ;
287320
288321 let mut command = Command :: new ( "swift" ) ;
289322 command. current_dir ( & package. path ) ;
@@ -324,8 +357,6 @@ impl SwiftLinker {
324357 . args ( [ "-Xcc" , & format ! ( "--target={swift_target_triple}" ) ] )
325358 . args ( [ "-Xcxx" , & format ! ( "--target={swift_target_triple}" ) ] ) ;
326359
327- println ! ( "Command `{command:?}`" ) ;
328-
329360 if !command. status ( ) . unwrap ( ) . success ( ) {
330361 panic ! ( "Failed to compile swift package {}" , package. name) ;
331362 }
0 commit comments