Skip to content

Commit 812cd40

Browse files
committed
Add post-install script to SDK bundle
1 parent 0d149c4 commit 812cd40

File tree

2 files changed

+60
-35
lines changed

2 files changed

+60
-35
lines changed

swift-ci/sdks/android/build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,5 @@ if [[ ! -d ${PATCHDIR} ]]; then
107107
popd
108108
fi
109109

110-
./scripts/build.sh --products-dir ${SDKROOT}/products --source-dir ${SDKROOT}/src --build-dir ${SDKROOT}/build --ndk-home ${ANDROID_NDK_HOME} --android-api ${ANDROID_API} --host-toolchain ${HOST_TOOLCHAIN} --archs ${TARGET_ARCHS}
110+
./scripts/build.sh --products-dir ${SDKROOT}/products --source-dir ${SDKROOT}/src --build-dir ${SDKROOT}/build --ndk-home ${ANDROID_NDK_HOME} --android-api ${ANDROID_API} --host-toolchain ${HOST_TOOLCHAIN} --archs ${TARGET_ARCHS} ${@}
111111

swift-ci/sdks/android/scripts/build.sh

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
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

431429
cp -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"
444432
mkdir -p ${swift_res_root}
445433

446434
cat > $swift_res_root/SDKSettings.json <<EOF
@@ -469,15 +457,8 @@ for arch in $archs; do
469457
fi
470458

471459
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+
mv lib/swift lib/swift-$arch
461+
ln -s ../swift/clang lib/swift-$arch/clang
481462

482463
mv lib/swift_static lib/swift_static-$arch
483464
mv lib/lib*.a lib/swift_static-$arch/android
@@ -497,18 +478,66 @@ for arch in $archs; do
497478
rsync -a ${sdk_staging}/${arch}/usr ${swift_res_root}
498479
done
499480

500-
if [ "${NDK_LOCATION}" = "external" ]; then
481+
ndk_sysroot="ndk-sysroot"
482+
483+
# whether to include the ndk-sysroot in the SDK bundle
484+
INCLUDE_NDK_SYSROOT=${INCLUDE_NDK_SYSROOT:-0}
485+
if [[ ${INCLUDE_NDK_SYSROOT} != 1 ]]; then
486+
# if we do not include the NDK, then create an install script
487+
#ANDROID_NDK_HOME="/opt/homebrew/share/android-ndk"
488+
mkdir scripts/
489+
cat > scripts/setup-android-sdk.sh <<'EOF'
490+
#/bin/sh
491+
set -e
492+
if [ -z "${ANDROID_NDK_HOME}" ]; then
493+
echo "$(basename $0): error: missing environment variable ANDROID_NDK_HOME"
494+
exit 1
495+
fi
496+
PREBUILT="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt"
497+
if [ ! -d "${PREBUILT}" ]; then
498+
echo "$(basename $0): error: ANDROID_NDK_HOME not found: ${PREBUILT}"
499+
exit 1
500+
fi
501+
DESTINATION=$(dirname $(dirname $(realpath $0)))/ndk-sysroot
502+
# clear out any previous NDK setup
503+
rm -rf ${DESTINATION}
504+
cp -a ${PREBUILT}/*/sysroot ${DESTINATION}
505+
506+
mkdir -p ${DESTINATION}/usr/lib/swift/android
507+
508+
# copy each architecture's swiftrt.o into the sysroot
509+
cp -a ${DESTINATION}/../swift-resources/usr/lib/swift-*/android/* ${DESTINATION}/usr/lib/swift/android/
510+
511+
echo "$(basename $0): success: ndk-sysroot copied to Android SDK"
512+
EOF
513+
chmod +x scripts/setup-android-sdk.sh
514+
else
515+
COPY_NDK_SYSROOT=${COPY_NDK_SYSROOT:-1}
516+
if [[ ${COPY_NDK_SYSROOT} == 1 ]]; then
517+
cp -a ${ndk_installation}/sysroot ${ndk_sysroot}
518+
else
519+
# rather than copying the sysroot, we can instead make links to
520+
# the various sub-folders this won't work for the distribution,
521+
# since the NDK is going to be located in different places
522+
# for different machines
523+
mkdir -p ${ndk_sysroot}/usr/lib
524+
ln -sv $(realpath ${ndk_installation}/sysroot/usr/include) ${ndk_sysroot}/usr/include
525+
for triplePath in ${ndk_installation}/sysroot/usr/lib/*; do
526+
triple=$(basename ${triplePath})
527+
ln -sv $(realpath ${triplePath}) ${ndk_sysroot}/usr/lib/${triple}
528+
ls ${ndk_sysroot}/usr/lib/${triple}/
529+
done
530+
fi
531+
501532
# need to manually copy over swiftrt.o or else:
502533
# error: link command failed with exit code 1 (use -v to see invocation)
503534
# 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'
504535
# see: https://github.com/swiftlang/swift-driver/pull/1822#issuecomment-2762811807
536+
# should be fixed by: https://github.com/swiftlang/swift/pull/79621
505537
for arch in $archs; do
506538
mkdir -p ${ndk_sysroot}/usr/lib/swift/android/${arch}
507539
ln -srv ${swift_res_root}/usr/lib/swift-${arch}/android/${arch}/swiftrt.o ${ndk_sysroot}/usr/lib/swift/android/${arch}/swiftrt.o
508540
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}*
512541
fi
513542

514543
rm -r ${swift_res_root}/usr/share/{doc,man}
@@ -535,11 +564,7 @@ for api in $(eval echo "{$android_api..36}"); do
535564
EOF
536565
fi
537566

538-
if [ "${NDK_LOCATION}" = "external" ]; then
539-
SWIFT_RES_DIR="swift-${arch}"
540-
else
541-
SWIFT_RES_DIR="swift"
542-
fi
567+
SWIFT_RES_DIR="swift-${arch}"
543568
SWIFT_STATIC_RES_DIR="swift_static-${arch}"
544569

545570
cat >> swift-sdk.json <<EOF

0 commit comments

Comments
 (0)