Xcode配置设置文件 (xcconfig)

1,942 阅读3分钟

Xcode配置设置文件 (xcconfig)

Xcode配置设置文件(是具有 .xcconfig 文件扩展名的文件),也称为构建配置文件或 xcconfig 文件,是一个纯文本文件,用于定义和覆盖project或target的特定构建配置。
这种类型的文件可以在 Xcode 之外进行编辑,并且方便的进行版本控制。(跟.project文件相比简单明了)
构建配置文件遵循特定的格式规则,如果不符合,则生成构建警告。

注意:要将配置设置文件添加到您的项目,请参阅添加构建配置 (xcconfig) 文件。

注释

使用单行注释来包含注释或其他应被构建系统忽略的信息。每个注释都以两个正斜杠 (//) 开头,并一直持续到行尾。例如:

//
// 配置.xcconfig
// 我的 iOS 应用
//
// 由 Johnny Appleseed 于 16 年 11 月 15 日创建。
// 版权所有 © 2018 Apple。版权所有。
//

注释可以单独驻留在一行上,也可以跟在构建设置值之后。例如:

ASSETCATALOG_COMPILER_APPICON_NAME = MyAppIcon // 这是一条评论。

指定构建设置值 构建配置文件不需要列出所有可能的构建设置。它只需要包含您想要自定义的那些。每个构建设置值都以以下格式定义在一行中:

BUILD_SETTING_DECLARATION_NAME = BUILD_SETTING_VALUE_DEFINITION

例如:

ONLY_ACTIVE_ARCH = Yes

有许多值类型,但以下是最常见的:

类型说明
BooleanA value of YES or NO.
stringA specified text value.
enumeration (string)A predefined text value.
string listA space-separated list of string values. If a string within a string list contains spaces, then the string must be surrounded by quotes.
pathA file or directory path, in POSIX form.
path listA space-separated list of path values. If a path within a path list contains spaces, then the path must be surrounded by quotes.

注意

Build Setting里可以显示标题,也可以显示Key。选择一个设置项,然后点击右上角的问号,查看实际的设置Key。

防止值被覆盖

要将现有项目或目标构建设置值保留为新定义值的一部分,请使用以下格式的 $(inherited) 变量:

BUILD_SETTING_DECLARATION_NAME = $(inherited)ADDITIONAL_VALUE

例如:

OTHER_SWIFT_FLAGS = $(inherited) -v

引用另一个构建设置的值

要引用另一个构建设置的值,请按以下格式引用另一个构建设置名称:

BUILD_SETTING_DECLARATION_NAME = $(OTHER_BUILD_SETTING_DECLARATION_NAME)

例如:

OBJROOT = $(SYMROOT)

其他构建设置值可以被内联引用,如下所示:

DSTROOT = /tmp/$(PROJECT_NAME).dst

或者:

CONFIGURATION_BUILD_DIR = $(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)

Add a platform condition to a value

A project may need to build differently when targeting one platform versus another. To allow for this, the build system supports the following conditionals:

ConditionalCondition value
sdkAn SDK, such as macos10.12 or iphoneos10.2. To match any SDK of a given platform, provide an asterisk () instead of a version. For example, a condition value of macos matches any macOS SDK.
archAn architecture, such as x86_64 or arm64.

Conditions appear after the build setting name and are enclosed within brackets. For example:

BUILD_SETTING_DECLARATION_NAME[CONDITIONAL=CONDITION_VALUE] = VALUE_DEFINITION

For example, the following line sets the value of the OTHER_LDFLAGS build setting to -lncurses when building with any macOS SDK:

OTHER_LDFLAGS[sdk=macosx*] = -lncurses

Multiple conditions can also be specified using the format:

BUILD_SETTING_DECLARATION_NAME[CONDITIONAL1=CONDITION_VALUE1][CONDITIONAL2=CONDITION_VALUE2] = VALUE_DEFINITION

For example, the following line sets the value of the OTHER_LDFLAGS build setting to -lncurses whenever the SDK matches a value of macosx* and the architecture matches a value of x86_64:

OTHER_LDFLAGS[sdk=macosx*][arch=x86_64] = -lncurses

Include Settings from Other Build Configuration Files

A build configuration file can import build settings from other build configuration files. To import settings from another file, use the prefix #include, followed a reference to the file in quotes. For example:

#include "MyOtherConfigFile.xcconfig"

If the specified file isn’t found at build time, build warnings are produced. To suppress warnings for missing build configuration files, place a question mark (?) after the #include prefix. For example:

#include? "MyOtherConfigFile.xcconfig"

You can refer to other build configuration files using file names, relative paths, and absolute paths.

Reference typeDescriptionExample
File nameThe name of a build configuration file in the same folder as the current build configuration file.#include "MyOtherConfigFile.xcconfig"
Relative pathThe path to a build configuration file relative to the location of the current build configuration file.#include "../MyOtherConfigFile.xcconfig"
Absolute pathAn absolute path to a build configuration file on disk.#include "/Users/MyUserName/Desktop/MyOtherConfigFile.xcconfig"

Note: References to other build configuration files are processed before interpreting any build settings.

原文:help.apple.com