1#!/bin/sh
2APPBUNDLE=ioquake3.app
3BINARY=ioquake3.ub
4DEDBIN=ioq3ded.ub
5PKGINFO=APPLIOQ3
6ICNS=misc/quake3.icns
7DESTDIR=build/release-darwin-ub
8BASEDIR=baseq3
9MPACKDIR=missionpack
10
11BIN_OBJ="
12	build/release-darwin-ppc/ioquake3-smp.ppc
13	build/release-darwin-i386/ioquake3-smp.i386
14"
15BIN_DEDOBJ="
16	build/release-darwin-ub/ioq3ded.ppc
17	build/release-darwin-i386/ioq3ded.i386
18"
19BASE_OBJ="
20	build/release-darwin-ppc/$BASEDIR/cgameppc.dylib
21	build/release-darwin-i386/$BASEDIR/cgamei386.dylib
22	build/release-darwin-ppc/$BASEDIR/uippc.dylib
23	build/release-darwin-i386/$BASEDIR/uii386.dylib
24	build/release-darwin-ppc/$BASEDIR/qagameppc.dylib
25	build/release-darwin-i386/$BASEDIR/qagamei386.dylib
26"
27MPACK_OBJ="
28	build/release-darwin-ppc/$MPACKDIR/cgameppc.dylib
29	build/release-darwin-i386/$MPACKDIR/cgamei386.dylib
30	build/release-darwin-ppc/$MPACKDIR/uippc.dylib
31	build/release-darwin-i386/$MPACKDIR/uii386.dylib
32	build/release-darwin-ppc/$MPACKDIR/qagameppc.dylib
33	build/release-darwin-i386/$MPACKDIR/qagamei386.dylib
34"
35
36cd `dirname $0`
37if [ ! -f Makefile ]; then
38	echo "This script must be run from the ioquake3 build directory"
39	exit 1
40fi
41
42Q3_VERSION=`grep '^VERSION=' Makefile | sed -e 's/.*=\(.*\)/\1/'`
43
44# We only care if we're >= 10.4, not if we're specifically Tiger.
45# "8" is the Darwin major kernel version.
46#TIGERHOST=`uname -r | grep ^8.`
47TIGERHOST=`uname -r |perl -w -p -e 's/\A(\d+)\..*\Z/$1/; $_ = (($_ >= 8) ? "1" : "0");'`
48
49# we want to use the oldest available SDK for max compatiblity
50unset PPC_CLIENT_SDK
51PPC_CLIENT_CC=gcc
52unset PPC_CLIENT_CFLAGS
53unset PPC_CLIENT_LDFLAGS
54unset PPC_SERVER_SDK
55unset PPC_SERVER_CFLAGS
56unset PPC_SERVER_LDFLAGS
57unset X86_SDK
58unset X86_CFLAGS
59unset X86_LDFLAGS
60if [ -d /Developer/SDKs/MacOSX10.5.sdk ]; then
61	PPC_CLIENT_SDK=/Developer/SDKs/MacOSX10.5.sdk
62	PPC_CLIENT_CC=gcc-4.0
63	PPC_CLIENT_CFLAGS="-arch ppc -isysroot /Developer/SDKs/MacOSX10.5.sdk \
64			-DMAC_OS_X_VERSION_MIN_REQUIRED=1050"
65	PPC_CLIENT_LDFLAGS="-arch ppc \
66			-isysroot /Developer/SDKs/MacOSX10.5.sdk \
67			-mmacosx-version-min=10.5"
68	PPC_SERVER_SDK=/Developer/SDKs/MacOSX10.5.sdk
69	PPC_SERVER_CFLAGS=$PPC_CLIENT_CFLAGS
70	PPC_SERVER_LDFLAGS=$PPC_CLIENT_LDFLAGS
71
72	X86_SDK=/Developer/SDKs/MacOSX10.5.sdk
73	X86_CFLAGS="-arch i386 -isysroot /Developer/SDKs/MacOSX10.5.sdk \
74			-DMAC_OS_X_VERSION_MIN_REQUIRED=1050"
75	X86_LDFLAGS="-arch i386 \
76			-isysroot /Developer/SDKs/MacOSX10.5.sdk \
77			-mmacosx-version-min=10.5"
78	X86_ENV="CFLAGS=$CFLAGS LDFLAGS=$LDFLAGS"
79fi
80
81if [ -d /Developer/SDKs/MacOSX10.4u.sdk ]; then
82	PPC_CLIENT_SDK=/Developer/SDKs/MacOSX10.4u.sdk
83	PPC_CLIENT_CC=gcc-4.0
84	PPC_CLIENT_CFLAGS="-arch ppc -isysroot /Developer/SDKs/MacOSX10.4u.sdk \
85			-DMAC_OS_X_VERSION_MIN_REQUIRED=1040"
86	PPC_CLIENT_LDFLAGS="-arch ppc \
87			-isysroot /Developer/SDKs/MacOSX10.4u.sdk \
88			-mmacosx-version-min=10.4"
89	PPC_SERVER_SDK=/Developer/SDKs/MacOSX10.4u.sdk
90	PPC_SERVER_CFLAGS=$PPC_CLIENT_CFLAGS
91	PPC_SERVER_LDFLAGS=$PPC_CLIENT_LDFLAGS
92
93	X86_SDK=/Developer/SDKs/MacOSX10.4u.sdk
94	X86_CFLAGS="-arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk \
95			-DMAC_OS_X_VERSION_MIN_REQUIRED=1040"
96	X86_LDFLAGS="-arch i386 \
97			-isysroot /Developer/SDKs/MacOSX10.4u.sdk \
98			-mmacosx-version-min=10.4"
99	X86_ENV="CFLAGS=$CFLAGS LDFLAGS=$LDFLAGS"
100fi
101
102if [ -d /Developer/SDKs/MacOSX10.3.9.sdk ] && [ $TIGERHOST ]; then
103	PPC_CLIENT_SDK=/Developer/SDKs/MacOSX10.3.9.sdk
104	PPC_CLIENT_CC=gcc-4.0
105	PPC_CLIENT_CFLAGS="-arch ppc -isysroot /Developer/SDKs/MacOSX10.3.9.sdk \
106			-DMAC_OS_X_VERSION_MIN_REQUIRED=1030"
107	PPC_CLIENT_LDFLAGS="-arch ppc \
108			-isysroot /Developer/SDKs/MacOSX10.3.9.sdk \
109			-mmacosx-version-min=10.3"
110	PPC_SERVER_SDK=/Developer/SDKs/MacOSX10.3.9.sdk
111	PPC_SERVER_CFLAGS=$PPC_CLIENT_CFLAGS
112	PPC_SERVER_LDFLAGS=$PPC_CLIENT_LDFLAGS
113fi
114
115if [ -d /Developer/SDKs/MacOSX10.2.8.sdk ] && [ -x /usr/bin/gcc-3.3 ] && [ $TIGERHOST ]; then
116	PPC_CLIENT_SDK=/Developer/SDKs/MacOSX10.2.8.sdk
117	PPC_CLIENT_CC=gcc-3.3
118	PPC_CLIENT_CFLAGS="-arch ppc \
119		-nostdinc \
120		-F/Developer/SDKs/MacOSX10.2.8.sdk/System/Library/Frameworks \
121		-I/Developer/SDKs/MacOSX10.2.8.sdk/usr/include/gcc/darwin/3.3 \
122		-isystem /Developer/SDKs/MacOSX10.2.8.sdk/usr/include \
123		-DMAC_OS_X_VERSION_MIN_REQUIRED=1020"
124	PPC_CLIENT_LDFLAGS="-arch ppc \
125		-L/Developer/SDKs/MacOSX10.2.8.sdk/usr/lib/gcc/darwin/3.3 \
126		-F/Developer/SDKs/MacOSX10.2.8.sdk/System/Library/Frameworks \
127		-Wl,-syslibroot,/Developer/SDKs/MacOSX10.2.8.sdk,-m"
128fi
129
130if [ -z $PPC_CLIENT_SDK ] || [ -z $PPC_SERVER_SDK ] || [ -z $X86_SDK ]; then
131	echo "\
132ERROR: This script is for building a Universal Binary.  You cannot build
133       for a different architecture unless you have the proper Mac OS X SDKs
134       installed.  If you just want to to compile for your own system run
135       'make' instead of this script."
136	exit 1
137fi
138
139echo "Building PPC Dedicated Server against \"$PPC_SERVER_SDK\""
140echo "Building PPC Client against \"$PPC_CLIENT_SDK\""
141echo "Building X86 Client/Dedicated Server against \"$X86_SDK\""
142if [ "$PPC_CLIENT_SDK" != "/Developer/SDKs/MacOSX10.2.8.sdk" ] || \
143	[ "$PPC_SERVER_SDK" != "/Developer/SDKs/MacOSX10.3.9.sdk" ] || \
144	[ "$X86_SDK" != "/Developer/SDKs/MacOSX10.4u.sdk" ]; then
145	echo "\
146WARNING: in order to build a binary with maximum compatibility you must
147         build on Mac OS X 10.4 using Xcode 2.3 or 2.5 and have the
148         MacOSX10.2.8, MacOSX10.3.9, and MacOSX10.4u SDKs installed
149         from the Xcode install disk Packages folder."
150fi
151sleep 3
152
153if [ ! -d $DESTDIR ]; then
154	mkdir -p $DESTDIR
155fi
156
157# For parallel make on multicore boxes...
158NCPU=`sysctl -n hw.ncpu`
159
160# ppc dedicated server
161echo "Building Dedicated Server using $PPC_SERVER_SDK"
162sleep 2
163if [ -d build/release-darwin-ppc ]; then
164	rm -r build/release-darwin-ppc
165fi
166(ARCH=ppc BUILD_CLIENT_SMP=0 BUILD_CLIENT=0 BUILD_GAME_VM=0 BUILD_GAME_SO=0 \
167	CFLAGS=$PPC_SERVER_CFLAGS LDFLAGS=$PPC_SERVER_LDFLAGS make -j$NCPU) || exit 1;
168cp build/release-darwin-ppc/ioq3ded.ppc $DESTDIR
169
170# ppc client
171if [ -d build/release-darwin-ppc ]; then
172	rm -r build/release-darwin-ppc
173fi
174(ARCH=ppc USE_OPENAL_DLOPEN=1 BUILD_SERVER=0 CC=$PPC_CLIENT_CC \
175	CFLAGS=$PPC_CLIENT_CFLAGS LDFLAGS=$PPC_CLIENT_LDFLAGS make -j$NCPU) || exit 1;
176
177# intel client and server
178if [ -d build/release-darwin-i386 ]; then
179	rm -r build/release-darwin-i386
180fi
181(ARCH=i386 CFLAGS=$X86_CFLAGS LDFLAGS=$X86_LDFLAGS make -j$NCPU) || exit 1;
182
183echo "Creating .app bundle $DESTDIR/$APPBUNDLE"
184if [ ! -d $DESTDIR/$APPBUNDLE/Contents/MacOS/$BASEDIR ]; then
185	mkdir -p $DESTDIR/$APPBUNDLE/Contents/MacOS/$BASEDIR || exit 1;
186fi
187if [ ! -d $DESTDIR/$APPBUNDLE/Contents/MacOS/$MPACKDIR ]; then
188	mkdir -p $DESTDIR/$APPBUNDLE/Contents/MacOS/$MPACKDIR || exit 1;
189fi
190if [ ! -d $DESTDIR/$APPBUNDLE/Contents/Resources ]; then
191	mkdir -p $DESTDIR/$APPBUNDLE/Contents/Resources
192fi
193cp $ICNS $DESTDIR/$APPBUNDLE/Contents/Resources/ioquake3.icns || exit 1;
194echo $PKGINFO > $DESTDIR/$APPBUNDLE/Contents/PkgInfo
195echo "
196	<?xml version=\"1.0\" encoding=\"UTF-8\"?>
197	<!DOCTYPE plist
198		PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"
199		\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
200	<plist version=\"1.0\">
201	<dict>
202		<key>CFBundleDevelopmentRegion</key>
203		<string>English</string>
204		<key>CFBundleExecutable</key>
205		<string>$BINARY</string>
206		<key>CFBundleGetInfoString</key>
207		<string>ioquake3 $Q3_VERSION</string>
208		<key>CFBundleIconFile</key>
209		<string>ioquake3.icns</string>
210		<key>CFBundleIdentifier</key>
211		<string>org.ioquake.ioquake3</string>
212		<key>CFBundleInfoDictionaryVersion</key>
213		<string>6.0</string>
214		<key>CFBundleName</key>
215		<string>ioquake3</string>
216		<key>CFBundlePackageType</key>
217		<string>APPL</string>
218		<key>CFBundleShortVersionString</key>
219		<string>$Q3_VERSION</string>
220		<key>CFBundleSignature</key>
221		<string>$PKGINFO</string>
222		<key>CFBundleVersion</key>
223		<string>$Q3_VERSION</string>
224		<key>NSExtensions</key>
225		<dict/>
226		<key>NSPrincipalClass</key>
227		<string>NSApplication</string>
228	</dict>
229	</plist>
230	" > $DESTDIR/$APPBUNDLE/Contents/Info.plist
231
232lipo -create -o $DESTDIR/$APPBUNDLE/Contents/MacOS/$BINARY $BIN_OBJ
233lipo -create -o $DESTDIR/$APPBUNDLE/Contents/MacOS/$DEDBIN $BIN_DEDOBJ
234rm $DESTDIR/ioq3ded.ppc
235cp $BASE_OBJ $DESTDIR/$APPBUNDLE/Contents/MacOS/$BASEDIR/
236cp $MPACK_OBJ $DESTDIR/$APPBUNDLE/Contents/MacOS/$MPACKDIR/
237cp code/libs/macosx/*.dylib $DESTDIR/$APPBUNDLE/Contents/MacOS/
238
239