This is an archive of the discontinued LLVM Phabricator instance.

clang/apple: Infer simulator env from -mios-simulator-version-min flag
ClosedPublic

Authored by thakis on Aug 19 2022, 12:26 PM.

Details

Summary

Before this patch, open-source clang would consider
-target x86_64-apple-darwin -mios-simulator-version-min=11.0 as
targeting the iOS simulator, due to the mios flag informing it
that we want to target iOS, and logic in the driver then realizing
that x86 iOS builds must be the simulator.

However, for -target arm64-apple-darwin -mios-simulator-version-min=11.0
that didn't work and clang thought that it's building for actual iOS,
and not for the simulator.

Due to this, building compiler-rt for arm64 iossim would lead to
all .o files in RTSanitizerCommonSymbolizer.iossim.dir being built
for iOS instead of for iOS simulator, and clang would ask ld64 to
link for iOS, but using the iPhoneSimulator sysroot. This would then
lead to many warnings from ld64 looking like:

ld: warning: building for iOS, but linking in .tbd file (/Users/thakis/src/llvm-project/sysroot/iPhoneSimulator.sdk/usr/lib/libc++abi.tbd) built for iOS Simulator

Worse, with ld64.lld, this diagnostic is currently an error instead
of a warning.

This patch makes it so that the presence of mios-simulator-version-min
now informs clang that we're building for simulator. That way, all the
.o files are built for simulator, the linker is informed that we're
building for simulator, and everything Just Works.

(Xcode's clang already behaves like this, so this makes open-source clang
match Xcode clang.)

We can now likely remove the hack to treat non-mac darwin x86 as
simulator, but doing that feels slightly risky, so I'm leaving that
for a follow-up patch.

(This patch is made neccessary by the existence of arm64 macs.)

Diff Detail

Event Timeline

thakis created this revision.Aug 19 2022, 12:26 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 19 2022, 12:26 PM
thakis requested review of this revision.Aug 19 2022, 12:26 PM
thakis added a subscriber: Restricted Project.Aug 19 2022, 4:11 PM

To reproduce the warnings that this attempts to fix:

% /Applications/CMake.app/Contents/bin/cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS='clang;lld' -DLLVM_ENABLE_RUNTIMES='compiler-rt' -DLLVM_APPEND_VC_REV=NO -DLLVM_TARGETS_TO_BUILD='X86;ARM;AArch64' ../llvm-project/llvm
% ninja runtimes
...
[167/1159] Linking CXX shared library /Users/thakis/src/llvm-build-runtimes/lib/clang/16.0.0/lib/darwin/libclang_rt.ubsan_minimal_iossim_dynamic.dylib
ld: warning: building for iOS, but linking in .tbd file (/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator15.5.sdk/usr/lib/libc++abi.tbd) built for iOS Simulator
ld: warning: building for iOS, but linking in .tbd file (/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator15.5.sdk/usr/lib/libc++.tbd) built for iOS Simulator
ld: warning: building for iOS, but linking in .tbd file (/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator15.5.sdk/usr/lib/libc.tbd) built for iOS Simulator
ld: warning: building for iOS, but linking in .tbd file (/usr/lib/system/libcache.dylib) built for iOS Simulator
ld: warning: building for iOS, but linking in .tbd file (/usr/lib/system/libcommonCrypto.dylib) built for iOS Simulator
ld: warning: building for iOS, but linking in .tbd file (/usr/lib/system/libcompiler_rt.dylib) built for iOS Simulator
ld: warning: building for iOS, but linking in .tbd file (/usr/lib/system/libcopyfile.dylib) built for iOS Simulator
ld: warning: building for iOS, but linking in .tbd file (/usr/lib/system/libcorecrypto.dylib) built for iOS Simulator
ld: warning: building for iOS, but linking in .tbd file (/usr/lib/system/libdispatch.dylib) built for iOS Simulator

(and many more warnings like this.)

This ultimately boils down to:

% echo 'int main () {}' > main.c
% bin/clang -mios-simulator-version-min=9.0 main.c -isysroot $(xcrun -show-sdk-path --sdk iphonesimulator)   
ld: warning: building for iOS, but linking in .tbd file (/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator15.5.sdk/usr/lib/libSystem.tbd) built for iOS Simulator
ld: warning: building for iOS, but linking in .tbd file (/usr/lib/system/libcache.dylib) built for iOS Simulator

Interestingly, this doesn't happen with Xcode's clang:

% clang -mios-simulator-version-min=9.0 main.c -isysroot $(xcrun -show-sdk-path --sdk iphonesimulator) 
# no diags

Passing -v to both clangs shows that bin/clang passes -triple arm64-apple-ios9.0.0 to -cc1 and -platform_version ios 9.0.0 15.5 to ld64, while Xcode's clang passes -triple arm64-apple-ios9.0.0-simulator and -platform_version ios-simulator respectively.

This patch makes open-source clang match Xcode clang's behavior.

thakis edited the summary of this revision. (Show Details)Aug 22 2022, 9:02 AM
hans accepted this revision.Aug 22 2022, 9:12 AM
hans added a subscriber: hans.

lgtm since it matches xcode clang

For the commit message, perhaps the "(Xcode's clang already behaves like this," part should be moved further down, probably to just after "This patch makes it so that", since the "already behaves like this" should refer to the new behaviour.

This revision is now accepted and ready to land.Aug 22 2022, 9:12 AM
thakis edited the summary of this revision. (Show Details)Aug 22 2022, 9:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 22 2022, 9:20 AM