Find, Install, or Uninstall XCode CLI
xcode-cli.RdSet of functions that seek to identify whether XCode CLI was installed, allow XCode CLI to be installed, and removing XCode CLI.
Usage
is_xcode_cli_installed()
xcode_cli_version()
xcode_cli_path()
xcode_cli_install(
password = base::getOption("macrtools.password"),
verbose = TRUE
)
xcode_cli_uninstall(
password = base::getOption("macrtools.password"),
verbose = TRUE
)
xcode_cli_switch(
path = install_directory_xcode_cli(),
password = base::getOption("macrtools.password"),
verbose = TRUE
)
xcode_cli_reset(
password = base::getOption("macrtools.password"),
verbose = TRUE
)Arguments
- password
User password to access
sudo.- verbose
Display status messages
- path
Developer directory, or
Xcode.appbundle, to make active. Defaults to the Command Line Tools location. Usexcode_toolchains()to see the available options.
Value
The Command Line Tools version from the package receipt, or
NA_character_ when they are not installed.
Check if XCode CLI is installed
is_xcode_cli_installed() reports whether the Command Line Tools package
is present, by looking for the compiler it installs:
This is independent of which developer directory is selected. The previous
implementation compared xcode-select -p against that path with
identical(), so it reported FALSE on every machine where anything else
was active, including the versioned Xcode that CI runners select.
For the selection, see developer_dir() and is_xcode_toolchain_usable().
XCode CLI Installation
The xcode_cli_install() function performs a headless or non-interactive
installation of the Xcode CLI tools. This installation process requires
three steps:
Place a temporary file indicating the need to download Xcode CLI
Determine the latest version of Xcode CLI by running
softwareupdateInstall the latest version using
softwareupdatewithsudo.
The alternative approach would be an interactive installation of Xcode CLI by typing into Terminal:
This command will trigger a pop up window that will walk through the package installation.
Steps of the Headless CLI Installation
The temporary file is created using:
From there, we deduce the latest version of Xcode available to the user through an R sanitized version of the chained shell commands:
product_information=softwareupdate -l |
grep '\\*.*Command Line' |
tail -n 1 |
awk -F"*" '{print $2}' |
sed -e 's/^ *//' |
sed 's/Label: //g' |
tr -d '\n'Then, we trigger the installation process with sudo using:
where $product_information is obtained from the previous command.
Finally, we remove the temporary installation file.
These steps were obtained from Timothy Sutton's xcode-cli-tools.sh script and, slightly modernized.
Uninstalling Xcode CLI
The xcode_cli_uninstall() attempts to remove only the Xcode CLI tools.
Per the Apple Technical Note TN2339:
Xcode includes all of the command-line tools. If it is installed on your system, remove it to uninstall the command-line tools.
If the
/Library/Developer/CommandLineToolsdirectory exists on your system, remove it to uninstall the command-line tools
Thus, the xcode_cli_uninstall() opts to perform the second step only.
We use an R sanitized shell version of:
If the Xcode application is detect, we note that we did not uninstall the Xcode application. Instead, we request the user uninstall the Xcode app using the following steps:
Make sure that Xcode is closed. Quit Xcode if needed.
Open Finder > Applications, select Xcode and move it to Trash.
Empty the trash.
In addition, open Terminal and run:
Change Xcode CLI Location
The path reported by xcode-select may not point at the toolchain you want
to build against. xcode_cli_switch() changes it, defaulting to the
Command Line Tools:
Pass path to select something else, using a path from
xcode_toolchains(). The target is validated first, and the selection being
replaced is named, since the change is machine-wide and affects every build.
Note that DEVELOPER_DIR overrides the stored selection, so this has no
visible effect while that variable is set.
If this does not fix the issue, we recommend using the xcode_cli_reset()
function.