Getting incompatible-sysroot warning when compiling for iOS with sdk and target set

I'm getting this warning when compiling for iOS, despite specifying target and sdk:

clang: warning: using sysroot for 'MacOSX' but targeting 'iPhone' [-Wincompatible-sysroot]

Example:

swiftc -emit-executable -target arm64-apple-ios18.5 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS18.5.sdk hello.swift

// hello.swift
print("hello!")

What am I missing?

you might have luck with $(xcrun -f swiftc) -emit-executable ...

technical details: I believe this is an issue in libxcselect where the macOS CLI stubs override SDKROOT. that is, /usr/bin/swiftc sets SDKROOT=/path/to/MacOSX.sdk before jumping to the actual swiftc binary.

3 Likes

Using swiftc reported by xcrun -f swiftc (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc) did the trick, thank you!

Manually setting the SDKROOT environment variable to iphoneos or its SDK path before calling swiftc at /usr/bin/swiftc also seems to work.

For what it’s worth, xcrun also sets SDKROOT, so once you’re in workaround territory the simplest one might be xcrun -sdk iphoneos swiftc.

1 Like