How do I xcodebuild a static library with Bitcode enabled?
With Xcode 8, I couldn't get OTHER_CFLAGS="-fembed-bitcode"
to work. I kept running into something along the lines of was built without full bitcode. All frameworks and dylibs for bitcode must be generated from Xcode Archive or Install build
when I tried to create an Archive build of an app containing my static framework.
What I was really looking for was this:
BITCODE_GENERATION_MODE=bitcode
I'm actually using a Run Script inside of an aggregate target, the full xcodebuild line looks like this (just for reference):
xcodebuild BITCODE_GENERATION_MODE=bitcode OTHER_CFLAGS="-fembed-bitcode" -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
Bitcode is a compile-time feature (not a link-time feature) which means that every .o file should contain an extra section called __bitcode when built with bitcode. You can confirm whether your binary is bitcode-compatible by running otool -l (my .o or .a file) | grep __LLVM
.
When you build normally, Xcode adds the build flag -fembed-bitcode-marker
to any clang invocation. This seems to be some sort of 'this is where bitcode would go, if bitcode was enabled' thing, and doesn't actually enable bitcode.
When you "Build & Archive", this flag is replaced by -fembed-bitcode
, which really does build a Bitcode-enabled binary.
There seems to be two ways to make xcodebuild
use -fembed-bitcode
:
- Use the 'archive' action, as in
xcodebuild -target LookbackSDK archive
instead ofxcodebuild -target LookbackSDK build
. This has the side-effect of putting binaries in your Xcode Organizer instead of thebuild/
folder, though you can work around that by using-exportArchive -archivePath ./build
(thanks @JensAyton) - Force usage of the flag by adding Other C Flags with
OTHER_CFLAGS="-fembed-bitcode"
. Yourxcodebuild
invocation would look something likexcodebuild OTHER_CFLAGS="-fembed-bitcode" -target LookbackSDK build
.
The latter is what I chose so that I don't have to change my build system, but it will generate warnings for every file, since now both -fembed-bitcode-marker
and -fembed-bitcode
are sent to clang. Luckilly the latter wins, generating a Bitcode-enabled library!
Resources
- Apple DevForums: Bitcode and Assembly?
- SO: iOS library to BitCode