Skip to content

Commit 7facd2b

Browse files
shivaspeaksAgraVator
authored andcommitted
Add support for macOS aarch64 with universal binary (#12319)
1 parent 9c7453d commit 7facd2b

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

buildscripts/kokoro/unix.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ ARCH="$ARCH" buildscripts/make_dependencies.sh
3939
# Set properties via flags, do not pollute gradle.properties
4040
GRADLE_FLAGS="${GRADLE_FLAGS:-}"
4141
GRADLE_FLAGS+=" -PtargetArch=$ARCH"
42+
43+
# For universal binaries on macOS, signal Gradle to use universal flags.
44+
if [[ "$(uname -s)" == "Darwin" ]]; then
45+
GRADLE_FLAGS+=" -PbuildUniversal=true"
46+
fi
4247
GRADLE_FLAGS+=" -Pcheckstyle.ignoreFailures=false"
4348
GRADLE_FLAGS+=" -PfailOnWarnings=true"
4449
GRADLE_FLAGS+=" -PerrorProne=true"

buildscripts/make_dependencies.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ else
4141
mkdir "$DOWNLOAD_DIR/protobuf-${PROTOBUF_VERSION}/build"
4242
pushd "$DOWNLOAD_DIR/protobuf-${PROTOBUF_VERSION}/build"
4343
# install here so we don't need sudo
44-
if [[ "$ARCH" == x86* ]]; then
44+
if [[ "$(uname -s)" == "Darwin" ]]; then
45+
cmake .. \
46+
-DCMAKE_CXX_STANDARD=14 -Dprotobuf_BUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF \
47+
-DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" -DABSL_INTERNAL_AT_LEAST_CXX17=0 \
48+
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
49+
-B. || exit 1
50+
elif [[ "$ARCH" == x86* ]]; then
4551
CFLAGS=-m${ARCH#*_} CXXFLAGS=-m${ARCH#*_} cmake .. \
4652
-DCMAKE_CXX_STANDARD=14 -Dprotobuf_BUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF \
4753
-DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" -DABSL_INTERNAL_AT_LEAST_CXX17=0 \

compiler/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ model {
103103
cppCompiler.args "--std=c++14"
104104
addEnvArgs("CXXFLAGS", cppCompiler.args)
105105
addEnvArgs("CPPFLAGS", cppCompiler.args)
106+
if (project.hasProperty('buildUniversal') &&
107+
project.getProperty('buildUniversal').toBoolean() &&
108+
osdetector.os == "osx") {
109+
cppCompiler.args "-arch", "arm64", "-arch", "x86_64"
110+
linker.args "-arch", "arm64", "-arch", "x86_64"
111+
}
106112
if (osdetector.os == "osx") {
107113
cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++"
108114
linker.args "-framework", "CoreFoundation"

compiler/check-artifact.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ checkArch ()
8686
fi
8787
fi
8888
elif [[ "$OS" == osx ]]; then
89-
format="$(file -b "$1" | grep -o "[^ ]*$")"
90-
echo Format=$format
91-
if [[ "$ARCH" == x86_32 ]]; then
92-
assertEq "$format" "i386" $LINENO
93-
elif [[ "$ARCH" == x86_64 ]]; then
94-
assertEq "$format" "x86_64" $LINENO
95-
elif [[ "$ARCH" == aarch_64 ]]; then
96-
assertEq "$format" "arm64" $LINENO
97-
else
98-
fail "Unsupported arch: $ARCH"
89+
# For macOS, we now build a universal binary. We check that both
90+
# required architectures are present.
91+
format="$(lipo -archs "$1")"
92+
echo "Architectures found: $format"
93+
if ! echo "$format" | grep -q "x86_64"; then
94+
fail "Universal binary is missing x86_64 architecture."
95+
fi
96+
if ! echo "$format" | grep -q "arm64"; then
97+
fail "Universal binary is missing arm64 architecture."
9998
fi
99+
echo "Universal binary check successful."
100100
else
101101
fail "Unsupported system: $OS"
102102
fi

0 commit comments

Comments
 (0)