2

I am trying to compile a C library to use it in my iOS project, and I want to embed bitcode.

I can successfully build static libraries targeting each arch. And those static library do contain bitcode (checked using otool), but the dynamic library doesn't contain bitcode. Why? Is bitcode not supported in dylib?

The library I am trying to build is xz. Here is the script

build_iOS()
{
    ARCH=$1

    if [ $ARCH == "i386" ] || [ $ARCH == "x86_64" ];
    then
        SDKROOT="$(xcodebuild -version -sdk iphonesimulator | grep -E '^Path' | sed 's/Path: //')"
    else
        SDKROOT="$(xcodebuild -version -sdk iphoneos | grep -E '^Path' | sed 's/Path: //')"
    fi

    export CC="$(xcrun -sdk iphoneos -find clang)"
    export CFLAGS="-fembed-bitcode -isysroot $SDKROOT -arch ${ARCH} -miphoneos-version-min=9.0"
    export LDFLAGS="-arch ${ARCH} -isysroot $SDKROOT"

    if [ $ARCH == "i386" ] || [ $ARCH == "x86_64" ];
    then
        ./configure --prefix=$XZPATH/build/iOS/$ARCH --host=i686-apple-darwin11 --disable-static --enable-shared
    else
        ./configure --prefix=$XZPATH/build/iOS/$ARCH --host=arm-apple-darwin --disable-static --enable-shared
    fi

    make && make install && make clean
}
build_iOS i386
build_iOS x86_64
build_iOS armv7
build_iOS armv7s
build_iOS arm64

Thanks!

Automatic
  • 70
  • 1
  • 9

2 Answers2

0

It looks like I cannot add bitcode to dylibs. I tried building several dylibs, then use otool -l path_to_dylib | grep bitcode to test if they contain any bitcode, all got nothing.

More evidence:

  • in Xcode(7.3.1), macOS (previously called OS X) targets don't have enable bitcode option in build settings
  • in the bitcode section of App Thinning, Apple didn't mentioned bitcode on macOS. Plus, App Thinning is only available on iOS, watchOS and tvOS.

I currently don't know why macOS apps don't have enable bitcode option in build setting. Maybe it's because Mac App store is not the only way for distributing mac apps? And people might copy one mac app from one mac app to another using USB sticks?

Automatic
  • 70
  • 1
  • 9
  • I am not sure about your answer, that dynamic libraries do not support bitcode. I found this answer, suggesting it does support bitcode: http://stackoverflow.com/a/38125767/2302437. Also adding `-fembed-bitcode` increases the filesize a lot, why is that? – electronix384128 Aug 31 '16 at 06:40
  • @BenMarten Well, I think bitcode could be supported in dylib too! But the experiments I have done in the time of the answer showed that I cannot extract bit code from the dylib. Also, after I tried to replace liblzma.a with liblzma.dylib, Xcode complained that liblzma.dylib doesn't contain bit code. Maybe I have done something wrong? – Automatic Aug 31 '16 at 16:00
  • If you want to check the bitcode, check out `ebcutil`: https://github.com/JDevlieghere/LibEBC. (Disclaimer: I'm the author of the library) – Jonas Mar 31 '17 at 06:35
0

I was not able to verify bitcode in my bitcode enabled dynamic library via cmd line tools (otool, file or clang). Also comparing the diff between bitcode and non-bitcode build showed no difference, except the filesize.

Interestingly when using the dynamic library withouth bitcode enabled in an actual app and archiving xcode will fail to archive when I use the non-bitcode build:

ld: bitcode bundle could not be generated because '...' was built without full bitcode. All frameworks and dylibs for bitcode must be generated from Xcode Archive or Install build for architecture armv7

When building the dylib with bitcode enabled the filesize increases a lot and also xcode does not fail on archiving a sample project with bitcode enabled. So I am pretty sure that bitcode must be included in the dynamic lib, although we haven't found a way to verify that via cmd line tools yet...

electronix384128
  • 6,625
  • 11
  • 45
  • 67