Darwin-Initialize.cmake wrongly initialize CMAKE_OSX_SYSROOT
I am using cmake 3.17.1, but I found that there is also seemingly the same issue with 3.21.4. The problem is that, after updating MACOS to 12.0.1, a simple Hello World program that compiles with clang directly, does not compile with the command found determined by cmake with some error like stdio.h
is not found. I uninstalled and reinstalled Xcode, Xcode command line tools, and also cmake (with pip in a Python virtual env), and that did not fix the issue.
What is quite strange is that the same HelloWorld program compiles and works fine if compiled directly with /usr/bin/clang
but not if I use the command which cmake
generates which I saw by doing make -n
.
After many experiments I founds that seemingly this is because Darwin-Initialize.cmake
initializes CMAKE_OSX_SYSROOT
to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk
while the /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/
directory contains only /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.0.sdk
and /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
.
So, what I did is that I made a hack to Darwin-Initialize.cmake
in which I inserted the following lines
execute_process(COMMAND xcrun --show-sdk-path
OUTPUT_VARIABLE CMAKE_OSX_SYSROOT
OUTPUT_STRIP_TRAILING_WHITESPACE)
just after the
#----------------------------------------------------------------------------
# CMAKE_OSX_SYSROOT
comment.
Please note that the output of xcrun --show-sdk-path
is /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
After doing this hack, things where working again. What is stranger is that after removing the hack things were still working and I found that cmake
was correctly setting CMAKE_OSX_SYSROOT
to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.0.sdk
and no longer to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk
.
It seems that having successfully compiled one program with the correct SDK by the commands generated by cmake had some border effect on the xcode toolchain, and this border effect causes that the bugs does not show again. Please note that this has not to do with the Cmake cache scripts, because I deleted the entire build directory between experiments.
Said otherwise, I could observe the issue once, but I cannot reproduce it any longer.