混乱的scheme、.xcconfig、build Settings梳理的清清楚楚

Xcode project 是一个仓库,该仓库包含了所有的文件,资源和用于生成一个或者多个products的信息,那么比如我们从当前project要生成两个products A 和B,该从哪里设置A和B各自需要的信息呢?

答案是target,每一个 target能够制定其自己的 build settings,指明了如何生成 products

target 的 build settings 会覆盖project 的build settings

如果我们每个target都需要debug和release两种环境,那么不同环境的设置又去哪里写明呢?

答案是可以在buildsettings中单独设置,

也可以在configurations中分别配置debug.xcconfig和release.xcconfig

我们已经用到了target 和 configurations,设置好之后,我现在想要在debug 模式下,运行target A,我应该如何通知xcode呢?

答案是scheme

至此,xcode 编译设置相关的三个基础我们都有所了解了:

1、build settings,分为project的build settings 和 target的 build settings

创建一个新的project,它自带了build settings,这些设置并不是存在.xcconfig文件中的,而是存在project.pbxproj 中的 XCBuildConfiguration 区块,可以通过Xcode 界面:TARGETS → Build Settings修改,也可以直接编辑 project.pbxproj

bash 复制代码
YourProject.xcodeproj/    
└── project.pbxproj  # 所有工程配置的实际载体

project.pbxproj包含 所有 Build Settings 的键值对 、文件引用、Target 定义等,这里为啥没有直接给开发者生成一个.xcconfig文件,我猜可能是xcode的开发者想要将基础配置内置,降低新手门槛,在需要满足一些高级要求时允许开发者使用 .xcconfig 机制,以达到配置隔离的目的

  • Target 设置:针对特定构建目标
  • .xcconfig:针对构建环境(Debug/Release)
  • Project:全局默认值

2、project -> info -> configuration ,下文将其称为配置注册表

上图是没有cocoapods的配置注册表

配置注册表主要是对project或target绑定对应的.xcconfig,可以理解为这里是对debug/release环境、target、.xcconfig的整合

开发者没有自己创建.xcconfig,但是通过cocoapods引入了三方库的情况,

Pods 会生成配置文件(如 Pods-YourApp.debug.xcconfig),并且会被自动加到配置注册表中

3、scheme,相对更好理解,主要是用来安排开发者现在要运行什么,比如运行的环境,target等

其实针对项目的设置基本就是这三个元素之间的组合

cocoaPods

pod installpod update过程中,当CocoaPods确定了要安装的每个Pod的版本和配置后,会为每个Pod生成相应的Support Files。这些文件由CocoaPods的Generator类创建,特别是TargetEnvironmentSupportFilesGeneratorTargetSupportFilesGenerator等类负责生成。

css 复制代码
Pods/
└── [Pod名称]/    
    └── Support Files/        
        ├── [Pod名称]-dummy.m        
        ├── [Pod名称].xcconfig        
        ├── [Pod名称]-prefix.pch        
        ├── [Pod名称]-umbrella.h        
        └── [Pod名称]-Info.plist

同样在pod installpod update过程中,当所有Pod的target都生成后,CocoaPods会为整个Pods项目生成Target Support Files,包括聚合的xcconfig文件。这些文件由CocoaPods的AggregateTargetSupportFilesGeneratorPodsProjectGenerator等类生成。

xcconfig 、build settings的优先级

Target build settings 设置 > xcconfig设置 > Project build settings 设置

那么如果我是想在.xcconfig中设置other_ldflags, 如何在原来的基础上追加呢?这里就要聊到$(inherited)

在 Xcode 的构建系统中,$(inherited) 是一个特殊的变量,用于表示从更高层级继承的设置

另外.xcconfig可以通过#include指令形成链式继承。

举个例子在A.xcconfig中设置如下,并将其配置到配置注册表中

arduino 复制代码
#include "B.xcconfig"

B.xcconfig中的内容

ini 复制代码
OTHER_LDFLAGS = $(inherited) -framework "CoreData"

在target的build settings中如下设置在project 的build settings中如下设置验证一下最终的other_ldflags的值

java 复制代码
xcodebuild -target pasteEasy -configuration Debug -showBuildSettings | grep OTHER_LDFLAGSOTHER_LDFLAGS
=  -framework "CoreVideo" -framework "CoreData" -framework "CoreGraphics"

从最终OTHER_LDFLAGS的值的顺序也可以看出继承的方向project build settings <- .xcconfig <- target build settings

当在 .xcconfig 文件中使用 $(inherited) 时,它代表的是当前配置层级之上(更低优先级)的设置。

具体来说,对于绑定到某个构建配置(如 Debug)的 .xcconfig 文件,其 $(inherited) 的值来自:

  • target/Project 层级的 Build Settings(存储在 project.pbxproj 中)
  • Xcode 的默认构建设置

冲突

build setting中设置DEBUG_INFORMATION_FORMAT 为 DWARF.xcconfig中设置为dwarf-with-dsym查看最终值:

perl 复制代码
xcodebuild -target pasteEasy -configuration Debug -showBuildSettings | grep DEBUG_INFORMATION_FORMAT
dwarf
继承顺序:最高优先级的target build settings都是从其他继承
  • 当前构建配置对应的 .xcconfig 文件中的设置(例如 Debug.xcconfig 中的 OTHER_LDFLAGS
  • target 层级的 Build Settings(存储在 project.pbxproj 中)
  • Project 层级的 Build Settings(存储在 project.pbxproj 中)
  • Xcode 的默认构建设置

关注我,还能发现更多干货~

相关推荐
2501_915921432 小时前
iOS 虚拟位置设置实战,多工具协同打造精准调试与场景模拟环境
android·ios·小程序·https·uni-app·iphone·webview
QuantumLeap丶2 小时前
《Flutter全栈开发实战指南:从零到高级》- 11 -状态管理Provider
android·flutter·ios
2501_916008892 小时前
App 上架需要什么?从开发者账号到开心上架(Appuploader)免 Mac 上传的完整流程指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
QuantumLeap丶20 小时前
《Flutter全栈开发实战指南:从零到高级》- 09 -常用UI组件库实战
flutter·ios·dart
2501_9159184121 小时前
App 上架苹果商店全流程详解 从开发者账号申请到开心上架(Appuploader)跨平台免 Mac 上传实战指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
2501_916007471 天前
从零开始学习iOS App开发:Xcode、Swift和发布到App Store完整教程
android·学习·ios·小程序·uni-app·iphone·xcode
Pluto5381 天前
第一个app产品的迭代
ios·github
2501_915921431 天前
iOS 26 CPU 使用率监控策略 多工具协同构建性能探索体系
android·ios·小程序·https·uni-app·iphone·webview
狂团商城小师妹1 天前
JAVA国际版同城打车源码同城服务线下结账系统源码适配PAD支持Android+IOS+H5
android·java·ios·小程序·交友
游戏开发爱好者81 天前
iOS 应用逆向对抗手段,多工具组合实战(iOS 逆向防护/IPA 混淆/无源码加固/Ipa Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview