Skip to content

Commit 7dbcf92

Browse files
authored
Merge pull request #419 from sourceryinstitute/issue-325-xcode-clt
Add logic to install Xcode-CLT if needed - Fixes #325
2 parents 71e9f4f + 2e751f3 commit 7dbcf92

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

install.sh

+11
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 "${@:-}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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)" || true
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+
app_store_label="$(softwareupdate -l | grep -B 1 -E "Command Line (Developer|Tools)" | awk -F"*" '/^ +\\*/ {print $2}' | sed 's/^ *//' | tail -n1)" || return 0
18+
if [[ "${arg_y}" == "${__flag_present}" ]]; then
19+
info "-y or --yes-to-all flag present. Proceeding with non-interactive download and install."
20+
else
21+
info "If you proceed with the Xcode-CLT installation you may be prompted for your password."
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 proceed without XCode CLT."
27+
fi
28+
fi
29+
info "install.sh will attempt to download and install XCode CLT for you now."
30+
info "Note: sudo privileges required. Please enter your password if prompted."
31+
place_holder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
32+
sudo /usr/bin/touch "${place_holder}" || true
33+
sudo /usr/sbin/softwareupdate -i app_store_label || return 0
34+
sudo /bin/rm -f "${place_holder}" || true
35+
sudo /usr/bin/xcode-select --switch "/Library/Developer/CommandLineTools" || return 1
36+
}
37+
38+
headless_xcode_clt_install () {
39+
# Fallback here if headless install/upgrade failed. This is the usual path through this code.
40+
if [[ "${arg_y}" == "${__flag_present}" ]]; then
41+
info "-y or --yes-to-all flag present. Proceeding with non-interactive download and install."
42+
else
43+
info "If you proceed with the Xcode-CLT installation you may be prompted for your password."
44+
printf "Ready to install Xcode-CLT using xcode-select? (Y/n)"
45+
read -r install_xcodeclt
46+
printf '%s\n' "${install_xcodeclt}"
47+
if [[ "${install_xcodeclt}" == [nN]* ]]; then
48+
emergency "${this_script}: Aborting: cannot proceed without XCode CLT."
49+
fi
50+
fi
51+
info "Installing Xcode Command Line Tools (CLT), using \`xcode-select --install\`."
52+
info "Note: sudo privileges required. Please enter your password if prompted."
53+
sudo /usr/bin/xcode-select --install || emergency "${this_script}: unable to run \`sudo xcode-select --install\`"
54+
printf "Please press <enter> once installation of Xcode command line tools has completed."
55+
read -r
56+
sudo /usr/bin/xcode-select --switch "/Library/Developer/CommandLineTools" || \
57+
emergency "${this_script}: Xcode-CLT installation and activation failed, unable to continue"
58+
}
59+
60+
maybe_install_xcodeCLT () {
61+
if [[ "$(need_xcodeCLT)" == "true" ]]; then
62+
info "It appears that you are on Mac OS and do not have the Xcode command line tools (CLT) installed,"
63+
info "or they need to be upgraded.install.sh will now attempt to install/upgrade the Xcode-CLT using \`softwareupdate\`"
64+
info "This may take some time, please be patient."
65+
xcode_clt_install || true # This usually fails since `softwareupdate -i` doesn't return CLTs needing update
66+
fi
67+
if [[ "$(need_xcodeCLT)" == "true" ]]; then
68+
info "\`softwareupdate\` installation/upgrade failed. This is normal. Now trying with \`xcode-select --install\`"
69+
headless_xcode_clt_install || emergency "${this_script}: Could not install Xcode command line tools, unable to proceed. Please install them via the app store before retrying this installation."
70+
fi
71+
72+
clang_output="$(/usr/bin/xzrun clang 2>&1)" || true
73+
if [[ "${clang_output}" =~ license ]]; then
74+
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 achieved by opening Xcode.app or running \`sudo xcodebuild -license\`"
75+
fi
76+
}

0 commit comments

Comments
 (0)