1#!/bin/sh
2#
3# Build Universal binaries on Mac OS X, thanks Ryan!
4#
5# Usage: ./configure CC="sh gcc-fat.sh" && make && rm -rf x86 x64
6
7# Intel 32-bit compiler flags (10.4 runtime compatibility)
8GCC_COMPILE_X86="gcc-4.0 -arch i386 -mmacosx-version-min=10.4 \
9-DMAC_OS_X_VERSION_MIN_REQUIRED=1040 \
10-nostdinc \
11-F/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks \
12-I/Developer/SDKs/MacOSX10.4u.sdk/usr/lib/gcc/i686-apple-darwin10/4.0.1/include \
13-isystem /Developer/SDKs/MacOSX10.4u.sdk/usr/include"
14
15GCC_LINK_X86="\
16-L/Developer/SDKs/MacOSX10.4u.sdk/usr/lib/gcc/i686-apple-darwin10/4.0.1 \
17-Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk"
18
19# Intel 64-bit compiler flags (10.5 runtime compatibility)
20GCC_COMPILE_X64="gcc-4.0 -arch x86_64 -mmacosx-version-min=10.5 \
21-DMAC_OS_X_VERSION_MIN_REQUIRED=1050 \
22-nostdinc \
23-F/Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks \
24-I/Developer/SDKs/MacOSX10.5.sdk/usr/lib/gcc/i686-apple-darwin10/4.0.1/include \
25-isystem /Developer/SDKs/MacOSX10.5.sdk/usr/include"
26
27GCC_LINK_X64="\
28-L/Developer/SDKs/MacOSX10.5.sdk/usr/lib/gcc/i686-apple-darwin10/4.0.1/x86_64 \
29-Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk"
30
31# Output both PowerPC and Intel object files
32args="$*"
33compile=yes
34link=yes
35while test x$1 != x; do
36    case $1 in
37        --version) exec gcc $1;;
38        -v) exec gcc $1;;
39        -V) exec gcc $1;;
40        -print-prog-name=*) exec gcc $1;;
41        -print-search-dirs) exec gcc $1;;
42        -E) GCC_COMPILE_X86="$GCC_COMPILE_X86 -E"
43            GCC_COMPILE_X64="$GCC_COMPILE_X64 -E"
44            compile=no; link=no;;
45        -c) link=no;;
46        -o) output=$2;;
47        *.c|*.cc|*.cpp|*.S) source=$1;;
48    esac
49    shift
50done
51if test x$link = xyes; then
52    GCC_COMPILE_X86="$GCC_COMPILE_X86 $GCC_LINK_X86"
53    GCC_COMPILE_X64="$GCC_COMPILE_X64 $GCC_LINK_X64"
54fi
55if test x"$output" = x; then
56    if test x$link = xyes; then
57        output=a.out
58    elif test x$compile = xyes; then
59        output=`echo $source | sed -e 's|.*/||' -e 's|\(.*\)\.[^\.]*|\1|'`.o
60    fi
61fi
62
63# Compile X86 32-bit
64if test x"$output" != x; then
65    dir=x86/`dirname $output`
66    if test -d $dir; then
67        :
68    else
69        mkdir -p $dir
70    fi
71fi
72set -- $args
73while test x$1 != x; do
74    if test -f "x86/$1" && test "$1" != "$output"; then
75        x86_args="$x86_args x86/$1"
76    else
77        x86_args="$x86_args $1"
78    fi
79    shift
80done
81$GCC_COMPILE_X86 $x86_args || exit $?
82if test x"$output" != x; then
83    cp $output x86/$output
84fi
85
86# Compile X86 32-bit
87if test x"$output" != x; then
88    dir=x64/`dirname $output`
89    if test -d $dir; then
90        :
91    else
92        mkdir -p $dir
93    fi
94fi
95set -- $args
96while test x$1 != x; do
97    if test -f "x64/$1" && test "$1" != "$output"; then
98        x64_args="$x64_args x64/$1"
99    else
100        x64_args="$x64_args $1"
101    fi
102    shift
103done
104$GCC_COMPILE_X64 $x64_args || exit $?
105if test x"$output" != x; then
106    cp $output x64/$output
107fi
108
109if test x"$output" != x; then
110    lipo -create -o $output x86/$output x64/$output
111fi
112