try_compile COPY_FILE fails when building for iOS with Makefiles / Ninja
Minimum project code:
cmake_minimum_required(VERSION 3.14.0)
set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "")
project(app LANGUAGES CXX)
try_compile(
result
"${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/test.cpp"
COPY_FILE "${CMAKE_CURRENT_BINARY_DIR}/test_app"
OUTPUT_VARIABLE out
COPY_FILE_ERROR file_error
)
message("result ${result}")
message("output ${out}")
message("file error ${file_error}")
test.cpp content
int main(int argc, char* argv[]) {
return 0;
}
CMake invocation:
cmake .. -DCMAKE_SYSTEM_NAME=iOS -GNinja --debug-trycompile
for Ninja
cmake .. -DCMAKE_SYSTEM_NAME=iOS --debug-trycompile
for Unix makefiles
With Xcode generator it fails due to a different bug (see #18993)
Output:
result TRUE
output Change Dir: /Volumes/T3/Dev/projects/ios/cmake_ios_try_compile/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/make cmTC_e7132/fast && /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/cmTC_e7132.dir/build.make CMakeFiles/cmTC_e7132.dir/build
Building CXX object CMakeFiles/cmTC_e7132.dir/test.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk -o CMakeFiles/cmTC_e7132.dir/test.cpp.o -c /Volumes/T3/Dev/projects/ios/cmake_ios_try_compile/test.cpp
Linking CXX executable cmTC_e7132.app/cmTC_e7132
/Users/alex/Dev/cmake/build/bin/cmake -E cmake_link_script CMakeFiles/cmTC_e7132.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.1.sdk -Wl,-headerpad_max_install_names CMakeFiles/cmTC_e7132.dir/test.cpp.o -o cmTC_e7132.app/cmTC_e7132
file error Cannot copy output executable
''
to destination specified by COPY_FILE:
'/Volumes/T3/Dev/projects/ios/cmake_ios_try_compile/build/test_app'
Unable to find the executable at any of:
/Volumes/T3/Dev/projects/ios/cmake_ios_try_compile/build/CMakeFiles/CMakeTmp/cmTC_e7132
/Volumes/T3/Dev/projects/ios/cmake_ios_try_compile/build/CMakeFiles/CMakeTmp/Debug/cmTC_e7132
/Volumes/T3/Dev/projects/ios/cmake_ios_try_compile/build/CMakeFiles/CMakeTmp/Debug/cmTC_e7132.app/cmTC_e7132
/Volumes/T3/Dev/projects/ios/cmake_ios_try_compile/build/CMakeFiles/CMakeTmp/Development/cmTC_e7132
-- Configuring done
-- Generating done
-- Build files have been written to: /Volumes/T3/Dev/projects/ios/cmake_ios_try_compile/build
List of files in CMakeTmp:
$ ls
CMakeCache.txt CMakeFiles CMakeLists.txt Makefile cmTC_5522d.app cmake_install.cmake
Note that CMake is looking for the "cmTC_e7132.app" bundle in a Debug subfolder, which is not present when using a single-config generator like Ninja/Makefiles, thus the copying fails.
Also note that it is not possible to override CMAKE_MACOSX_BUNDLE variable to disable the bundle creation, via the source file variant of try_compile. Even if -DCMAKE_MACOSX_BUNDLE:BOOL=OFF is passed via CMAKE_FLAGS, the default iOS toolchain overrides the value to ON. So the only workaround would be to use the project variant of try_compile.