Skip to content

Commit ab47e79

Browse files
committed
Support cross-compiling to aarch64-darwin from linux in CI
Build the android unit-test megazords with support for darwin-aarch64 to make it easier to run tests locally that use Nimbus, etc. Since our NSS CI doesn't have darwin-aarch64 artifacts, this patch builds NSPR/NSS directly similar to how libs/build-nss-ios.sh does.
1 parent 9d874b1 commit ab47e79

File tree

7 files changed

+124
-4
lines changed

7 files changed

+124
-4
lines changed

libs/build-all.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fi
1818
if [[ "${#}" -ne 1 ]]
1919
then
2020
echo "Usage:"
21-
echo "./build-all.sh [ios|android|desktop|darwin-x86-64]"
21+
echo "./build-all.sh [ios|android|desktop|darwin-x86-64|darwin-aarch64]"
2222
exit 1
2323
fi
2424

@@ -113,6 +113,9 @@ then
113113
elif [[ "${PLATFORM}" == "darwin-x86-64" ]]
114114
then
115115
./build-nss-desktop.sh "${NSS_SRC_PATH}" "${PLATFORM}"
116+
elif [[ "${PLATFORM}" == "darwin-aarch64" ]]
117+
then
118+
./build-nss-macos-cross.sh "${NSS_SRC_PATH}" "${PLATFORM}"
116119
else
117120
echo "Unrecognized platform"
118121
exit 1

libs/build-nss-macos-cross.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env bash
2+
3+
# This script cross-compiles NSS and NSPR for macOS from Linux.
4+
# It is specifically designed for darwin cross-compilation in CI.
5+
6+
set -euvx
7+
8+
if [[ "${#}" -lt 1 ]] || [[ "${#}" -gt 2 ]]
9+
then
10+
echo "Usage:"
11+
echo "./build-nss-macos-cross.sh <ABSOLUTE_SRC_DIR> [CROSS_COMPILE_TARGET]"
12+
exit 1
13+
fi
14+
15+
NSS_SRC_DIR=${1}
16+
CROSS_COMPILE_TARGET=${2:-darwin-aarch64}
17+
18+
# Set architecture-specific variables based on target
19+
if [[ "${CROSS_COMPILE_TARGET}" == "darwin-aarch64" ]]; then
20+
DIST_DIR=$(abspath "desktop/darwin-aarch64/nss")
21+
NSS_TARGET="aarch64-apple-darwin"
22+
GYP_ARCH="arm64"
23+
elif [[ "${CROSS_COMPILE_TARGET}" == "darwin-x86-64" ]]; then
24+
DIST_DIR=$(abspath "desktop/darwin-x86-64/nss")
25+
NSS_TARGET="x86_64-apple-darwin"
26+
GYP_ARCH="x64"
27+
else
28+
echo "Unsupported cross-compile target: ${CROSS_COMPILE_TARGET}"
29+
exit 1
30+
fi
31+
32+
if [[ -d "${DIST_DIR}" ]]; then
33+
echo "${DIST_DIR} folder already exists. Skipping build."
34+
exit 0
35+
fi
36+
37+
# Read toolchain configuration from ORG_GRADLE_PROJECT environment variables
38+
# These are set by cross-compile-setup.sh in CI
39+
RUST_ANDROID_PREFIX=$(echo "ORG_GRADLE_PROJECT_RUST_ANDROID_GRADLE_TARGET_${NSS_TARGET}" | tr '[:lower:]-' '[:upper:]_')
40+
41+
# Check that NSS_DIR is set to detect CI environment
42+
nss_dir_var="${RUST_ANDROID_PREFIX}_NSS_DIR"
43+
if [[ -z "${!nss_dir_var}" ]]; then
44+
echo "Error: ${nss_dir_var} is not set"
45+
echo "This script must be run in a CI environment with cross-compile-setup.sh sourced"
46+
exit 1
47+
fi
48+
49+
# Use toolchain configuration from environment
50+
eval "CC=\$${RUST_ANDROID_PREFIX}_CC"
51+
eval "AR=\$${RUST_ANDROID_PREFIX}_AR"
52+
eval "RANLIB=\$${RUST_ANDROID_PREFIX}_RANLIB"
53+
eval "STRIP=\$${RUST_ANDROID_PREFIX}_TOOLCHAIN_PREFIX/${NSS_TARGET}-strip"
54+
eval "CFLAGS=\$${RUST_ANDROID_PREFIX}_CFLAGS_${NSS_TARGET//-/_}"
55+
eval "LDFLAGS=\$${RUST_ANDROID_PREFIX}_LDFLAGS_${NSS_TARGET//-/_}"
56+
57+
# Build NSPR
58+
NSPR_BUILD_DIR=$(mktemp -d)
59+
pushd "${NSPR_BUILD_DIR}"
60+
"${NSS_SRC_DIR}"/nspr/configure \
61+
STRIP="${STRIP}" \
62+
RANLIB="${RANLIB}" \
63+
AR="${AR}" \
64+
AS="${AS:-${AR}}" \
65+
LD="${LD:-${AR}}" \
66+
CC="${CC}" \
67+
CCC="${CC}" \
68+
CFLAGS="${CFLAGS}" \
69+
LDFLAGS="${LDFLAGS}" \
70+
--target="${NSS_TARGET}" \
71+
--enable-64bit \
72+
--disable-debug \
73+
--enable-optimize
74+
make
75+
popd
76+
77+
# Build NSS using gyp
78+
NSS_BUILD_DIR=$(mktemp -d)
79+
rm -rf "${NSS_SRC_DIR}/nss/out"
80+
81+
gyp -f ninja "${NSS_SRC_DIR}/nss/nss.gyp" \
82+
--depth "${NSS_SRC_DIR}/nss/" \
83+
--generator-output=. \
84+
-DOS=mac \
85+
-Dnspr_lib_dir="${NSPR_BUILD_DIR}/dist/lib" \
86+
-Dnspr_include_dir="${NSPR_BUILD_DIR}/dist/include/nspr" \
87+
-Dnss_dist_dir="${NSS_BUILD_DIR}" \
88+
-Dnss_dist_obj_dir="${NSS_BUILD_DIR}" \
89+
-Dhost_arch="${GYP_ARCH}" \
90+
-Dtarget_arch="${GYP_ARCH}" \
91+
-Dstatic_libs=1 \
92+
-Ddisable_dbm=1 \
93+
-Dsign_libs=0 \
94+
-Denable_sslkeylogfile=0 \
95+
-Ddisable_tests=1 \
96+
-Ddisable_libpkix=1 \
97+
-Dpython=python3
98+
99+
GENERATED_DIR="${NSS_SRC_DIR}/nss/out/Release/"
100+
ninja -C "${GENERATED_DIR}"
101+
102+
# Assemble the DIST_DIR with relevant libraries and headers
103+
./copy-nss-libs.sh \
104+
"mac" \
105+
"${GYP_ARCH}" \
106+
"${DIST_DIR}" \
107+
"${NSS_BUILD_DIR}/lib" \
108+
"${NSPR_BUILD_DIR}/dist/lib" \
109+
"${NSS_BUILD_DIR}/public/nss" \
110+
"${NSPR_BUILD_DIR}/dist/include/nspr"
111+
112+
# Cleanup
113+
rm -rf "${NSS_BUILD_DIR}"
114+
rm -rf "${NSPR_BUILD_DIR}"

megazords/full/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ configurations {
4747
// For libmegazord, we copy the desktop libs from the
4848
// [rust-android-gradle plugin](https://github.com/mozilla/rust-android-gradle), which is
4949
// configurable via `local.properties`. The official packages are built in taskcluster include
50-
// `linux-x86-64` and `darwin-x86-64` and the list is controlled by
50+
// `linux-x86-64`, `darwin-x86-64`, and `darwin-aarch64` and the list is controlled by
5151
// taskcluster/kinds/module-build/kind.yml
5252
//
5353
// For libjnidispatch, we include all libraries included in the official JAR file.

taskcluster/kinds/module-build/kind.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ task-defaults:
3232
- [source, taskcluster/scripts/toolchain/setup-fetched-rust-toolchain.sh]
3333
- [source, taskcluster/scripts/toolchain/cross-compile-setup.sh]
3434
- [source, taskcluster/scripts/toolchain/copy-libs-dir.sh, libs]
35-
- [bash, '-c', 'echo "rust.targets=arm,arm64,x86_64,darwin-x86-64,linux-x86-64\n" > local.properties']
35+
- [bash, '-c', 'echo "rust.targets=arm,arm64,x86_64,darwin-x86-64,darwin-aarch64,linux-x86-64\n" > local.properties']
3636
gradlew:
3737
- ':{module_name}:assembleRelease'
3838
- ':{module_name}:publish'

taskcluster/scripts/toolchain/build-rust-toolchain.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cd "$VCS_PATH"
2828
rustup --version
2929
# cross-compilation targets
3030
rustup target add x86_64-apple-darwin
31+
rustup target add aarch64-apple-darwin
3132
rustup target add x86_64-pc-windows-gnu
3233
rustup target add x86_64-unknown-linux-musl
3334
rustup target add aarch64-unknown-linux-gnu

taskcluster/scripts/toolchain/cross-compile-setup.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ for TARGET in x86_64-apple-darwin aarch64-apple-darwin; do
3232
export ${RUST_ANDROID_PREFIX}_RANLIB=${CCTOOL_BIN}/${TARGET}-ranlib
3333
export ${RUST_ANDROID_PREFIX}_LD_LIBRARY_PATH=${CLANG_LIB}
3434
export ${RUST_ANDROID_PREFIX}_RUSTFLAGS="-C linker=${CLANG_BIN}/clang-20 -C link-arg=-fuse-ld=${CCTOOL_BIN}/${TARGET}-ld -C link-arg=-B -C link-arg=${CCTOOL_BIN} -C link-arg=-target -C link-arg=${TARGET} -C link-arg=-isysroot -C link-arg=${SYSROOT} -C link-arg=-Wl,-syslibroot,${SYSROOT} -C link-arg=-Wl,-dead_strip"
35-
export ${RUST_ANDROID_PREFIX}_CFLAGS_${TARGET//-/_}="-B ${CCTOOL_BIN} -target ${TARGET} -isysroot ${SYSROOT} -Wl,-syslibroot,${SYSROOT} -Wl,-dead_strip"
35+
export ${RUST_ANDROID_PREFIX}_CFLAGS_${TARGET//-/_}="-B ${CCTOOL_BIN} -target ${TARGET} -isysroot ${SYSROOT}"
36+
export ${RUST_ANDROID_PREFIX}_LDFLAGS_${TARGET//-/_}="-B ${CCTOOL_BIN} -target ${TARGET} -isysroot ${SYSROOT} -Wl,-syslibroot,${SYSROOT} -Wl,-dead_strip"
3637
export ${RUST_ANDROID_PREFIX}_BINDGEN_EXTRA_CLANG_ARGS="--sysroot ${SYSROOT}"
3738
done
3839

taskcluster/scripts/toolchain/desktop-macos.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ git submodule update --init
66
. ./taskcluster/scripts/toolchain/cross-compile-setup.sh
77
pushd libs
88
./build-all.sh darwin-x86-64
9+
./build-all.sh darwin-aarch64
910
popd
1011
mkdir -p "$UPLOAD_DIR"
1112
tar -czf "$UPLOAD_DIR"/macos.tar.gz libs/desktop

0 commit comments

Comments
 (0)