1
1
#! /bin/bash
2
2
# 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" }
4
8
5
9
# Docker sets TERM to xterm if using a pty; we probably want
6
10
# xterm-256color, otherwise we only get eight colors
@@ -188,7 +192,7 @@ if [[ $swift_version == swift-* ]]; then
188
192
fi
189
193
190
194
if [[ -z " $sdk_name " ]]; then
191
- sdk_name=swift-${swift_version} _android -${android_sdk_version}
195
+ sdk_name=swift-${swift_version} -android -${android_sdk_version}
192
196
fi
193
197
194
198
libxml2_version=$( versionFromTag ${source_dir} /libxml2)
@@ -212,14 +216,13 @@ header "Swift Android SDK build script"
212
216
swift_dir=$( realpath $( dirname " $swiftc " ) /..)
213
217
HOST=linux-x86_64
214
218
# 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
217
220
218
221
echo " Swift found at ${swift_dir} "
219
222
echo " Host toolchain found at ${host_toolchain} "
220
223
${host_toolchain} /bin/swift --version
221
224
echo " Android NDK found at ${ndk_home} "
222
- ${ndk_toolchain } /bin/clang --version
225
+ ${ndk_installation } /bin/clang --version
223
226
echo " Building for ${archs} "
224
227
echo " Sources are in ${source_dir} "
225
228
echo " Build will happen in ${build_dir} "
@@ -387,7 +390,7 @@ for arch in $archs; do
387
390
# need to remove symlink that gets created in the NDK to the previous arch's build
388
391
# or else we get errors like:
389
392
# 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
391
394
quiet_popd
392
395
groupend
393
396
done
@@ -425,12 +428,22 @@ EOF
425
428
mkdir -p $sdk_base
426
429
quiet_pushd $sdk_base
427
430
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
432
443
433
- cat > $sysroot_path /SDKSettings.json << EOF
444
+ mkdir -p ${swift_res_root}
445
+
446
+ cat > $swift_res_root /SDKSettings.json << EOF
434
447
{
435
448
"DisplayName": "Swift Android SDK",
436
449
"Version": "${android_sdk_version} ",
@@ -439,10 +452,10 @@ cat > $sysroot_path/SDKSettings.json <<EOF
439
452
}
440
453
EOF
441
454
442
- cp -a ${build_dir} /sdk_root ${sdk_staging}
443
455
# 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
+
446
459
447
460
for arch in $archs ; do
448
461
quiet_pushd ${sdk_staging} /${arch} /usr
@@ -455,26 +468,50 @@ for arch in $archs; do
455
468
arch_triple=" arm-linux-androideabi"
456
469
fi
457
470
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
460
481
461
482
mv lib/swift_static lib/swift_static-$arch
462
483
mv lib/lib* .a lib/swift_static-$arch /android
463
484
464
- rm -r lib /swift{,_static -$arch } /clang
485
+ ln -sv .. /swift/clang lib/swift_static -$arch /clang
465
486
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} /
469
494
quiet_popd
470
495
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 }
473
498
done
474
499
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}
478
515
rm -r ${sdk_staging}
479
516
480
517
cat > swift-sdk.json << EOF
@@ -497,13 +534,23 @@ for api in $(eval echo "{$android_api..36}"); do
497
534
},
498
535
EOF
499
536
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
+
500
545
cat >> swift-sdk.json << EOF
501
546
"${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 } ",
505
550
"toolsetPaths": [ "swift-toolset.json" ]
506
551
EOF
552
+ # "librarySearchPaths": [ "${swift_res_root}/usr/lib/swift-x86_64/android/x86_64" ],
553
+ # "includeSearchPaths": [ "${ndk_sysroot}/usr/include" ],
507
554
done
508
555
done
509
556
0 commit comments