How to append values in xcconfig variables?

前端 未结 8 726
借酒劲吻你
借酒劲吻你 2020-12-12 21:58

I\'m using Xcode and .xcconfig files. I\'m trying to append some values in the preprocessor definitions, but I simply can\'t make it work.

I tried the following (as

相关标签:
8条回答
  • 2020-12-12 22:29

    This works for me in Xcode 2.4.1:

    GCC_PREPROCESSOR_DEFINITIONS = "$(GCC_PREPROCESSOR_DEFINITIONS) NEW_VALUE"
    

    You do have to sometimes allow a few seconds between editing a config file and the change showing up in a target's Build Info.

    0 讨论(0)
  • 2020-12-12 22:30

    You want to use the placeholder $(inherited) to represent the value inherited from lower levels, e.g.

    GCC_PREPROCESSOR_DEFINITIONS = "$(inherited) NEW_VALUE"
    
    0 讨论(0)
  • 2020-12-12 22:39

    As stated in other answers, prior to Xcode 10, xcconfig files could not simply inherit and extend each other's values. But,

    Since Xcode 10, xcconfig now work as one might expect them to : $(inherited) actually expand to the previously defined value of the variable.

    When an .xcconfig file contains multiple assignments of the same build setting, later assignments using $(inherited) or $(<setting_name>) will inherit from earlier assignments in the .xcconfig. The legacy build system caused every use of $(inherited) or $(<setting_name>) to skip any other values defined within the .xcconfig. To detect whether your .xcconfig is affected by this improvement, running defaults write com.apple.dt.XCBuild EnableCompatibilityWarningsForXCBuildTransition -bool YES in Terminal will cause Xcode to generate a warning about this situation.

    (Xcode 10 beta 1 release notes)

    So for example, given two simple .xcconfig files:

    // Generic.xcconfig
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_GENERIC_FLAG
    
    // Debug.xcconfig
    #include "Generic.xcconfig"
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_DEBUG_FLAG
    

    Assuming your project uses Debug.xcconfig for its Debug configuration, you'll get the expected value -DMY_GENERIC_FLAG -DMY_DEBUG_FLAG for OTHER_SWIFT_FLAGS.

    (instead of just -DMY_DEBUG_FLAG in Xcode 9 and earlier releases)


    The new behavior is pretty straightforward: $(inherited) is simply replaced by the previously defined value of the variable, if any.

    So in the previous example, if we expand the #include statement, we'll get the following xcconfig file:

    // Merged xcconfig files after resolving #include
    OTHER_SWIFT_FLAGS = -DMY_GENERIC_FLAG
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_DEBUG_FLAG
    
    • On the first line OTHER_SWIFT_FLAGS value is -DMY_GENERIC_FLAG ( $(inherited) expands to nothing, because this is the first definition of OTHER_SWIFT_FLAGS we encounter1).
    • On the second line OTHER_SWIFT_FLAGS if overwritten, and its value is now -DMY_GENERIC_FLAG -DMY_DEBUG_FLAG (its previous value + the newly added flag).

    On a more complex xcconfig setup, things could look like this:

    // First.xcconfig
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_FIRST_FLAG
    
    // Second.xcconfig
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_SECOND_FLAG
    
    // Last.xcconfig
    #include "Generic.xcconfig"
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_LAST_FLAG
    
    // Merge.xcconfig
    #include "First.xcconfig"
    #include "Second.xcconfig"
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_INTERMEDIATE_FLAG
    #include "Last.xcconfig"
    

    We'll assume this time we're using the Merge.xcconfig in our configuration.

    The resolved value for OTHER_SWIFT_FLAGS will then be -DMY_FIRST_FLAG -DMY_SECOND_FLAG -DMY_INTERMEDIATE_FLAG -DMY_LAST_FLAG.

    This might be surprising at first, but it actually makes sense: once the #include are resolved, we end up with this xcconfig:

    OTHER_SWIFT_FLAGS = $(inherited) -DMY_FIRST_FLAG
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_SECOND_FLAG
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_INTERMEDIATE_FLAG
    OTHER_SWIFT_FLAGS = $(inherited) -DMY_LAST_FLAG
    

    The final resolved value is then the one defined on the last line, which is -DMY_LAST_FLAG plus the value it inherited from the previous line -DMY_INTERMEDIATE_FLAG etc etc.

    Note that naturally, if you forget $(inherited) in one of the definitions, you'll break the inheritance chain and only get the values from the bottom definitions, up the definition without $(inherited).


    1 One may expect the xcconfig file to inherit previous values defined at the Project level, but it doesn't seem to be the case


    0 讨论(0)
  • 2020-12-12 22:41

    This works:

    xcodebuild GCC_PREPROCESSOR_DEFINITIONS='$(value) NEW_VALUE'
    
    0 讨论(0)
  • 2020-12-12 22:43

    According to the Xcode Build System Guide:

    When a configuration unit contains more than one definition for a particular build setting, Xcode uses the last definition in the unit. Keep in mind that configuration files do not have access to build setting definitions made in configuration files they include. That is, you cannot modify the definition made in an included configuration file; you can only replace it.

    So, I guess this mean that it is not possible to append values to a given variable.

    0 讨论(0)
  • 2020-12-12 22:44

    There is another question with an answer that might help with this particular problem. It describes a technique that has each layer compose a subset of the definitions, then bring them all together at the leaf-level xcconfig.

    0 讨论(0)
提交回复
热议问题