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
@@ -188,7 +192,7 @@ if [[ $swift_version == swift-* ]]; then
188192fi
189193
190194if [[ -z " $sdk_name " ]]; then
191- sdk_name=swift-${swift_version} _android -${android_sdk_version}
195+ sdk_name=swift-${swift_version} -android -${android_sdk_version}
192196fi
193197
194198libxml2_version=$( versionFromTag ${source_dir} /libxml2)
@@ -212,14 +216,13 @@ 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
216-
219+ ndk_installation=$ndk_home /toolchains/llvm/prebuilt/$HOST
217220
218221echo " Swift found at ${swift_dir} "
219222echo " Host toolchain found at ${host_toolchain} "
220223${host_toolchain} /bin/swift --version
221224echo " Android NDK found at ${ndk_home} "
222- ${ndk_toolchain } /bin/clang --version
225+ ${ndk_installation } /bin/clang --version
223226echo " Building for ${archs} "
224227echo " Sources are in ${source_dir} "
225228echo " Build will happen in ${build_dir} "
@@ -387,7 +390,7 @@ for arch in $archs; do
387390 # need to remove symlink that gets created in the NDK to the previous arch's build
388391 # or else we get errors like:
389392 # 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
393+ rm -f $ndk_installation /sysroot/usr/lib/swift
391394 quiet_popd
392395 groupend
393396done
@@ -425,12 +428,22 @@ EOF
425428mkdir -p $sdk_base
426429quiet_pushd $sdk_base
427430
428- # sysroot_path="ndk-sysroot"
429- # sysroot_path="android-27c-sysroot"
430- sysroot_path=" sysroot"
431- cp -a ${ndk_toolchain} /sysroot ${sysroot_path}
431+ cp -a ${build_dir} /sdk_root ${sdk_staging}
432+
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
432443
433- cat > $sysroot_path /SDKSettings.json << EOF
444+ mkdir -p ${swift_res_root}
445+
446+ cat > $swift_res_root /SDKSettings.json << EOF
434447{
435448 "DisplayName": "Swift Android SDK",
436449 "Version": "${android_sdk_version} ",
@@ -439,10 +452,10 @@ cat > $sysroot_path/SDKSettings.json <<EOF
439452}
440453EOF
441454
442- cp -a ${build_dir} /sdk_root ${sdk_staging}
443455# 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
456+ mkdir -p $swift_res_root /usr/lib/swift/clang/lib
457+ cp -r $host_toolchain /lib/clang/* /include $swift_res_root /usr/lib/swift/clang
458+
446459
447460for arch in $archs ; do
448461 quiet_pushd ${sdk_staging} /${arch} /usr
@@ -455,26 +468,50 @@ for arch in $archs; do
455468 arch_triple=" arm-linux-androideabi"
456469 fi
457470
458- mkdir lib/${arch_triple}
459- mv lib/pkgconfig lib/swift/android/lib* .{a,so} lib/${arch_triple}
471+ 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
460481
461482 mv lib/swift_static lib/swift_static-$arch
462483 mv lib/lib* .a lib/swift_static-$arch /android
463484
464- rm -r lib /swift{,_static -$arch } /clang
485+ ln -sv .. /swift/clang lib/swift_static -$arch /clang
465486
466- mkdir -p lib/swift/clang/lib
467- cp -a ${ndk_toolchain} /lib/clang/* /lib/linux lib/swift/clang/lib
468- ln -s ../swift/clang lib/swift_static-$arch /clang
487+ # copy the clang libraries that we need to build for each architecture
488+ aarch=${arch/ armv7/ arm}
489+ mkdir -p lib/swift/clang/lib/linux/${aarch}
490+
491+ # match clang version 21, 22, etc.
492+ cp -av ${ndk_installation} /lib/clang/[0-9]* /lib/linux/libclang_rt.builtins-${aarch} -android.a lib/swift/clang/lib/linux/
493+ cp -av ${ndk_installation} /lib/clang/[0-9]* /lib/linux/${aarch} /libunwind.a lib/swift/clang/lib/linux/${aarch} /
469494 quiet_popd
470495
471- # now sync the massaged sdk_root into the sysroot_path
472- rsync -a ${sdk_staging} /${arch} /usr ${sysroot_path }
496+ # now sync the massaged sdk_root into the swift_res_root
497+ rsync -a ${sdk_staging} /${arch} /usr ${swift_res_root }
473498done
474499
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}*
500+ if [ " ${NDK_LOCATION} " = " external" ]; then
501+ # need to manually copy over swiftrt.o or else:
502+ # error: link command failed with exit code 1 (use -v to see invocation)
503+ # 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+ # see: https://github.com/swiftlang/swift-driver/pull/1822#issuecomment-2762811807
505+ for arch in $archs ; do
506+ mkdir -p ${ndk_sysroot} /usr/lib/swift/android/${arch}
507+ ln -srv ${swift_res_root} /usr/lib/swift-${arch} /android/${arch} /swiftrt.o ${ndk_sysroot} /usr/lib/swift/android/${arch} /swiftrt.o
508+ 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+ fi
513+
514+ rm -r ${swift_res_root} /usr/share/{doc,man}
478515rm -r ${sdk_staging}
479516
480517cat > swift-sdk.json << EOF
@@ -497,13 +534,23 @@ for api in $(eval echo "{$android_api..36}"); do
497534 },
498535EOF
499536 fi
537+
538+ if [ " ${NDK_LOCATION} " = " external" ]; then
539+ SWIFT_RES_DIR=" swift-${arch} "
540+ else
541+ SWIFT_RES_DIR=" swift"
542+ fi
543+ SWIFT_STATIC_RES_DIR=" swift_static-${arch} "
544+
500545 cat >> swift-sdk.json << EOF
501546 "${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 } ",
547+ "sdkRootPath": "${ndk_sysroot } ",
548+ "swiftResourcesPath": "${swift_res_root } /usr/lib/${SWIFT_RES_DIR} ",
549+ "swiftStaticResourcesPath": "${swift_res_root } /usr/lib/${SWIFT_STATIC_RES_DIR } ",
505550 "toolsetPaths": [ "swift-toolset.json" ]
506551EOF
552+ # "librarySearchPaths": [ "${swift_res_root}/usr/lib/swift-x86_64/android/x86_64" ],
553+ # "includeSearchPaths": [ "${ndk_sysroot}/usr/include" ],
507554 done
508555done
509556
0 commit comments