diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 582b2b9b..13c57b39 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -8,8 +8,6 @@ env: jobs: build: name: Build Docker images - # disabled for CI testing - if: false runs-on: ubuntu-latest steps: - name: Checkout repository @@ -26,8 +24,6 @@ jobs: static-linux-build: name: Build Static Linux image - # disabled for CI testing - if: false runs-on: ubuntu-latest steps: - name: Checkout repository @@ -37,16 +33,18 @@ jobs: run: ./build android-build: - name: Build Android ${{ matrix.swift-version }} ${{ matrix.arch }} SDK + name: Build Android ${{ matrix.build-type }} ${{ matrix.swift-version }} ${{ matrix.arch }} SDK strategy: fail-fast: false matrix: + build-type: ['docker'] + #build-type: ['docker', 'local'] # blank arch builds all (aarch64,x86_64,armv7) - #arch: [''] + arch: [''] # builds only x86_64 to speed up the validation #arch: ['x86_64'] # build both the quick (x86_64) and complete (aarch64,x86_64,armv7) SDKs - arch: ['x86_64', ''] + #arch: ['x86_64', ''] swift-version: ['release', 'devel', 'trunk'] runs-on: ubuntu-24.04 steps: @@ -59,31 +57,60 @@ jobs: sudo docker image prune --all --force sudo docker builder prune -a df -h + - name: Setup + id: config + run: | + # these variabes are used by build-docker and build-local + # to determine which Swift version to build for + echo "BUILD_VERSION=${{ matrix.swift-version }}" >> $GITHUB_ENV + echo "TARGET_ARCHS=${{ matrix.arch }}" >> $GITHUB_ENV + echo "WORKDIR=${{ runner.temp }}/swift-android-sdk" >> $GITHUB_ENV - name: Checkout repository uses: actions/checkout@v4 - - name: Install Dependencies + with: + submodules: 'true' + - name: Build Android SDK (Local) + if: ${{ matrix.build-type == 'local' }} + working-directory: swift-ci/sdks/android run: | sudo apt install -q ninja-build patchelf - - name: Build Android SDK + ./build-local ${BUILD_VERSION} ${WORKDIR} + - name: Build Android SDK (Docker) + if: ${{ matrix.build-type == 'docker' }} + working-directory: swift-ci/sdks/android + run: | + ./build-docker ${BUILD_VERSION} ${WORKDIR} + - name: Install Host Toolchain + if: ${{ matrix.build-type == 'docker' }} working-directory: swift-ci/sdks/android run: | - BUILD_VERSION=${{ matrix.swift-version }} TARGET_ARCHS=${{ matrix.arch }} ./build + # when building in a Docker container, we don't have a local host toolchain, + # but we need one in order to run the SDK validation tests, so we install it now + HOST_OS=ubuntu$(lsb_release -sr) + source ./scripts/toolchain-vars.sh + mkdir -p ${WORKDIR}/host-toolchain + ./scripts/install-swift.sh ${WORKDIR}/host-toolchain/$SWIFT_BASE/usr + ls ${WORKDIR}/host-toolchain + ${WORKDIR}/host-toolchain/*/usr/bin/swift --version - name: Get artifact info id: info shell: bash run: | set -ex - SWIFT_ROOT=$(dirname ${{ runner.temp }}/swift-android-sdk/host-toolchain/*/usr) + SWIFT_ROOT=$(dirname ${WORKDIR}/host-toolchain/*/usr) echo "swift-root=${SWIFT_ROOT}" >> $GITHUB_OUTPUT echo "swift-path=${SWIFT_ROOT}/usr/bin/swift" >> $GITHUB_OUTPUT - ARTIFACT_BUILD=$(realpath ${{ runner.temp }}/swift-android-sdk/build/*.artifactbundle) - ARTIFACT_PATH=$(realpath ${{ runner.temp }}/swift-android-sdk/products/*.artifactbundle.tar.gz) + ARTIFACT_PATH=$(realpath ${WORKDIR}/products/*.artifactbundle.tar.gz) echo "artifact-path=${ARTIFACT_PATH}" >> $GITHUB_OUTPUT echo "sdk-id=x86_64-unknown-linux-android28" >> $GITHUB_OUTPUT ARTIFACT_EXT=".artifactbundle.tar.gz" ARTIFACT_NAME="$(basename ${ARTIFACT_PATH} ${ARTIFACT_EXT})" + # depending on whether we are building locally or in a container, add a maker to the name + if [[ "${{ matrix.build-type }}" == 'local' ]]; then + ARTIFACT_NAME="${ARTIFACT_NAME}-local" + fi # artifacts need a unique name so we suffix with the matrix arch(s) if [[ ! -z "${{ matrix.arch }}" ]]; then ARTIFACT_NAME="${ARTIFACT_NAME}-$(echo ${{ matrix.arch }} | tr ',' '-')" @@ -95,9 +122,6 @@ jobs: # so the actual artifact download will look like: # swift-6.1-RELEASE_android-0.1-x86_64.artifactbundle.tar.gz.zip echo "artifact-name=${ARTIFACT_NAME}" >> $GITHUB_OUTPUT - - # show an abridged tree - tree ${ARTIFACT_BUILD} --filesfirst --prune -P 'Android.swiftmodule' -P 'libswiftAndroid.*' -P 'libFoundation.*' -P 'swiftrt.o' -P 'swift*.json' -P 'info.json' -P 'api-level.h' -P 'android.modulemap' -P 'SwiftAndroidNDK.h' -P 'bridging.modulemap' -P 'linux' -P 'libclang*.a' -P 'libunwind.a' -P 'libclang_rt.builtins-*-android.a' - name: Upload SDK artifactbundle uses: actions/upload-artifact@v4 with: @@ -108,7 +132,9 @@ jobs: run: | # need to free up some space or else when installing we get: No space left on device df -h - rm -rf ${{ runner.temp }}/swift-android-sdk/{build,src} + rm -rf ${WORKDIR}/{build,source} + sudo docker image prune --all --force + sudo docker builder prune -a df -h - name: Install artifactbundle shell: bash @@ -117,7 +143,7 @@ jobs: ${{ steps.info.outputs.swift-path }} sdk install ${{ steps.info.outputs.artifact-path }} ${{ steps.info.outputs.swift-path }} sdk configure --show-configuration $(${{ steps.info.outputs.swift-path }} sdk list | head -n 1) ${{ steps.info.outputs.sdk-id }} # recent releases require that ANDROID_NDK_ROOT *not* be set - # see https://github.com/finagolfin/swift-android-sdk/issues/207 + # see https://github.com/swiftlang/swift-driver/pull/1879 echo "ANDROID_NDK_ROOT=" >> $GITHUB_ENV - name: Create Demo Project diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..f3724176 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "swift-ci/sdks/android/resources/patches"] + path = swift-ci/sdks/android/resources/patches + url = https://github.com/swift-android-sdk/swift-android-sdk diff --git a/swift-ci/sdks/android/Dockerfile b/swift-ci/sdks/android/Dockerfile new file mode 100644 index 00000000..2be3fc37 --- /dev/null +++ b/swift-ci/sdks/android/Dockerfile @@ -0,0 +1,89 @@ +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Docker-based build +# +# ===----------------------------------------------------------------------=== + +FROM ubuntu:24.04 + +# Architecture to build on (empty means x86-64) +ARG OS_ARCH_SUFFIX= + +# the Swift toolchain URL to download +ARG SWIFT_TOOLCHAIN_URL= + +# ............................................................................ + +# Install development tools +RUN apt-get -q update \ + && DEBIAN_FRONTEND=noninteractive apt-get -q install -y \ + build-essential \ + cmake \ + ninja-build \ + python3 \ + golang \ + git \ + gnupg2 \ + libsqlite3-dev \ + libcurl4-openssl-dev \ + libedit-dev \ + libicu-dev \ + libncurses5-dev \ + libpython3-dev \ + libsqlite3-dev \ + libxml2-dev \ + rsync \ + uuid-dev \ + uuid-runtime \ + tzdata \ + curl \ + unzip \ + && rm -rf /var/lib/apt-lists/* + +# Install Swift +ARG SWIFT_SIGNING_KEY=E813C892820A6FA13755B268F167DF1ACF9CE069 +ARG SWIFT_PLATFORM=ubuntu +ARG OS_MAJOR_VER=24 +ARG OS_MINOR_VER=04 + +ENV SWIFT_SIGNING_KEY=$SWIFT_SIGNING_KEY \ + SWIFT_PLATFORM=$SWIFT_PLATFORM \ + OS_MAJOR_VER=$OS_MAJOR_VER \ + OS_MINOR_VER=$OS_MINOR_VER \ + OS_VER=$SWIFT_PLATFORM$OS_MAJOR_VER.$OS_MINOR_VER + +COPY scripts/install-swift.sh /scripts/install-swift.sh +RUN chmod ugo+x /scripts/install-swift.sh +RUN /scripts/install-swift.sh /usr/local/swift +ENV PATH="/usr/local/swift/bin:${PATH}" + +ARG ANDROID_NDK_VERSION= + +ENV ANDROID_NDK_VERSION=$ANDROID_NDK_VERSION + +COPY scripts/install-ndk.sh /scripts/install-ndk.sh +RUN chmod ugo+x /scripts/install-ndk.sh +RUN /scripts/install-ndk.sh +ENV ANDROID_NDK_HOME="/usr/local/ndk/${ANDROID_NDK_VERSION}" + +ENV SWIFT_VERSION=$SWIFT_VERSION \ + LIBXML2_VERSION=$LIBXML2_VERSION \ + CURL_VERSION=$CURL_VERSION \ + BORINGSSL_VERSION=$BORINGSSL_VERSION \ + ICU_VERSION=$ICU_VERSION \ + ZLIB_VERSION=$ZLIB_VERSION + +ENV SWIFT_BUILD_DOCKER="1" + +COPY scripts /scripts +RUN chmod ugo+x /scripts/* + +COPY resources /resources + +# Create a user +RUN groupadd -g 998 build-user && \ + useradd -m -r -u 998 -g build-user build-user + +USER build-user + +WORKDIR /home/build-user diff --git a/swift-ci/sdks/android/README.md b/swift-ci/sdks/android/README.md index 531aa88a..10d4db3e 100644 --- a/swift-ci/sdks/android/README.md +++ b/swift-ci/sdks/android/README.md @@ -1,11 +1,39 @@ -# Build scripts for Swift Android SDK +# Dockerfile-based build for Swift Android SDK -This folder contains scripts to build a Swift Android SDK -in the form of an artifactbundle. +This is a Dockerfile-based build set-up for the Swift Android SDK. + +The top-level `./build-docker` script will create a +Docker container and install a host toolchain and the +Android NDK, and then invoke `scripts/fetch-source.sh` which will +fetch tagged sources for libxml2, curl, boringssl, and swift. + +It can be run with: + +``` +$ ./build-docker +``` + +for example: + +``` +$ ./build-docker release /tmp/android-sdk +``` + +This will create an Ubuntu 24.04 container with the necessary dependencies +to build the Android SDK, including a Swift host toolchain and the +Android NDK that will be used for cross-compilation. + +The `version` argument can be one of the following values: + +| version | Swift version | +| --- | --- | +| `release` | swift-6.1-RELEASE | +| `devel` | swift-6.2-DEVELOPMENT-SNAPSHOT-yyyy-mm-dd | +| `trunk` | swift-DEVELOPMENT-SNAPSHOT-yyyy-mm-dd | ## Running -The top-level `./build` script installs a host toolchain and the +The top-level `./build-docker` script installs a host toolchain and the Android NDK, and then invokes `scripts/fetch-source.sh` which will fetch tagged sources for libxml2, curl, boringssl, and swift. @@ -27,7 +55,7 @@ whereas building for all the architectures takes over an hour. To build an artifactbundle for just the `x86_64` architecture, run: ``` -TARGET_ARCHS=x86_64 ./build +TARGET_ARCHS=aarch64 ./build-docker release /tmp/android-sdk ``` ## Installing and validating the SDK @@ -38,6 +66,5 @@ will create and upload an installable SDK named something like: The workflow will also install the SDK locally and use [swift-android-action](https://github.com/marketplace/actions/swift-android-action) -to build and test various Swift packages in an Android emulator. - - +to build and test various Swift packages in an Android emulator using the +freshly-created SDK bundle. diff --git a/swift-ci/sdks/android/build b/swift-ci/sdks/android/build deleted file mode 100755 index a2834f71..00000000 --- a/swift-ci/sdks/android/build +++ /dev/null @@ -1,111 +0,0 @@ -#!/bin/bash -e -# Swift Android SDK: Top-level Build Script - -# default architectures to build for -TARGET_ARCHS=${TARGET_ARCHS:-aarch64,x86_64,armv7} - -ANDROID_NDK_VERSION=android-ndk-r27c -ANDROID_API=28 - -SDKROOT=${RUNNER_TEMP:-${TMPDIR:-'/tmp'}}/swift-android-sdk -mkdir -p ${SDKROOT} - -# Install a Swift host toolchain if it isn't already present -SWIFT_ROOT=${SDKROOT}/host-toolchain -HOST_OS=ubuntu$(lsb_release -sr) - -#BUILD_VERSION='release' -#BUILD_VERSION='trunk' -BUILD_VERSION=${BUILD_VERSION:-'devel'} - -if [ "${BUILD_VERSION}" = 'release' ]; then - LATEST_TOOLCHAIN_VERSION=$(curl -sL https://github.com/apple/swift/releases | grep -m1 swift-6.1 | cut -d- -f2) - SWIFT_TAG="swift-${LATEST_TOOLCHAIN_VERSION}-RELEASE" - SWIFT_BRANCH="swift-$(echo $SWIFT_TAG | cut -d- -f2)-release" -elif [ "${BUILD_VERSION}" = 'devel' ]; then - LATEST_TOOLCHAIN_VERSION=$(curl -sL https://github.com/apple/swift/tags | grep -m1 swift-6.2-DEV | cut -d- -f8-10) - SWIFT_TAG="swift-6.2-DEVELOPMENT-SNAPSHOT-${LATEST_TOOLCHAIN_VERSION}-a" - SWIFT_BRANCH="swift-$(echo $SWIFT_TAG | cut -d- -f2)-branch" -else - LATEST_TOOLCHAIN_VERSION=$(curl -sL https://github.com/apple/swift/tags | grep -m1 swift-DEV | cut -d- -f7-9) - SWIFT_TAG="swift-DEVELOPMENT-SNAPSHOT-${LATEST_TOOLCHAIN_VERSION}-a" - SWIFT_BRANCH="development" -fi - -SWIFT_BASE=$SWIFT_TAG-$HOST_OS - -if [[ ! -d "$SWIFT_ROOT/$SWIFT_BASE" ]]; then - mkdir -p $SWIFT_ROOT - pushd $SWIFT_ROOT - - SWIFT_URL="https://download.swift.org/$SWIFT_BRANCH/$(echo $HOST_OS | tr -d '.')/$SWIFT_TAG/$SWIFT_BASE.tar.gz" - wget -q $SWIFT_URL - tar xzf $SWIFT_BASE.tar.gz - - popd -fi - -#HOST_TOOLCHAIN=${HOST_TOOLCHAIN:-$(dirname $(dirname $(which swiftc)))} -HOST_TOOLCHAIN=$SWIFT_ROOT/$SWIFT_BASE/usr -$HOST_TOOLCHAIN/bin/swift --version - -# ensure the correct Swift is first in the PATH -export PATH=$HOST_TOOLCHAIN/bin:$PATH - -export ANDROID_NDK_HOME=${SDKROOT}/ndk/${ANDROID_NDK_VERSION} -export ANDROID_NDK=${ANDROID_NDK_HOME} - -if [[ ! -d ${ANDROID_NDK_HOME} ]]; then - mkdir -p $(dirname ${ANDROID_NDK_HOME}) - pushd $(dirname ${ANDROID_NDK_HOME}) - NDKFILE=$(basename $ANDROID_NDK_HOME)-linux.zip - wget -q https://dl.google.com/android/repository/${NDKFILE} - unzip -q ${NDKFILE} - popd -fi - -mkdir -p ${SDKROOT}/products - -# Check-out the sources -if [[ ! -d ${SDKROOT}/src ]]; then - scripts/fetch-source.sh --source-dir ${SDKROOT}/src --swift-tag ${SWIFT_TAG} -fi - -# fetch and apply the patches -PATCHDIR=${SDKROOT}/patches -if [[ ! -d ${PATCHDIR} ]]; then - git clone https://github.com/finagolfin/swift-android-sdk.git ${PATCHDIR} - - # TODO: need to selectively apply patches based on release or not release - pushd ${SDKROOT}/src/swift-project - echo "Applying patches" - - # patch the patch, which seems to only be needed for an API less than 28 - # https://github.com/finagolfin/swift-android-sdk/blob/main/swift-android.patch#L110 - perl -pi -e 's/#if os\(Windows\)/#if os\(Android\)/g' $PATCHDIR/swift-android.patch - - # remove the need to link in android-execinfo - perl -pi -e 's/dispatch android-execinfo/dispatch/g' $PATCHDIR/swift-android.patch - - git apply -v $PATCHDIR/swift-android.patch - # swift-android-ci.patch is not needed, since it is only used for llbuild, etc. - #git apply -C1 $PATCHDIR/swift-android-ci.patch - #git apply -v $PATCHDIR/swift-android-ci-release.patch - if [ "${BUILD_VERSION}" = 'release' ]; then - git apply -v $PATCHDIR/swift-android-testing-release.patch - else - git apply -v $PATCHDIR/swift-android-testing-except-release.patch - fi - - perl -pi -e 's%String\(cString: getpass%\"fake\" //%' swiftpm/Sources/PackageRegistryCommand/PackageRegistryCommand+Auth.swift - # disable backtrace() for Android (needs either API33+ or libandroid-execinfo, or to manually add in backtrace backport) - perl -pi -e 's/os\(Android\)/os\(AndroidDISABLED\)/g' swift-testing/Sources/Testing/SourceAttribution/Backtrace.swift - - # need to un-apply libandroid-spawn since we don't need it for API28+ - perl -pi -e 's/MATCHES "Android"/MATCHES "AndroidDISABLED"/g' llbuild/lib/llvm/Support/CMakeLists.txt - perl -pi -e 's/ STREQUAL Android\)/ STREQUAL AndroidDISABLED\)/g' swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt - popd -fi - -./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} ${@} - diff --git a/swift-ci/sdks/android/build-docker b/swift-ci/sdks/android/build-docker new file mode 100755 index 00000000..fb3b377a --- /dev/null +++ b/swift-ci/sdks/android/build-docker @@ -0,0 +1,67 @@ +#!/bin/bash -e +# +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Docker Container Build Script +# +# ===----------------------------------------------------------------------=== + +# default architectures to build for +TARGET_ARCHS=${TARGET_ARCHS:-aarch64,x86_64,armv7} + +ANDROID_NDK_VERSION=android-ndk-r27c +ANDROID_API=28 + +export BUILD_VERSION=${1} +# note that WORKDIR must not be under the current checkout or the patches will fail to apply +WORKDIR=${2} +if [[ "${WORKDIR}" == '' ]]; then + echo "Usage: $(basename $0) " + exit 1 +fi +mkdir -p ${WORKDIR} +WORKDIR=$(realpath ${WORKDIR}) + +HOST_OS=ubuntu24.04 +source ./scripts/toolchain-vars.sh + +# Check-out and patch the sources +./scripts/fetch-source.sh --source-dir ${WORKDIR}/source --swift-tag ${SWIFT_TAG} +./scripts/patch-sources.sh ${WORKDIR}/source + +mkdir -p ${WORKDIR}/products +chmod ugo+rwx ${WORKDIR}/products + +if [[ "$DOCKER" == "" ]]; then + DOCKER=docker +fi + +case $(arch) in + arm64|aarch64) + OS_ARCH_SUFFIX=-aarch64 + ;; + amd64|x86_64) + OS_ARCH_SUFFIX= + ;; + *) + echo "Unknown architecture $(arch)" + exit 1 + ;; +esac + +CONTAINER_NAME="swift-android" + +# Build the Docker image +$DOCKER build --build-arg OS_ARCH_SUFFIX=$OS_ARCH_SUFFIX --build-arg SWIFT_TOOLCHAIN_URL=$SWIFT_TOOLCHAIN_URL --build-arg ANDROID_NDK_VERSION=$ANDROID_NDK_VERSION -t ${CONTAINER_NAME} . + +$DOCKER run -i --rm \ + -v ${WORKDIR}/source:/source \ + -v ${WORKDIR}/products:/products:rw \ + ${CONTAINER_NAME} \ + /scripts/build.sh \ + --source-dir /source \ + --products-dir /products \ + --host-toolchain /usr/local/swift \ + --android-api ${ANDROID_API} \ + --ndk-home /usr/local/ndk/${ANDROID_NDK_VERSION} \ + --archs ${TARGET_ARCHS} diff --git a/swift-ci/sdks/android/build-local b/swift-ci/sdks/android/build-local new file mode 100755 index 00000000..30bd4f92 --- /dev/null +++ b/swift-ci/sdks/android/build-local @@ -0,0 +1,64 @@ +#!/bin/bash -e +# +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Local (non-Docker-containerized) Build Script +# +# ===----------------------------------------------------------------------=== + +# default architectures to build for +TARGET_ARCHS=${TARGET_ARCHS:-aarch64,x86_64,armv7} + +ANDROID_NDK_VERSION=android-ndk-r27c +ANDROID_API=28 + +export BUILD_VERSION=${1} +# note that WORKDIR must not be under the current checkout or the patches will fail to apply +WORKDIR=${2} +if [[ "${WORKDIR}" == '' ]]; then + echo "Usage: $(basename $0) " + exit 1 +fi +mkdir -p ${WORKDIR} +WORKDIR=$(realpath ${WORKDIR}) + +HOST_OS=ubuntu$(lsb_release -sr) +source ./scripts/toolchain-vars.sh + +SWIFT_ROOT=${WORKDIR}/host-toolchain +HOST_TOOLCHAIN=$SWIFT_ROOT/$SWIFT_BASE/usr +if [[ ! -d "$HOST_TOOLCHAIN" ]]; then + ./scripts/install-swift.sh ${HOST_TOOLCHAIN} +fi + +$HOST_TOOLCHAIN/bin/swift --version + +# ensure the correct Swift is first in the PATH +export PATH=$HOST_TOOLCHAIN/bin:$PATH + +export ANDROID_NDK_HOME=${WORKDIR}/ndk/${ANDROID_NDK_VERSION} + +if [[ ! -d ${ANDROID_NDK_HOME} ]]; then + mkdir -p $(dirname ${ANDROID_NDK_HOME}) + pushd $(dirname ${ANDROID_NDK_HOME}) + NDKFILE=$(basename $ANDROID_NDK_HOME)-linux.zip + wget -q https://dl.google.com/android/repository/${NDKFILE} + unzip -q ${NDKFILE} + popd +fi + + +# Check-out and patch the sources +./scripts/fetch-source.sh --source-dir ${WORKDIR}/source --swift-tag ${SWIFT_TAG} +./scripts/patch-sources.sh ${WORKDIR}/source + +mkdir -p ${WORKDIR}/products + +./scripts/build.sh \ + --source-dir ${WORKDIR}/source \ + --products-dir ${WORKDIR}/products \ + --build-dir ${WORKDIR}/build \ + --host-toolchain ${HOST_TOOLCHAIN} \ + --android-api ${ANDROID_API} \ + --ndk-home ${ANDROID_NDK_HOME} \ + --archs ${TARGET_ARCHS} diff --git a/swift-ci/sdks/android/resources/patches b/swift-ci/sdks/android/resources/patches new file mode 160000 index 00000000..d0828093 --- /dev/null +++ b/swift-ci/sdks/android/resources/patches @@ -0,0 +1 @@ +Subproject commit d08280938ff70b790bbd0f559c0b420b518877ca diff --git a/swift-ci/sdks/android/scripts/build.sh b/swift-ci/sdks/android/scripts/build.sh index 879aa1e4..20877272 100755 --- a/swift-ci/sdks/android/scripts/build.sh +++ b/swift-ci/sdks/android/scripts/build.sh @@ -109,7 +109,6 @@ declare_package libxml2 "libxml2" "MIT" \ declare_package curl "curl" "MIT" "https://curl.se" declare_package boringssl "boringssl" "OpenSSL AND ISC AND MIT" \ "https://boringssl.googlesource.com/boringssl/" -declare_package zlib "zlib" "Zlib" "https://zlib.net" # Parse command line arguments android_sdk_version=0.1 @@ -166,12 +165,14 @@ if ! swiftc=$(which swiftc); then exit 1 fi -script_dir=$(dirname -- "${BASH_SOURCE[0]}") -resource_dir="${script_dir}/../resources" - # Find the version numbers of the various dependencies function describe { pushd $1 >/dev/null 2>&1 + # this is needed for docker containers or else we get the error: + # fatal: detected dubious ownership in repository at '/source/curl' + if [[ "${SWIFT_BUILD_DOCKER}" == "1" ]]; then + git config --global --add safe.directory $(pwd) + fi git describe --tags popd >/dev/null 2>&1 } @@ -200,8 +201,6 @@ curl_version=${curl_desc#curl-} boringssl_version=$(describe ${source_dir}/boringssl) -zlib_version=$(versionFromTag ${source_dir}/zlib) - function quiet_pushd { pushd "$1" >/dev/null 2>&1 } @@ -214,8 +213,24 @@ header "Swift Android SDK build script" swift_dir=$(realpath $(dirname "$swiftc")/..) HOST=linux-x86_64 #HOST=$(uname -s -m | tr '[:upper:]' '[:lower:]' | tr ' ' '-') + +# in a Docker container, the pre-installed NDK is read-only, +# but the build script needs to write to it to work around +# https://github.com/swiftlang/swift-driver/pull/1822 +# so we copy it to a read-write location for the purposes of the build +# this can all be removed once that PR lands +mkdir -p ${build_dir}/ndk/ +ndk_home_tmp=${build_dir}/ndk/$(basename $ndk_home) +cp -a $ndk_home $ndk_home_tmp +ndk_home=$ndk_home_tmp + ndk_installation=$ndk_home/toolchains/llvm/prebuilt/$HOST +# ANDROID_NDK env needed by the swift-android.patch for: +# call ln -sf "${SWIFT_BUILD_PATH}/lib/swift" "${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib" +export ANDROID_NDK_HOME=$ndk_home +export ANDROID_NDK=$ndk_home + echo "Swift found at ${swift_dir}" echo "Host toolchain found at ${host_toolchain}" ${host_toolchain}/bin/swift --version @@ -231,7 +246,12 @@ echo " - Swift ${swift_version}" echo " - libxml2 ${libxml2_version}" echo " - curl ${curl_version}" echo " - BoringSSL ${boringssl_version}" -echo " - zlib ${zlib_version}" + +# make sure the products_dir is writeable +ls -lad $products_dir +touch $products_dir/products_dir_write_test.tmp +rm $products_dir/products_dir_write_test.tmp +#chown -R $(id -u):$(id -g) $products_dir function run() { echo "$@" @@ -239,11 +259,6 @@ function run() { } for arch in $archs; do - # enable short-circuiting the individual builds - if [[ ! -z "$SWIFT_ANDROID_ARCHIVEONLY" ]]; then - continue - fi - case $arch in armv7) target_host="arm-linux-androideabi"; compiler_target_host="armv7a-linux-androideabi$android_api"; android_abi="armeabi-v7a" ;; aarch64) target_host="aarch64-linux-android"; compiler_target_host="$target_host$android_api"; android_abi="arm64-v8a" ;; @@ -326,8 +341,9 @@ for arch in $archs; do -DOPENSSL_INCLUDE_DIR=$sdk_root/usr/include \ -DOPENSSL_SSL_LIBRARY=$sdk_root/usr/lib/libssl.a \ -DOPENSSL_CRYPTO_LIBRARY=$sdk_root/usr/lib/libcrypto.a \ - -DCURL_USE_OPENSSL=ON \ -DCURLSSLOPT_NATIVE_CA=ON \ + -DCURL_USE_OPENSSL=ON \ + -DCURL_USE_LIBSSH2=OFF \ -DTHREADS_PREFER_PTHREAD_FLAG=OFF \ -DCMAKE_THREAD_PREFER_PTHREAD=OFF \ -DCMAKE_THREADS_PREFER_PTHREAD_FLAG=OFF \ @@ -477,16 +493,17 @@ for arch in $archs; do rsync -a ${sdk_staging}/${arch}/usr ${swift_res_root} done +rm -r ${swift_res_root}/usr/share/{doc,man} +rm -r ${sdk_staging} + +# create an install script to set up the NDK links +#ANDROID_NDK_HOME="/opt/homebrew/share/android-ndk" +mkdir scripts/ + ndk_sysroot="ndk-sysroot" -# whether to include the ndk-sysroot in the SDK bundle -INCLUDE_NDK_SYSROOT=${INCLUDE_NDK_SYSROOT:-0} -if [[ ${INCLUDE_NDK_SYSROOT} != 1 ]]; then - # if we do not include the NDK, then create an install script - #ANDROID_NDK_HOME="/opt/homebrew/share/android-ndk" - mkdir scripts/ - cat > scripts/setup-android-sdk.sh <<'EOF' -#/bin/sh +cat > scripts/setup-android-sdk.sh <<'EOF' +#/bin/bash # this script will setup the ndk-sysroot with links to the # local installation indicated by ANDROID_NDK_HOME set -e @@ -494,72 +511,64 @@ if [ -z "${ANDROID_NDK_HOME}" ]; then echo "$(basename $0): error: missing environment variable ANDROID_NDK_HOME" exit 1 fi -PREBUILT="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt" -if [ ! -d "${PREBUILT}" ]; then - echo "$(basename $0): error: ANDROID_NDK_HOME not found: ${PREBUILT}" + +ndk_prebuilt="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt" +if [ ! -d "${ndk_prebuilt}" ]; then + echo "$(basename $0): error: ANDROID_NDK_HOME not found: ${ndk_prebuilt}" exit 1 fi -DESTINATION=$(dirname $(dirname $(realpath $0)))/ndk-sysroot -# clear out any previous NDK setup -rm -rf ${DESTINATION} -# copy vs. link the NDK files +#Pkg.Revision = 27.0.12077973 +#Pkg.Revision = 28.1.13356709 +ndk_version=$(grep '^Pkg.Revision = ' "${ANDROID_NDK_HOME}/source.properties" | cut -f3- -d' ' | cut -f 1 -d '.') +if [[ "${ndk_version}" -lt 27 ]]; then + echo "$(basename $0): error: minimum NDK version 27 required; found ${ndk_version} in ${ANDROID_NDK_HOME}/source.properties" + exit 1 +fi + +cd $(dirname $(dirname $(realpath -- "${BASH_SOURCE[0]}"))) +swift_resources=swift-resources +ndk_sysroot=ndk-sysroot + +if [[ -d "${ndk_sysroot}" ]]; then + # clear out any previous NDK setup + rm -rf ${ndk_sysroot} + ndk_re="re-" +fi + +# link vs. copy the NDK files SWIFT_ANDROID_NDK_LINK=${SWIFT_ANDROID_NDK_LINK:-1} -if [[ "${SWIFT_ANDROID_NDK_LINK}" != 1 ]]; then - ANDROID_NDK_DESC="copied" - cp -a ${PREBUILT}/*/sysroot ${DESTINATION} -else - ANDROID_NDK_DESC="linked" - mkdir -p ${DESTINATION}/usr/lib - ln -s $(realpath ${PREBUILT}/*/sysroot/usr/include) ${DESTINATION}/usr/include - for triplePath in ${PREBUILT}/*/sysroot/usr/lib/*; do +if [[ "${SWIFT_ANDROID_NDK_LINK}" == 1 ]]; then + ndk_action="${ndk_re}linked" + mkdir -p ${ndk_sysroot}/usr/lib + ln -s ${ndk_prebuilt}/*/sysroot/usr/include ${ndk_sysroot}/usr/include + for triplePath in ${ndk_prebuilt}/*/sysroot/usr/lib/*; do triple=$(basename ${triplePath}) - ln -s $(realpath ${triplePath}) ${DESTINATION}/usr/lib/${triple} + ln -s ${triplePath} ${ndk_sysroot}/usr/lib/${triple} done +else + ndk_action="${ndk_re}copied" + cp -a ${ndk_prebuilt}/*/sysroot ${ndk_sysroot} fi # copy each architecture's swiftrt.o into the sysroot, # working around https://github.com/swiftlang/swift/pull/79621 -for swiftrt in ${DESTINATION}/../swift-resources/usr/lib/swift-*/android/*/swiftrt.o; do - arch=$(basename $(dirname ${swiftrt})) - mkdir -p ${DESTINATION}/usr/lib/swift/android/${arch} - cp -a ${swiftrt} ${DESTINATION}/usr/lib/swift/android/${arch} +for folder in swift swift_static; do + for swiftrt in ${swift_resources}/usr/lib/${folder}-*/android/*/swiftrt.o; do + arch=$(basename $(dirname ${swiftrt})) + mkdir -p ${ndk_sysroot}/usr/lib/${folder}/android/${arch} + if [[ "${SWIFT_ANDROID_NDK_LINK}" == 1 ]]; then + ln -s ../../../../../../${swiftrt} ${ndk_sysroot}/usr/lib/${folder}/android/${arch}/ + else + cp -a ${swiftrt} ${ndk_sysroot}/usr/lib/${folder}/android/${arch}/ + fi + done done -echo "$(basename $0): success: ndk-sysroot ${ANDROID_NDK_DESC} to Android SDK" +echo "$(basename $0): success: ndk-sysroot ${ndk_action} to Android NDK at ${ndk_prebuilt}" EOF - chmod +x scripts/setup-android-sdk.sh -else - COPY_NDK_SYSROOT=${COPY_NDK_SYSROOT:-1} - if [[ ${COPY_NDK_SYSROOT} == 1 ]]; then - cp -a ${ndk_installation}/sysroot ${ndk_sysroot} - else - # rather than copying the sysroot, we can instead make links to - # the various sub-folders this won't work for the distribution, - # since the NDK is going to be located in different places - # for different machines - mkdir -p ${ndk_sysroot}/usr/lib - ln -sv $(realpath ${ndk_installation}/sysroot/usr/include) ${ndk_sysroot}/usr/include - for triplePath in ${ndk_installation}/sysroot/usr/lib/*; do - triple=$(basename ${triplePath}) - ln -sv $(realpath ${triplePath}) ${ndk_sysroot}/usr/lib/${triple} - ls ${ndk_sysroot}/usr/lib/${triple}/ - done - fi - # need to manually copy over swiftrt.o or else: - # error: link command failed with exit code 1 (use -v to see invocation) - # 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' - # see: https://github.com/swiftlang/swift-driver/pull/1822#issuecomment-2762811807 - # should be fixed by: https://github.com/swiftlang/swift/pull/79621 - for arch in $archs; do - mkdir -p ${ndk_sysroot}/usr/lib/swift/android/${arch} - ln -srv ${swift_res_root}/usr/lib/swift-${arch}/android/${arch}/swiftrt.o ${ndk_sysroot}/usr/lib/swift/android/${arch}/swiftrt.o - done -fi - -rm -r ${swift_res_root}/usr/share/{doc,man} -rm -r ${sdk_staging} +chmod +x scripts/setup-android-sdk.sh cat > swift-sdk.json </dev/null + +if [[ "${ANDROID_NDK_VERSION}" == "" ]]; then + echo "$0: Missing ANDROID_NDK_VERSION environment" + exit 1 +fi + + +NDKFILE=${ANDROID_NDK_VERSION}-linux.zip + +NDKURL="https://dl.google.com/android/repository/${NDKFILE}" +echo "Going to fetch ${NDKURL}" + +curl -fsSL "${NDKURL}" -o ${NDKFILE} + +echo "Extracting NDK" +unzip -q ${NDKFILE} + +rm ${NDKFILE} + +popd >/dev/null + diff --git a/swift-ci/sdks/android/scripts/install-swift.sh b/swift-ci/sdks/android/scripts/install-swift.sh new file mode 100755 index 00000000..0acd9e02 --- /dev/null +++ b/swift-ci/sdks/android/scripts/install-swift.sh @@ -0,0 +1,54 @@ +#!/bin/bash +# +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Install Swift +# +# ===----------------------------------------------------------------------=== + +set -e + +if [[ "${SWIFT_TOOLCHAIN_URL}" == "" ]]; then + echo "$0: Missing SWIFT_TOOLCHAIN_URL environment" + exit 1 +fi + +destination=$1 +if [[ "${destination}" == "" ]]; then + echo "$0: Usage: $(basename $0) " + exit 1 +fi + +echo "Installing Swift from: ${SWIFT_TOOLCHAIN_URL} into: ${destination}" + +# Make a temporary directory +tmpdir=$(mktemp -d) +function cleanup { + rm -rf "$tmpdir" +} +trap cleanup EXIT + +pushd "$tmpdir" >/dev/null +export GNUPGHOME="$tmpdir" + +# Fetch the toolchain and signature +echo "Going to fetch ${SWIFT_TOOLCHAIN_URL}" +curl -fsSL "${SWIFT_TOOLCHAIN_URL}" -o toolchain.tar.gz + +echo "Going to fetch ${SWIFT_TOOLCHAIN_URL}.sig" +curl -fsSL "${SWIFT_TOOLCHAIN_URL}.sig" -o toolchain.sig + +echo "Fetching keys" +curl -fsSL --compressed https://swift.org/keys/all-keys.asc | gpg --import - + +echo "Verifying signature" +gpg --batch --verify toolchain.sig toolchain.tar.gz + +# Extract and install the toolchain +echo "Extracting Swift" +mkdir -p ${destination} +tar -xzf toolchain.tar.gz --directory ${destination} --strip-components=2 +chmod -R o+r ${destination}/lib/swift + +popd >/dev/null + diff --git a/swift-ci/sdks/android/scripts/patch-sources.sh b/swift-ci/sdks/android/scripts/patch-sources.sh new file mode 100755 index 00000000..ee425c6a --- /dev/null +++ b/swift-ci/sdks/android/scripts/patch-sources.sh @@ -0,0 +1,87 @@ +#!/bin/bash +# Swift Android SDK: Patch Sources +set -e + +source_dir=$1 + +if [[ ! -d "${source_dir}" ]]; then + echo "$0: source_dir ${source_dir} does not exist" + exit 1 +fi + +script_dir=$(dirname -- "${BASH_SOURCE[0]}") +resource_dir=$(realpath "${script_dir}/../resources") +patches_dir="${resource_dir}/patches" + +if [[ ! -d "${patches_dir}" ]]; then + echo "$0: patches_dir ${patches_dir} does not exist" + exit 1 +fi + +cd ${source_dir}/swift-project +swift_android_patch="${patches_dir}/swift-android.patch" + +ls -la swift/utils/build-script-impl +ls -la ${swift_android_patch} +ls -la swiftpm/Sources/PackageRegistryCommand/PackageRegistryCommand+Auth.swift + +# patch the patch, which seems to only be needed for an API less than 28 +# https://github.com/finagolfin/swift-android-sdk/blob/main/swift-android.patch#L110 +perl -pi -e 's/#if os\(Windows\)/#if os\(Android\)/g' $swift_android_patch + +# remove the need to link in android-execinfo +perl -pi -e 's;dispatch android-execinfo;dispatch;g' $swift_android_patch + +# debug symbolic link setup +perl -pi -e 's;call ln -sf;call ln -svf;g' $swift_android_patch +perl -pi -e 's%linux-x86_64/sysroot/usr/lib"%linux-x86_64/sysroot/usr/lib"; echo "VALIDATING SYMBOLIC LINK"; ls -la "\${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib"; ls -la "\${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/swift"; %g' $swift_android_patch + +case "${BUILD_VERSION}" in + release) + testing_patch="${patches_dir}/swift-android-testing-release.patch" + ;; + devel) + testing_patch="${patches_dir}/swift-android-testing-except-release.patch" + ;; + trunk) + testing_patch="${patches_dir}/swift-android-testing-except-release.patch" + dispatch_patch="${patches_dir}/swift-android-trunk-libdispatch.patch" + ;; + *) + echo "$0: invalid BUILD_VERSION=${BUILD_VERSION}" + exit 1 + ;; +esac + +for patch in "$swift_android_patch" "$testing_patch" "$dispatch_patch"; do + if [[ "${patch}" == "" ]]; then + continue + fi + + echo "applying patch $patch in $PWD…" + # first check to make sure the patches can apply and fail if not + git apply -v --check -C1 "$patch" + git apply --no-index -v -C1 "$patch" + + #if git apply -C1 --reverse --check "$patch" >/dev/null 2>&1 ; then + # echo "already patched" + #elif git apply -C1 "$patch" ; then + # echo "done" + #else + # echo "failed to apply patch $patch in $PWD" + # exit 1 + #fi +done + +perl -pi -e 's%String\(cString: getpass%\"fake\" //%' swiftpm/Sources/PackageRegistryCommand/PackageRegistryCommand+Auth.swift +# disable backtrace() for Android (needs either API33+ or libandroid-execinfo, or to manually add in backtrace backport) +perl -pi -e 's;os\(Android\);os\(AndroidDISABLED\);g' swift-testing/Sources/Testing/SourceAttribution/Backtrace.swift + +# need to un-apply libandroid-spawn since we don't need it for API28+ +perl -pi -e 's;MATCHES "Android";MATCHES "AndroidDISABLED";g' llbuild/lib/llvm/Support/CMakeLists.txt +perl -pi -e 's; STREQUAL Android\); STREQUAL AndroidDISABLED\);g' swift-corelibs-foundation/Sources/Foundation/CMakeLists.txt + +# validate the patches +ls -la swift/utils/build-script-impl +grep 'VALIDATING SYMBOLIC LINK' swift/utils/build-script-impl + diff --git a/swift-ci/sdks/android/scripts/toolchain-vars.sh b/swift-ci/sdks/android/scripts/toolchain-vars.sh new file mode 100644 index 00000000..c1001931 --- /dev/null +++ b/swift-ci/sdks/android/scripts/toolchain-vars.sh @@ -0,0 +1,44 @@ +#!/bin/bash -e +# +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Toolchain source variables +# +# ===----------------------------------------------------------------------=== + +# This script is meant to be sourced from another script that sets the +# BUILD_VERSION environment variable to one of "release", "devel", or "trunk" +# and will set check the latest builds for each build type in order +# to provide information about the Swift tag name in use and where to +# obtain the latest toolchain for building. + +# TODO: we could instead use the latest-build.yml files for this, like: +# https://download.swift.org/swift-6.2-branch/ubuntu2404/latest-build.yml +# https://download.swift.org/development/ubuntu2404/latest-build.yml +# but there doesn't seem to be one for the current release build. + +case "${BUILD_VERSION}" in + release) + LATEST_TOOLCHAIN_VERSION=$(curl -sL https://github.com/swiftlang/swift/releases | grep -m1 swift-6.1 | cut -d- -f2) + SWIFT_TAG="swift-${LATEST_TOOLCHAIN_VERSION}-RELEASE" + SWIFT_BRANCH="swift-$(echo $SWIFT_TAG | cut -d- -f2)-release" + ;; + devel) + LATEST_TOOLCHAIN_VERSION=$(curl -sL https://github.com/swiftlang/swift/tags | grep -m1 swift-6.2-DEV | cut -d- -f8-10) + SWIFT_TAG="swift-6.2-DEVELOPMENT-SNAPSHOT-${LATEST_TOOLCHAIN_VERSION}-a" + SWIFT_BRANCH="swift-$(echo $SWIFT_TAG | cut -d- -f2)-branch" + ;; + trunk) + LATEST_TOOLCHAIN_VERSION=$(curl -sL https://github.com/swiftlang/swift/tags | grep -m1 swift-DEV | cut -d- -f7-9) + SWIFT_TAG="swift-DEVELOPMENT-SNAPSHOT-${LATEST_TOOLCHAIN_VERSION}-a" + SWIFT_BRANCH="development" + ;; + *) + echo "$0: invalid BUILD_VERSION=${BUILD_VERSION}" + exit 1 + ;; +esac + +SWIFT_BASE=$SWIFT_TAG-$HOST_OS +export SWIFT_TOOLCHAIN_URL="https://download.swift.org/$SWIFT_BRANCH/$(echo $HOST_OS | tr -d '.')/$SWIFT_TAG/$SWIFT_BASE.tar.gz" +