macos-build

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Build macOS App

构建macOS应用

Build any native macOS Xcode project from the command line using
xcodebuild
.
使用
xcodebuild
从命令行构建任何原生macOS Xcode项目。

Finding the Project

查找项目

Before building, locate the Xcode project or workspace:
bash
find . -maxdepth 2 -name "*.xcodeproj" -o -name "*.xcworkspace" | head -5
Then list available schemes:
bash
xcodebuild -list -project "YourApp.xcodeproj" 2>/dev/null | grep -A 20 "Schemes:"
构建前,先定位Xcode项目或工作区:
bash
find . -maxdepth 2 -name "*.xcodeproj" -o -name "*.xcworkspace" | head -5
然后列出可用的scheme:
bash
xcodebuild -list -project "YourApp.xcodeproj" 2>/dev/null | grep -A 20 "Schemes:"

Build Command

构建命令

Use this command template, replacing the project path and scheme:
bash
DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer xcodebuild build \
  -project "YourApp.xcodeproj" \
  -scheme "YourApp" \
  -configuration Debug \
  -destination "platform=macOS" \
  2>&1 | grep -E "(BUILD SUCCEEDED|BUILD FAILED|error:)" | head -20
For workspaces (projects with SPM dependencies or CocoaPods):
bash
DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer xcodebuild build \
  -workspace "YourApp.xcworkspace" \
  -scheme "YourApp" \
  -configuration Debug \
  -destination "platform=macOS" \
  2>&1 | grep -E "(BUILD SUCCEEDED|BUILD FAILED|error:)" | head -20
使用以下命令模板,替换项目路径和scheme:
bash
DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer xcodebuild build \
  -project "YourApp.xcodeproj" \
  -scheme "YourApp" \
  -configuration Debug \
  -destination "platform=macOS" \
  2>&1 | grep -E "(BUILD SUCCEEDED|BUILD FAILED|error:)" | head -20
对于包含SPM依赖或CocoaPods的工作区:
bash
DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer xcodebuild build \
  -workspace "YourApp.xcworkspace" \
  -scheme "YourApp" \
  -configuration Debug \
  -destination "platform=macOS" \
  2>&1 | grep -E "(BUILD SUCCEEDED|BUILD FAILED|error:)" | head -20

Interpreting Results

结果解读

  • BUILD SUCCEEDED -- the build passed, report success to the user.
  • BUILD FAILED with
    error:
    lines -- read each error, identify the source file and line, and help the user fix them. After fixing, re-run the build to verify.
  • If the output is empty or unclear, re-run without the grep filter to get full output for diagnosis.
  • BUILD SUCCEEDED——构建通过,向用户报告成功。
  • BUILD FAILED并带有
    error:
    行——读取每个错误,识别源文件和行号,帮助用户修复。修复后,重新运行构建以验证。
  • 如果输出为空或不清晰,去掉grep过滤器重新运行以获取完整输出进行诊断。

When to Build

何时构建

  • After making code changes, if the user asks to verify they compile
  • When the user explicitly says "build", "compile", or "check if it builds"
  • After fixing build errors, to confirm the fix worked
  • 代码更改后,用户要求验证是否可编译时
  • 用户明确说“构建”、“编译”或“检查是否可构建”时
  • 修复构建错误后,确认修复生效时

Xcode Beta Toolchains

Xcode Beta工具链

If the project targets a beta SDK (e.g., macOS 26 Tahoe), you may need to point to the beta Xcode:
bash
DEVELOPER_DIR=/Applications/Xcode-beta.app/Contents/Developer xcodebuild build ...
Check which Xcode is available:
bash
ls /Applications/ | grep -i xcode
如果项目针对Beta版SDK(例如macOS 26 Tahoe),可能需要指向Beta版Xcode:
bash
DEVELOPER_DIR=/Applications/Xcode-beta.app/Contents/Developer xcodebuild build ...
检查可用的Xcode:
bash
ls /Applications/ | grep -i xcode

Common Build Failures

常见构建失败

ErrorFix
no such module 'Sparkle'
SPM dependency not resolved. Try
xcodebuild -resolvePackageDependencies
first
no signing identity found
Set
CODE_SIGN_IDENTITY=""
and
CODE_SIGNING_ALLOWED=NO
for command-line builds
SDK "macosx" cannot be located
Wrong
DEVELOPER_DIR
. Check Xcode installation path
scheme not found
Run
xcodebuild -list
to see available schemes
错误修复方法
no such module 'Sparkle'
SPM依赖未解析。先尝试运行
xcodebuild -resolvePackageDependencies
no signing identity found
为命令行构建设置
CODE_SIGN_IDENTITY=""
CODE_SIGNING_ALLOWED=NO
SDK "macosx" cannot be located
DEVELOPER_DIR
路径错误。检查Xcode安装路径
scheme not found
运行
xcodebuild -list
查看可用的scheme