# airbuild-xcode

> 在 AirBuild 构建脚本中运行 xcodebuild install、archive 或 test，配置、目标设备、签名和缓存使用 AirBuild 的默认设置。

Source: https://air.build/cn/docs/cli/airbuild-xcode/

`airbuild-xcode` 以一个动作调用一次 `xcodebuild`，并为你没有传入的部分补上 AirBuild 的默认设置。它只存在于 `runbuild`、`runtest` 和 `runmacos` 运行的脚本中（包括这些脚本调用的脚本），位于云端 macOS 构建机或 Mac 团队的 Mac 上。其他地方都没有安装，Linux 构建机上也没有。

对于 `install`、`archive`、`test` 和 `-showBuildSettings`，用它代替 `xcodebuild`。其他 Xcode 操作请直接调用 `xcodebuild`。

## 用法

```bash
airbuild-xcode install|archive|test|-showBuildSettings [xcodebuild options and build settings]
airbuild-xcode --help
```

动作必须放在最前面。其他参数原样传给 `xcodebuild`，`airbuild-xcode` 以 `xcodebuild` 的退出码退出。

## 动作

| 动作 | 用途 | 缓存 |
| --- | --- | --- |
| `install` | 日常构建：`xcodebuild install` 把 scheme 的 Archive target 增量构建到 `DSTROOT`。放在 `build.script` 中。 | 在云端构建中，一次运行里第一次 `install` 会取回项目的 DerivedData。 |
| `archive` | 生成签名 `.ipa` 或未签名设备安装包所用的归档。放在 `release.script` 中。 | 无：每次运行归档都从头开始。 |
| `test` | 运行测试。 | 在云端构建中，一次运行里第一次 `test` 会取回项目的 DerivedData（与 `install` 的是独立的一份）。 |
| `-showBuildSettings` | 输出构建设置，使用的默认值与构建时相同。 | — |

签名构建和设备构建拒绝 `install`：它们要导出归档，所以运行的是 `release:` 配方。

## 默认设置

`install`、`archive` 和 `-showBuildSettings` 遵循 `runbuild` 设置的 `AIRBUILD_SIGN`（默认 `0`）：

| 构建 | 配置 | 目标 | 签名 |
| --- | --- | --- | --- |
| 普通（`AIRBUILD_SIGN=0`） | `Debug` | `generic/platform=iOS Simulator` | `CODE_SIGN_IDENTITY=-`；保留项目的其他签名设置 |
| 设备安装包（`AIRBUILD_SIGN=0`、`AIRBUILD_DEVICE=1`）：`runbuild --unsigned`，或未使用托管签名的 **安装到设备** | `Release` | `generic/platform=iOS` | 使用项目的 entitlements 进行 ad-hoc 签名，不带团队或描述文件。模拟器的 `-destination` 和 `-sdk` 值会被丢弃。 |
| 签名（`AIRBUILD_SIGN=1`）：`runbuild --sign` | `Release` | `generic/platform=iOS` | 由 AirBuild 准备的托管签名 |

`test` 默认使用 iPhone 模拟器，运行时为装有普通（非折叠屏）iPhone 的最新 iOS 运行时，优先选择已启动的那台；同时加上 `CODE_SIGN_IDENTITY=-` 和 `-collect-test-diagnostics never`。它不会创建模拟器。

## 可以覆盖的设置

- 显式指定的 `-configuration`、`-destination`（测试时可以指定多个），以及普通构建和测试中的 `CODE_SIGN_IDENTITY=…`，会替换默认值。
- `-collect-test-diagnostics on-failure` 会为 `test` 重新打开模拟器诊断收集。
- 命令行上的 `ARCHS`、`COMPILATION_CACHE_*`、`SWIFT_ENABLE_PREFIX_MAPPING` 和 `CLANG_ENABLE_PREFIX_MAPPING` 优先于构建机的平台设置。在构建机上，模拟器和 macOS 构建只构建 arm64，除非你传入 `ARCHS`。
- 托管签名会覆盖项目、`-xcconfig` 和命令行中的签名设置：`CODE_SIGNING_ALLOWED`、`CODE_SIGNING_REQUIRED`、`CODE_SIGN_IDENTITY`、`CODE_SIGN_STYLE`、`DEVELOPMENT_TEAM`、`PROVISIONING_PROFILE`、`PROVISIONING_PROFILE_SPECIFIER` 和 `OTHER_CODE_SIGN_FLAGS`。`CODE_SIGN_ENTITLEMENTS` 会保留。

## 版本号

由用户在 AirBuild App 中发起的设备归档或托管签名归档，会被提升到项目的版本下限：只有在源代码中的值更低时，才会覆盖 `MARKETING_VERSION` 和 `CURRENT_PROJECT_VERSION`。智能体发起的构建永远不会这样处理：请自己在项目中设置构建号。

## 输出

在运行 `xcodebuild` 之前，`airbuild-xcode` 会在标准错误输出中打印它的选择：

```text
[airbuild-xcode] sign=0; configuration=Debug (default); destination=generic/platform=iOS\ Simulator (default).
[airbuild-xcode] CODE_SIGN_IDENTITY=default (-); project signing settings are preserved.
```

```text
[airbuild-xcode] default test device: iPhone 17 (iOS 26.1).
airbuild-xcode: skipping simulator diagnostics collection (simctl diagnose waits 600 s on Xcode 27); pass -collect-test-diagnostics on-failure to collect them.
[airbuild-xcode] action=test; destination=id=<UDID> (default); CODE_SIGN_IDENTITY=default (-).
```

你还可能看到这些行：

| 行 | 含义 |
| --- | --- |
| `[airbuild-xcode] archive starts over every run and keeps no cache; …` | 普通构建正在归档。要增量构建，请在 `build:` 中使用 `install`，把 `archive` 留在 `release:` 中。`runbuild` 会在结果旁以警告形式重复这条信息。 |
| `[airbuild-xcode] device build: dropped -destination …` | 已从设备构建中移除模拟器目标或 SDK。 |
| `[airbuild-xcode] device build: ad-hoc signed, no team or profile; CODE_SIGN_ENTITLEMENTS is preserved.` | 设备安装包构建。 |
| `[airbuild-xcode] Managed signing overrides via XCODE_XCCONFIG_FILE: …` | 签名构建。 |
| `[airbuild-xcode] command-line ARCHS=… kept over the platform build settings.` | 你的设置优先于构建机的设置。 |
| `[airbuild-xcode] version floor: raised …` / `nothing raised.` | 从 App 发起的构建所执行的版本下限步骤。 |

## 退出码和错误

`airbuild-xcode` 以 `xcodebuild` 的退出码退出。以下情况会在运行 `xcodebuild` 之前以 `64` 停止：

| 消息 | 怎么办 |
| --- | --- |
| `airbuild-xcode: start with install, archive, test or -showBuildSettings; use xcodebuild directly for other operations.` | 把动作放在最前面，或者调用 `xcodebuild`。 |
| `airbuild-xcode: use one leading action; run other Xcode operations separately.` | 去掉 `-list`、`-version`、`-show…`、`-exportArchive`、`-resolvePackageDependencies` 等，用 `xcodebuild` 运行它们。（允许使用 `-showBuildTimingSummary`。） |
| `airbuild-xcode: -configuration requires a value.`（或 `-destination`） | 给出值。 |
| `airbuild-xcode: a signed or device build exports an archive; put airbuild-xcode archive in the release: recipe of airbuild.yaml …` | 添加一个运行 `airbuild-xcode archive` 的 `release:` 配方。 |
| `airbuild-xcode: no available iOS simulator could be selected. Install an iOS simulator runtime and device in Xcode, or pass -destination explicitly.` | 传入 `-destination`，或安装模拟器运行时。 |
| `airbuild-xcode: AIRBUILD_SIGN must be 0 or 1. Start managed signing through AirBuild.` | 不要自己设置 `AIRBUILD_SIGN`；请使用 `runbuild --sign`。 |
| `airbuild-xcode: signing preparation is incomplete. Start signing through AirBuild again.` | 再次运行 `runbuild --sign`。 |

## 示例

```bash
# build.script: the everyday, incremental build
airbuild-xcode install -workspace App.xcworkspace -scheme App \
  DSTROOT="$PWD/.airbuild/artifacts/install"
```

```bash
# release.script: the archive for --sign and --unsigned
rm -rf .airbuild/artifacts/App.xcarchive
airbuild-xcode archive -workspace App.xcworkspace -scheme App \
  -archivePath .airbuild/artifacts/App.xcarchive
```

```bash
# A test entry, with runtest's --additional-options
airbuild-xcode test -workspace App.xcworkspace -scheme App \
  -resultBundlePath .airbuild/artifacts/Tests.xcresult $additional_options
```
