Tuesday, 10 November 2009

Creating an universal static OpenCV library in (Snow) Leopard

More that a year ago i wrote a post about creating a fat OpenCV 1.0.0 static lib for OS X 10.5.

It seems it attracted some attention and it seems that some people found it useful. Unfortunately I have not been working directly with OpenCV for quite a lot of time, until recently, when I found myself in need of recompiling it for Snow Leopard.

So I created for myself a shell script that neatly compiles OpenCV 1.0.0 (of which you can download the source from Sourceforge). It creates the libs for Leopard or Snow Leopard with i386, x86_64 and ppc architectures.

I have no time to contribute the iPhone part but if someone is willing to submit it I will add it to the script.

To use it first, unpack the OpenCV 1.0.0 sources in a folder, open a terminal window and cd to the folder you unpacked the sources: the 'ls' command will list a 'opencv-1.0.0' directory in it. Then select the text below and in the terminal launch the 'pico opencv-build-mac.sh' command, paste the text, exit pico (with ctrl-x) and execute with 'bash opencv-build-mac.sh'.

# Download opencv-1.0.0.tar.gz from http://sourceforge.net/projects/opencvlibrary/
# Unpack it in the same directory where this file resides.
# Execute this file from a terminal with the command "bash opencv-build-mac.sh"
# Compiles for Leopard by default.

#set -x


# Leopard
SYSROOT="-isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5"

# Snow Leopard
#SYSROOT="-isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6"


CONFFLAGS="--without-imageio --without-python --without-swig --disable-apps \
--disable-dependency-tracking --without-carbon --without-quicktime \
--enable-shared=no --without-gtk"

# i686
build_i686() {

rm -rf $I686DIR
mkdir -p $I686DIR
pushd $I686DIR

${SRCDIR}/configure ${CONFFLAGS} CXXFLAGS="${SYSROOT} -arch i686" --target=i686-apple-darwin10 || exit 1
make || exit 1

# x86_64
build_x86_64 () {

rm -rf $X86_64DIR
mkdir -p $X86_64DIR
pushd $X86_64DIR

${SRCDIR}/configure ${CONFFLAGS} CXXFLAGS="${SYSROOT} -arch x86_64" --target=x86_64-apple-darwin10 || exit 1
make || exit 1

# ppc
build_ppc () {

rm -rf $PPCDIR
mkdir -p $PPCDIR
pushd $PPCDIR

${SRCDIR}/configure ${CONFFLAGS} CXXFLAGS="${SYSROOT} -arch ppc" --host=i686-apple-darwin10 --target=ppc-apple-darwin10 || exit 1
make || exit 1

# creating fat lib
create_fat_lib() {

lipo -create $I686DIR/cv/src/.libs/libcv.a \
$X86_64DIR/cv/src/.libs/libcv.a \
$PPCDIR/cv/src/.libs/libcv.a \
-output libcv${SUFFIX}.a || exit 1

lipo -create $I686DIR/cxcore/src/.libs/libcxcore.a \
$X86_64DIR/cxcore/src/.libs/libcxcore.a \
$PPCDIR/cxcore/src/.libs/libcxcore.a \
-output libcxcore${SUFFIX}.a || exit 1

lipo -create $I686DIR/cvaux/src/.libs/libcvaux.a \
$X86_64DIR/cvaux/src/.libs/libcvaux.a \
$PPCDIR/cvaux/src/.libs/libcvaux.a \
-output libcvaux${SUFFIX}.a || exit 1

lipo -create $I686DIR/ml/src/.libs/libml.a \
$X86_64DIR/ml/src/.libs/libml.a \
$PPCDIR/ml/src/.libs/libml.a \
-output libml${SUFFIX}.a || exit 1

lipo -create $I686DIR/otherlibs/highgui/.libs/libhighgui.a \
$X86_64DIR/otherlibs/highgui/.libs/libhighgui.a \
$PPCDIR/otherlibs/highgui/.libs/libhighgui.a \
-output libhighgui${SUFFIX}.a || exit 1

# main

rm *.a