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
456
444
mkdir -p $swift_res_root /usr/lib/swift/clang/lib
457
445
cp -r $host_toolchain /lib/clang/* /include $swift_res_root /usr/lib/swift/clang
458
446
459
-
460
447
for arch in $archs ; do
461
448
quiet_pushd ${sdk_staging} /${arch} /usr
462
449
rm -r bin
@@ -469,15 +456,8 @@ for arch in $archs; do
469
456
fi
470
457
471
458
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
459
+ mv lib/swift lib/swift-$arch
460
+ ln -s ../swift/clang lib/swift-$arch /clang
481
461
482
462
mv lib/swift_static lib/swift_static-$arch
483
463
mv lib/lib* .a lib/swift_static-$arch /android
@@ -497,18 +477,85 @@ for arch in $archs; do
497
477
rsync -a ${sdk_staging} /${arch} /usr ${swift_res_root}
498
478
done
499
479
500
- if [ " ${NDK_LOCATION} " = " external" ]; then
480
+ ndk_sysroot=" ndk-sysroot"
481
+
482
+ # whether to include the ndk-sysroot in the SDK bundle
483
+ INCLUDE_NDK_SYSROOT=${INCLUDE_NDK_SYSROOT:- 0}
484
+ if [[ ${INCLUDE_NDK_SYSROOT} != 1 ]]; then
485
+ # if we do not include the NDK, then create an install script
486
+ # ANDROID_NDK_HOME="/opt/homebrew/share/android-ndk"
487
+ mkdir scripts/
488
+ cat > scripts/setup-android-sdk.sh << 'EOF '
489
+ #/bin/sh
490
+ # this script will setup the ndk-sysroot with links to the
491
+ # local installation indicated by ANDROID_NDK_HOME
492
+ set -e
493
+ if [ -z "${ANDROID_NDK_HOME}" ]; then
494
+ echo "$(basename $0): error: missing environment variable ANDROID_NDK_HOME"
495
+ exit 1
496
+ fi
497
+ PREBUILT="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt"
498
+ if [ ! -d "${PREBUILT}" ]; then
499
+ echo "$(basename $0): error: ANDROID_NDK_HOME not found: ${PREBUILT}"
500
+ exit 1
501
+ fi
502
+ DESTINATION=$(dirname $(dirname $(realpath $0)))/ndk-sysroot
503
+ # clear out any previous NDK setup
504
+ rm -rf ${DESTINATION}
505
+
506
+ # copy vs. link the NDK files
507
+ SWIFT_ANDROID_NDK_LINK=${SWIFT_ANDROID_NDK_LINK:-1}
508
+ if [[ "${SWIFT_ANDROID_NDK_LINK}" != 1 ]]; then
509
+ ANDROID_NDK_DESC="copied"
510
+ cp -a ${PREBUILT}/*/sysroot ${DESTINATION}
511
+ else
512
+ ANDROID_NDK_DESC="linked"
513
+ mkdir -p ${DESTINATION}/usr/lib
514
+ ln -s $(realpath ${PREBUILT}/*/sysroot/usr/include) ${DESTINATION}/usr/include
515
+ for triplePath in ${PREBUILT}/*/sysroot/usr/lib/*; do
516
+ triple=$(basename ${triplePath})
517
+ ln -s $(realpath ${triplePath}) ${DESTINATION}/usr/lib/${triple}
518
+ done
519
+ fi
520
+
521
+ # copy each architecture's swiftrt.o into the sysroot,
522
+ # working around https://github.com/swiftlang/swift/pull/79621
523
+ for swiftrt in ${DESTINATION}/../swift-resources/usr/lib/swift-*/android/*/swiftrt.o; do
524
+ arch=$(basename $(dirname ${swiftrt}))
525
+ mkdir -p ${DESTINATION}/usr/lib/swift/android/${arch}
526
+ cp -a ${swiftrt} ${DESTINATION}/usr/lib/swift/android/${arch}
527
+ done
528
+
529
+ echo "$(basename $0): success: ndk-sysroot ${ANDROID_NDK_DESC} to Android SDK"
530
+ EOF
531
+ chmod +x scripts/setup-android-sdk.sh
532
+ else
533
+ COPY_NDK_SYSROOT=${COPY_NDK_SYSROOT:- 1}
534
+ if [[ ${COPY_NDK_SYSROOT} == 1 ]]; then
535
+ cp -a ${ndk_installation} /sysroot ${ndk_sysroot}
536
+ else
537
+ # rather than copying the sysroot, we can instead make links to
538
+ # the various sub-folders this won't work for the distribution,
539
+ # since the NDK is going to be located in different places
540
+ # for different machines
541
+ mkdir -p ${ndk_sysroot} /usr/lib
542
+ ln -sv $( realpath ${ndk_installation} /sysroot/usr/include) ${ndk_sysroot} /usr/include
543
+ for triplePath in ${ndk_installation} /sysroot/usr/lib/* ; do
544
+ triple=$( basename ${triplePath} )
545
+ ln -sv $( realpath ${triplePath} ) ${ndk_sysroot} /usr/lib/${triple}
546
+ ls ${ndk_sysroot} /usr/lib/${triple} /
547
+ done
548
+ fi
549
+
501
550
# need to manually copy over swiftrt.o or else:
502
551
# error: link command failed with exit code 1 (use -v to see invocation)
503
552
# 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
553
# see: https://github.com/swiftlang/swift-driver/pull/1822#issuecomment-2762811807
554
+ # should be fixed by: https://github.com/swiftlang/swift/pull/79621
505
555
for arch in $archs ; do
506
556
mkdir -p ${ndk_sysroot} /usr/lib/swift/android/${arch}
507
557
ln -srv ${swift_res_root} /usr/lib/swift-${arch} /android/${arch} /swiftrt.o ${ndk_sysroot} /usr/lib/swift/android/${arch} /swiftrt.o
508
558
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
559
fi
513
560
514
561
rm -r ${swift_res_root} /usr/share/{doc,man}
@@ -535,11 +582,7 @@ for api in $(eval echo "{$android_api..36}"); do
535
582
EOF
536
583
fi
537
584
538
- if [ " ${NDK_LOCATION} " = " external" ]; then
539
- SWIFT_RES_DIR=" swift-${arch} "
540
- else
541
- SWIFT_RES_DIR=" swift"
542
- fi
585
+ SWIFT_RES_DIR=" swift-${arch} "
543
586
SWIFT_STATIC_RES_DIR=" swift_static-${arch} "
544
587
545
588
cat >> swift-sdk.json << EOF
0 commit comments