diff --git a/swift-ci/sdks/android/Dockerfile b/swift-ci/sdks/android/Dockerfile new file mode 100644 index 00000000..71d2399d --- /dev/null +++ b/swift-ci/sdks/android/Dockerfile @@ -0,0 +1,96 @@ +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Docker-based build +# +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2024 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +# +# ===----------------------------------------------------------------------=== + +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 \ + clang-19 \ + cmake \ + ninja-build \ + python3 \ + golang \ + git \ + gnupg2 \ + 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/lib/llvm-19/bin:/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/* + +# 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 new file mode 100644 index 00000000..238803a0 --- /dev/null +++ b/swift-ci/sdks/android/README.md @@ -0,0 +1,85 @@ +# Dockerfile-based build for Swift Android SDK + +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 tag:swift-6.2-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 a branch scheme, like "scheme:release/6.2", or a +tag, like "tag:swift-6.2-DEVELOPMENT-SNAPSHOT-2025-09-04-a". + +> [!WARNING] +> The workdir argument must not be located in a git repository (e.g., it cannot be the +> current directory) + +## Running + +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. + +It then applies some perl substitutions and invokes `scripts/build.sh`, +which will build the sources for each of the specified +architectures and then combines the SDKs into a single +artifactbundle with targetTriples for each of the supported +architectures (`aarch64`, `x86_64`, `aarmv7`) +and Android API levels (28-35). + +## Specifying Architectures + +By default, all the supported Android architectures +will be built, but this can be reduced in order to speed +up the build. This can be useful, e.g., as part of a CI that +validates a pull request, as building a single architecture +takes around 30 minutes on a standard ubuntu-24.04 GitHub runner, +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-docker scheme:main /tmp/android-sdk +``` + +## Building the Swift compiler from source and running the validation suite + +All tags that are specified will download the official release or snapshot +toolchain and build only the bundle by default, while building from a branch +scheme always builds the full Swift compiler from the latest commit in that +branch. If you want to build the Swift compiler from source for a tag also and +run the compiler validation suite, specify the `BUILD_COMPILER` variable: + +``` +BUILD_COMPILER=yes ./build-docker tag:swift-DEVELOPMENT-SNAPSHOT-2025-09-04-a /tmp/android-sdk +``` + +## Building locally + +Instead of building within a Docker container, the script can also +perform the build locally on an Ubuntu 24.04 machine with all the +build prerequisites already installed. This will generate +the same artifacts in approximately half the time, and +may be suitable to an already containerized envrionment (such as +a GitHub runner). A local build can be run with the +`build-local` script, such as: + +``` +./build-local scheme:release/6.2 /tmp/android-sdk +``` diff --git a/swift-ci/sdks/android/build-docker b/swift-ci/sdks/android/build-docker new file mode 100755 index 00000000..3b88952c --- /dev/null +++ b/swift-ci/sdks/android/build-docker @@ -0,0 +1,75 @@ +#!/bin/bash -e +# +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Docker Container Build Script +# +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2025 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +# +# ===----------------------------------------------------------------------=== + +# default architectures to build for +TARGET_ARCHS=${TARGET_ARCHS:-aarch64,x86_64,armv7} + +ANDROID_NDK_VERSION=android-ndk-r27d +ANDROID_API=28 + +BASEPATH=$(dirname $(realpath $0)) +cd ${BASEPATH} + +export SWIFT_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 + +# This `git grep` invocation in a trunk test fails in our Docker for some +# reason, so just turn it into a plain `grep` again. +perl -pi -e 's:"git",:#:' ${WORKDIR}/source/swift-project/swift/test/Misc/verify-swift-feature-testing.test-sh + +# Work around swiftlang/swift-driver#1822 for now +perl -pi -g -we "s#(call rm ... \".\{LIBDISPATCH_BUILD_DIR\}\"\n(\s+)fi\n)#\1\2if [[ -d \"\\\${ANDROID_NDK}\" ]]; then call ln -sf \"\\\${SWIFT_BUILD_PATH}/lib/swift\" \"\\\${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib\"; fi#" ${WORKDIR}/source/swift-project/swift/utils/build-script-impl + +# 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' ${WORKDIR}/source/swift-project/swift-testing/Sources/Testing/SourceAttribution/Backtrace.swift + +mkdir -p ${WORKDIR}/products +chmod ugo+rwx ${WORKDIR}/products + +if [[ "$DOCKER" == "" ]]; then + DOCKER=docker +fi + +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" \ + --build-compiler "${BUILD_COMPILER}" \ + --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..b5f078c5 --- /dev/null +++ b/swift-ci/sdks/android/build-local @@ -0,0 +1,84 @@ +#!/bin/bash -e +# +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Local (non-Docker-containerized) Build Script +# +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2025 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +# +# ===----------------------------------------------------------------------=== + +# default architectures to build for +TARGET_ARCHS=${TARGET_ARCHS:-aarch64,x86_64,armv7} + +ANDROID_NDK_VERSION=android-ndk-r27d +ANDROID_API=28 + +BASEPATH=$(dirname $(realpath $0)) +cd ${BASEPATH} + +export SWIFT_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 +# This `git grep` invocation in a trunk test fails in our Docker for some +# reason, so just turn it into a plain `grep` again. +perl -pi -e 's:"git",:#:' ${WORKDIR}/source/swift-project/swift/test/Misc/verify-swift-feature-testing.test-sh + +# Work around swiftlang/swift-driver#1822 for now +perl -pi -g -we "s#(call rm ... \".\{LIBDISPATCH_BUILD_DIR\}\"\n(\s+)fi\n)#\1\2if [[ -d \"\\\${ANDROID_NDK}\" ]]; then call ln -sf \"\\\${SWIFT_BUILD_PATH}/lib/swift\" \"\\\${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib\"; fi#" ${WORKDIR}/source/swift-project/swift/utils/build-script-impl + +# 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' ${WORKDIR}/source/swift-project/swift-testing/Sources/Testing/SourceAttribution/Backtrace.swift + +mkdir -p ${WORKDIR}/products + +./scripts/build.sh \ + --source-dir "${WORKDIR}/source" \ + --products-dir "${WORKDIR}/products" \ + --build-dir "${WORKDIR}/build" \ + --build-compiler "${BUILD_COMPILER}" \ + --host-toolchain "${HOST_TOOLCHAIN}" \ + --android-api "${ANDROID_API}" \ + --ndk-home "${ANDROID_NDK_HOME}" \ + --archs "${TARGET_ARCHS}" diff --git a/swift-ci/sdks/android/scripts/build.sh b/swift-ci/sdks/android/scripts/build.sh new file mode 100755 index 00000000..db95012f --- /dev/null +++ b/swift-ci/sdks/android/scripts/build.sh @@ -0,0 +1,798 @@ +#!/bin/bash +# +# ===----------------------------------------------------------------------=== +# +# Swift SDK for Android: Build Script +# +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2024 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +# +# ===----------------------------------------------------------------------=== +set -e + +# Docker sets TERM to xterm if using a pty; we probably want +# xterm-256color, otherwise we only get eight colors +if [ -t 1 ]; then + if [[ "$TERM" == "xterm" ]]; then + export TERM=xterm-256color + fi +fi + +if [[ -n "$TERM" ]]; then + bold="" + white="" + grey="" + reset="" +else + bold=$(tput bold) + white=$(tput setaf 15) + grey=$(tput setaf 8) + reset=$(tput sgr0) +fi + +function cleanup { + echo "${reset}" +} +trap cleanup EXIT + +function header { + local text="$1" + echo "" + echo "${white}${bold}*** ${text} ***${reset}${grey}" + echo "" +} + +function groupstart { + local text="$1" + if [[ ! -z "$CI" ]]; then + echo "::group::${text}" + fi + header $text +} + +function groupend { + if [[ ! -z "$CI" ]]; then + echo "::endgroup::" + fi +} + +function usage { + cat < --products-dir --ndk-home + [--name ] [--version ] [--build-dir ] + [--archs [, ...]] + +Build the Swift Android SDK. + +Options: + + --name Specify the name of the SDK bundle. + --version Specify the version of the Android SDK. + --source-dir Specify the path in which the sources can be found. + --ndk-home Specify the path to the Android NDK + --host-toolchain Specify the path to the host Swift toolchain + --build-compiler Whether to build and validate the host compiler + --products-dir Specify the path in which the products should be written. + --build-dir Specify the path in which intermediates should be stored. + --android-api Specify the Android API level + (Default is ${android_api}). + --archs [, ...] + Specify the architectures for which we should build + the SDK. + (Default is ${archs}). + --build Specify the CMake build type to use (Release, Debug, + RelWithDebInfo). + (Default is ${build_type}). + -j + --jobs Specify the number of parallel jobs to run at a time. + (Default is ${parallel_jobs}.) +EOF +} + +# Declare all the packages we depend on +declare -a packages + +function declare_package +{ + local name=$1 + local userVisibleName=$2 + local license=$3 + local url=$4 + + local snake=$(echo ${name} | tr '_' '-') + + declare -g ${name}_snake="$snake" + declare -g ${name}_name="$userVisibleName" + declare -g ${name}_license="$license" + declare -g ${name}_url="$url" + + packages+=(${name}) +} + +declare_package swift_android_sdk \ + "Swift SDK for Android" \ + "Apache-2.0" "https://swift.org/install" +declare_package swift "swift" "Apache-2.0" "https://swift.org" +declare_package libxml2 "libxml2" "MIT" \ + "https://github.com/GNOME/libxml2" +declare_package curl "curl" "MIT" "https://curl.se" +declare_package boringssl "boringssl" "OpenSSL AND ISC AND MIT" \ + "https://boringssl.googlesource.com/boringssl/" + +# Parse command line arguments +android_sdk_version=0.1 +sdk_name= +archs=aarch64,armv7,x86_64 +android_api=28 +build_type=Release +parallel_jobs=$(($(nproc --all) + 2)) +source_dir= +ndk_home=${ANDROID_NDK} +build_dir=$(pwd)/build +products_dir= + +while [ "$#" -gt 0 ]; do + case "$1" in + --source-dir) + source_dir="$2"; shift ;; + --ndk-home) + ndk_home="$2"; shift ;; + --host-toolchain) + host_toolchain="$2"; shift ;; + --build-compiler) + build_compiler="$2"; shift ;; + --build-dir) + build_dir="$2"; shift ;; + --android-api) + android_api="$2"; shift ;; + --products-dir) + products_dir="$2"; shift ;; + --name) + sdk_name="$2"; shift ;; + --archs) + archs="$2"; shift ;; + --build) + build_type="$2"; shift ;; + --version) + android_sdk_version="$2"; shift ;; + -j|--jobs) + parallel_jobs=$2; shift ;; + *) + echo "Unknown argument '$1'"; usage; exit 0 ;; + esac + shift +done + +# Change the commas for spaces +archs="${archs//,/ }" + +if [[ -z "$source_dir" || -z "$products_dir" || -z "$ndk_home" ]]; then + usage + exit 1 +fi + +if ! swiftc=$(which swiftc); then + echo "build.sh: Unable to find Swift compiler. You must have a Swift toolchain installed to build the Android SDK." + exit 1 +fi + +# 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 +} +function versionFromTag { + desc=$(describe $1) + if [[ $desc == v* ]]; then + echo "${desc#v}" + else + echo "${desc}" + fi +} + +swift_source_dir=${source_dir}/swift-project + +swift_version=$(describe ${swift_source_dir}/swift) +swift_tag_date=$(git -C ${swift_source_dir}/swift log -1 --format=%ct 2>/dev/null) + +if [[ $swift_version == swift-* ]]; then + swift_version=${swift_version#swift-} +fi + +if [[ -z "$sdk_name" ]]; then + sdk_name=swift-${swift_version}-android-${android_sdk_version} +fi + +libxml2_version=$(versionFromTag ${swift_source_dir}/libxml2) + +curl_desc=$(describe ${swift_source_dir}/curl | tr '_' '.') +curl_version=${curl_desc#curl-} + +boringssl_version=$(describe ${source_dir}/boringssl) + +function quiet_pushd { + pushd "$1" >/dev/null 2>&1 +} +function quiet_popd { + popd >/dev/null 2>&1 +} + +header "Swift Android SDK build script" + +swift_dir=$(realpath $(dirname "$swiftc")/..) +HOST=linux-x86_64 +# The Linux NDK only supports x86 +#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 +ndk_clang_version=18 + +echo "Swift found at ${swift_dir}" +if [[ ! -z "${host_toolchain}" ]]; then + echo "Host toolchain found at ${host_toolchain}" + ${host_toolchain}/bin/swift --version +fi +echo "Android NDK found at ${ndk_home}" +${ndk_installation}/bin/clang --version +echo "Building for ${archs}" +echo "Sources are in ${source_dir}" +echo "Build will happen in ${build_dir}" +echo "Products will be placed in ${products_dir}" +echo +echo "Building from:" +echo " - Swift ${swift_version}" +echo " - libxml2 ${libxml2_version}" +echo " - curl ${curl_version}" +echo " - BoringSSL ${boringssl_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 "$@" + "$@" +} + +for arch in $archs; do + 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" + ;; + x86_64) + target_host="x86_64-linux-android" + compiler_target_host="$target_host$android_api" + android_abi="x86_64" + ;; + x86) + target_host="x86-linux-android" + compiler_target_host="$target_host$android_api" + android_abi="x86" + ;; + *) + echo "Unknown architecture '$1'" + usage + exit 0 + ;; + esac + + sdk_root=${build_dir}/sdk_root/${arch} + mkdir -p "$sdk_root" + + groupstart "Building libxml2 for $arch" + quiet_pushd ${swift_source_dir}/libxml2 + run cmake \ + -G Ninja \ + -S ${swift_source_dir}/libxml2 \ + -B ${build_dir}/$arch/libxml2 \ + -DANDROID_ABI=$android_abi \ + -DANDROID_PLATFORM=android-$android_api \ + -DCMAKE_TOOLCHAIN_FILE=$ndk_home/build/cmake/android.toolchain.cmake \ + -DCMAKE_BUILD_TYPE=$build_type \ + -DCMAKE_EXTRA_LINK_FLAGS="-rtlib=compiler-rt -unwindlib=libunwind -stdlib=libc++ -fuse-ld=lld -lc++ -lc++abi -Wl,-z,max-page-size=16384" \ + -DCMAKE_BUILD_TYPE=$build_type \ + -DCMAKE_INSTALL_PREFIX=$sdk_root/usr \ + -DLIBXML2_WITH_PYTHON=NO \ + -DLIBXML2_WITH_ICU=NO \ + -DLIBXML2_WITH_ICONV=NO \ + -DLIBXML2_WITH_LZMA=NO \ + -DBUILD_SHARED_LIBS=OFF \ + -DBUILD_STATIC_LIBS=ON + + quiet_pushd ${build_dir}/$arch/libxml2 + run ninja -j$parallel_jobs + quiet_popd + + header "Installing libxml2 for $arch" + quiet_pushd ${build_dir}/$arch/libxml2 + run ninja -j$parallel_jobs install + quiet_popd + quiet_popd + groupend + + groupstart "Building boringssl for ${compiler_target_host}" + quiet_pushd ${source_dir}/boringssl + run cmake \ + -GNinja \ + -B ${build_dir}/$arch/boringssl \ + -DANDROID_ABI=$android_abi \ + -DANDROID_PLATFORM=android-$android_api \ + -DCMAKE_TOOLCHAIN_FILE=$ndk_home/build/cmake/android.toolchain.cmake \ + -DCMAKE_BUILD_TYPE=$build_type \ + -DCMAKE_INSTALL_PREFIX=$sdk_root/usr \ + -DCMAKE_EXTRA_LINK_FLAGS="-Wl,-z,max-page-size=16384" \ + -DBUILD_SHARED_LIBS=OFF \ + -DBUILD_STATIC_LIBS=ON \ + -DBUILD_TESTING=OFF + + quiet_pushd ${build_dir}/$arch/boringssl + run ninja -j$parallel_jobs + quiet_popd + + header "Installing BoringSSL for $arch" + quiet_pushd ${build_dir}/$arch/boringssl + run ninja -j$parallel_jobs install + quiet_popd + quiet_popd + groupend + + groupstart "Building libcurl for ${compiler_target_host}" + run cmake \ + -G Ninja \ + -S ${swift_source_dir}/curl \ + -B ${build_dir}/$arch/curl \ + -DANDROID_ABI=$android_abi \ + -DANDROID_PLATFORM=android-$android_api \ + -DCMAKE_TOOLCHAIN_FILE=$ndk_home/build/cmake/android.toolchain.cmake \ + -DCMAKE_BUILD_TYPE=$build_type \ + -DCMAKE_INSTALL_PREFIX=$sdk_root/usr \ + -DCMAKE_EXTRA_LINK_FLAGS="-Wl,-z,max-page-size=16384" \ + -DOPENSSL_ROOT_DIR=$sdk_root/usr \ + -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 \ + -DCURLSSLOPT_NATIVE_CA=ON \ + -DCURL_USE_OPENSSL=ON \ + -DCURL_USE_LIBSSH2=OFF \ + -DCURL_USE_LIBPSL=OFF \ + -DTHREADS_PREFER_PTHREAD_FLAG=OFF \ + -DCMAKE_THREAD_PREFER_PTHREAD=OFF \ + -DCMAKE_THREADS_PREFER_PTHREAD_FLAG=OFF \ + -DCMAKE_HAVE_LIBC_PTHREAD=YES \ + -DBUILD_CURL_EXE=NO \ + -DBUILD_SHARED_LIBS=OFF \ + -DBUILD_STATIC_LIBS=ON \ + -DCURL_BUILD_TESTS=OFF + + quiet_pushd ${build_dir}/$arch/curl + run ninja -j$parallel_jobs + quiet_popd + + header "Installing libcurl for $arch" + quiet_pushd ${build_dir}/$arch/curl + run cmake --install ${build_dir}/${arch}/curl + quiet_popd + groupend + + groupstart "Building Android SDK for ${compiler_target_host}" + quiet_pushd ${swift_source_dir} + build_type_flag="--debug" + case $build_type in + Debug) build_type_flag="--debug" ;; + Release) build_type_flag="--release" ;; + RelWithDebInfo) build_type_flag="--release-debuginfo" ;; + esac + + case $build_compiler in + 1|true|yes|YES) + build_cmark="" + local_build="" + build_llvm="1" + install_llvm="--install-llvm" + build_swift_tools="1" + validation_test="1" + native_swift_tools_path="" + native_clang_tools_path="" + ;; + *) + build_cmark="--skip-build-cmark" + local_build="--skip-local-build" + build_llvm="0" + install_llvm="" + build_swift_tools="0" + validation_test="0" + native_swift_tools_path="--native-swift-tools-path=$host_toolchain/bin" + native_clang_tools_path="--native-clang-tools-path=$host_toolchain/bin" + ;; + esac + + # use an out-of-tree build folder + export SWIFT_BUILD_ROOT=${build_dir}/swift-project + + ./swift/utils/build-script \ + $build_type_flag \ + --reconfigure \ + --no-assertions \ + --validation-test=$validation_test \ + --android \ + --android-ndk=$ndk_home \ + --android-arch=$arch \ + --android-api-level=$android_api \ + --cross-compile-hosts=android-$arch \ + --cross-compile-deps-path=$sdk_root \ + --install-destdir=$sdk_root \ + --build-llvm=$build_llvm ${install_llvm} \ + --build-swift-tools=$build_swift_tools \ + ${native_swift_tools_path} \ + ${native_clang_tools_path} \ + ${build_cmark} \ + ${local_build} \ + --host-test \ + --skip-test-linux \ + --skip-test-xctest --skip-test-foundation \ + --build-swift-static-stdlib \ + --swift-install-components='compiler;clang-resource-dir-symlink;license;stdlib;sdk-overlay' \ + --install-swift \ + --install-libdispatch \ + --install-foundation \ + --xctest --install-xctest \ + --swift-testing --install-swift-testing \ + --swift-testing-macros --install-swift-testing-macros \ + --cross-compile-build-swift-tools=False \ + --libdispatch-cmake-options=-DCMAKE_SHARED_LINKER_FLAGS= \ + --foundation-cmake-options=-DCMAKE_SHARED_LINKER_FLAGS= \ + --cross-compile-append-host-target-to-destdir=False + # --extra-cmake-options='-DCMAKE_EXTRA_LINK_FLAGS="-Wl,-z,max-page-size=16384"' + # need to remove symlink that gets created in the NDK to the previous arch's build + # or else we get errors like: + # 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 + rm -f $ndk_installation/sysroot/usr/lib/swift + quiet_popd + groupend +done + +# Now generate the bundle +groupstart "Bundling SDK" + +sdk_base=swift-android +sdk_staging="sdk_staging" + +bundle="${sdk_name}.artifactbundle" + +rm -rf ${build_dir}/$bundle +mkdir -p ${build_dir}/$bundle +quiet_pushd ${build_dir}/$bundle + +# First the info.json, for SwiftPM +cat > info.json < sbom.spdx.json <> sbom.spdx.json <> sbom.spdx.json <> sbom.spdx.json <> sbom.spdx.json <> sbom.spdx.json <> sbom.spdx.json < $swift_res_root/SDKSettings.json < scripts/setup-android-sdk.sh <<'EOF' +#!/usr/bin/env bash +# this script will setup the ndk-sysroot with links to the +# local installation indicated by ANDROID_NDK_HOME +set -e +if [ -z "${ANDROID_NDK_HOME}" ]; then + echo "$(basename $0): error: missing environment variable ANDROID_NDK_HOME" + exit 1 +fi + +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 + +#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 + 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 ${triplePath} ${ndk_sysroot}/usr/lib/${triple} + done +else + ndk_action="${ndk_re}copied" + cp -a ${ndk_prebuilt}/*/sysroot ${ndk_sysroot} +fi + +# link the NDK's clang resource directory +# e.g., ~/Library/Android/sdk/ndk/27.0.12077973/toolchains/llvm/prebuilt/darwin-x86_64/lib/clang/18 or /opt/homebrew/share/android-ndk/toolchains/llvm/prebuilt/darwin-x86_64/lib/clang/19 +ln -sf ${ndk_prebuilt}/*/lib/clang/* ${swift_resources}/usr/lib/swift/clang + +# copy each architecture's swiftrt.o into the sysroot, +# working around https://github.com/swiftlang/swift/pull/79621 +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 ${ndk_action} to Android NDK at ${ndk_prebuilt}" +EOF + +chmod +x scripts/setup-android-sdk.sh + +cat > swift-sdk.json <> swift-sdk.json <> swift-sdk.json <> swift-sdk.json < swift-toolset.json <|--swift-tag + |--swift-version ] + [--boringssl-version ] + [--clone-with-ssh] + [--source-dir ] + +Fetch all the sources required to build the fully statically linked Linux +SDK for Swift. Options are: + + --clone-with-ssh Use git-over-SSH rather than HTTPS where possible. + --source-dir Specify the path in which the sources should be checked + out. This directory will be created it if does not exist. + --swift-scheme + --swift-tag + --swift-version + Select the version of Swift to check out sources for. + If starts with "scheme:" or "tag:", it will + select a scheme or tag; otherwise it will be treated as + a version number. + --boringssl-version +EOF +} + +# Defaults +if [[ -z "${SWIFT_VERSION}" || ($SWIFT_VERSION != scheme:* && $SWIFT_VERSION != tag:*) ]]; then + SWIFT_VERSION=scheme:release/6.2 +fi +if [[ -z "${BORINGSSL_VERSION}" ]]; then + BORINGSSL_VERSION=fips-20220613 +fi + +clone_with_ssh=false +while [ "$#" -gt 0 ]; do + case "$1" in + --swift-scheme) + SWIFT_VERSION="scheme:$2"; shift ;; + --swift-tag) + SWIFT_VERSION="tag:$2"; shift ;; + --swift-version) + SWIFT_VERSION="$2"; shift ;; + --boringssl-version) + BORINGSSL_VERSION="$2"; shift ;; + --clone-with-ssh) + clone_with_ssh=true ;; + --source-dir) + source_dir="$2"; shift ;; + *) + usage; exit 0 ;; + esac + shift +done + +if [[ ! -z "$source_dir" ]]; then + mkdir -p "$source_dir" +else + source_dir=. +fi + +if [[ "$clone_with_ssh" == "true" ]]; then + github=git@github.com: + clone_arg=--clone-with-ssh +else + github=https://github.com/ + clone_arg=--clone +fi + +cd "$source_dir" + +# Fetch Swift +mkdir -p swift-project + +groupstart "Fetching Swift" +pushd swift-project >/dev/null + +[[ -d swift ]] || git clone ${github}swiftlang/swift.git +cd swift + +# Get its dependencies +header "Fetching Swift Dependencies" + +extra_args="--skip-history --all-repositories" +if [[ $SWIFT_VERSION == scheme:* ]]; then + utils/update-checkout ${clone_arg} --scheme ${SWIFT_VERSION#scheme:} ${extra_args} +elif [[ $SWIFT_VERSION == tag:* ]]; then + utils/update-checkout ${clone_arg} --tag ${SWIFT_VERSION#tag:} ${extra_args} +else + utils/update-checkout ${clone_arg} --tag swift-${SWIFT_VERSION}-RELEASE ${extra_args} +fi + +popd >/dev/null +groupend + +# Fetch BoringSSL +groupstart "Fetching BoringSSL" +[[ -d boringssl ]] || git clone https://boringssl.googlesource.com/boringssl +pushd boringssl >/dev/null 2>&1 +git checkout ${BORINGSSL_VERSION} +popd >/dev/null 2>&1 +groupend diff --git a/swift-ci/sdks/android/scripts/install-ndk.sh b/swift-ci/sdks/android/scripts/install-ndk.sh new file mode 100755 index 00000000..11a2130b --- /dev/null +++ b/swift-ci/sdks/android/scripts/install-ndk.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Install NDK +# +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2024 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +# +# ===----------------------------------------------------------------------=== + +set -e + +echo "Installing Android NDK" + +mkdir -p /usr/local/ndk +pushd /usr/local/ndk >/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..dc1aa43c --- /dev/null +++ b/swift-ci/sdks/android/scripts/install-swift.sh @@ -0,0 +1,61 @@ +#!/bin/bash +# +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Install Swift +# +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2024 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +# +# ===----------------------------------------------------------------------=== + +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/toolchain-vars.sh b/swift-ci/sdks/android/scripts/toolchain-vars.sh new file mode 100644 index 00000000..cb0945c2 --- /dev/null +++ b/swift-ci/sdks/android/scripts/toolchain-vars.sh @@ -0,0 +1,80 @@ +#!/bin/bash -e +# +# ===----------------------------------------------------------------------=== +# +# Swift Android SDK: Toolchain source variables +# +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2025 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +# +# ===----------------------------------------------------------------------=== + +# This script is meant to be sourced from another script that sets the +# SWIFT_VERSION environment variable to one of "scheme:release/6.2" or +# "tag:swift-6.2-RELEASE" and will get the latest builds for each build +# type. + +OS=$(echo $HOST_OS | tr -d '.') +# e.g., "swift-6.1-RELEASE" +# there is no latest-build.yml for releases, so we need to get it from the API +RELEASE_TAG=$(curl -fsSL https://www.swift.org/api/v1/install/releases.json | jq -r '.[-1].tag') +# e.g., "swift-6.1-release" +RELEASE_BRANCH=$(echo "${RELEASE_TAG}" | tr '[A-Z]' '[a-z]') + +if [[ $SWIFT_VERSION == tag:* ]]; then + SWIFT_TAG=${SWIFT_VERSION#tag:} + case "${SWIFT_TAG}" in + swift-*-RELEASE) + SWIFT_BRANCH=$(echo "${SWIFT_TAG}" | tr '[A-Z]' '[a-z]') + ;; + swift-6.*-DEVELOPMENT-SNAPSHOT-*) + # e.g., swift-6.2-DEVELOPMENT-SNAPSHOT-2025-05-15-a + SWIFT_BRANCH=${SWIFT_TAG//DEVEL*/branch} + ;; + swift-DEVELOPMENT-SNAPSHOT-*) + # e.g., swift-DEVELOPMENT-SNAPSHOT-2025-05-14-a + SWIFT_BRANCH=development + ;; + *) + echo "$0: invalid tag=${SWIFT_TAG}" + exit 1 + ;; + esac +elif [[ $SWIFT_VERSION == scheme:* ]]; then + echo "Building $SWIFT_VERSION with prebuilt Swift $RELEASE_TAG compiler" + BUILD_COMPILER=yes + echo "Branch scheme builds always build the Swift compiler from source and take much longer." +else + echo "Invalid Swift version=${SWIFT_VERSION}" + exit 1 +fi + +SWIFT_BASE=$SWIFT_TAG-$HOST_OS + +case $(arch) in + arm64|aarch64) + export OS_ARCH_SUFFIX=-aarch64 + ;; + amd64|x86_64) + export OS_ARCH_SUFFIX= + ;; + *) + echo "Unknown architecture $(arch)" + exit 1 + ;; +esac + + +case $BUILD_COMPILER in + 1|true|yes|YES) + export SWIFT_TOOLCHAIN_URL="https://download.swift.org/$RELEASE_BRANCH/$OS$OS_ARCH_SUFFIX/$RELEASE_TAG/$RELEASE_TAG-$HOST_OS$OS_ARCH_SUFFIX.tar.gz" + ;; + *) + export SWIFT_TOOLCHAIN_URL="https://download.swift.org/$SWIFT_BRANCH/$OS$OS_ARCH_SUFFIX/$SWIFT_TAG/$SWIFT_BASE$OS_ARCH_SUFFIX.tar.gz" + ;; +esac