Skip to content

Instantly share code, notes, and snippets.

@sakrist
Last active November 9, 2022 06:27
Show Gist options
  • Save sakrist/0ceaa98fb16535998020 to your computer and use it in GitHub Desktop.
Save sakrist/0ceaa98fb16535998020 to your computer and use it in GitHub Desktop.
GMP and MPFR compile for iOS
First need run build_gmp.sh then build_mpfr.sh
./build_gmp.sh -ios --min-ios-version 10.0
./build_mpfr.sh -ios --min-ios-version 10.0
./build_gmp.sh -osx --min-macosx-version 10.14
./build_mpfr.sh -osx --min-macosx-version 10.14
#!/bin/bash
#===============================================================================
# Filename: build_gmp.sh
# Created by Volodymyr Boichentsov on 18/09/2015.
# Copyright © 2015 3D4Medical. All rights reserved.
# Property of 3D4Medical.
#===============================================================================
#-emit-obj -fembed-bitcode -disable-llvm-optzns -O3
BUILD_IOS=
BUILD_OSX=
unknownParameter()
{
if [[ -n $2 && $2 != "" ]]; then
echo Unknown argument \"$2\" for parameter $1.
else
echo Unknown argument $1
fi
die
}
parseArgs()
{
while [ "$1" != "" ]; do
case $1 in
-ios)
BUILD_IOS=1
;;
-osx)
BUILD_OSX=1
;;
--min-ios-version)
if [ -n $2 ]; then
SDKVERSION=$2
shift
else
missingParameter $1
fi
;;
--min-macosx-version)
if [ -n $2 ]; then
SDKVERSION=$2
shift
else
missingParameter $1
fi
;;
*)
unknownParameter $1
;;
esac
shift
done
}
parseArgs $@
if [[ -z $BUILD_IOS && -z $BUILD_OSX ]]; then
BUILD_IOS=1
fi
CURRENT=`pwd`
# if [ -d "${CURRENT}/gmp/gmplib" ]; then
# exit
# fi
__pr="--print-path"
__name="xcode-select"
DEVELOPER=`${__name} ${__pr}`
GMP_VERSION="6.1.0"
if [[ -z $SDKVERSION ]]; then
SDKVERSION=`xcrun -sdk iphoneos --show-sdk-version`
fi
OSX_PLATFORM=`xcrun --sdk macosx --show-sdk-platform-path`
OSX_SDK=`xcrun --sdk macosx --show-sdk-path`
IPHONEOS_PLATFORM=`xcrun --sdk iphoneos --show-sdk-platform-path`
IPHONEOS_SDK=`xcrun --sdk iphoneos --show-sdk-path`
IPHONESIMULATOR_PLATFORM=`xcrun --sdk iphonesimulator --show-sdk-platform-path`
IPHONESIMULATOR_SDK=`xcrun --sdk iphonesimulator --show-sdk-path`
CLANG=`xcrun --sdk iphoneos --find clang`
CLANGPP=`xcrun --sdk iphoneos --find clang++`
BITCODE_FLAGS=" -disable-llvm-optzns -O3"
downloadGMP()
{
if [ ! -d "${CURRENT}/gmp" ]; then
echo "Downloading GMP"
curl -L -o "${CURRENT}/gmp-${GMP_VERSION}.tar.bz2" http://ftp.gnu.org/gnu/gmp/gmp-${GMP_VERSION}.tar.bz2
tar xfj "gmp-${GMP_VERSION}.tar.bz2"
mv gmp-${GMP_VERSION} gmp
rm "gmp-${GMP_VERSION}.tar.bz2"
fi
}
build()
{
ARCH=$1
SDK=$2
PLATFORM=$3
ARGS=$4
TYPE=$5
make clean &> "${CURRENT}/clean.log"
make distclean &> "${CURRENT}/clean.log"
export PATH="${PLATFORM}/Developer/usr/bin:${DEVELOPER}/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
if [ -d "gmplib-${ARCH}-${TYPE}" ] ; then
rm -rf "gmplib-${ARCH}-${TYPE}"
fi
mkdir "gmplib-${ARCH}-${TYPE}"
EXTRAS="-arch ${ARCH}"
if [ "${TYPE}" == "ios" ]; then
EXTRAS="$EXTRAS -miphoneos-version-min=${SDKVERSION} -no-integrated-as -target ${ARCH}-apple-darwin"
fi
if [ "${TYPE}" == "osx" ]; then
echo "macOS deployment target ${SDKVERSION}"
EXTRAS="$EXTRAS -mmacosx-version-min=${SDKVERSION} "
fi
if [ "${ARCH}" == "i386" ]; then
EXTRAS="${EXTRAS} -m32"
fi
CFLAGS=" ${BITCODE_FLAGS} -isysroot ${SDK} -Wno-error -Wno-implicit-function-declaration ${EXTRAS} -fvisibility=hidden"
echo "Configuring for ${ARCH} ${TYPE}..."
./configure --prefix="${CURRENT}/gmplib-${ARCH}-${TYPE}" CC="${CLANG} ${CFLAGS}" LDFLAGS="${CFLAGS}" CPP="${CLANG} -E" CPPFLAGS="${CFLAGS}" \
--host=x86_64-apple-darwin --disable-assembly --enable-static --disable-shared ${ARGS} &> "${CURRENT}/gmplib-${ARCH}-${TYPE}-configure.log"
echo "Make in progress for ${ARCH} ${TYPE}..."
make -j`sysctl -n hw.logicalcpu_max` &> "${CURRENT}/gmplib-${ARCH}-${TYPE}-build.log"
# if [ "${ARCH}" == "i386" ]; then
# echo "check in progress for ${ARCH}"
# make check &> "${CURRENT}/gmplib-${ARCH}-check.log"
# fi
echo "Install in progress for ${ARCH} ${TYPE}..."
make install &> "${CURRENT}/gmplib-${ARCH}-${TYPE}-install.log"
}
downloadGMP
cd gmp
CURRENT=`pwd`
echo "GMP $CURRENT"
if [[ -n $BUILD_IOS ]]; then
# build "armv7" "${IPHONEOS_SDK}" "${IPHONEOS_PLATFORM}" "" "ios"
# build "armv7s" "${IPHONEOS_SDK}" "${IPHONEOS_PLATFORM}" "" "ios"
build "arm64" "${IPHONEOS_SDK}" "${IPHONEOS_PLATFORM}" "" "ios"
# build "i386" "${IPHONESIMULATOR_SDK}" "${IPHONESIMULATOR_PLATFORM}" "" "ios"
build "x86_64" "${IPHONESIMULATOR_SDK}" "${IPHONESIMULATOR_PLATFORM}" "" "ios"
[ -d gmplib/ios ] || mkdir -p gmplib/ios
cp -r "${CURRENT}/gmplib-arm64-ios/include" gmplib/include
echo "Creating Fat library"
lipo \
"${CURRENT}/gmplib-arm64-ios/lib/libgmp.a" \
"${CURRENT}/gmplib-x86_64-ios/lib/libgmp.a" \
-create -output "${CURRENT}/gmplib/ios/libgmp.a"
# "${CURRENT}/gmplib-armv7-ios/lib/libgmp.a"
# "${CURRENT}/gmplib-i386-ios/lib/libgmp.a"
fi
if [[ -n $BUILD_OSX ]]; then
# build "i386" "${OSX_SDK}" "${OSX_PLATFORM}" "ABI=32" "osx"
build "x86_64" "${OSX_SDK}" "${OSX_PLATFORM}" "ABI=64" "osx"
[ -d gmplib/osx ] || mkdir -p gmplib/osx
cp -r "${CURRENT}/gmplib-x86_64-osx/include" gmplib/include
echo "Creating Fat library"
cp "${CURRENT}/gmplib-x86_64-osx/lib/libgmp.a" "${CURRENT}/gmplib/osx/libgmp.a"
# lipo \
# "${CURRENT}/gmplib-i386-osx/lib/libgmp.a" \
# "${CURRENT}/gmplib-x86_64-osx/lib/libgmp.a" \
# -create -output "${CURRENT}/gmplib/osx/libgmp.a"
fi
echo "Done with GMP!"
#!/bin/bash
#===============================================================================
# Filename: build_mpfr.sh
# Author: Volodymyr Boichentsov
# Copyright: (c) Copyright 2015 3D4Medical.com
# Licence: Only for 3D4Medical.com company needs
#===============================================================================
BUILD_IOS=
BUILD_OSX=
unknownParameter()
{
if [[ -n $2 && $2 != "" ]]; then
echo Unknown argument \"$2\" for parameter $1.
else
echo Unknown argument $1
fi
die
}
parseArgs()
{
while [ "$1" != "" ]; do
case $1 in
-ios)
BUILD_IOS=1
;;
-osx)
BUILD_OSX=1
;;
--min-ios-version)
if [ -n $2 ]; then
SDKVERSION=$2
shift
else
missingParameter $1
fi
;;
--min-macosx-version)
if [ -n $2 ]; then
SDKVERSION=$2
shift
else
missingParameter $1
fi
;;
*)
unknownParameter $1
;;
esac
shift
done
}
parseArgs $@
if [[ -z $BUILD_IOS && -z $BUILD_OSX ]]; then
BUILD_IOS=1
fi
CURRENT=`pwd`
# if [ -d "${CURRENT}/mpfr/mpfrlib" ]; then
# exit
# fi
__pr="--print-path"
__name="xcode-select"
DEVELOPER=`${__name} ${__pr}`
MPFR_VERSION="3.1.3"
if [[ -z $SDKVERSION ]]; then
SDKVERSION=`xcrun -sdk iphoneos --show-sdk-version`
fi
BITCODE_FLAGS=" -disable-llvm-optzns -O3"
if [ "${SDKVERSION}" == "9.0" ]; then
BITCODE_FLAGS=""
fi
OSX_PLATFORM=`xcrun --sdk macosx --show-sdk-platform-path`
OSX_SDK=`xcrun --sdk macosx --show-sdk-path`
IPHONEOS_PLATFORM=`xcrun --sdk iphoneos --show-sdk-platform-path`
IPHONEOS_SDK=`xcrun --sdk iphoneos --show-sdk-path`
IPHONESIMULATOR_PLATFORM=`xcrun --sdk iphonesimulator --show-sdk-platform-path`
IPHONESIMULATOR_SDK=`xcrun --sdk iphonesimulator --show-sdk-path`
CLANG=`xcrun --sdk iphoneos --find clang`
CLANGPP=`xcrun --sdk iphoneos --find clang++`
downloadMPFR()
{
if [ ! -d "${CURRENT}/mpfr" ]; then
echo "Downloading MPFR"
curl -L -o "${CURRENT}/mpfr-${MPFR_VERSION}.tar.bz2" http://ftp.gnu.org/gnu/mpfr/mpfr-${MPFR_VERSION}.tar.bz2
tar xfj "mpfr-${MPFR_VERSION}.tar.bz2"
mv mpfr-${MPFR_VERSION} mpfr
rm "mpfr-${MPFR_VERSION}.tar.bz2"
fi
}
build()
{
ARCH=$1
SDK=$2
PLATFORM=$3
ARGS=$4
TYPE=$5
make clean &> "${CURRENT}/clean.log"
make distclean &> "${CURRENT}/clean.log"
export PATH="${PLATFORM}/Developer/usr/bin:${DEVELOPER}/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
if [ -d "mpfrlib-${ARCH}-${TYPE}" ] ; then
rm -rf "mpfrlib-${ARCH}-${TYPE}"
fi
mkdir "mpfrlib-${ARCH}-${TYPE}"
EXTRAS="-arch ${ARCH}"
if [ "${TYPE}" == "ios" ]; then
EXTRAS="${EXTRAS} -miphoneos-version-min=${SDKVERSION} -no-integrated-as -target ${ARCH}-apple-darwin"
fi
if [ "${TYPE}" == "osx" ]; then
echo "macOS deployment target ${SDKVERSION}"
EXTRAS="${EXTRAS} -mmacosx-version-min=${SDKVERSION} "
fi
if [ "${ARCH}" == "i386" ]; then
EXTRAS="${EXTRAS} -m32"
fi
CFLAGS=" ${BITCODE_FLAGS} -isysroot ${SDK} -Wno-error -Wno-implicit-function-declaration ${EXTRAS} -fvisibility=hidden"
GMP_LIB_PATH="${CURRENT}/../gmp/gmplib-${ARCH}-${TYPE}"
GMP_PATH="${CURRENT}/../gmp"
echo "Configuring for ${ARCH} ${TYPE}..."
./configure --prefix="${CURRENT}/mpfrlib-${ARCH}-${TYPE}" CC="${CLANG} ${CFLAGS}" CPP="${CLANG} -E" --with-gmp="${GMP_LIB_PATH}" \
--host=x86_64-apple-darwin --disable-assembly --enable-static --disable-shared ${ARGS} &> "${CURRENT}/mpfrlib-${ARCH}-${TYPE}-configure.log"
echo "Make in progress for ${ARCH} ${TYPE}..."
make -j`sysctl -n hw.logicalcpu_max` &> "${CURRENT}/mpfrlib-${ARCH}-${TYPE}-build.log"
# if [ "${ARCH}" == "i386" ]; then
# echo "check in progress for ${ARCH}"
# make check &> "${CURRENT}/mpfrlib-${ARCH}-check.log"
# fi
echo "Install in progress for ${ARCH} ${TYPE}..."
make install &> "${CURRENT}/mpfrlib-${ARCH}-${TYPE}-install.log"
}
downloadMPFR
cd mpfr
CURRENT=`pwd`
if [[ -n $BUILD_IOS ]]; then
# build "armv7" "${IPHONEOS_SDK}" "${IPHONEOS_PLATFORM}" "" "ios"
# build "armv7s" "${IPHONEOS_SDK}" "${IPHONEOS_PLATFORM}" "" "ios"
build "arm64" "${IPHONEOS_SDK}" "${IPHONEOS_PLATFORM}" "" "ios"
# build "i386" "${IPHONESIMULATOR_SDK}" "${IPHONESIMULATOR_PLATFORM}" "" "ios"
build "x86_64" "${IPHONESIMULATOR_SDK}" "${IPHONESIMULATOR_PLATFORM}" "" "ios"
[ -d mpfrlib/ios ] || mkdir -p mpfrlib/ios
cp -r "${CURRENT}/mpfrlib-arm64-ios/include" mpfrlib/include
echo "Creating Fat library"
lipo \
"${CURRENT}/mpfrlib-arm64-ios/lib/libmpfr.a" \
"${CURRENT}/mpfrlib-x86_64-ios/lib/libmpfr.a" \
-create -output "${CURRENT}/mpfrlib/ios/libmpfr.a"
# "${CURRENT}/mpfrlib-armv7-ios/lib/libmpfr.a"
# "${CURRENT}/mpfrlib-i386-ios/lib/libmpfr.a"
fi
if [[ -n $BUILD_OSX ]]; then
# build "i386" "${OSX_SDK}" "${OSX_PLATFORM}" "" "osx"
build "x86_64" "${OSX_SDK}" "${OSX_PLATFORM}" "" "osx"
[ -d mpfrlib/osx ] || mkdir -p mpfrlib/osx
cp -r "${CURRENT}/mpfrlib-x86_64-osx/include" mpfrlib/include
echo "Creating Fat library"
cp "${CURRENT}/mpfrlib-x86_64-osx/lib/libmpfr.a" "${CURRENT}/mpfrlib/osx/libmpfr.a"
# lipo \
# "${CURRENT}/mpfrlib-i386-osx/lib/libmpfr.a" \
# "${CURRENT}/mpfrlib-x86_64-osx/lib/libmpfr.a" \
# -create -output "${CURRENT}/mpfrlib/osx/libmpfr.a"
fi
echo "Done with MPFR!"
@ivme
Copy link

ivme commented Jan 7, 2016

Thanks for the great script!

Note: I found that this script produced a gmp library that did not work with iPhone 5s and later (both physical devices and simulators) because of the switch to x86_64 architecture for iPhone 5s and higher. Although the script does compile a library for the x86_64 architecture and include that library in the universal binary it produces, the x86_64 library it produces is tagged for the OSX platform rather than the iPhoneOS platform (see this post). If you try to link against it in Xcode when building for iPhone 5s or 5s simulator or higher, you get a "building for iOS simulator, but linking in object file built for OSX, for architecture x86_64" error. The solution I found was to replace line 92 of the script with:

build "x84_64" "${IPHONESIMULATOR_SDK}" "${IPHONESIMULATOR_PLATFORM}"

I also changed all of the author's other references to "x64_86" with "x86_64." Not sure that that makes a difference, but I thought it was a typo.

After making this change I was able to link against the fat binary produced by this script with iPhone 5 and 6 Plus hardware and all simulators.

@westinpigott
Copy link

This script is clutch. Thanks so much!

@neteali
Copy link

neteali commented May 31, 2016

GMP script work fine with the ivme corrections.
MPFR script need a path without whitespace (example: /users/Documents/my projects/mprf)

@dangaffey
Copy link

Awesome stuff. Exactly what I needed.

@sakrist
Copy link
Author

sakrist commented Jul 20, 2016

@nijindal If you was thinking get some attention, you need mention name.

@ivme it's actually producing different arch armv7 and arm64 included in one lib. Maybe script is old for now.
Will see if I can share newer version.

thanks guys :)

@himamis
Copy link

himamis commented Mar 8, 2017

Awesome script, thank you very much!
One note though, it seems that the mpfr script doesn't use BITCODE variable in the CFLAGS, while gmp does.

@zwang
Copy link

zwang commented Aug 10, 2017

@sakrist thanks for the great script. Just want I need

@ivme thanks for the fix. It make sense.

Does any one got error after uploading the app to app store? I have a super simple app with the lib embed in the project. When I upload the app with "Include BitCode" checked to the app store, I got an email from app store like below:

We have discovered one or more issues with your recent delivery for "Stage". To process your delivery, the following issues must be corrected:

Invalid Bundle - The app cannot be processed because options not allowed to be embedded in bitcode are detected in the submission. It is likely that you are not building the app with the toolchain provided in Xcode. Rebuild your entire app with the latest GM Xcode and submit the app again.

If I have "Include BitCode" unchecked, then it will be fine.

My build env is iOS SDK 10.3, Xcode 8.3.3. With latest Mac OS X 10.12.6

I create a sample project here (the testGMPLibOnly branch): https://github.com/zwang/TestFactorize/tree/testGMPLibOnly

Thank you in advance for any help.

@podkovyrin
Copy link

Hey, @zwang! Have you solved the issue with bitcode? I'm facing the same issue

@DzmitrySmaliakou
Copy link

@zwang, @podkovyrin, Have you found a solution for bitcode error?

@podkovyrin
Copy link

@DmitrySmolyakov I had, take a look at the prepare command in the following podspec:
https://github.com/dashevo/dashsync-iOS/blob/master/bls-signatures-pod/bls-signatures-pod.podspec

@DzmitrySmaliakou
Copy link

@podkovyrin Thank you! I will take a look

@quaternionboy
Copy link

quaternionboy commented Apr 15, 2021

@sakrist thanks for the script . Looks awesome but I'm not able to generate GMP for iOS:

Downloading GMP
 % Total  % Received % Xferd Average Speed  Time  Time   Time Current
                 Dload Upload  Total  Spent  Left Speed
100 2435k 100 2435k  0   0  367k   0 0:00:06 0:00:06 --:--:-- 498k
GMP /Users/ferran/Documents/Maker/Projects/Metal Generative Framework/CGAL/GMP iOS/C/x/gmp
Configuring for arm64 ios...
Make in progress for arm64 ios...
Install in progress for arm64 ios...
Configuring for x86_64 ios...
Make in progress for x86_64 ios...
Install in progress for x86_64 ios...
cp: /Users/_/Documents/Maker/Projects/Metal Generative Framework/CGAL/GMP iOS/C/x/gmp/gmplib-arm64-ios/include: No such file or directory
Creating Fat library
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't open input file: /Users/_/Documents/Maker/Projects/Metal Generative Framework/CGAL/GMP iOS/C/x/gmp/gmplib-arm64-ios/lib/libgmp.a (No such file or directory)
Done with GMP!

I tried to fix it without success... Can you take a look, please?

@anatoliy281
Copy link

 Thank you, my good man!

@primetimer
Copy link

Anyone has a hint to use it on AppleSilicon: I get the error:
Building for iOS Simulator, but the linked library 'libgmp.a' was built for iOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment