1
1
#! /bin/bash
2
2
# Swift SDK for Android: Build Script
3
- set -ex
4
-
5
- # temporary for splitting out NDK installation from the rest of the SDK
6
- # NDK_LOCATION=${NDK_LOCATION:-"merged"}
7
- NDK_LOCATION=${NDK_LOCATION:- " external" }
3
+ set -e
8
4
9
5
# Docker sets TERM to xterm if using a pty; we probably want
10
6
# xterm-256color, otherwise we only get eight colors
@@ -145,6 +141,8 @@ while [ "$#" -gt 0 ]; do
145
141
sdk_name=" $2 " ; shift ;;
146
142
--archs)
147
143
archs=" $2 " ; shift ;;
144
+ --build)
145
+ build_type=" $2 " ; shift ;;
148
146
--version)
149
147
android_sdk_version=" $2 " ; shift ;;
150
148
-j|--jobs)
@@ -430,17 +428,7 @@ quiet_pushd $sdk_base
430
428
431
429
cp -a ${build_dir} /sdk_root ${sdk_staging}
432
430
433
- if [ " ${NDK_LOCATION} " = " external" ]; then
434
- swift_res_root=" swift-resources"
435
- ndk_sysroot=" ndk-sysroot"
436
- cp -a ${ndk_installation} /sysroot ${ndk_sysroot}
437
- else
438
- merged_sysroot_path=" sysroot"
439
- swift_res_root=${merged_sysroot_path}
440
- ndk_sysroot=${merged_sysroot_path}
441
- cp -a ${ndk_installation} /sysroot ${ndk_sysroot}
442
- fi
443
-
431
+ swift_res_root=" swift-resources"
444
432
mkdir -p ${swift_res_root}
445
433
446
434
cat > $swift_res_root /SDKSettings.json << EOF
@@ -469,15 +457,8 @@ for arch in $archs; do
469
457
fi
470
458
471
459
rm -r lib/swift{,_static}/clang
472
- if [ " ${NDK_LOCATION} " = " external" ]; then
473
- # mkdir lib/swift-$arch
474
- # mv lib/pkgconfig lib/swift/android/lib*.{a,so} lib/swift-$arch
475
- mv lib/swift lib/swift-$arch
476
- ln -s ../swift/clang lib/swift-$arch /clang
477
- else
478
- mkdir lib/${arch_triple}
479
- mv lib/pkgconfig lib/swift/android/lib* .{a,so} lib/${arch_triple}
480
- fi
460
+ mv lib/swift lib/swift-$arch
461
+ ln -s ../swift/clang lib/swift-$arch /clang
481
462
482
463
mv lib/swift_static lib/swift_static-$arch
483
464
mv lib/lib* .a lib/swift_static-$arch /android
@@ -497,18 +478,66 @@ for arch in $archs; do
497
478
rsync -a ${sdk_staging} /${arch} /usr ${swift_res_root}
498
479
done
499
480
500
- if [ " ${NDK_LOCATION} " = " external" ]; then
481
+ ndk_sysroot=" ndk-sysroot"
482
+
483
+ # whether to include the ndk-sysroot in the SDK bundle
484
+ INCLUDE_NDK_SYSROOT=${INCLUDE_NDK_SYSROOT:- 0}
485
+ if [[ ${INCLUDE_NDK_SYSROOT} != 1 ]]; then
486
+ # if we do not include the NDK, then create an install script
487
+ # ANDROID_NDK_HOME="/opt/homebrew/share/android-ndk"
488
+ mkdir scripts/
489
+ cat > scripts/setup-android-sdk.sh << 'EOF '
490
+ #/bin/sh
491
+ set -e
492
+ if [ -z "${ANDROID_NDK_HOME}" ]; then
493
+ echo "$(basename $0): error: missing environment variable ANDROID_NDK_HOME"
494
+ exit 1
495
+ fi
496
+ PREBUILT="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt"
497
+ if [ ! -d "${PREBUILT}" ]; then
498
+ echo "$(basename $0): error: ANDROID_NDK_HOME not found: ${PREBUILT}"
499
+ exit 1
500
+ fi
501
+ DESTINATION=$(dirname $(dirname $(realpath $0)))/ndk-sysroot
502
+ # clear out any previous NDK setup
503
+ rm -rf ${DESTINATION}
504
+ cp -a ${PREBUILT}/*/sysroot ${DESTINATION}
505
+
506
+ mkdir -p ${DESTINATION}/usr/lib/swift/android
507
+
508
+ # copy each architecture's swiftrt.o into the sysroot
509
+ cp -a ${DESTINATION}/../swift-resources/usr/lib/swift-*/android/* ${DESTINATION}/usr/lib/swift/android/
510
+
511
+ echo "$(basename $0): success: ndk-sysroot copied to Android SDK"
512
+ EOF
513
+ chmod +x scripts/setup-android-sdk.sh
514
+ else
515
+ COPY_NDK_SYSROOT=${COPY_NDK_SYSROOT:- 1}
516
+ if [[ ${COPY_NDK_SYSROOT} == 1 ]]; then
517
+ cp -a ${ndk_installation} /sysroot ${ndk_sysroot}
518
+ else
519
+ # rather than copying the sysroot, we can instead make links to
520
+ # the various sub-folders this won't work for the distribution,
521
+ # since the NDK is going to be located in different places
522
+ # for different machines
523
+ mkdir -p ${ndk_sysroot} /usr/lib
524
+ ln -sv $( realpath ${ndk_installation} /sysroot/usr/include) ${ndk_sysroot} /usr/include
525
+ for triplePath in ${ndk_installation} /sysroot/usr/lib/* ; do
526
+ triple=$( basename ${triplePath} )
527
+ ln -sv $( realpath ${triplePath} ) ${ndk_sysroot} /usr/lib/${triple}
528
+ ls ${ndk_sysroot} /usr/lib/${triple} /
529
+ done
530
+ fi
531
+
501
532
# need to manually copy over swiftrt.o or else:
502
533
# error: link command failed with exit code 1 (use -v to see invocation)
503
534
# clang: error: no such file or directory: '${HOME}/.swiftpm/swift-sdks/swift-6.2-DEVELOPMENT-SNAPSHOT-2025-04-24-a-android-0.1.artifactbundle/swift-android/ndk-sysroot/usr/lib/swift/android/x86_64/swiftrt.o'
504
535
# see: https://github.com/swiftlang/swift-driver/pull/1822#issuecomment-2762811807
536
+ # should be fixed by: https://github.com/swiftlang/swift/pull/79621
505
537
for arch in $archs ; do
506
538
mkdir -p ${ndk_sysroot} /usr/lib/swift/android/${arch}
507
539
ln -srv ${swift_res_root} /usr/lib/swift-${arch} /android/${arch} /swiftrt.o ${ndk_sysroot} /usr/lib/swift/android/${arch} /swiftrt.o
508
540
done
509
- else
510
- rm -r ${swift_res_root} /usr/{include,lib}/{i686,riscv64}-linux-android
511
- rm -r ${swift_res_root} /usr/lib/swift/clang/lib/linux/* {i[36]86,riscv64}*
512
541
fi
513
542
514
543
rm -r ${swift_res_root} /usr/share/{doc,man}
@@ -535,11 +564,7 @@ for api in $(eval echo "{$android_api..36}"); do
535
564
EOF
536
565
fi
537
566
538
- if [ " ${NDK_LOCATION} " = " external" ]; then
539
- SWIFT_RES_DIR=" swift-${arch} "
540
- else
541
- SWIFT_RES_DIR=" swift"
542
- fi
567
+ SWIFT_RES_DIR=" swift-${arch} "
543
568
SWIFT_STATIC_RES_DIR=" swift_static-${arch} "
544
569
545
570
cat >> swift-sdk.json << EOF
0 commit comments