Tuesday 15 May 2012

XCode and static libraries - auto-archiving the .a files for iOS and Mac

I was scratching my head figuring-out how to copy-out the .a files from an XCode static library build for iOS, so that I could distribute the static library variants easily to my customers.

For example, I had builds both for iOS and Simulator, and they were both going somewhere like this:

/Users/me/Library/Developer/Xcode/DerivedData/mylib_blahblahblahblah/Build/Intermediates/...
...ArchiveIntermediates/mylib/IntermediateBuildFilesPath/UninstalledProducts/libmylib.a

... which is a bit tricky to deal with!

The solution for me was to create a Run Script entry in the Build Phases for my Target under the XCode project/target settings...:

mkdir -p /Users/me/myfolder/$CONFIGURATION-$PLATFORM_NAME
cp -p $TARGET_BUILD_DIR/libmylib.a /Users/me/myfolder/$CONFIGURATION-$PLATFORM_NAME
if [ $CONFIGURATION eq Release ]
then
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip -S \
  /Users/me/myfolder/$CONFIGURATION-$PLATFORM_NAME/libmylib.a
fi

Note the strip command, which is essential as the script is always run before XCode's normal strip behaviour!

Note also that to generate a library for Release, for both iOS and simulator, you need to Build for Profile.

Here is the command-line I use to build it all automatically!

xcodebuild -project mylib.xcodeproj Release -sdk iphonesimulator clean
xcodebuild -project mylib.xcodeproj Release -sdk iphoneos clean
xcodebuild -project mylib.xcodeproj Release -sdk iphonesimulator
xcodebuild -project mylib.xcodeproj Release -sdk iphoneos

To configure an app to link-in the appropriate version of the (copied!) static library to match your app's Release/Debug configuration and target (iOS/Simulator), simply put this in the Other Linker Flags section of your app...:

/Users/me/myfolder/$CONFIGURATION-$PLATFORM_NAME/libmylib.a

No comments: