Xcode Command Lines Tool

After a brief introduction to the Xcode command line (1), we know how many ‘groups’ the Xcode Command Line tool can have on a MAC.

With the help of Xcode-select, we can specify a particular version of Command Line Tools, and then the entire computer will know which SDK to adjust without setting env variable…. It sounds intuitive, but it’s a little too mysterious… 😰

Let’s dig a little deeper

Due to my knowledge is limited, so this article do extra meals, let’s point to 😅

things we know

As a refresher, when you type a command on a terminal (for example, xcodeBuild), the system looks for the object file of the command you typed in the PATH given in the $PATH environment variable.

echo $PATH

So by typing git, you’re executing /usr/bin/xcodebuild

which git

These are all familiar principles.

things we don’t know

Now that we have XCodeBuild as an example, let’s look at the flow of Xcode Command Line Tools.

I am not familiar with the detailed inverse functions, so I have compiled some reference articles, and those who want to understand more can click the links at the bottom of the article.

Look at/usr/bin/xcodebuild under instruction is how to cooperate with xcode – select, find/Applications/xcode. The app/Contents/Developer/usr/bin/xcodebuild?

Let’s start with /usr/bin/xcodebuild

Lists the textSections of /usr/bin/xcodeBuild

Print the name list of /usr/bin/xcodebuild

Combining these two pieces of information, we can conclude that

  1. An _xcselect_invoke_xcrun function is called
  2. This function is an external symbol for /usr/bin/xcodebuild, from another dynamic library, left to the dynamic link.

Then,

Print the libraries that /usr/bin/xcodeBuild depends on

You can see libxcself. dylib, libsystem.b.dylib,

Print the name list of libxcself. dylib and see if the _xcselect_invoke_xcrun symbol is here

bingo! To find the

Following the Swift-Swiftc author’s tracking, you can get the following

libxcselect.dylib
->
_xcselect_invoke_xcrun
-> 
libxcrun.dylib
->
xcrun_main
Copy the code

The final xcrun_main -> refers to the xcrun directive

xcrun

So far, we’ve been able to abstract all of these stacks down to a line of functions that looks something like this

xcrun('xcodebuild')

And XCRun will lead us to

/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild

We’ll find out how xcrun is used next time.

A dark day seem bright

Reference article:

  • Swift-Swiftc
  • mans-special-xcode-support
  • os-x-and-xcode-doing-it-apple-way