What are the standard architecture names of mobile platforms, namely ARM-based, for autotools?
I've tried to build some C/C++ libs for iOS including arm64
, armv7
, and iOS simulators (i386
and x86_64
), but I always ended up having identical binaries for arm64
and x86_64-simulator
, which in turns fails the universal binary build by lipo
. I've tried to set the --build
and --host
switches to one of the following pairs
uname -r
, --host=arm64uname -r
, --host=x86_64-apple-darwinuname -r
To my surprise, running the following script,
## Environments
ScriptDir="$( cd "$( dirname "$0" )" && pwd )"
cd - &> /dev/null
# Exit the build pass if any command returns a non-zero value
#set -o errexit
# Echo commands
set -x
DARWIN=darwin`uname -r`
MIN_SDK_VERSION=8.0
IPHONEOS_SYSROOT=`xcrun --sdk iphoneos --show-sdk-path`
IPHONESIMULATOR_SYSROOT=`xcrun --sdk iphonesimulator --show-sdk-path`
# Verbose clang output
#CLANG_VERBOSE="--verbose"
CC=/usr/bin/clang
CXX=/usr/bin/clang
SILENCED_WARNINGS="-Wno-unused-local-typedef -Wno-unused-function"
STDLIB=libc++
CFLAGS="${CLANG_VERBOSE} ${SILENCED_WARNINGS} -DNDEBUG -g -O0 -pipe -fPIC -fcxx-exceptions"
CXXFLAGS="${CFLAGS} -std=c++11 -stdlib=${STDLIB}"
LDFLAGS="-stdlib=${STDLIB}"
LIBS="-lc++ -lc++abi"
PROTOC=`which protoc`
SOURCE_DIR="$ScriptDir"
PREFIX="$ScriptDir"/_build
if [ -d ${PREFIX} ]
then
rm -rf "${PREFIX}"
fi
mkdir -p "${PREFIX}/platform" &> /dev/null
## Functions
build_arch() {
HOST=$1
ARCH=$2
PLATFORM_CFLAGS=$3
PLATFORM_NAME=${ARCH}
SYSROOT=${IPHONEOS_SYSROOT}
ARCH_BITS=x86_64
if [[ $ARCH==armv7 || $ARCH==armv7s ]]; then
ARCH_BITS=i386
fi
CC="${CC}"
CFLAGS="${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch ${ARCH} -isysroot ${SYSROOT} ${PLATFORM_CFLAGS}" \
CXX="${CXX}" \
CXXFLAGS="${CXXFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch ${ARCH} -isysroot ${SYSROOT}" \
LDFLAGS="-arch ${ARCH} -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" \
LIBS="${LIBS}" \
./configure \
--without-gcc \
--disable-ld-version-script \
--build=x86_64-apple-${DARWIN} \
--host=${HOST} \
--with-protoc=${PROTOC} \
--disable-shared \
--prefix=${PREFIX} \
--exec-prefix=${PREFIX}/platform/${PLATFORM_NAME}
make -j8
make install
}
build_simulator() {
ARCH=$1
PLATFORM_CFLAGS=$2
HOST=${ARCH}-apple-${DARWIN}
PLATFORM_NAME=${ARCH}-simulator
SYSROOT=${IPHONESIMULATOR_SYSROOT}
CC="${CC}" \
CFLAGS="${CFLAGS} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch ${ARCH} -isysroot ${SYSROOT} ${PLATFORM_CFLAGS}" \
CXX="${CXX}" \
CXXFLAGS="${CXXFLAGS} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch ${ARCH} -isysroot ${SYSROOT}" \
LDFLAGS="-arch ${ARCH} -mios-simulator-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" \
LIBS="${LIBS}" \
./configure \
--without-gcc \
--disable-ld-version-script \
--build=x86_64-apple-${DARWIN} \
--host=${HOST} \
--with-protoc=${PROTOC} \
--disable-shared \
--prefix=${PREFIX} \
--exec-prefix=${PREFIX}/platform/${PLATFORM_NAME}
make -j8
make install
}
build_fat_lib() {
OUT=${PREFIX}/universal
mkdir -p ${OUT}
PLATFORM_ROOT=${PREFIX}/platform
LIPO=lipo
LIB=libprotobuf.a
${LIPO} ${PLATFORM_ROOT}/arm64/lib/${LIB} \
${PLATFORM_ROOT}/x86_64-simulator/lib/${LIB} \
-create \
-output ${OUT}/${LIB}
LIB_LITE=libprotobuf-lite.a
${LIPO} ${PLATFORM_ROOT}/arm64/lib/${LIB_LITE} \
${PLATFORM_ROOT}/x86_64-simulator/lib/${LIB_LITE} \
-create \
-output ${OUT}/${LIB_LITE}
}
## Build pass
cd ${SOURCE_DIR}
./autogen.sh
build_simulator x86_64 "-fembed-bitcode"
build_arch arm arm64 "-fembed-bitcode"
build_fat_lib
echo DONE!
, I end up getting complaints from lipo:
+ lipo /path/to/protobuf/_build/platform/arm64/lib/libprotobuf.a /path/to/protobuf/_build/platform/x86_64-simulator/lib/libprotobuf.a -create -output /path/to/protobuf/_build/universal/libprotobuf.a
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: /path/to/protobuf/_build/platform/arm64/lib/libprotobuf.a and /path/to/protobuf/_build/platform/x86_64-simulator/lib/libprotobuf.a have the same architectures (x86_64) and can't be in the same fat output file
My first impression is that I'm misusing architecture names, and I don't know where to get the standard names. Is that it?
Solved it myself.
It turns out that the real problem is not a wrong arch triplet, but an inappropriate compiler executable assigned to CC/CXX. You gotta use
SDK="iphoneos"
export CC=$(xcrun --find --sdk "${SDK}" clang)
export CXX=$(xcrun --find --sdk "${SDK}" clang++)
export CPP=$(xcrun --find --sdk "${SDK}" cpp)
GOTCHA #1
config.guess
as suggested by @JohnBolinger will always return
x86_64-apple-darwin18.7.0
This leads to the wrong x86_64 build that I had before.
GOTCHA #2
Observe the Xcode build log and you may find -target arm64-apple-ios13.1
as a Clang option, not to be confused with the autotool configure option --target
. This one is useless either, unfortunately.