XcodeDeps¶
XcodeDeps
工具是 Xcode 的依赖信息生成器。它将生成多个 .xcconfig 配置文件,供使用 xcodebuild 或 Xcode 的消费者使用。要使用它们,只需将生成的配置文件添加到 Xcode 项目中,或通过命令行设置 -xcconfig
参数。
XcodeDeps
生成器可以在 conanfile 中按名称使用
class Pkg(ConanFile):
generators = "XcodeDeps"
[generators]
XcodeDeps
它也可以在 conanfile 的 generate()
方法中完全实例化
from conan import ConanFile
from conan.tools.apple import XcodeDeps
class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
requires = "libpng/1.6.37@" # Note libpng has zlib as transitive dependency
def generate(self):
xcode = XcodeDeps(self)
xcode.generate()
当使用 XcodeDeps
生成器时,每次调用 conan install
都会为每个依赖和配置生成多个配置文件。例如,对于上面的 conanfile.py
$ conan install conanfile.py # default is Release
$ conan install conanfile.py -s build_type=Debug
此生成器是多配置的。它将为每个需求的 Debug/Release 配置生成不同的文件。它还将生成一个单独的文件 (conandeps.xcconfig),聚合所有直接依赖项的文件(本例中仅为 libpng)。上述命令生成以下文件
.
├── conan_config.xcconfig
├── conan_libpng.xcconfig
├── conan_libpng_libpng.xcconfig
├── conan_libpng_libpng_debug_x86_64.xcconfig
├── conan_libpng_libpng_release_x86_64.xcconfig
├── conan_zlib.xcconfig
├── conan_zlib_zlib.xcconfig
├── conan_zlib_zlib_debug_x86_64.xcconfig
├── conan_zlib_zlib_release_x86_64.xcconfig
└── conandeps.xcconfig
第一次 conan install
使用默认的 Release 和 x86_64 配置,生成
conan_libpng_libpng_release_x86_64.xcconfig:声明具有条件逻辑的变量,仅在 Xcode 中或通过命令行传递给 xcodebuild 的活动配置中考虑。
conan_libpng_libpng.xcconfig:包含 conan_libpng_libpng_release_x86_64.xcconfig 并声明以下 Xcode 构建设置:
SYSTEM_HEADER_SEARCH_PATHS
、GCC_PREPROCESSOR_DEFINITIONS
、OTHER_CFLAGS
、OTHER_CPLUSPLUSFLAGS
、FRAMEWORK_SEARCH_PATHS
、LIBRARY_SEARCH_PATHS
、OTHER_LDFLAGS
。它还包含用于传递依赖项的生成 xcconfig 文件(本例中为 conan_zlib_zlib.xcconfig)。conan_libpng.xcconfig:在这种情况下,它只包含 conan_libpng_libpng.xcconfig,但如果所需的包具有组件,此文件将包含包的所有组件。
图中每个依赖项都将生成相同的 3 个文件。在这种情况下,由于 zlib 是 libpng 的依赖项,它将生成:conan_zlib_zlib_release_x86_64.xcconfig、conan_zlib_zlib.xcconfig 和 conan_zlib.xcconfig。
conandeps.xcconfig:包含所有直接依赖项的配置文件,在这种情况下,它只包含
conan_libpng.xcconfig
。主 conan_config.xcconfig 文件,要添加到项目中。它包括此生成器生成的文件以及 XcodeToolchain 生成的文件(如果也已设置)。
第二次 conan install -s build_type=Debug
生成
conan_libpng_libpng_debug_x86_64.xcconfig:与下面 Debug 配置的变量相同。
conan_libpng_libpng.xcconfig:此文件已由上一个命令创建,现在已修改为添加 conan_libpng_debug_x86_64.xcconfig 的包含。
conan_libpng.xcconfig:此文件将保持不变。
与上一个命令一样,图中每个依赖项都将生成相同的 3 个文件。在这种情况下,由于 zlib 是 libpng 的依赖项,它将生成:conan_zlib_zlib_debug_x86_64.xcconfig、conan_zlib_zlib.xcconfig 和 conan_zlib.xcconfig。
conandeps.xcconfig:包含所有直接依赖项的配置文件,在这种情况下,它只包含
conan_libpng.xcconfig
。主 conan_config.xcconfig 文件,要添加到项目中。它包括此生成器生成的文件以及 XcodeToolchain 生成的文件(如果也已设置)。
如果您想将这些依赖项添加到 Xcode 项目中,您只需为您要使用的所有配置(通常是 Debug 和 Release)添加 conan_config.xcconfig 配置文件。
定义了其他变量¶
除了定义上述 Xcode 构建设置的变量外,还声明了其他变量,这些变量可能对您的 Xcode 项目有用
PACKAGE_ROOT_<package_name>:设置为 package_folder 属性 的位置。
组件支持¶
此生成器支持带组件的包。这意味着
如果依赖项
package_info()
在某些组件上声明了cpp_info.requires
,则生成的 .xcconfig 文件将只包含对这些组件的包含。当前包
requires
将完全依赖于所有组件。请注意,package_info()
仅适用于消费者,而不适用于当前包。
自定义配置¶
如果您的 Xcode 项目定义了自定义配置,例如 ReleaseShared
或 MyCustomConfig
,则可以在 XcodeDeps
生成器中定义它,以便不同的项目配置可以使用不同的依赖项集。假设我们当前的项目可以构建为共享库,使用自定义配置 ReleaseShared
,并且包也通过 shared
选项控制这一点
from conan import ConanFile
from conan.tools.apple import XcodeDeps
class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
options = {"shared": [True, False]}
default_options = {"shared": False}
requires = "zlib/1.3.1"
def generate(self):
xcode = XcodeDeps(self)
# We assume that -o *:shared=True is used to install all shared deps too
if self.options.shared:
xcode.configuration = str(self.settings.build_type) + "Shared"
xcode.generate()
这将为自定义配置生成新的 .xcconfig 文件,当您在 IDE 中切换到此配置时,构建系统将根据我们是希望链接共享库还是静态库来获取正确的值。