xcrun --find(-f)は、選択中のXcodeに含まれるコマンドラインツールの絶対パスを表示する。ツールは実行されない。

$ xcrun --find clang
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang

/usr/bin/clangや/usr/bin/gitは、選択中のXcodeにある同名のツールを呼び出すシムである。実体のパスを調べたいときや、スクリプトで使うツールを固定したいときは、xcrun --findを使う。

実行例はmacOS 27.0.1、Xcode 27.0のもの。

ツールのパスを調べる

ツール名を1つ指定する。

$ xcrun --find swift
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
$ xcrun --find xcodebuild
/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild
$ xcrun --find simctl
/Applications/Xcode.app/Contents/Developer/usr/bin/simctl

ツールの場所は、Xcodeの中でもToolchains/XcodeDefault.xctoolchain/usr/binとusr/binのように、ツールによって異なる。xcrun --findは、場所を意識せずにパスを取得できる。

ツール名を複数指定すると、エラーになる。

$ xcrun --find clang swift
xcrun: error: invalid argument 'swift', cannot specify multiple utility names

選択中のXcodeによって結果が変わる

xcrunは、xcode-selectで選択中のXcode、または環境変数DEVELOPER_DIRで指定されたXcodeの中から、ツールを探す。

参考: 【GitHub Actions,Xcode】CIで使うXcodeのバージョンを固定する (xcode-select)

DEVELOPER_DIRにCommand Line Toolsを指定すると、Command Line Toolsの中から探す。

$ DEVELOPER_DIR=/Library/Developer/CommandLineTools xcrun --find clang
/Library/Developer/CommandLineTools/usr/bin/clang

Command Line Toolsに含まれないツールは、見つからない。

$ DEVELOPER_DIR=/Library/Developer/CommandLineTools xcrun --find xcodebuild
xcrun: error: unable to find utility "xcodebuild", not a developer tool or in PATH

見つからないとき

ツールが見つからないと、xcrunは標準エラー出力にエラーを表示して、終了コード72で終了する。標準出力には何も出ない。

$ xcrun --find nonexist 2>/dev/null
$ echo $?
72

終了コードを使うと、ツールがあるかを判定できる。

if ! xcrun --find swift-format > /dev/null 2>&1; then
  echo "swift-format not found"
fi

PATHもフォールバックで探される

エラーメッセージのnot a developer tool or in PATHのとおり、xcrunは、Xcodeの中で見つからなかったツールをPATHから探す。

$ PATH=[ツールのフォルダ]:$PATH xcrun --find mytool
[ツールのフォルダ]/mytool

Xcodeの中とPATHに同名のツールがあるときは、Xcodeのツールが優先される。PATHの先頭に偽のgitを置いても、xcrun --find gitはXcodeのgitを返す。

$ PATH=[ツールのフォルダ]:$PATH which git
[ツールのフォルダ]/git
$ PATH=[ツールのフォルダ]:$PATH xcrun --find git
/Applications/Xcode.app/Contents/Developer/usr/bin/git

whichとxcrun --findの結果が異なる場合は、PATHの先頭に別のツールがある。

SDKを指定して探す

--sdkでSDKを指定すると、そのSDKに対応するツールを探す。xcrun --sdk iphoneos --findのように、--findの前に指定する。

$ xcrun --sdk iphoneos --find pngcrush
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/pngcrush
$ xcrun --sdk macosx --find pngcrush
/Applications/Xcode.app/Contents/Developer/usr/bin/pngcrush

pngcrushは、iOSのSDKを指定するとiPhoneOS.platformの中のツールが返る。macOSのSDKを指定すると、usr/binのツールが返る。

存在しないSDKを指定すると、エラーで終了コード72になる。

$ xcrun --sdk bogus --find clang
xcodebuild: error: SDK "bogus" cannot be located.
...
xcrun: error: unable to find utility "clang", not a developer tool or in PATH

SDKの場所自体は--show-sdk-pathで調べる。

$ xcrun --sdk iphoneos --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS27.0.sdk

スクリプトで使う

スクリプトの中で特定のツールを呼ぶときは、xcrun --findの出力を変数に入れる。

CLANG=$(xcrun --find clang)
"$CLANG" --version

ツールを探してすぐ実行するだけなら、xcrunにツール名と引数を渡す。

$ xcrun clang --version

xcode-select -pの出力からパスを組み立てる方法は、ツールの場所がXcodeのバージョンによって変わると壊れる。manページでも、スクリプトやツールはxcrunを使うよう勧めている。

どちらの方法を選ぶか

やりたいことコマンド
ツールを実行するxcrun [ツール名] [引数]
ツールのパスを調べるxcrun --find [ツール名]
SDKごとのツールのパスを調べるxcrun --sdk [SDK名] --find [ツール名]
SDKのパスを調べるxcrun --sdk [SDK名] --show-sdk-path
選択中のXcodeを調べるxcode-select -p

どのXcodeのどのツールが使われるかを確認したいときは、xcrun --findでパスを表示する。