1#!/bin/sh
2#
3# Build a fat binary on Mac OS X, thanks Ryan!
4
5# Number of CPUs (for make -j)
6NCPU=`sysctl -n hw.ncpu`
7if test x$NJOB = x; then
8    NJOB=$NCPU
9fi
10
11# SDK path
12if test x$SDK_PATH = x; then
13    SDK_PATH=/Developer/SDKs
14fi
15
16# Generic, cross-platform CFLAGS you always want go here.
17CFLAGS="-O3 -g -pipe"
18
19# We dynamically load X11, so using the system X11 headers is fine.
20BASE_CONFIG_FLAGS="--build=`uname -p`-apple-darwin \
21--x-includes=/usr/X11R6/include --x-libraries=/usr/X11R6/lib"
22
23# PowerPC 32-bit compiler flags
24CONFIG_PPC="--host=powerpc-apple-darwin"
25CC_PPC="gcc-4.0"
26CXX_PPC="g++-4.0"
27BUILD_FLAGS_PPC="-arch ppc -mmacosx-version-min=10.4"
28
29# Intel 32-bit compiler flags
30CONFIG_X86="--host=i386-apple-darwin"
31CC_X86="gcc"
32CXX_X86="g++"
33BUILD_FLAGS_X86="-arch i386 -mmacosx-version-min=10.4"
34
35# Intel 64-bit compiler flags
36CONFIG_X64="--host=x86_64-apple-darwin"
37CC_X64="gcc"
38CXX_X64="g++"
39BUILD_FLAGS_X64="-arch x86_64 -mmacosx-version-min=10.6"
40
41#
42# Find the configure script
43#
44srcdir=`dirname $0`/..
45auxdir=$srcdir/build-scripts
46cd $srcdir
47
48allow_ppc="yes"
49which gcc-4.0 >/dev/null 2>/dev/null
50if [ "x$?" = "x1" ]; then
51    #echo "WARNING: Can't find gcc-4.0, which means you don't have Xcode 3."
52    #echo "WARNING: Therefore, we can't do PowerPC support."
53    allow_ppc="no"
54fi
55
56#
57# Figure out which phase to build:
58# all,
59# configure, configure-ppc, configure-x86, configure-x64
60# make, make-ppc, make-x86, make-x64, merge
61# install
62# clean
63if test x"$1" = x; then
64    phase=all
65else
66    phase="$1"
67fi
68case $phase in
69    all)
70        configure_ppc="$allow_ppc"
71        configure_x86="yes"
72        configure_x64="yes"
73        make_ppc="$allow_ppc"
74        make_x86="yes"
75        make_x64="yes"
76        merge="yes"
77        ;;
78    configure)
79        configure_ppc="$allow_ppc"
80        configure_x86="yes"
81        configure_x64="yes"
82        ;;
83    configure-ppc)
84        configure_ppc="$allow_ppc"
85        ;;
86    configure-x86)
87        configure_x86="yes"
88        ;;
89    configure-x64)
90        configure_x64="yes"
91        ;;
92    make)
93        make_ppc="$allow_ppc"
94        make_x86="yes"
95        make_x64="yes"
96        merge="yes"
97        ;;
98    make-ppc)
99        make_ppc="$allow_ppc"
100        ;;
101    make-x86)
102        make_x86="yes"
103        ;;
104    make-x64)
105        make_x64="yes"
106        ;;
107    merge)
108        merge="yes"
109        ;;
110    install)
111        install_bin="yes"
112        install_hdrs="yes"
113        install_lib="yes"
114        install_data="yes"
115        install_man="yes"
116        ;;
117    install-bin)
118        install_bin="yes"
119        ;;
120    install-hdrs)
121        install_hdrs="yes"
122        ;;
123    install-lib)
124        install_lib="yes"
125        ;;
126    install-data)
127        install_data="yes"
128        ;;
129    install-man)
130        install_man="yes"
131        ;;
132    clean)
133        clean_ppc="yes"
134        clean_x86="yes"
135        clean_x64="yes"
136        ;;
137    clean-ppc)
138        clean_ppc="yes"
139        ;;
140    clean-x86)
141        clean_x86="yes"
142        ;;
143    clean-x64)
144        clean_x64="yes"
145        ;;
146    *)
147        echo "Usage: $0 [all|configure[-ppc|-x86|-x64]|make[-ppc|-x86|-x64]|merge|install|clean[-ppc|-x86|-x64]]"
148        exit 1
149        ;;
150esac
151case `uname -p` in
152    *86)
153        native_path=x86
154        ;;
155    *powerpc)
156        native_path=ppc
157        ;;
158    x86_64)
159        native_path=x64
160        ;;
161    *)
162        echo "Couldn't figure out native architecture path"
163        exit 1
164        ;;
165esac
166
167#
168# Create the build directories
169#
170for dir in build build/ppc build/x86 build/x64; do
171    if test -d $dir; then
172        :
173    else
174        mkdir $dir || exit 1
175    fi
176done
177
178
179#
180# Build the PowerPC 32-bit binary
181#
182if test x$configure_ppc = xyes; then
183    (cd build/ppc && \
184     sh ../../configure $BASE_CONFIG_FLAGS $CONFIG_PPC CC="$CC_PPC" CXX="$CXX_PPC" CFLAGS="$CFLAGS $BUILD_FLAGS_PPC $CFLAGS_PPC" LDFLAGS="$BUILD_FLAGS_PPC $LFLAGS_PPC") || exit 2
185fi
186if test x$make_ppc = xyes; then
187    (cd build/ppc && make -j$NJOB) || exit 3
188fi
189#
190# Build the Intel 32-bit binary
191#
192if test x$configure_x86 = xyes; then
193    (cd build/x86 && \
194     sh ../../configure $BASE_CONFIG_FLAGS $CONFIG_X86 CC="$CC_X86" CXX="$CXX_X86" CFLAGS="$CFLAGS $BUILD_FLAGS_X86 $CFLAGS_X86" LDFLAGS="$BUILD_FLAGS_X86 $LFLAGS_X86") || exit 2
195fi
196if test x$make_x86 = xyes; then
197    (cd build/x86 && make -j$NJOB) || exit 3
198fi
199
200#
201# Build the Intel 64-bit binary
202#
203if test x$configure_x64 = xyes; then
204    (cd build/x64 && \
205     sh ../../configure $BASE_CONFIG_FLAGS $CONFIG_X64 CC="$CC_X64" CXX="$CXX_X64" CFLAGS="$CFLAGS $BUILD_FLAGS_X64 $CFLAGS_X64" LDFLAGS="$BUILD_FLAGS_X64 $LFLAGS_X64") || exit 2
206fi
207if test x$make_x64 = xyes; then
208    (cd build/x64 && make -j$NJOB) || exit 3
209fi
210
211#
212# Combine into fat binary
213#
214if test x$merge = xyes; then
215    output=.libs
216    sh $auxdir/mkinstalldirs build/$output
217    cd build
218    target=`find . -mindepth 4 -maxdepth 4 -type f -name '*.dylib' | head -1 | sed 's|.*/||'`
219    (lipo -create -o $output/$target `find . -mindepth 4 -maxdepth 4 -type f -name "*.dylib"` &&
220     ln -sf $target $output/libSDL.dylib &&
221     lipo -create -o $output/libSDL.a */build/.libs/libSDL.a &&
222     cp $native_path/build/.libs/libSDL.la $output &&
223     cp $native_path/build/.libs/libSDL.lai $output &&
224     cp $native_path/build/libSDL.la . &&
225     lipo -create -o $output/libSDLmain.a */build/.libs/libSDLmain.a &&
226     cp $native_path/build/.libs/libSDLmain.la $output &&
227     cp $native_path/build/.libs/libSDLmain.lai $output &&
228     cp $native_path/build/libSDLmain.la . &&
229     echo "Build complete!" &&
230     echo "Files can be found in the build directory.") || exit 4
231    cd ..
232fi
233
234#
235# Install
236#
237do_install()
238{
239    echo $*
240    $* || exit 5
241}
242if test x$prefix = x; then
243    prefix=/usr/local
244fi
245if test x$exec_prefix = x; then
246    exec_prefix=$prefix
247fi
248if test x$bindir = x; then
249    bindir=$exec_prefix/bin
250fi
251if test x$libdir = x; then
252    libdir=$exec_prefix/lib
253fi
254if test x$includedir = x; then
255    includedir=$prefix/include
256fi
257if test x$datadir = x; then
258    datadir=$prefix/share
259fi
260if test x$mandir = x; then
261    mandir=$prefix/man
262fi
263if test x$install_bin = xyes; then
264    do_install sh $auxdir/mkinstalldirs $bindir
265    do_install /usr/bin/install -c -m 755 build/$native_path/sdl-config $bindir/sdl-config
266fi
267if test x$install_hdrs = xyes; then
268    do_install sh $auxdir/mkinstalldirs $includedir/SDL
269    for src in $srcdir/include/*.h; do \
270        file=`echo $src | sed -e 's|^.*/||'`; \
271        do_install /usr/bin/install -c -m 644 $src $includedir/SDL/$file; \
272    done
273    do_install /usr/bin/install -c -m 644 $srcdir/include/SDL_config_macosx.h $includedir/SDL/SDL_config.h
274fi
275if test x$install_lib = xyes; then
276    do_install sh $auxdir/mkinstalldirs $libdir
277    do_install sh build/$native_path/libtool --mode=install /usr/bin/install -c  build/libSDL.la $libdir/libSDL.la
278    do_install sh build/$native_path/libtool --mode=install /usr/bin/install -c  build/libSDLmain.la $libdir/libSDLmain.la
279fi
280if test x$install_data = xyes; then
281    do_install sh $auxdir/mkinstalldirs $datadir/aclocal
282    do_install /usr/bin/install -c -m 644 $srcdir/sdl.m4 $datadir/aclocal/sdl.m4
283    do_install sh $auxdir/mkinstalldirs $libdir/pkgconfig
284    do_install /usr/bin/install -m 644 build/$native_path/sdl.pc $libdir/pkgconfig/sdl.pc
285fi
286if test x$install_man = xyes; then
287    do_install sh $auxdir/mkinstalldirs $mandir/man3
288    for src in $srcdir/docs/man3/*.3; do \
289        file=`echo $src | sed -e 's|^.*/||'`; \
290        do_install /usr/bin/install -c -m 644 $src $mandir/man3/$file; \
291    done
292fi
293
294#
295# Clean up
296#
297do_clean()
298{
299    echo $*
300    $* || exit 6
301}
302if test x$clean_ppc = xyes; then
303    do_clean rm -r build/ppc
304fi
305if test x$clean_x86 = xyes; then
306    do_clean rm -r build/x86
307fi
308if test x$clean_x64 = xyes; then
309    do_clean rm -r build/x64
310fi
311