forked from swiftlang/github-workflows
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall-and-build-with-sdk.sh
More file actions
executable file
·862 lines (705 loc) · 30.2 KB
/
install-and-build-with-sdk.sh
File metadata and controls
executable file
·862 lines (705 loc) · 30.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift.org open source project
##
## Copyright (c) 2025 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See https://swift.org/LICENSE.txt for license information
## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
##
##===----------------------------------------------------------------------===##
set -euo pipefail
log() { printf -- "** %s\n" "$*" >&2; }
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
fatal() { error "$@"; exit 1; }
# Retry configuration
CURL_MAX_RETRIES=5
CURL_RETRY_DELAY=5
CURL_TIMEOUT=300
SDK_INSTALL_MAX_RETRIES=5
SDK_INSTALL_INITIAL_RETRY_DELAY=10
curl_with_retry() {
local attempt=1
local exit_code=0
while [ $attempt -le $CURL_MAX_RETRIES ]; do
if [ $attempt -gt 1 ]; then
log "Retry attempt $attempt of $CURL_MAX_RETRIES after ${CURL_RETRY_DELAY}s delay..."
sleep $CURL_RETRY_DELAY
fi
# Run curl with connection timeout and max time limits
if curl --connect-timeout 30 --max-time $CURL_TIMEOUT --retry 3 --retry-delay 2 --retry-max-time 60 "$@"; then
return 0
else
exit_code=$?
log "curl failed with exit code $exit_code on attempt $attempt"
fi
attempt=$((attempt + 1))
done
error "curl failed after $CURL_MAX_RETRIES attempts"
return $exit_code
}
swift_sdk_install_with_retry() {
local swift_executable="$1"
local sdk_url="$2"
local checksum="$3"
local sdk_type="$4"
local attempt=1
local retry_delay=$SDK_INSTALL_INITIAL_RETRY_DELAY
# Extract SDK name from URL for checking if already installed
local sdk_filename
sdk_filename=$(basename "$sdk_url")
local sdk_name="${sdk_filename%.artifactbundle.tar.gz}"
while [ $attempt -le $SDK_INSTALL_MAX_RETRIES ]; do
if [ $attempt -gt 1 ]; then
log "Retry attempt $attempt of $SDK_INSTALL_MAX_RETRIES for ${sdk_type} SDK installation after ${retry_delay}s delay..."
# Before retrying, check if SDK was partially installed and remove it
log "Checking for partially installed SDK..."
if "$swift_executable" sdk list 2>/dev/null | grep -q "^${sdk_name}"; then
log "Found partially installed SDK, attempting to remove it..."
if "$swift_executable" sdk remove "$sdk_name" 2>/dev/null; then
log "Successfully removed partially installed SDK"
else
log "Warning: Failed to remove partially installed SDK, continuing anyway..."
fi
fi
sleep $retry_delay
fi
log "Attempt $attempt: Installing ${sdk_type} SDK from ${sdk_url}"
if "$swift_executable" sdk install "$sdk_url" --checksum "$checksum"; then
log "✅ ${sdk_type} SDK installed successfully"
return 0
else
local exit_code=$?
log "swift sdk install failed with exit code $exit_code on attempt $attempt"
# Exponential backoff: double the delay each time
retry_delay=$((retry_delay * 2))
fi
attempt=$((attempt + 1))
done
error "${sdk_type} SDK installation failed after $SDK_INSTALL_MAX_RETRIES attempts"
return 1
}
# Parse command line options
INSTALL_ANDROID=false
INSTALL_STATIC_LINUX=false
INSTALL_WASM=false
BUILD_EMBEDDED_WASM=false
SWIFT_VERSION_INPUT=""
SWIFT_BUILD_FLAGS=""
SWIFT_BUILD_COMMAND="swift build"
ANDROID_SDK_TRIPLES=()
while [[ $# -gt 0 ]]; do
case $1 in
--android)
INSTALL_ANDROID=true
shift
;;
--android-ndk-version=*)
ANDROID_NDK_VERSION="${1#*=}"
shift
;;
--android-sdk-triple=*)
ANDROID_SDK_TRIPLES+=("${1#*=}")
shift
;;
--static)
INSTALL_STATIC_LINUX=true
shift
;;
--wasm)
INSTALL_WASM=true
shift
;;
--embedded-wasm)
INSTALL_WASM=true
BUILD_EMBEDDED_WASM=true
shift
;;
--flags=*)
SWIFT_BUILD_FLAGS="${1#*=}"
shift
;;
--build-command=*)
SWIFT_BUILD_COMMAND="${1#*=}"
shift
;;
-*)
fatal "Unknown option: $1"
;;
*)
if [[ -z "$SWIFT_VERSION_INPUT" ]]; then
SWIFT_VERSION_INPUT="$1"
else
fatal "Multiple Swift versions specified: $SWIFT_VERSION_INPUT and $1"
fi
shift
;;
esac
done
# Validate arguments
if [[ -z "$SWIFT_VERSION_INPUT" ]]; then
fatal "Usage: $0 [--android] [--static] [--wasm] [--flags=\"<build-flags>\"] [--build-command=\"<build-command>\"] <swift-version>"
fi
if [[ "$INSTALL_ANDROID" == false && "$INSTALL_STATIC_LINUX" == false && "$INSTALL_WASM" == false ]]; then
fatal "At least one of --android or --static or --wasm must be specified"
fi
log "Requested Swift version: $SWIFT_VERSION_INPUT"
log "Install Android Swift SDK: $INSTALL_ANDROID"
log "Install Static Linux Swift SDK: $INSTALL_STATIC_LINUX"
log "Install Wasm Swift SDK: $INSTALL_WASM"
if [[ -n "$SWIFT_BUILD_FLAGS" ]]; then
log "Additional build flags: $SWIFT_BUILD_FLAGS"
fi
install_package() {
# Detect package manager
if command -v apt >/dev/null 2>&1; then
INSTALL_PACKAGE_COMMAND="apt update -q && apt install -yq"
elif command -v dnf >/dev/null 2>&1; then
INSTALL_PACKAGE_COMMAND="dnf install -y"
elif command -v yum >/dev/null 2>&1; then
INSTALL_PACKAGE_COMMAND="yum install -y"
else
fatal "No supported package manager found"
fi
eval "$INSTALL_PACKAGE_COMMAND $1"
}
# Install dependencies
command -v curl >/dev/null || install_package curl
command -v jq >/dev/null || install_package jq
SWIFT_API_INSTALL_ROOT="https://www.swift.org/api/v1/install"
# Transforms a minor Swift release version into its latest patch version
# and gets the checksum for the patch version's Android, Static Linux, or Wasm Swift SDK.
#
# $1 (string): A minor Swift version, e.g. "6.1"
# Output: A string of the form "<patch-version>|<android-checksum>|<static-checksum>|<static-version>|<wasm-checksum>
find_latest_swift_version() {
local minor_version="$1"
log "Finding latest patch version for Swift ${minor_version}"
log "Fetching releases from swift.org API..."
local releases_json
releases_json=$(curl_with_retry -fsSL "${SWIFT_API_INSTALL_ROOT}/releases.json") || fatal "Failed to fetch Swift releases"
# Find all releases that start with the minor version (e.g, "6.1")
# Sort them and get the latest one
local latest_version
latest_version=$(echo "$releases_json" | jq -r --arg minor "$minor_version" '
.[]
| select(.name | startswith($minor))
| .name
' | sort -V | tail -n1)
if [[ -z "$latest_version" ]]; then
fatal "No Swift release found for version $minor_version"
fi
log "Found latest patch version: $latest_version"
local android_sdk_checksum=""
if [[ "$INSTALL_ANDROID" == true ]]; then
android_sdk_checksum=$(echo "$releases_json" | jq -r --arg version "$latest_version" '
.[]
| select(.name == $version)
| .platforms[]
| select(.platform == "android-sdk")
| .checksum
')
if [[ -z "$android_sdk_checksum" ]]; then
fatal "No Android Swift SDK checksum found for Swift $latest_version"
fi
log "Found Android Swift SDK checksum: ${android_sdk_checksum:0:12}..."
fi
local static_linux_sdk_checksum=""
local static_linux_sdk_version=""
if [[ "$INSTALL_STATIC_LINUX" == true ]]; then
static_linux_sdk_checksum=$(echo "$releases_json" | jq -r --arg version "$latest_version" '
.[]
| select(.name == $version)
| .platforms[]
| select(.platform == "static-sdk")
| .checksum
')
if [[ -z "$static_linux_sdk_checksum" ]]; then
fatal "No Static Linux Swift SDK checksum found for Swift $latest_version"
fi
log "Found Static Linux Swift SDK checksum: ${static_linux_sdk_checksum:0:12}..."
static_linux_sdk_version=$(echo "$releases_json" | jq -r --arg version "$latest_version" '
.[]
| select(.name == $version)
| .platforms[]
| select(.platform == "static-sdk")
| .version
')
if [[ -z "$static_linux_sdk_version" ]]; then
fatal "No Static Linux Swift SDK version found for Swift $latest_version"
fi
log "Found Static Linux Swift SDK version: ${static_linux_sdk_version}"
fi
local wasm_sdk_checksum=""
if [[ "$INSTALL_WASM" == true ]]; then
wasm_sdk_checksum=$(echo "$releases_json" | jq -r --arg version "$latest_version" '
.[]
| select(.name == $version)
| .platforms[]
| select(.platform == "wasm-sdk")
| .checksum
')
if [[ -z "$wasm_sdk_checksum" ]]; then
fatal "No Swift SDK for Wasm checksum found for Swift $latest_version"
fi
log "Found Swift SDK for Wasm checksum: ${wasm_sdk_checksum:0:12}..."
fi
echo "${latest_version}|${android_sdk_checksum}|${static_linux_sdk_checksum}|${static_linux_sdk_version}|${wasm_sdk_checksum}"
}
# Finds the latest Android or Static Linux or Wasm
# Swift SDK development snapshot for the inputted
# Swift version and its checksum.
#
# $1 (string): Nightly Swift version, e.g. "6.2" or "main"
# $2 (string): "android" or "static" or "wasm"
# Output: A string of the form "<snapshot>|<sdk-checksum>|<download-filename>",
# e.g. "swift-6.2-DEVELOPMENT-SNAPSHOT-2025-07-29-a|<sdk-checksum>|swift-6.2-DEVELOPMENT-SNAPSHOT-2025-07-29-a_static-linux-0.0.1.artifactbundle.tar.gz"
find_latest_sdk_snapshot() {
local version="$1"
local sdk_name="$2"
log "Finding latest ${sdk_name}-sdk for Swift nightly-${version}"
log "Fetching development snapshots from swift.org API..."
local sdk_json
sdk_json=$(curl_with_retry -fsSL "${SWIFT_API_INSTALL_ROOT}/dev/${version}/${sdk_name}-sdk.json") || fatal "Failed to fetch ${sdk_name}-sdk development snapshots"
# Extract the snapshot tag from the "dir" field of the first (newest) element
local snapshot_tag
snapshot_tag=$(echo "$sdk_json" | jq -r '.[0].dir')
if [[ -z "$snapshot_tag" || "$snapshot_tag" == "null" ]]; then
fatal "No ${version} snapshot tag found for ${sdk_name}-sdk"
fi
log "Found latest ${version} ${sdk_name}-sdk snapshot: $snapshot_tag"
# Extract the checksum
local checksum
checksum=$(echo "$sdk_json" | jq -r '.[0].checksum')
if [[ -z "$checksum" || "$checksum" == "null" ]]; then
fatal "No checksum found for ${sdk_name}-sdk snapshot"
fi
log "Found ${sdk_name}-sdk checksum: ${checksum:0:12}..."
# Extract the download filename
local download
download=$(echo "$sdk_json" | jq -r '.[0].download')
if [[ -z "$download" || "$download" == "null" ]]; then
fatal "No download filename found for ${sdk_name}-sdk snapshot"
fi
log "Found ${sdk_name}-sdk download filename: $download"
echo "${snapshot_tag}|${checksum}|${download}"
}
SWIFT_VERSION_BRANCH=""
ANDROID_SDK_TAG=""
ANDROID_SDK_CHECKSUM=""
ANDROID_SDK_PATH_SEP="-"
STATIC_LINUX_SDK_TAG=""
STATIC_LINUX_SDK_CHECKSUM=""
STATIC_LINUX_SDK_VERSION=""
STATIC_LINUX_SDK_FILENAME=""
WASM_SDK_TAG=""
WASM_SDK_CHECKSUM=""
# Parse Swift version input which may contain "nightly-"
if [[ "$SWIFT_VERSION_INPUT" == nightly-* ]]; then
version="${SWIFT_VERSION_INPUT#nightly-}"
if [[ "$version" == "main" ]]; then
SWIFT_VERSION_BRANCH="development"
else
SWIFT_VERSION_BRANCH="swift-${version}-branch"
fi
if [[ "$INSTALL_ANDROID" == true ]]; then
android_sdk_info=$(find_latest_sdk_snapshot "$version" "android")
ANDROID_SDK_TAG=$(echo "$android_sdk_info" | cut -d'|' -f1)
ANDROID_SDK_CHECKSUM=$(echo "$android_sdk_info" | cut -d'|' -f2)
fi
if [[ "$INSTALL_STATIC_LINUX" == true ]]; then
static_linux_sdk_info=$(find_latest_sdk_snapshot "$version" "static")
STATIC_LINUX_SDK_TAG=$(echo "$static_linux_sdk_info" | cut -d'|' -f1)
STATIC_LINUX_SDK_CHECKSUM=$(echo "$static_linux_sdk_info" | cut -d'|' -f2)
STATIC_LINUX_SDK_FILENAME=$(echo "$static_linux_sdk_info" | cut -d'|' -f3)
fi
if [[ "$INSTALL_WASM" == true ]]; then
wasm_sdk_info=$(find_latest_sdk_snapshot "$version" "wasm")
WASM_SDK_TAG=$(echo "$wasm_sdk_info" | cut -d'|' -f1)
WASM_SDK_CHECKSUM=$(echo "$wasm_sdk_info" | cut -d'|' -f2)
fi
else
latest_version_info=$(find_latest_swift_version "$SWIFT_VERSION_INPUT")
latest_version=$(echo "$latest_version_info" | cut -d'|' -f1)
SWIFT_VERSION_BRANCH="swift-${latest_version}-release"
ANDROID_SDK_TAG="swift-${latest_version}-RELEASE"
ANDROID_SDK_CHECKSUM=$(echo "$latest_version_info" | cut -d'|' -f2)
STATIC_LINUX_SDK_TAG="swift-${latest_version}-RELEASE"
STATIC_LINUX_SDK_CHECKSUM=$(echo "$latest_version_info" | cut -d'|' -f3)
STATIC_LINUX_SDK_VERSION=$(echo "$latest_version_info" | cut -d'|' -f4)
STATIC_LINUX_SDK_FILENAME="${STATIC_LINUX_SDK_TAG}_static-linux-${STATIC_LINUX_SDK_VERSION}.artifactbundle.tar.gz"
WASM_SDK_TAG="swift-${latest_version}-RELEASE"
WASM_SDK_CHECKSUM=$(echo "$latest_version_info" | cut -d'|' -f5)
fi
# Validate that required Swift SDK tags are set
if [[ "$INSTALL_ANDROID" == true && -z "$ANDROID_SDK_TAG" ]]; then
fatal "ANDROID_SDK_TAG is not set but Android Swift SDK installation was requested"
fi
if [[ "$INSTALL_STATIC_LINUX" == true && -z "$STATIC_LINUX_SDK_TAG" ]]; then
fatal "STATIC_LINUX_SDK_TAG is not set but Static Linux Swift SDK installation was requested"
fi
if [[ "$INSTALL_WASM" == true && -z "$WASM_SDK_TAG" ]]; then
fatal "WASM_SDK_TAG is not set but Wasm Swift SDK installation was requested"
fi
get_installed_swift_tag() {
if ! command -v swift >/dev/null 2>&1; then
log "Swift is not currently installed"
echo "none"
return 0
fi
# Check for /.swift_tag file
if [[ -f "/.swift_tag" ]]; then
local swift_tag
swift_tag=$(tr -d '\n' < /.swift_tag | tr -d ' ')
if [[ -n "$swift_tag" ]]; then
log "✅ Found Swift snapshot tag in /.swift_tag: $swift_tag"
echo "$swift_tag"
return 0
fi
fi
# Try to get release version from swift command if available
local swift_tag
swift_tag=$(swift --version 2>/dev/null | grep -o "(swift-.*-RELEASE)" | tr -d "()" | head -n1)
if [[ -n "$swift_tag" ]]; then
log "✅ Found Swift release tag via 'swift --version': $swift_tag"
echo "$swift_tag"
return 0
fi
log "Could not find tag of the installed Swift version"
echo "none"
}
OS_NAME=""
OS_NAME_NO_DOT=""
OS_ARCH_SUFFIX=""
# Detects OS from /etc/os-release and sets global variables
#
# OS_NAME: Lowercased OS name with the version dot included, e.g. ubuntu22.04
# OS_NAME_NO_DOT: Version dot excluded, e.g. ubuntu2204
# OS_ARCH_SUFFIX: "-aarch64" for aarch64 platforms, otherwise ""
initialize_os_info() {
if [[ -n "$OS_NAME" ]]; then
log "Already detected OS: $OS_NAME"
return 0
fi
if [[ ! -f /etc/os-release ]]; then
fatal "Cannot detect OS: /etc/os-release not found"
fi
local os_id
os_id=$(grep '^ID=' /etc/os-release | cut -d'=' -f2 | tr -d '"' | tr '[:upper:]' '[:lower:]')
local version_id
version_id=$(grep '^VERSION_ID=' /etc/os-release | cut -d'=' -f2 | tr -d '"')
if [[ -z "$os_id" || -z "$version_id" ]]; then
fatal "Could not parse OS information from /etc/os-release"
fi
log "✅ Detected OS from /etc/os-release: ${os_id}${version_id}"
if [[ "$os_id" == "rhel" && "$version_id" == 9* ]]; then
OS_NAME="ubi9"
OS_NAME_NO_DOT="ubi9"
elif [[ "$os_id" == "amzn" && "$version_id" == "2" ]]; then
OS_NAME="amazonlinux2"
OS_NAME_NO_DOT="amazonlinux2"
else
# Ubuntu, Debian, Fedora
OS_NAME="${os_id}${version_id}"
OS_NAME_NO_DOT="${os_id}$(echo "$version_id" | tr -d '.')"
fi
log "Using OS name: $OS_NAME"
local arch
arch=$(uname -m)
if [[ "$arch" == "aarch64" ]]; then
OS_ARCH_SUFFIX="-aarch64"
log "Detected aarch64 architecture, using suffix: $OS_ARCH_SUFFIX"
else
OS_ARCH_SUFFIX=""
log "Detected $arch architecture, using no suffix"
fi
}
# Directory for extracted toolchains (if needed to match the SDKs)
TOOLCHAIN_DIR="${HOME}/.swift-toolchains"
SWIFT_DOWNLOAD_ROOT="https://download.swift.org"
download_and_verify() {
local url="$1"
local sig_url="$2"
local output_file="$3"
local temp_sig="${output_file}.sig"
log "Downloading ${url}"
curl_with_retry -fsSL "$url" -o "$output_file"
log "Downloading signature"
curl_with_retry -fsSL "$sig_url" -o "$temp_sig"
log "Setting up GPG for verification"
local gnupghome
gnupghome="$(mktemp -d)"
export GNUPGHOME="$gnupghome"
curl_with_retry -fSsL https://swift.org/keys/all-keys.asc | zcat -f | gpg --import - >/dev/null 2>&1
log "Verifying signature"
if gpg --batch --verify "$temp_sig" "$output_file" >/dev/null 2>&1; then
log "✅ Signature verification successful"
else
fatal "Signature verification failed"
fi
rm -rf "$GNUPGHOME" "$temp_sig"
}
readonly EXIT_TOOLCHAIN_NOT_FOUND=44
# Downloads and extracts the Swift toolchain for the given snapshot tag
#
# $1 (string): A snapshot tag, e.g. "swift-6.2-DEVELOPMENT-SNAPSHOT-2025-07-29-a"
# Output: Path to the installed swift executable
download_and_extract_toolchain() {
local snapshot_tag="$1"
log "Downloading Swift toolchain: $snapshot_tag"
# "https://download.swift.org/swift-6.2-branch/ubuntu2204/swift-6.2-DEVELOPMENT-SNAPSHOT-2025-07-29-a"
local snapshot_root="${SWIFT_DOWNLOAD_ROOT}/${SWIFT_VERSION_BRANCH}/${OS_NAME_NO_DOT}${OS_ARCH_SUFFIX}/${snapshot_tag}"
# "swift-6.2-DEVELOPMENT-SNAPSHOT-2025-07-29-a-ubuntu22.04.tar.gz"
# "swift-6.2-DEVELOPMENT-SNAPSHOT-2025-07-29-a-ubuntu22.04.tar.gz.sig"
local toolchain_filename="${snapshot_tag}-${OS_NAME}${OS_ARCH_SUFFIX}.tar.gz"
local toolchain_sig_filename="${toolchain_filename}.sig"
local toolchain_url="${snapshot_root}/${toolchain_filename}"
local toolchain_sig_url="${snapshot_root}/${toolchain_sig_filename}"
# Check if toolchain is available
local http_code
http_code=$(curl_with_retry -sSL --head -w "%{http_code}" -o /dev/null "$toolchain_url")
if [[ "$http_code" == "404" ]]; then
log "Toolchain not found: ${toolchain_filename}"
log "Exiting workflow..."
# Don't fail the workflow if we can't find the right toolchain
exit $EXIT_TOOLCHAIN_NOT_FOUND
fi
# Create toolchain directory
mkdir -p "$TOOLCHAIN_DIR"
local toolchain_path="${TOOLCHAIN_DIR}/${snapshot_tag}"
# Check if toolchain already exists
if [[ -d "$toolchain_path" && -f "${toolchain_path}/usr/bin/swift" ]]; then
log "✅ Toolchain already exists at: $toolchain_path"
echo "$toolchain_path/usr/bin/swift"
return 0
fi
# Create temporary directory
local temp_dir
temp_dir=$(mktemp -d)
local toolchain_file="${temp_dir}/swift_toolchain.tar.gz"
# Download and verify toolchain
download_and_verify "$toolchain_url" "$toolchain_sig_url" "$toolchain_file"
log "Extracting toolchain to: $toolchain_path"
mkdir -p "$toolchain_path"
tar -xzf "$toolchain_file" --directory "$toolchain_path" --strip-components=1
# Clean up
rm -rf "$temp_dir"
local swift_executable="${toolchain_path}/usr/bin/swift"
if [[ -f "$swift_executable" ]]; then
log "✅ Swift toolchain extracted successfully"
echo "$swift_executable"
else
fatal "Swift executable not found at expected path: $swift_executable"
fi
}
INSTALLED_SWIFT_TAG=$(get_installed_swift_tag)
SWIFT_EXECUTABLE_FOR_ANDROID_SDK=""
SWIFT_EXECUTABLE_FOR_STATIC_LINUX_SDK=""
SWIFT_EXECUTABLE_FOR_WASM_SDK=""
if [[ "$INSTALL_ANDROID" == true ]]; then
if [[ "$INSTALLED_SWIFT_TAG" == "$ANDROID_SDK_TAG" ]]; then
log "Current toolchain matches Android Swift SDK snapshot: $ANDROID_SDK_TAG"
SWIFT_EXECUTABLE_FOR_ANDROID_SDK="swift"
else
log "Installing Swift toolchain to match Android Swift SDK snapshot: $ANDROID_SDK_TAG"
initialize_os_info
SWIFT_EXECUTABLE_FOR_ANDROID_SDK=$(download_and_extract_toolchain "$ANDROID_SDK_TAG")
if [[ $? -eq $EXIT_TOOLCHAIN_NOT_FOUND ]]; then
# Don't fail the workflow if we can't find the right toolchain
exit 0
fi
fi
fi
if [[ "$INSTALL_STATIC_LINUX" == true ]]; then
if [[ "$INSTALLED_SWIFT_TAG" == "$STATIC_LINUX_SDK_TAG" ]]; then
log "Current toolchain matches Static Linux Swift SDK snapshot: $STATIC_LINUX_SDK_TAG"
SWIFT_EXECUTABLE_FOR_STATIC_LINUX_SDK="swift"
else
log "Installing Swift toolchain to match Static Linux Swift SDK snapshot: $STATIC_LINUX_SDK_TAG"
initialize_os_info
SWIFT_EXECUTABLE_FOR_STATIC_LINUX_SDK=$(download_and_extract_toolchain "$STATIC_LINUX_SDK_TAG")
if [[ $? -eq $EXIT_TOOLCHAIN_NOT_FOUND ]]; then
# Don't fail the workflow if we can't find the right toolchain
exit 0
fi
fi
fi
if [[ "$INSTALL_WASM" == true ]]; then
if [[ "$INSTALLED_SWIFT_TAG" == "$WASM_SDK_TAG" ]]; then
log "Current toolchain matches Wasm Swift SDK snapshot: $WASM_SDK_TAG"
SWIFT_EXECUTABLE_FOR_WASM_SDK="swift"
else
log "Installing Swift toolchain to match Wasm Swift SDK snapshot: $WASM_SDK_TAG"
initialize_os_info
SWIFT_EXECUTABLE_FOR_WASM_SDK=$(download_and_extract_toolchain "$WASM_SDK_TAG")
if [[ $? -eq $EXIT_TOOLCHAIN_NOT_FOUND ]]; then
# Don't fail the workflow if we can't find the right toolchain
exit 0
fi
fi
fi
ANDROID_SDK_DOWNLOAD_ROOT="${SWIFT_DOWNLOAD_ROOT}/${SWIFT_VERSION_BRANCH}/android-sdk"
STATIC_LINUX_SDK_DOWNLOAD_ROOT="${SWIFT_DOWNLOAD_ROOT}/${SWIFT_VERSION_BRANCH}/static-sdk"
WASM_SDK_DOWNLOAD_ROOT="${SWIFT_DOWNLOAD_ROOT}/${SWIFT_VERSION_BRANCH}/wasm-sdk"
install_android_ndk() {
local ndk_version="$1"
log "Installing Android NDK: $ndk_version"
curl_with_retry -fsSL -o ndk.zip https://dl.google.com/android/repository/android-ndk-"${ndk_version}"-"$(uname -s)".zip
command -v unzip >/dev/null || install_package unzip
unzip -q ndk.zip
rm ndk.zip
export ANDROID_NDK_HOME="${PWD}"/android-ndk-"${ndk_version}"
}
install_android_sdk() {
# Check if the Android Swift SDK is already installed
if "$SWIFT_EXECUTABLE_FOR_ANDROID_SDK" sdk list 2>/dev/null | grep -q "^${ANDROID_SDK_TAG}_android"; then
log "✅ Android Swift SDK ${ANDROID_SDK_TAG} is already installed, skipping installation"
return 0
fi
log "Installing Android Swift SDK: $ANDROID_SDK_TAG"
local android_sdk_name="${ANDROID_SDK_TAG}_android"
local android_sdk_bundle_name="${android_sdk_name}.artifactbundle"
local android_sdk_filename="${android_sdk_bundle_name}.tar.gz"
local sdk_url="${ANDROID_SDK_DOWNLOAD_ROOT}/${ANDROID_SDK_TAG}/${android_sdk_filename}"
if ! swift_sdk_install_with_retry "$SWIFT_EXECUTABLE_FOR_ANDROID_SDK" "$sdk_url" "$ANDROID_SDK_CHECKSUM" "Android Swift"; then
fatal "Failed to install Android Swift SDK"
fi
rm -f "${sdk_url}"
# now setup the link to the local ANDROID_NDK_HOME
swift sdk configure --show-configuration "$(swift sdk list | grep android | tail -n 1)"
# guess some common places where the swift-sdks file lives
cd ~/Library/org.swift.swiftpm || cd ~/.config/swiftpm || cd ~/.local/swiftpm || cd ~/.swiftpm || cd /root/.swiftpm
# permit the "--android-ndk" flag to override the default
local android_ndk_version="${ANDROID_NDK_VERSION:-r27d}"
log "Checking for Android NDK $android_ndk_version at $ANDROID_NDK_HOME"
# Download and install the Android NDK.
# Note that we could use the system package manager, but it is
# named different things for different distributions
# (e.g., "google-android-ndk-r26-installer" on Debian)
if [[ ! -d "${ANDROID_NDK_HOME:-}" ]]; then
install_android_ndk "$android_ndk_version"
elif ! grep -q "Pkg.ReleaseName = ${android_ndk_version}" "${ANDROID_NDK_HOME}/source.properties"; then
log "Android NDK $android_ndk_version did not match $(grep "Pkg.ReleaseName" "$ANDROID_NDK_HOME/source.properties")"
# Check if the correct NDK is already cached in the same directory, as
# it often is on GitHub runners, and use it if so
ndk_release=$(echo "$android_ndk_version" | tr -d "[:alpha:]")
try_ndk_path=$(find "$(dirname "${ANDROID_NDK_HOME}")" -name "${ndk_release}*" -maxdepth 1)
if [[ -d "${try_ndk_path}" ]] && grep -q "Pkg.ReleaseName = ${android_ndk_version}" "${try_ndk_path}/source.properties"; then
log "Found a matching Android NDK $android_ndk_version at $try_ndk_path instead"
export ANDROID_NDK_HOME="$try_ndk_path"
else
install_android_ndk "$android_ndk_version"
fi
fi
./swift-sdks/"${android_sdk_bundle_name}"/swift-android/scripts/setup-android-sdk.sh
cd -
}
install_static_linux_sdk() {
local sdk_name="${STATIC_LINUX_SDK_FILENAME%.artifactbundle.tar.gz}"
# Check if the Static Linux Swift SDK is already installed
if "$SWIFT_EXECUTABLE_FOR_STATIC_LINUX_SDK" sdk list 2>/dev/null | grep -q "^${sdk_name}"; then
log "✅ Static Linux Swift SDK ${STATIC_LINUX_SDK_TAG} is already installed, skipping installation"
return 0
fi
log "Installing Static Linux Swift SDK: $STATIC_LINUX_SDK_TAG"
local sdk_url="${STATIC_LINUX_SDK_DOWNLOAD_ROOT}/${STATIC_LINUX_SDK_TAG}/${STATIC_LINUX_SDK_FILENAME}"
if ! swift_sdk_install_with_retry "$SWIFT_EXECUTABLE_FOR_STATIC_LINUX_SDK" "$sdk_url" "$STATIC_LINUX_SDK_CHECKSUM" "Static Linux Swift"; then
fatal "Failed to install Static Linux Swift SDK"
fi
rm -f "${sdk_url}"
}
install_wasm_sdk() {
# Check if Swift SDK for Wasm is already installed
if "$SWIFT_EXECUTABLE_FOR_WASM_SDK" sdk list 2>/dev/null | grep -q "^${WASM_SDK_TAG}_wasm"; then
log "✅ Swift SDK for Wasm ${WASM_SDK_TAG} is already installed, skipping installation"
return 0
fi
log "Installing Swift SDK for Wasm: $WASM_SDK_TAG"
local wasm_sdk_filename="${WASM_SDK_TAG}_wasm.artifactbundle.tar.gz"
local sdk_url="${WASM_SDK_DOWNLOAD_ROOT}/${WASM_SDK_TAG}/${wasm_sdk_filename}"
if ! swift_sdk_install_with_retry "$SWIFT_EXECUTABLE_FOR_WASM_SDK" "$sdk_url" "$WASM_SDK_CHECKSUM" "Swift Wasm"; then
fatal "Failed to install Swift SDK for Wasm"
fi
rm -f "${sdk_url}"
}
install_sdks() {
if [[ "$INSTALL_ANDROID" == true ]]; then
log "Starting install of Swift ${SWIFT_VERSION_INPUT} Android Swift SDK"
install_android_sdk
fi
if [[ "$INSTALL_STATIC_LINUX" == true ]]; then
log "Starting install of Swift ${SWIFT_VERSION_INPUT} Static Linux Swift SDK"
install_static_linux_sdk
fi
if [[ "$INSTALL_WASM" == true ]]; then
log "Starting install of Swift ${SWIFT_VERSION_INPUT} Wasm Swift SDK"
install_wasm_sdk
fi
}
build() {
# Enable alias expansion to use a 'swift' alias for the executable path
shopt -s expand_aliases
if [[ "$INSTALL_ANDROID" == true ]]; then
log "Running Swift build with Android Swift SDK"
local sdk_name="${ANDROID_SDK_TAG}${ANDROID_SDK_PATH_SEP}android"
alias swift='$SWIFT_EXECUTABLE_FOR_ANDROID_SDK'
# This can become a single invocation in the future when `swift build` supports multiple Android triples at once
for android_sdk_triple in "${ANDROID_SDK_TRIPLES[@]}" ; do
local build_command="$SWIFT_BUILD_COMMAND --swift-sdk ${android_sdk_triple}"
if [[ -n "$SWIFT_BUILD_FLAGS" ]]; then
build_command="$build_command $SWIFT_BUILD_FLAGS"
fi
log "Running: $build_command"
# clear the ANDROID_NDK_ROOT environment variable if it is set
# due to https://github.com/swiftlang/swift-driver/pull/1879
# otherwise build error: missing required module 'SwiftAndroid'
export ANDROID_NDK_ROOT=""
if eval "$build_command"; then
log "✅ Swift build with Android Swift SDK completed successfully"
else
fatal "Swift build with Android Swift SDK failed"
fi
done
fi
if [[ "$INSTALL_STATIC_LINUX" == true ]]; then
log "Running Swift build with Static Linux Swift SDK"
local sdk_name="${STATIC_LINUX_SDK_FILENAME%.artifactbundle.tar.gz}"
alias swift='$SWIFT_EXECUTABLE_FOR_STATIC_LINUX_SDK'
local build_command="$SWIFT_BUILD_COMMAND --swift-sdk $sdk_name"
if [[ -n "$SWIFT_BUILD_FLAGS" ]]; then
build_command="$build_command $SWIFT_BUILD_FLAGS"
fi
log "Running: $build_command"
if eval "$build_command"; then
log "✅ Swift build with Static Linux Swift SDK completed successfully"
else
fatal "Swift build with Static Linux Swift SDK failed"
fi
fi
if [[ "$INSTALL_WASM" == true ]]; then
log "Running Swift build with Swift SDK for Wasm"
if [[ "$BUILD_EMBEDDED_WASM" == true ]]; then
local sdk_name="${WASM_SDK_TAG}_wasm-embedded"
else
local sdk_name="${WASM_SDK_TAG}_wasm"
fi
alias swift='$SWIFT_EXECUTABLE_FOR_WASM_SDK'
local build_command="$SWIFT_BUILD_COMMAND --swift-sdk $sdk_name"
if [[ -n "$SWIFT_BUILD_FLAGS" ]]; then
build_command="$build_command $SWIFT_BUILD_FLAGS"
fi
log "Running: $build_command"
if eval "$build_command"; then
log "✅ Swift build with Swift SDK for Wasm completed successfully"
else
fatal "Swift build with Swift SDK for Wasm failed"
fi
fi
}
main() {
install_sdks
build
}
main "$@"