11#! /bin/bash
22# 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
84
95# Docker sets TERM to xterm if using a pty; we probably want
106# xterm-256color, otherwise we only get eight colors
@@ -145,6 +141,8 @@ while [ "$#" -gt 0 ]; do
145141 sdk_name=" $2 " ; shift ;;
146142 --archs)
147143 archs=" $2 " ; shift ;;
144+ --build)
145+ build_type=" $2 " ; shift ;;
148146 --version)
149147 android_sdk_version=" $2 " ; shift ;;
150148 -j|--jobs)
@@ -430,17 +428,7 @@ quiet_pushd $sdk_base
430428
431429cp -a ${build_dir} /sdk_root ${sdk_staging}
432430
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"
444432mkdir -p ${swift_res_root}
445433
446434cat > $swift_res_root /SDKSettings.json << EOF
456444mkdir -p $swift_res_root /usr/lib/swift/clang/lib
457445cp -r $host_toolchain /lib/clang/* /include $swift_res_root /usr/lib/swift/clang
458446
459-
460447for arch in $archs ; do
461448 quiet_pushd ${sdk_staging} /${arch} /usr
462449 rm -r bin
@@ -469,15 +456,8 @@ for arch in $archs; do
469456 fi
470457
471458 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
481461
482462 mv lib/swift_static lib/swift_static-$arch
483463 mv lib/lib* .a lib/swift_static-$arch /android
@@ -497,18 +477,85 @@ for arch in $archs; do
497477 rsync -a ${sdk_staging} /${arch} /usr ${swift_res_root}
498478done
499479
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+
501550 # need to manually copy over swiftrt.o or else:
502551 # error: link command failed with exit code 1 (use -v to see invocation)
503552 # 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'
504553 # see: https://github.com/swiftlang/swift-driver/pull/1822#issuecomment-2762811807
554+ # should be fixed by: https://github.com/swiftlang/swift/pull/79621
505555 for arch in $archs ; do
506556 mkdir -p ${ndk_sysroot} /usr/lib/swift/android/${arch}
507557 ln -srv ${swift_res_root} /usr/lib/swift-${arch} /android/${arch} /swiftrt.o ${ndk_sysroot} /usr/lib/swift/android/${arch} /swiftrt.o
508558 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}*
512559fi
513560
514561rm -r ${swift_res_root} /usr/share/{doc,man}
@@ -535,11 +582,7 @@ for api in $(eval echo "{$android_api..36}"); do
535582EOF
536583 fi
537584
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} "
543586 SWIFT_STATIC_RES_DIR=" swift_static-${arch} "
544587
545588 cat >> swift-sdk.json << EOF
0 commit comments