Cross compile GMP for Apple Silicon

Pierre Chatelier pierre at
Sun Nov 22 21:15:34 UTC 2020

Here are scripts to cross-compile GMP from a x86_64 host for an arm64 system, and merge x86_64 and arm64 builds in a fat (universal) binary.

Those scripts are not perfect and will certainly be deprecated or broken some day, but it helps understand the process

Pierre Chatelier

======================= script to configure/compile each arch =====================

#command line tools path can be queried by xcode-select -p
export CC="${CLT}/usr/bin/clang"
export CXX="${CLT}/usr/bin/clang"
export LD="${CLT}/usr/bin/ld"
export AR="${CLT}/usr/bin/ar"
export AS="${CLT}/usr/bin/as"
export LIBTOOL="${CLT}/usr/bin/libtool"
export STRIP="${CLT}/usr/bin/strip"
export RANLIB="${CLT}/usr/bin/ranlib"
export NM="${CLT}/usr/bin/nm"
export STRIP="${CLT}/usr/bin/strip"

#cleanup x86_64 build
make clean
\rm -rf libs_x86_64

#configure x86_64 build (in my case explicit darwin13.0.0 instead of just darwin)
export CFLAGS=" -O2 -fno-stack-check -isysroot /Applications/ --target=x86_64-apple-darwin13.0.0"
export LDFLAGS=" -isysroot /Applications/"
./configure --enable-fat --build=x86_64-apple-darwin --host=x86_64-apple-darwin13.0.0
make -j 12

#move x86_64 build aside
mv .libs libs_x86_64

#cleanup arm64 build
make clean
\rm -rf libs_arm64

#configure arm64 build
export CFLAGS=" -O2 -fno-stack-check -isysroot /Applications/ --target=arm64-apple-darwin"
export LDFLAGS=" -isysroot /Applications/ -arch arm64"
./configure --enable-fat --build=x86_64-apple-darwin --host=arm64-apple-darwin
make -j 12

#move arm64 build aside
mv .libs libs_arm64

======================= script to merge binaries of different architectures  =====================

#cleanup .libs directory
\rm -rf .libs

#create fat libraries
mkdir .libs
lipo -create -output .libs/libgmp.10.dylib libs_x86_64/libgmp.10.dylib libs_arm64/libgmp.10.dylib
lipo -create -output .libs/libgmp.a libs_x86_64/libgmp.a libs_arm64/libgmp.a
cp -f libs_x86_64/libgmp.lai .libs/

#additional symbolic links
pushd .libs
ln -fs libgmp.10.dylib libgmp.dylib
ln -fs ../ .

More information about the gmp-discuss mailing list