Xcode: Imported object libraries break configuration when building an iOS project
We're hitting this while trying to build an iOS app that uses a CMake-built Qt 6, which exports object libraries.
cmake_minimum_required(VERSION 3.17)
set(CMAKE_OSX_SYSROOT iphonesimulator CACHE STRING "" FORCE)
set(CMAKE_OSX_ARCHITECTURES x86_64 CACHE STRING "" FORCE)
project(app LANGUAGES CXX)
set(source_file "${CMAKE_CURRENT_BINARY_DIR}/main.cpp")
file(WRITE "${source_file}" "int main(int argc, char** argv) {return 0;}")
add_executable(app "${source_file}")
add_library(objlib OBJECT IMPORTED)
target_link_libraries(app PRIVATE objlib)
$ cmake .. -DCMAKE_SYSTEM_NAME=iOS -GXcode
Output:
CMake Error at CMakeLists.txt:17 (add_library):
The OBJECT library type may not be used for IMPORTED libraries under Xcode
with multiple architectures $(CURRENT_ARCH).
-- Configuring incomplete, errors occurred!
The relevant piece of code is
bool cmGlobalXCodeGenerator::HasKnownObjectFileLocation(
std::string* reason) const
{
if (this->ObjectDirArch.find('$') != std::string::npos) {
if (reason != nullptr) {
*reason = cmStrCat(" under Xcode with multiple architectures " , this->ObjectDirArch) ;
}
return false;
}
return true;
}
void cmGlobalXCodeGenerator::ComputeObjectDirArch(cmMakefile* mf)
{
if (this->Architectures.size() > 1 || this->UseEffectivePlatformName(mf)) {
this->ObjectDirArch = "$(CURRENT_ARCH)";
} else if (!this->Architectures.empty()) {
this->ObjectDirArch = this->Architectures[0];
} else {
this->ObjectDirArch = this->ObjectDirArchDefault;
}
}
Regardless of the architectures size (which is 1 in this case), ComputeObjectDirArch
returns "$(CURRENT_ARCH)"
, because UseEffectivePlatformName() -> PlatformIsAppleEmbedded()
returns true, and XCODE_EMIT_EFFECTIVE_PLATFORM_NAME
is unset.
Is this a bug?
Who should set XCODE_EMIT_EFFECTIVE_PLATFORM_NAME
? The consuming project? The docs mention that setting it will break TARGET_FILE
genexes, which sounds bad.
Edited by alcroito