Skip to content

Instantly share code, notes, and snippets.

@jooneyp
Forked from evands/combine_static_libraries.sh
Last active May 17, 2016 08:16
Show Gist options
  • Select an option

  • Save jooneyp/17369d1939f53bf38bbfc050dea26423 to your computer and use it in GitHub Desktop.

Select an option

Save jooneyp/17369d1939f53bf38bbfc050dea26423 to your computer and use it in GitHub Desktop.
Compile specified C sources into libraries and combine all in the current directory into a single, mult-architecture static library
#!/bin/sh
# Works on OS X El Capitan, 10.11.4
# Compile specified C sources into libraries and combine all in the current directory into a single static library
# It is hardcoded to compile sources on srcs variable, to the arm64, armv7, and armv7s architectures; this can easily be changed via the 'srcs/archs' variable at the top
# The script takes a single argument, which is the name of the final, combined library to be created.
#
# For example:
# => ios-combine-static-lib.sh libName
#
# Script by JooneyP, forked the gist of Evan Schoenberg, Regular Rate and Rhythm Software
# Thanks to Claudiu Ursache for his blog post at http://www.cvursache.com/2013/10/06/Combining-Multi-Arch-Binaries/ which detailed the technique automated by this script
#####
# $1 = Name of output archive
#####
if [ $# -eq 0 ]
then
echo "ERROR : Please specify lib[NAME]"
echo "example - ${0} libMylib"
exit 1
fi
srcs=(test1 test2)
archs=(arm64 armv7 armv7s)
echo "Making libraries for architecture [${archs[*]}]...\n"
for arch in ${archs[*]}
do
for src in ${srcs[*]}
do
make ARCH=${arch} SOURCE=${src} | grep error
done
done
libraries=(*.a)
libtool="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool"
echo "\nDone."
echo "Combining ${libraries[*]} INTO ${1}.a..."
# for library in ${libraries[*]}
# do
# lipo -info $library
# done
# Combine results of the same architecture into a library for that architecture
source_combined=""
for arch in ${archs[*]}
do
source_libraries=""
for library in ${libraries[*]}
do
source_libraries="${source_libraries} ${library}"
done
done
$libtool -v -static ${source_libraries} -o "${1}.a"
# Delete intermediate files
echo "Cleaning..."
for arch in ${archs[*]}
do
for src in ${srcs[*]}
do
rm lib${src}_${arch}.a
done
done
# Show info on the output library as confirmation
echo "\nProcess Done."
lipo -info $1.a
DEVROOT = /Applications/Xcode.app/Contents/Developer
SDKROOT = $(DEVROOT)/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk
TOOLROOT = $(DEVROOT)/Toolchains/XcodeDefault.xctoolchain
PPREFIX = .
CC = $(DEVROOT)/usr/bin/gcc
LD = $(DEVROOT)/usr/bin/ld
CXX = $(DEVROOT)/usr/bin/g++
CPP = $(TOOLROOT)/usr/bin/cpp
AR = $(TOOLROOT)/usr/bin/ar
AS = $(TOOLROOT)/usr/bin/as
NM = $(TOOLROOT)/usr/bin/nm
CXXCPP = $(TOOLROOT)/usr/bin/cpp
LDFLAGS = -arch $(ARCH) -pipe -no-cpp-precomp -isysroot $(SDKROOT) -L$(PPREFIX)/lib
CFLAGS = -arch $(ARCH) -pipe -no-cpp-precomp -isysroot $(SDKROOT) -I$(PPREFIX)/include
CXXFLAGS = -arch $(ARCH) -pipe -no-cpp-precomp -isysroot $(SDKROOT) -I$(PPREFIX)/include
$(SOURCE)_$(ARCH).o : $(SOURCE).c
$(CC) $(CFLAGS) -c $(SOURCE).c -o $(SOURCE)_$(ARCH).o $(LDFLAGS)
$(AR) rscv lib$(SOURCE)_$(ARCH).a $(SOURCE)_$(ARCH).o
@rm $(SOURCE)_$(ARCH).o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment