xcrun
是 Xcode 的工具链的一部分,一般用来配置编译环境,在跨平台的编译配置上非常有用。
内置的帮助说明非常简洁:
Usage: xcrun [options] <tool name> ... arguments ...
Find and execute the named command line tool from the active developer
directory.
The active developer directory can be set using `xcode-select`, or via the
DEVELOPER_DIR environment variable. See the xcrun and xcode-select manual
pages for more information.
Options:
-h, --help show this help message and exit
--version show the xcrun version
-v, --verbose show verbose logging output
--sdk <sdk name> find the tool for the given SDK name
--toolchain <name> find the tool for the given toolchain
-l, --log show commands to be executed (with --run)
-f, --find only find and print the tool path
-r, --run find and execute the tool (the default behavior)
-n, --no-cache do not use the lookup cache
-k, --kill-cache invalidate all existing cache entries
--show-sdk-path show selected SDK install path
--show-sdk-version show selected SDK version
--show-sdk-build-version show selected SDK build version
--show-sdk-platform-path show selected SDK platform path
--show-sdk-platform-version show selected SDK platform version
自动配置 sysroot
编译的时候需要指定 -sysroot=/root/path/to/sdk
,include
和 lib
就基于 sysroot 指定的目录里进行查找,如果是在 macOS 上编译 iOS 程序,sysroot 就指向 iOS sdk 的目录。
macOS 的 sysroot:
xcrun --sdk macosx --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk
iOS 的 sysroot:
xcrun --sdk iphoneos --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.2.sdk
xcrun --sdk iphonesimulator --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.2.sdk
tvOS 的 sysroot:
xcrun --sdk appletvos --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS14.2.sdk
xcrun --sdk appletvsimulator --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator14.2.sdk
watchOS 的 sysroot:
xcrun --sdk watchos --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS7.1.sdk
xcrun --sdk watchsimulator --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator7.1.sdk
自动配置 clang
指定 clang
的位置:
xcrun -find clang
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
clang++
的位置:
xcrun -find clang++
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
配置使用的 Toolchain
一般情况下 Xcode 只有一个 Toolchain,但如果你的 Xcode 安装有多个 Toolchain(比如自己做了一个),可以使用 --toolchain
指定要使用哪一个 toolchain。
xcrun --toolchain "LLVM 11.0.0" --find clang
toolchain 可以使用显示名称也可以使用标识符,比如:
xcrun --toolchain org.llvm.releases.11.0.0 --find clang
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang