TExcelTAD

xcconfig Files & Build Settings

Build settings edited in Xcode's UI end up in project.pbxproj, a generated file of object IDs that is painful to review and a classic source of merge conflicts. Build configuration files (.xcconfig) move settings into plain text: one setting per line, with includes, variables and conditions. Most teams that run iOS builds on CI keep (almost) all settings there.

The syntax

text
// Configs/Base.xcconfig — shared by every configuration
PRODUCT_NAME = Hello
PRODUCT_BUNDLE_IDENTIFIER = com.example.hello
IPHONEOS_DEPLOYMENT_TARGET = 17.0
MARKETING_VERSION = 1.4.2
CURRENT_PROJECT_VERSION = 1
SWIFT_VERSION = 6.0
API_BASE_URL = https:/$()/api.example.com      // $() stops "//" from starting a comment

// Configs/Debug.xcconfig
#include "Base.xcconfig"
SWIFT_OPTIMIZATION_LEVEL = -Onone
SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) DEBUG
PRODUCT_BUNDLE_IDENTIFIER = $(inherited).debug
API_BASE_URL = https:/$()/staging.example.com

// Configs/Release.xcconfig
#include "Base.xcconfig"
SWIFT_OPTIMIZATION_LEVEL = -O
DEBUG_INFORMATION_FORMAT = dwarf-with-dsym
OTHER_LDFLAGS[sdk=iphoneos*] = $(inherited) -ObjC   // only when building for devices
#include? "Local.xcconfig"                            // optional: developer overrides, git-ignored
Syntax Meaning
KEY = value set a build setting (or define your own variable)
$(KEY) / ${KEY} reference another setting
$(inherited) the value from the next layer down (append instead of replace)
KEY[sdk=iphoneos*], [arch=arm64], [config=Release] conditional settings
#include "x.xcconfig" / #include? "x.xcconfig" include (the ? form doesn't fail if the file is missing)
// comment to end of line

You assign files in the project editor (Info → Configurations), per configuration, at the project level and/or per target.

Getting values into the app

Custom settings like API_BASE_URL reach code through Info.plist, the iOS counterpart of buildConfigField/resValue:

xml
<!-- Info.plist -->
<key>APIBaseURL</key>
<string>$(API_BASE_URL)</string>
swift
let base = Bundle.main.object(forInfoDictionaryKey: "APIBaseURL") as! String
#if DEBUG
print("talking to", base)        // only compiled when SWIFT_ACTIVE_COMPILATION_CONDITIONS has DEBUG
#endif

As on Android, anything placed there is readable by anyone who downloads the app: it's configuration, not a secret store.

Where a value comes from

Xcode resolves a setting through layers, most specific first:

text
xcodebuild … KEY=value               command-line overrides (and -xcconfig file)   highest
target build settings                 (set in Xcode, stored in project.pbxproj)
target-level .xcconfig
project build settings
project-level .xcconfig
platform defaults                                                                   lowest

$(inherited) pulls in the value from the layer below. A runnable model of that resolution, including $(inherited), references and an sdk condition:

python · try it yourself
import re

layers = [  # most specific first
    ("command line", {}),
    ("target",       {"PRODUCT_BUNDLE_IDENTIFIER": "$(inherited).debug"}),
    ("target xcconfig", {"OTHER_LDFLAGS[sdk=iphoneos*]": "$(inherited) -ObjC",
                         "SWIFT_ACTIVE_COMPILATION_CONDITIONS": "$(inherited) DEBUG"}),
    ("project",      {}),
    ("project xcconfig", {"PRODUCT_BUNDLE_IDENTIFIER": "com.example.hello",
                          "OTHER_LDFLAGS": "-lz", "PRODUCT_NAME": "Hello"}),
    ("defaults",     {"SWIFT_ACTIVE_COMPILATION_CONDITIONS": "", "OTHER_LDFLAGS": ""}),
]

def lookup(key, sdk, start=0):
    for i in range(start, len(layers)):
        name, settings = layers[i]
        for k, v in settings.items():
            base, _, cond = k.partition("[")
            if base != key:
                continue
            if cond and not re.fullmatch(cond.rstrip("]").split("=")[1].replace("*", ".*"), sdk):
                continue
            inherited = lookup(key, sdk, i + 1)[0] if "$(inherited)" in v else ""
            value = " ".join(v.replace("$(inherited)", inherited).split())
            return value.replace(" .", "."), name
    return "", "unset"

for sdk in ["iphonesimulator26.0", "iphoneos26.0"]:
    print(f"sdk={sdk}")
    for key in ["PRODUCT_BUNDLE_IDENTIFIER", "OTHER_LDFLAGS", "SWIFT_ACTIVE_COMPILATION_CONDITIONS"]:
        value, source = lookup(key, sdk)
        print(f"  {key:<36} = {value!r:<28} (from {source})")

layers[0][1]["PRODUCT_BUNDLE_IDENTIFIER"] = "com.example.hello.ci"
print("with a command-line override:", lookup("PRODUCT_BUNDLE_IDENTIFIER", "iphoneos26.0"))
sdk=iphonesimulator26.0
  PRODUCT_BUNDLE_IDENTIFIER            = 'com.example.hello.debug'    (from target)
  OTHER_LDFLAGS                        = '-lz'                        (from project xcconfig)
  SWIFT_ACTIVE_COMPILATION_CONDITIONS  = 'DEBUG'                      (from target xcconfig)
sdk=iphoneos26.0
  PRODUCT_BUNDLE_IDENTIFIER            = 'com.example.hello.debug'    (from target)
  OTHER_LDFLAGS                        = '-lz -ObjC'                  (from target xcconfig)
  SWIFT_ACTIVE_COMPILATION_CONDITIONS  = 'DEBUG'                      (from target xcconfig)
with a command-line override: ('com.example.hello.ci', 'command line')

Two lessons from the model: a value set at the target level in Xcode beats the xcconfig file you're editing (the usual "my xcconfig change does nothing" mystery: select the setting in Xcode and press Delete to remove the target-level value), and command-line overrides beat everything, which CI uses for one-off values.

Asking Xcode instead of guessing

bash
# every resolved setting for a scheme and configuration (non-run: macOS only)
xcodebuild -project Hello.xcodeproj -scheme Hello -configuration Release -showBuildSettings \
  | grep -E ' (PRODUCT_BUNDLE_IDENTIFIER|MARKETING_VERSION|CURRENT_PROJECT_VERSION|SWIFT_OPTIMIZATION_LEVEL) '
text
    CURRENT_PROJECT_VERSION = 1
    MARKETING_VERSION = 1.4.2
    PRODUCT_BUNDLE_IDENTIFIER = com.example.hello
    SWIFT_OPTIMIZATION_LEVEL = -O

-showBuildSettings -json gives the same as JSON for scripts. On CI, override per run on the command line, e.g. the build number:

bash
xcodebuild -scheme Hello -configuration Release CURRENT_PROJECT_VERSION="$GITHUB_RUN_NUMBER" archive …

Test yourself

You changed SWIFT_OPTIMIZATION_LEVEL in Release.xcconfig but the build still uses the old value. Most likely cause?

  1. xcconfig files are only read on clean builds
  2. A target-level value in project.pbxproj overrides the xcconfig layer
  3. The setting name is case-insensitive
  4. Release.xcconfig must be included twice

What does $(inherited) mean in SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) DEBUG?

  1. The value of the same setting from the next lower layer, so DEBUG is appended
  2. The value from the parent folder
  3. The previous build's value
  4. An environment variable

Why write https:/$()/api.example.com in an xcconfig file?

  1. Xcode requires escaped URLs
  2. // starts a comment, and the empty $() splits it
  3. To force HTTPS
  4. It's a conditional setting