Cross compile GMP for Apple Silicon

Pierre Chatelier pierre at chachatelier.fr
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 =====================
#!/bin/sh


#command line tools path can be queried by xcode-select -p
CLT="/Library/Developer/CommandLineTools"
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/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk --target=x86_64-apple-darwin13.0.0"
export LDFLAGS=" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk"
./configure --enable-fat --build=x86_64-apple-darwin --host=x86_64-apple-darwin13.0.0
#build
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/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk --target=arm64-apple-darwin"
export LDFLAGS=" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -arch arm64"
./configure --enable-fat --build=x86_64-apple-darwin --host=arm64-apple-darwin
#build
make -j 12

#move arm64 build aside
mv .libs libs_arm64

======================= script to merge binaries of different architectures  =====================
#!/bin/sh

#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 ../libgmp.la .




More information about the gmp-discuss mailing list