#!/usr/bin/perl
# usage:
# - make sure $PREFIX and $VER are correct
# - Extract the boost source archive and cd into the extracted folder (e.g. "boost_1_40_0").
# - execute this script
# Written by mani from http://www.mani.de/backstage/?p=324
# Modified by Kevin Young

use strict;
use File::Basename;

my $PREFIX = "/Users/youngk/Downloads/build_external";
my $VER = "1_44";
my $GCC = "xgcc42-mt";

# compile boost for PPC 32-Bit:
# however: don't if you're using gcc4.2 upwards

#system("./bjam --prefix=$PREFIX --libdir=$PREFIX/libPPC32 --layout=versioned toolset=darwin-4.0 macosx-version=10.5 macosx-version-min=10.4 architecture=power address-model=32 link=static install");

# compile boost for PPC 64-Bit:
# same as for PPC 32-Bit

#system("./bjam --prefix=$PREFIX --libdir=$PREFIX/libPPC64 --layout=versioned toolset=darwin-4.0 macosx-version=10.5 macosx-version-min=10.4 architecture=power address-model=64 link=static install");

# compile boost for Intel 32-Bit:
# note the change of the toolset: toolset=darwin-4.2 for intel machines and gcc4.2

system("./bjam --prefix=$PREFIX --libdir=$PREFIX/libIntel32 --layout=versioned toolset=darwin-4.2 macosx-version=10.5 macosx-version-min=10.4 architecture=x86 address-model=32 link=static install");

# compile boost for Intel 64-Bit:

system("./bjam --prefix=$PREFIX --libdir=$PREFIX/libIntel64 --layout=versioned toolset=darwin-4.2 macosx-version=10.5 macosx-version-min=10.4 architecture=x86 address-model=64 link=static install");

#collect all boost libraries in libPPC32, libPPC64, libIntel32, and libIntel64:

system("mkdir $PREFIX/lib");

my %libs;
my @globLibs;
my $lib;

# For the following lines: PPC specific things are quoted out. See above why.

#@globLibs = glob("$PREFIX/libPPC32/libboost_*-$GCC-$VER.a");
#foreach $lib (@globLibs) {
#%libs = map { $_ => 1 } keys %libs, basename($lib);
#}
#@globLibs = glob("$PREFIX/libPPC64/libboost_*-$GCC-$VER.a");
#foreach $lib (@globLibs) {
#%libs = map { $_ => 1 } keys %libs, basename($lib);
#}
@globLibs = glob("$PREFIX/libIntel32/libboost_*-$GCC-$VER.a");
foreach $lib (@globLibs) {
%libs = map { $_ => 1 } keys %libs, basename($lib);
}
@globLibs = glob("$PREFIX/libIntel64/libboost_*-$GCC-$VER.a");
foreach $lib (@globLibs) {
%libs = map { $_ => 1 } keys %libs, basename($lib);
}

#iterate over all boost libraries and execute the appropriate lipo command to create universal libraries in lib:

my $cmd;
my $archs;
my $archLib;
foreach $lib (keys %libs) {
$archs = "";
$cmd = "lipo -create";
#$archLib = "$PREFIX/libPPC32/$lib";
#if (-e $archLib) {
#$cmd = $cmd." ".$archLib;
#$archs = $archs." ppc";
#}
#$archLib = "$PREFIX/libPPC64/$lib";
#if (-e $archLib) {
#$cmd = $cmd." ".$archLib;
#$archs = $archs." ppc64";
#}
$archLib = "$PREFIX/libIntel32/$lib";
if (-e $archLib) {
$cmd = $cmd." ".$archLib;
$archs = $archs." i386";
}
$archLib = "$PREFIX/libIntel64/$lib";
if (-e $archLib) {
$cmd = $cmd." ".$archLib;
$archs = $archs." x86_64";
}

$cmd = $cmd." -output $PREFIX/lib/$lib";

print("----------------------------------------------\nCreating universal$archs library $lib ...\n");
`$cmd`;
#	system("lipo -info $PREFIX/lib/$lib");
}
