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
@@ -212,14 +216,14 @@ 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
219
+ ndk_installation =$ndk_home /toolchains/llvm/prebuilt/$HOST
216
220
217
221
218
222
echo " Swift found at ${swift_dir} "
219
223
echo " Host toolchain found at ${host_toolchain} "
220
224
${host_toolchain} /bin/swift --version
221
225
echo " Android NDK found at ${ndk_home} "
222
- ${ndk_toolchain } /bin/clang --version
226
+ ${ndk_installation } /bin/clang --version
223
227
echo " Building for ${archs} "
224
228
echo " Sources are in ${source_dir} "
225
229
echo " Build will happen in ${build_dir} "
@@ -387,7 +391,7 @@ for arch in $archs; do
387
391
# need to remove symlink that gets created in the NDK to the previous arch's build
388
392
# or else we get errors like:
389
393
# 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
391
395
quiet_popd
392
396
groupend
393
397
done
@@ -425,12 +429,27 @@ EOF
425
429
mkdir -p $sdk_base
426
430
quiet_pushd $sdk_base
427
431
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}
432
451
433
- cat > $sysroot_path /SDKSettings.json << EOF
452
+ cat > $swift_res_root /SDKSettings.json << EOF
434
453
{
435
454
"DisplayName": "Swift Android SDK",
436
455
"Version": "${android_sdk_version} ",
@@ -439,10 +458,9 @@ cat > $sysroot_path/SDKSettings.json <<EOF
439
458
}
440
459
EOF
441
460
442
- cp -a ${build_dir} /sdk_root ${sdk_staging}
443
461
# 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
446
464
447
465
for arch in $archs ; do
448
466
quiet_pushd ${sdk_staging} /${arch} /usr
@@ -455,26 +473,47 @@ for arch in $archs; do
455
473
arch_triple=" arm-linux-androideabi"
456
474
fi
457
475
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
460
486
461
487
mv lib/swift_static lib/swift_static-$arch
462
488
mv lib/lib* .a lib/swift_static-$arch /android
463
489
464
- rm -r lib/swift{,_static-$arch }/clang
465
-
466
490
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
468
492
ln -s ../swift/clang lib/swift_static-$arch /clang
469
493
quiet_popd
470
494
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 }
473
497
done
474
498
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}
478
517
rm -r ${sdk_staging}
479
518
480
519
cat > swift-sdk.json << EOF
@@ -497,11 +536,18 @@ for api in $(eval echo "{$android_api..36}"); do
497
536
},
498
537
EOF
499
538
fi
539
+
540
+ if [ " ${NDK_LOCATION} " = " external" ]; then
541
+ SWIFT_RES_DIR=" swift-${arch} "
542
+ else
543
+ SWIFT_RES_DIR=" swift"
544
+ fi
545
+
500
546
cat >> swift-sdk.json << EOF
501
547
"${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} ",
505
551
"toolsetPaths": [ "swift-toolset.json" ]
506
552
EOF
507
553
done
0 commit comments