11#! /bin/bash
22# Swift SDK for Android: Build Script
3- set -e
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" }
48
59# Docker sets TERM to xterm if using a pty; we probably want
610# xterm-256color, otherwise we only get eight colors
@@ -212,14 +216,14 @@ header "Swift Android SDK build script"
212216swift_dir=$( realpath $( dirname " $swiftc " ) /..)
213217HOST=linux-x86_64
214218# HOST=$(uname -s -m | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
215- ndk_toolchain =$ndk_home /toolchains/llvm/prebuilt/$HOST
219+ ndk_installation =$ndk_home /toolchains/llvm/prebuilt/$HOST
216220
217221
218222echo " Swift found at ${swift_dir} "
219223echo " Host toolchain found at ${host_toolchain} "
220224${host_toolchain} /bin/swift --version
221225echo " Android NDK found at ${ndk_home} "
222- ${ndk_toolchain } /bin/clang --version
226+ ${ndk_installation } /bin/clang --version
223227echo " Building for ${archs} "
224228echo " Sources are in ${source_dir} "
225229echo " Build will happen in ${build_dir} "
@@ -387,7 +391,7 @@ for arch in $archs; do
387391 # need to remove symlink that gets created in the NDK to the previous arch's build
388392 # or else we get errors like:
389393 # error: could not find module '_Builtin_float' for target 'x86_64-unknown-linux-android'; found: aarch64-unknown-linux-android, at: /home/runner/work/_temp/swift-android-sdk/ndk/android-ndk-r27c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/swift/android/_Builtin_float.swiftmodule
390- rm -f $ndk_toolchain /sysroot/usr/lib/swift
394+ rm -f $ndk_installation /sysroot/usr/lib/swift
391395 quiet_popd
392396 groupend
393397done
@@ -425,12 +429,27 @@ EOF
425429mkdir -p $sdk_base
426430quiet_pushd $sdk_base
427431
428- # sysroot_path="ndk-sysroot"
429- # sysroot_path="android-27c-sysroot"
430- sysroot_path=" sysroot"
431- cp -a ${ndk_toolchain} /sysroot ${sysroot_path}
432+ cp -a ${build_dir} /sdk_root ${sdk_staging}
433+
434+ if [ " ${NDK_LOCATION} " = " external" ]; then
435+ swift_res_root=" swift-resources"
436+ # ndk_sysroot=${ndk_installation}/sysroot
437+ # ndk_sysroot=NONE
438+ ndk_sysroot=" ndk-sysroot"
439+ cp -a ${ndk_installation} /sysroot ${ndk_sysroot}
440+ else
441+ # sysroot_path="ndk-sysroot"
442+ # sysroot_path="android-27c-sysroot"
443+ merged_sysroot_path=" sysroot"
444+
445+ swift_res_root=${merged_sysroot_path}
446+ ndk_sysroot=${merged_sysroot_path}
447+ cp -a ${ndk_installation} /sysroot ${ndk_sysroot}
448+ fi
449+
450+ mkdir -p ${swift_res_root}
432451
433- cat > $sysroot_path /SDKSettings.json << EOF
452+ cat > $swift_res_root /SDKSettings.json << EOF
434453{
435454 "DisplayName": "Swift Android SDK",
436455 "Version": "${android_sdk_version} ",
@@ -439,10 +458,9 @@ cat > $sysroot_path/SDKSettings.json <<EOF
439458}
440459EOF
441460
442- cp -a ${build_dir} /sdk_root ${sdk_staging}
443461# Copy necessary headers and libraries from the toolchain and NDK clang resource directories
444- mkdir -p $sysroot_path /usr/lib/swift/clang/lib
445- cp -r $host_toolchain /lib/clang/* /include $sysroot_path /usr/lib/swift/clang
462+ mkdir -p $swift_res_root /usr/lib/swift/clang/lib
463+ cp -r $host_toolchain /lib/clang/* /include $swift_res_root /usr/lib/swift/clang
446464
447465for arch in $archs ; do
448466 quiet_pushd ${sdk_staging} /${arch} /usr
@@ -455,26 +473,47 @@ for arch in $archs; do
455473 arch_triple=" arm-linux-androideabi"
456474 fi
457475
458- mkdir lib/${arch_triple}
459- mv lib/pkgconfig lib/swift/android/lib* .{a,so} lib/${arch_triple}
476+ rm -r lib/swift{,_static}/clang
477+ if [ " ${NDK_LOCATION} " = " external" ]; then
478+ # mkdir lib/swift-$arch
479+ # mv lib/pkgconfig lib/swift/android/lib*.{a,so} lib/swift-$arch
480+ mv lib/swift lib/swift-$arch
481+ ln -s ../swift/clang lib/swift-$arch /clang
482+ else
483+ mkdir lib/${arch_triple}
484+ mv lib/pkgconfig lib/swift/android/lib* .{a,so} lib/${arch_triple}
485+ fi
460486
461487 mv lib/swift_static lib/swift_static-$arch
462488 mv lib/lib* .a lib/swift_static-$arch /android
463489
464- rm -r lib/swift{,_static-$arch }/clang
465-
466490 mkdir -p lib/swift/clang/lib
467- cp -a ${ndk_toolchain } /lib/clang/* /lib/linux lib/swift/clang/lib
491+ cp -a ${ndk_installation } /lib/clang/* /lib/linux lib/swift/clang/lib
468492 ln -s ../swift/clang lib/swift_static-$arch /clang
469493 quiet_popd
470494
471- # now sync the massaged sdk_root into the sysroot_path
472- rsync -a ${sdk_staging} /${arch} /usr ${sysroot_path }
495+ # now sync the massaged sdk_root into the swift_res_root
496+ rsync -a ${sdk_staging} /${arch} /usr ${swift_res_root }
473497done
474498
475- rm -r ${sysroot_path} /usr/share/{doc,man}
476- rm -r ${sysroot_path} /usr/{include,lib}/{i686,riscv64}-linux-android
477- rm -r ${sysroot_path} /usr/lib/swift/clang/lib/linux/* {i[36]86,riscv64}*
499+ if [ " ${NDK_LOCATION} " = " external" ]; then
500+ # need to manually copy over swiftrt.o or else:
501+ # error: link command failed with exit code 1 (use -v to see invocation)
502+ # 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'
503+ SWIFTRT=${swift_res_root} /usr/lib/swift-x86_64/android/x86_64/swiftrt.o
504+
505+ # see: https://github.com/swiftlang/swift-driver/pull/1822#issuecomment-2762811807
506+ mkdir -p ${ndk_sysroot} /usr/lib/swift/android/x86_64
507+ cp -v ${SWIFTRT} ${ndk_sysroot} /usr/lib/swift/android/x86_64/swiftrt.o
508+
509+ mkdir -p ${ndk_sysroot} /usr/lib/swift_static/android/x86_64
510+ cp -v ${SWIFTRT} ${ndk_sysroot} /usr/lib/swift_static/android/x86_64/swiftrt.o
511+ else
512+ rm -r ${swift_res_root} /usr/{include,lib}/{i686,riscv64}-linux-android
513+ rm -r ${swift_res_root} /usr/lib/swift/clang/lib/linux/* {i[36]86,riscv64}*
514+ fi
515+
516+ rm -r ${swift_res_root} /usr/share/{doc,man}
478517rm -r ${sdk_staging}
479518
480519cat > swift-sdk.json << EOF
@@ -497,11 +536,18 @@ for api in $(eval echo "{$android_api..36}"); do
497536 },
498537EOF
499538 fi
539+
540+ if [ " ${NDK_LOCATION} " = " external" ]; then
541+ SWIFT_RES_DIR=" swift-${arch} "
542+ else
543+ SWIFT_RES_DIR=" swift"
544+ fi
545+
500546 cat >> swift-sdk.json << EOF
501547 "${arch} -unknown-linux-android${api} ": {
502- "sdkRootPath": "${sysroot_path } ",
503- "swiftResourcesPath": "${sysroot_path } /usr/lib/swift ",
504- "swiftStaticResourcesPath": "${sysroot_path } /usr/lib/swift_static-${arch} ",
548+ "sdkRootPath": "${ndk_sysroot } ",
549+ "swiftResourcesPath": "${swift_res_root } /usr/lib/${SWIFT_RES_DIR} ",
550+ "swiftStaticResourcesPath": "${swift_res_root } /usr/lib/swift_static-${arch} ",
505551 "toolsetPaths": [ "swift-toolset.json" ]
506552EOF
507553 done
0 commit comments