Skip to content

Commit 20fdf56

Browse files
committed
Add logic to install Xcode-CLT if needed
On macOS, add logic to install script that will install Xcode command line tools. Most logic cribbed from Homebrew installer. Fixes #325
1 parent 71e9f4f commit 20fdf56

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

install.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ source $opencoarrays_src_dir/prerequisites/install-functions/build_opencoarrays.
234234
# shellcheck source=./prerequisites/install-functions/report_results.sh
235235
source $opencoarrays_src_dir/prerequisites/install-functions/report_results.sh
236236

237+
# shellcheck source=./prerequisites/install-functions/install-xcode-clt.sh
238+
source "${opencoarrays_src_dir}/prerequisites/install-functions/install-xcode-clt.sh"
239+
237240
# ___________________ End of function definitions for use in the Main Body __________________
238241

239242

@@ -303,6 +306,8 @@ elif [[ "${arg_p:-}" == "opencoarrays" ]]; then
303306

304307
else
305308

309+
# Install Xcode command line tools (CLT) if on macOS and if needed
310+
maybe_install_xcodeCLT
306311
# Install OpenCoarrays
307312
cd prerequisites || exit 1
308313
installation_record=install-opencoarrays.log
@@ -320,12 +325,18 @@ elif [[ "${arg_p:-}" == "opencoarrays" ]]; then
320325

321326
elif [[ "${arg_p:-}" == "ofp" ]]; then
322327

328+
# Install Xcode command line tools (CLT) if on macOS and if needed
329+
maybe_install_xcodeCLT
330+
323331
info "Invoking Open Fortran Parser build script with the following command:"
324332
info "\"${opencoarrays_src_dir}\"/prerequisites/install-ofp.sh"
325333
"${opencoarrays_src_dir}"/prerequisites/install-ofp.sh
326334

327335
elif [[ ! -z "${arg_p:-}" ]]; then
328336

337+
# Install Xcode command line tools (CLT) if on macOS and if needed
338+
maybe_install_xcodeCLT
339+
329340
info "Invoking build script with the following command:"
330341
info "\"${opencoarrays_src_dir}\"/prerequisites/build.sh ${*:-}"
331342
"${opencoarrays_src_dir}"/prerequisites/build.sh "${@:-}"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# shellcheck shell=bash disable=SC2154,SC2148
2+
need_xcodeCLT() {
3+
if [[ "${OSTYPE}" != [Dd][Aa][Rr][Ww][Ii][Nn]* ]]; then
4+
echo "false"
5+
else
6+
local xcode_dir
7+
xcode_dir="$(/usr/bin/xcode-select -print-path 2>/dev/null)"
8+
if [[ ! -d "${xcode_dir}" || ! -x "${xcode_dir}/usr/bin/make" ]]; then
9+
echo "true"
10+
else
11+
echo "false"
12+
fi
13+
fi
14+
}
15+
16+
xcode_clt_install () {
17+
info "It appears that you are on Mac OS and do not have the Xcode command line tools (CLT) installed."
18+
app_store_label="$(softwareupdate -l | grep -B 1 -E "Command Line (Developer|Tools)" | awk -F"*" '/^ +\\*/ {print $2}' | sed 's/^ *//' | tail -n1)"
19+
if [[ "${arg_y}" == "${__flag_present}" ]]; then
20+
info "-y or --yes-to-all flag present. Proceeding with non-interactive download and install."
21+
else
22+
printf "Ready to install %s? (Y/n)" "${app_store_label}"
23+
read -r install_xcodeclt
24+
printf '%s\n' "${install_xcodeclt}"
25+
if [[ "${install_xcodeclt}" == [nN]* ]]; then
26+
emergency "${this_script}: Aborting: cannot procede without XCode CLT."
27+
fi
28+
fi
29+
info "We will attempt to download and install XCode CLT for you now. Note: sudo priveledges required"
30+
place_holder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
31+
sudo /usr/bin/touch "${place_holder}" || true
32+
sudo /usr/sbin/softwareupdate -i app_store_label || true
33+
sudo /bin/rm -f "${place_holder}" || true
34+
sudo /usr/bin/xcode-select --switch "/Library/Developer/CommandLineTools" || true
35+
}
36+
37+
headless_xcode_clt_install () {
38+
# Fallback her if headless install failed
39+
if [[ "${arg_y}" == "${__flag_present}" ]]; then
40+
info "-y or --yes-to-all flag present. Proceeding with non-interactive download and install."
41+
else
42+
printf "Ready to install Xcode-CLT using xcode-select? (Y/n)"
43+
read -r install_xcodeclt
44+
printf '%s\n' "${install_xcodeclt}"
45+
if [[ "${install_xcodeclt}" == [nN]* ]]; then
46+
emergency "${this_script}: Aborting: cannot procede without XCode CLT."
47+
fi
48+
fi
49+
info "Installing Xcode Command Line Tools (CLE), please follow instructions on popup window."
50+
sudo /usr/bin/xcode-select --install || emergency "${this_script}: unable to run \`sudo xcode-select --install\`"
51+
printf "Press <enter> once installation has completed"
52+
read -r
53+
sudo /usr/bin/xcode-select --switch "/Library/Developer/CommandLineTools" || \
54+
emergency "${this_script}: Xcode-CLT installation and activation failed, unable to continue"
55+
}
56+
57+
maybe_install_xcodeCLT () {
58+
if [[ "$(need_xcodeCLT)" == "true" ]]; then
59+
xcode_clt_install
60+
fi
61+
if [[ "$(need_xcodeCLT)" == "true" ]]; then
62+
info "First Xcode-CLT installation failed, trying another method"
63+
headless_xcode_clt_install
64+
fi
65+
66+
clang_output="$(/usr/bin/xzrun clang 2>&1)" || true
67+
if [[ "${clang_output}" =~ license ]]; then
68+
emergency "${this_script}: It appears you have not agreed to the Xcode license. Please do so before attempting to run this script again. This may be acheived by opening Xcode.app or running \`sudo xcodebuild -license\`"
69+
fi
70+
}

0 commit comments

Comments
 (0)