iOS: Archiving fails when using object libraries without doing a build first
An Xcode project with an iOS executable that links against an object library will cause a failure when running the archive action, if a build is not done first.
This can happen in a CI setting, where the usual human flow of building before archiving might not happen.
The failure happens because the archive operation uses a different directory for building than CMAKE_BINARY_DIR
and CMake embeds object file absolute paths to CMAKE_BINARY_DIR
, where they will not exist.
cmake_minimum_required(VERSION 3.16)
set(CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "")
set(CMAKE_OSX_SYSROOT "iphonesimulator" CACHE STRING "")
set(CMAKE_OSX_DEPLOYMENT_TARGET "12" CACHE STRING "")
project(iosapp LANGUAGES CXX)
set(source_my_obj_lib "${CMAKE_BINARY_DIR}/source_my_obj_lib.cpp")
file(GENERATE OUTPUT "${source_my_obj_lib}" CONTENT "void bar() {}")
add_library(actual_my_obj_lib OBJECT ${source_my_obj_lib})
set( MACOSX_BUNDLE_BUNDLE_NAME "ObjLibApp" )
set( MACOSX_BUNDLE_GUI_IDENTIFIER "org.alcroito.cmake.obj.lib.test.app")
set( MACOSX_BUNDLE_INFO_STRING "test project" )
set( MACOSX_BUNDLE_SHORT_VERSION_STRING "0.1" )
set( MACOSX_BUNDLE_LONG_VERSION_STRING "0.1.0" )
set( MACOSX_BUNDLE_BUNDLE_VERSION "0.1" )
set( MACOSX_BUNDLE_COPYRIGHT "" )
set(app_cpp "${CMAKE_BINARY_DIR}/main.cpp")
file(GENERATE OUTPUT "${app_cpp}" CONTENT "int main() { return 0; }")
add_executable(iosapp ${app_cpp})
target_link_libraries(iosapp PRIVATE "$<TARGET_OBJECTS:actual_my_obj_lib>")
$ mkdir build && cd build && cmake .. -DCMAKE_SYSTEM_NAME=iOS -GXcode
$ xcodebuild -configuration Release -scheme iosapp -archivePath $PWD/my.xcarchive archive
...
CompileC /Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/Objects-normal/x86_64/source_my_obj_lib.o /Volumes/T3/Dev/projects/cmake/ios/cmake_ios_object_lib_archive/build/source_my_obj_lib.cpp normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler (in target 'actual_my_obj_lib' from project 'iosapp')
cd /Volumes/T3/Dev/projects/cmake/ios/cmake_ios_object_lib_archive
export LANG\=en_US.US-ASCII
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -target x86_64-apple-ios12-simulator -fmessage-length\=270 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit\=0 -fcolor-diagnostics -Wno-trigraphs -fpascal-strings -O3 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-float-conversion -Wno-non-literal-null-conversion -Wno-objc-literal-conversion -Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DCMAKE_INTDIR\=\"Release-iphonesimulator\" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.4.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -Wno-sign-conversion -Wno-infinite-recursion -Wno-move -Wno-comma -Wno-block-capture-autoreleasing -Wno-strict-prototypes -Wno-range-loop-analysis -Wno-semicolon-before-method-body -I/Volumes/T3/Dev/projects/cmake/ios/cmake_ios_object_lib_archive/build/iosapp.build/Release/actual_my_obj_lib.build/include -I/Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/DerivedSources-normal/x86_64 -I/Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/DerivedSources/x86_64 -I/Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/DerivedSources -F/Volumes/T3/Dev/projects/cmake/ios/cmake_ios_object_lib_archive/build/iosapp.build/Release/actual_my_obj_lib.build -DNDEBUG -MMD -MT dependencies -MF /Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/Objects-normal/x86_64/source_my_obj_lib.d --serialize-diagnostics /Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/Objects-normal/x86_64/source_my_obj_lib.dia -c /Volumes/T3/Dev/projects/cmake/ios/cmake_ios_object_lib_archive/build/source_my_obj_lib.cpp -o /Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/Objects-normal/x86_64/source_my_obj_lib.o
...
Ld /Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/UninstalledProducts/iphonesimulator/iosapp.app/iosapp normal (in target 'iosapp' from project 'iosapp')
cd /Volumes/T3/Dev/projects/cmake/ios/cmake_ios_object_lib_archive
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -target x86_64-apple-ios12-simulator -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.4.sdk -L/Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/BuildProductsPath/Release-iphonesimulator -F/Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/BuildProductsPath/Release-iphonesimulator -filelist /Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/iosapp.build/Objects-normal/x86_64/iosapp.LinkFileList -dead_strip -Xlinker -objc_abi_version -Xlinker 2 -Wl,-headerpad_max_install_names /Volumes/T3/Dev/projects/cmake/ios/cmake_ios_object_lib_archive/build/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/Objects-normal/x86_64/source_my_obj_lib.o -Xlinker -sectcreate -Xlinker __TEXT -Xlinker __entitlements -Xlinker /Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/iosapp.build/iosapp.app-Simulated.xcent -Xlinker -no_adhoc_codesign -Xlinker -dependency_info -Xlinker /Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/iosapp.build/Objects-normal/x86_64/iosapp_dependency_info.dat -o /Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/UninstalledProducts/iphonesimulator/iosapp.app/iosapp
clang: error: no such file or directory: '/Volumes/T3/Dev/projects/cmake/ios/cmake_ios_object_lib_archive/build/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/Objects-normal/x86_64/source_my_obj_lib.o'
Command Ld failed with a nonzero exit code
** ARCHIVE FAILED **
Note the path difference, during the archiving operation, Xcode by default places build files under a DerivedData
directory.
# expected
/Volumes/T3/Dev/projects/cmake/ios/cmake_ios_object_lib_archive/build/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/Objects-normal/x86_64/source_my_obj_lib.o
# actual
/Users/alex/Library/Developer/Xcode/DerivedData/iosapp-fwnrexkurribuubeemudmkkqbcsr/ArchiveIntermediates/iosapp/IntermediateBuildFilesPath/iosapp.build/Release-iphonesimulator/actual_my_obj_lib.build/Objects-normal/x86_64/source_my_obj_lib.o
For easier debugging, the derived data path can be controlled by passing another option to xcodebuild.
$ xcodebuild -configuration Release -scheme iosapp -archivePath $PWD/my.xcarchive archive -derivedDataPath $PWD/derivedData
Edited by alcroito