swiftxcodexcodebuildxcode-build-settings

xcodebuild ACTIVE_COMPILATION_CONDITIONS does not override target's ACTIVE_COMPILATION_CONDITIONS


We are trying to use xcodebuild to build our frameworks, instead of using manual Xcode IDE running buttons. The issue is that in our framework we use ACTIVE_COMPILATION_CONDITIONS, which have several values. Those values are then used to check at runtime if the particular framework is integrated, like checking for Sentry:

#if SENTRY_AVAILABLE
    import Sentry
#endif

The problem is that, for some builds, we need to override our project settings, specifically ACTIVE_COMPILATION_CONDITIONS. However, after the following script successfully executes, the Xcode still does not override our provided ACTIVE_COMPILATION_CONDITIONS with defined in the project.

The script:

xcodebuild -workspace project.xcworkspace -scheme SDKNR1 ONLY_ACTIVE_ARCH=NO 
EXCLUDED_ARCHS=arm64 ACTIVE_COMPILATION_CONDITIONS=SENTRY_AVAILABLE -configuration 
release -derivedDataPath $PROJECT_DIR/../simulators/SDKNR1 -sdk iphonesimulator
ENABLE_BITCODE=YES BITCODE_GENERATION_MODE=bitcode OTHER_CFLAGS="-fembed-bitcode" clean build

As you can see we define ACTIVE_COMPILATION_CONDITIONS=SENTRY_AVAILABLE, however it will not override target's (SDKNR1) project settings. Suppose, SDKNR1 does not have any ACTIVE_COMPILATION_CONDITIONS. We expected that xcodebuild command would override target's ACTIVE_COMPILATION_CONDITIONS and would include SENTRY_AVAILABLE

Would welcome any ideas, or perhaps it is not possible?


Solution

  • The proper build setting key is "SWIFT_ACTIVE_COMPILATION_CONDITIONS"

    You can double check this by using the command and verifying the key exists:

    xcodebuild -showBuildSettings <project/scheme/target/configuration flags>
    

    Results from -showBuildSettings (truncated, for RELEASE_CONDITION2 set in the Xcode project settings for release build [for some reason debug wont show]):

    .....
    SUPPORTS_TEXT_BASED_API = NO
    SWIFT_ACTIVE_COMPILATION_CONDITIONS = RELEASE_CONDITION2
    SWIFT_COMPILATION_MODE = wholemodule
    .....
    

    In build log, you should see (as example here, setting RELEASE_CONIDTION2, also note that the ACTIVE_COMPLIATION_CONIDTIONS gets translated into -D parameters for swiftc):

    Build settings from command line:
        .....
        SDKROOT = iphoneos14.5
        SWIFT_ACTIVE_COMPILATION_CONDITIONS = RELEASE_CONDITION2
        .....
    
    CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler .....
        .....
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc <most compiler options removed> -DRELEASE_CONDITION2
    

    Above is all with Xcode 12.5