2525# Unless the --no-install-system-deps flag is set, this script will attempt to install Swift's
2626# system dependencies using the system package manager.
2727#
28- # curl is required to run this script.
29-
30- set -o errexit
31- shopt -s extglob
28+ # curl and getopt (from the util-linux package) are required to run this script.
3229
3330has_command () {
3431 command -v " $1 " > /dev/null
@@ -170,26 +167,198 @@ install_system_deps () {
170167 set -o errexit
171168}
172169
173- SWIFTLY_INSTALL_VERSION=" 0.1.0"
170+ set_platform_ubuntu () {
171+ docker_platform_name=" ubuntu"
172+ package_manager=" apt-get"
173+ export DEBIAN_FRONTEND=noninteractive
174+
175+ PLATFORM_NAME=" ubuntu$1$2 "
176+ PLATFORM_NAME_FULL=" ubuntu$1 .$2 "
177+ docker_platform_version=" $1 .$2 "
178+
179+ if [[ -z " $PLATFORM_NAME_PRETTY " ]]; then
180+ PLATFORM_NAME_PRETTY=" Ubuntu $1 .$2 "
181+ fi
182+ }
183+
184+ set_platform_amazonlinux () {
185+ PLATFORM_NAME=" amazonlinux$1 "
186+ PLATFORM_NAME_FULL=" amazonlinux$1 "
187+ docker_platform_name=" amazonlinux"
188+ docker_platform_version=" $1 "
189+ package_manager=" yum"
190+
191+ if [[ -z " $PLATFORM_NAME_PRETTY " ]]; then
192+ PLATFORM_NAME_PRETTY=" Amazon Linux $1 "
193+ fi
194+ }
195+
196+ set_platform_rhel () {
197+ PLATFORM_NAME=" ubi$1 "
198+ PLATFORM_NAME_FULL=" ubi$1 "
199+ docker_platform_name=" rhel-ubi"
200+ docker_platform_version=" $1 "
201+ package_manager=" yum"
202+
203+ if [[ -z " $PLATFORM_NAME_PRETTY " ]]; then
204+ PLATFORM_NAME_PRETTY=" RHEL 9"
205+ fi
206+ }
207+
208+ detect_platform () {
209+ if [[ -f " /etc/os-release" ]]; then
210+ OS_RELEASE=" /etc/os-release"
211+ elif [[ -f " /usr/lib/os-release" ]]; then
212+ OS_RELEASE=" /usr/lib/os-release"
213+ else
214+ manually_select_platform
215+ fi
216+
217+ source " $OS_RELEASE "
218+ PLATFORM_NAME_PRETTY=" $PRETTY_NAME "
219+
220+ case " $ID$ID_LIKE " in
221+ * " amzn" * )
222+ if [[ " $VERSION_ID " != " 2" ]]; then
223+ manually_select_platform
224+ else
225+ set_platform_amazonlinux " 2"
226+ fi
227+ ;;
228+
229+ * " ubuntu" * )
230+ case " $UBUNTU_CODENAME " in
231+ " jammy" )
232+ set_platform_ubuntu " 22" " 04"
233+ ;;
234+
235+ " focal" )
236+ set_platform_ubuntu " 20" " 04"
237+ ;;
238+
239+ " bionic" )
240+ set_platform_ubuntu " 18" " 04"
241+ ;;
242+
243+ * )
244+ manually_select_platform
245+ ;;
246+ esac
247+ ;;
248+
249+ * " rhel" * )
250+ if [[ " $VERSION_ID " != 9* ]]; then
251+ manually_select_platform
252+ else
253+ set_platform_rhel " 9"
254+ fi
255+ ;;
256+
257+ * )
258+ manually_select_platform
259+ ;;
260+ esac
261+ }
262+
263+ manually_select_platform () {
264+ if [[ " $DISABLE_CONFIRMATION " == " true" ]]; then
265+ echo " Error: Unsupported platform: $PRETTY_NAME "
266+ exit 1
267+ fi
268+ echo " $PLATFORM_NAME_PRETTY is not an officially supported platform, but the toolchains for another platform may still work on it."
269+ echo " "
270+ echo " Please select the platform to use for toolchain downloads:"
271+
272+ echo " 0) Cancel"
273+ echo " 1) Ubuntu 22.04"
274+ echo " 2) Ubuntu 20.04"
275+ echo " 3) Ubuntu 18.04"
276+ echo " 4) RHEL 9"
277+ echo " 5) Amazon Linux 2"
278+
279+ read_input_with_default " 0"
280+ case " $READ_INPUT_RETURN " in
281+ " 1" | " 1)" )
282+ set_platform_ubuntu " 22" " 04"
283+ ;;
284+
285+ " 2" | " 2)" )
286+ set_platform_ubuntu " 20" " 04"
287+ ;;
288+
289+ " 3" | " 3)" )
290+ set_platform_ubuntu " 18" " 04"
291+ ;;
292+
293+ " 4" | " 4)" )
294+ set_platform_rhel " 9"
295+ ;;
296+
297+ " 5" | " 5)" )
298+ set_platform_amazonlinux " 2"
299+ ;;
300+
301+ * )
302+ echo " Cancelling installation."
303+ exit 0
304+ ;;
305+ esac
306+ }
307+
308+ verify_getopt_install () {
309+ if ! has_command " getopt" ; then
310+ return 1
311+ fi
312+
313+ getopt --test
314+ # getopt --test exiting with status code 4 implies getopt from util-linux is being used, which we need.
315+ [[ " $? " -eq 4 ]]
316+ return " $? "
317+ }
318+
319+ SWIFTLY_INSTALL_VERSION=" 0.2.0"
174320
175321MODIFY_PROFILE=" true"
176322SWIFTLY_INSTALL_SYSTEM_DEPS=" true"
177323
178- for arg in " $@ " ; do
179- case " $arg " in
324+ if ! has_command " curl" ; then
325+ echo " Error: curl must be installed to download swiftly"
326+ exit 1
327+ fi
328+
329+ if ! verify_getopt_install ; then
330+ echo " Error: getopt must be installed from the util-linux package to run swiftly-install"
331+ exit 1
332+ fi
333+
334+ set -o errexit
335+ shopt -s extglob
336+
337+ short_options=' yhvp:'
338+ long_options=' disable-confirmation,no-modify-profile,no-install-system-deps,help,version,platform:'
339+
340+ args=$( getopt --options " $short_options " --longoptions " $long_options " --name " swiftly-install" -- " ${@ } " )
341+ eval " set -- ${args} "
342+
343+ while [ true ]; do
344+ case " $1 " in
180345 " --help" | " -h" )
181346 cat << EOF
182347swiftly-install $SWIFTLY_INSTALL_VERSION
183348The installer for swiftly.
184349
185350USAGE:
186- swiftly-install [FLAGS ]
351+ swiftly-install [options ]
187352
188- FLAGS :
353+ OPTIONS :
189354 -y, --disable-confirmation Disable confirmation prompt.
190355 --no-modify-profile Do not attempt to modify the profile file to set environment
191356 variables (e.g. PATH) on login.
192357 --no-install-system-deps Do not attempt to install Swift's required system dependencies.
358+ -p, --platform <platform> Specifies which platform's toolchains swiftly will download. If
359+ unspecified, the platform will be automatically detected. Available
360+ options are "ubuntu22.04", "ubuntu20.04", "ubuntu18.04", "rhel9", and
361+ "amazonlinux2".
193362 -h, --help Prints help information.
194363 --version Prints version information.
195364EOF
@@ -198,100 +367,70 @@ EOF
198367
199368 " --disable-confirmation" | " -y" )
200369 DISABLE_CONFIRMATION=" true"
370+ shift
201371 ;;
202372
203373 " --no-modify-profile" )
204374 MODIFY_PROFILE=" false"
375+ shift
205376 ;;
206377
207378 " --no-install-system-deps" )
208379 SWIFTLY_INSTALL_SYSTEM_DEPS=" false"
380+ shift
209381 ;;
210382
211383 " --version" )
212384 echo " $SWIFTLY_INSTALL_VERSION "
213385 exit 0
214386 ;;
215387
388+ " --platform" | " -p" )
389+ case " $2 " in
390+ " ubuntu22.04" )
391+ set_platform_ubuntu " 22" " 04"
392+ ;;
393+
394+ " ubuntu20.04" )
395+ set_platform_ubuntu " 20" " 04"
396+ ;;
397+
398+ " ubuntu18.04" )
399+ set_platform_ubuntu " 18" " 04"
400+ ;;
401+
402+ " amazonlinux2" )
403+ set_platform_amazonlinux " 2"
404+ ;;
405+
406+ " rhel9" )
407+ set_platform_rhel " 9"
408+ ;;
409+
410+ * )
411+ echo " Error: unrecognized platform $2 "
412+ exit 1
413+ ;;
414+ esac
415+ shift 2
416+ ;;
417+
418+ --)
419+ shift
420+ break
421+ ;;
422+
216423 * )
217- echo " Error: unrecognized flag \" $arg \" "
424+ echo " Error: unrecognized option \" $arg \" "
218425 exit 1
219426 ;;
220427 esac
221428done
222429
223- if [[ -f " /etc/os-release" ]]; then
224- OS_RELEASE=" /etc/os-release"
225- elif [[ -f " /usr/lib/os-release" ]]; then
226- OS_RELEASE=" /usr/lib/os-release"
227- else
228- echo " Error: could not detect OS information"
229- exit 1
430+ if [[ -z " $PLATFORM_NAME " ]]; then
431+ detect_platform
230432fi
231433
232- source " $OS_RELEASE "
233-
234- case " $ID " in
235- " amzn" )
236- if [[ " $VERSION_ID " != " 2" ]]; then
237- echo " Error: Unsupported Amazon Linux version: $PRETTY_NAME "
238- exit 1
239- fi
240- PLATFORM_NAME=" amazonlinux2"
241- PLATFORM_NAME_FULL=" amazonlinux2"
242- docker_platform_name=" amazonlinux"
243- docker_platform_version=" 2"
244- package_manager=" yum"
245- ;;
246-
247- " ubuntu" )
248- docker_platform_name=" ubuntu"
249- package_manager=" apt-get"
250- export DEBIAN_FRONTEND=noninteractive
251- case " $UBUNTU_CODENAME " in
252- " jammy" )
253- PLATFORM_NAME=" ubuntu2204"
254- PLATFORM_NAME_FULL=" ubuntu22.04"
255- docker_platform_version=" 22.04"
256- ;;
257-
258- " focal" )
259- PLATFORM_NAME=" ubuntu2004"
260- PLATFORM_NAME_FULL=" ubuntu20.04"
261- docker_platform_version=" 20.04"
262- ;;
263-
264- " bionic" )
265- PLATFORM_NAME=" ubuntu1804"
266- PLATFORM_NAME_FULL=" ubuntu18.04"
267- docker_platform_version=" 18.04"
268- ;;
269-
270- * )
271- echo " Error: Unsupported Ubuntu version: $PRETTY_NAME "
272- exit 1
273- ;;
274- esac
275- ;;
276-
277- " rhel" )
278- if [[ " $VERSION_ID " != 9* ]]; then
279- echo " Error: Unsupported RHEL version: $PRETTY_NAME "
280- exit 1
281- fi
282- PLATFORM_NAME=" ubi9"
283- PLATFORM_NAME_FULL=" ubi9"
284- docker_platform_name=" rhel-ubi"
285- docker_platform_version=" 9"
286- package_manager=" yum"
287- ;;
288-
289- * )
290- echo " Error: Unsupported platform: $PRETTY_NAME "
291- exit 1
292- ;;
293- esac
294-
295434RAW_ARCH=" $( uname -m) "
296435case " $RAW_ARCH " in
297436 " x86_64" )
@@ -306,20 +445,16 @@ case "$RAW_ARCH" in
306445
307446 * )
308447 echo " Error: Unsupported CPU architecture: $RAW_ARCH "
448+ exit 1
309449 ;;
310450esac
311451
312- if ! has_command " curl" ; then
313- echo " Error: curl must be installed to download swiftly"
314- exit 1
315- fi
316-
317452JSON_OUT=$( cat << EOF
318453{
319454 "platform": {
320455 "name": "$PLATFORM_NAME ",
321456 "nameFull": "$PLATFORM_NAME_FULL ",
322- "namePretty": "$PRETTY_NAME ",
457+ "namePretty": "$PLATFORM_NAME_PRETTY ",
323458 "architecture": $PLATFORM_ARCH
324459 },
325460 "installedToolchains": [],
0 commit comments