#!/usr/local/bin/env sh

# Script adapted from 
# http://latenitesoft.blogspot.com/2008/10/iphone-programming-tips-building-unix.html

# This script builds the ARM version of the libsyck YAML parser library, and then
# merges it with the i386 version generated via Fink. It requires the file
# /sw/lib/libsyck.a to exist in the Fink library folder.

# To use it, expand the syck-0.45.tar.gz file, copy this script inside 
# the new folder, and execute it.

# Save binary generated by Fink
mkdir -p lnsout
cp /sw/lib/libsyck.a lnsout/libsyck.a.i386

# Set up the environment for the ARM binary
export DEVROOT=/Developer/Platforms/iPhoneOS.platform/Developer
export SDKROOT=$DEVROOT/SDKs/iPhoneOS3.0.sdk
U_CC=$CC
U_CFLAGS=$CFLAGS
U_LD=$LD
U_LDFLAGS=$LDFLAGS
U_CPP=$CPP
U_CPPFLAGS=$CPPFLAGS
export CPPFLAGS="-I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/4.0.1/include/ -I$SDKROOT/usr/include/"
export CFLAGS="$CPPFLAGS -arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT"
export CPP="/usr/bin/cpp $CPPFLAGS"

# Configure and build
./configure CC=$DEVROOT/usr/bin/arm-apple-darwin9-gcc-4.0.1 LD=$DEVROOT/usr/bin/ld --host=arm-apple-darwin
make

# Move the result to a subfolder
cp lib/libsyck.a lnsout/libsyck.a.arm

# Create fat lib
$DEVROOT/usr/bin/lipo -arch arm lnsout/libsyck.a.arm -arch i386 lnsout/libsyck.a.i386 -create -output lnsout/libsyck.a
