point at SDL2 for OS X and Windows.
[xonotic/xonotic.git] / misc / tools / all / xonotic.subr
1 handled=true
2 case "$cmd" in
3         update-maps)
4                 misc/tools/xonotic-map-compiler-autobuild download
5                 ;;
6         compile-map)
7                 if [ $# -eq 0 ] ; then
8                         msg "Usage: ./all compile-map mapname1 mapname2 mapname3"
9                         msg "For example: ./all compile-map dance xoylent"
10                 fi
11                 if ! [ -f "netradiant/install/q3map2" ] ; then
12                         msg "q3map2 needed! Building netradiant..."
13                         make -C netradiant BUILD=native install/q3map2
14                 fi
15                 for mapfile in "$@"; do
16                         mapfile="data/xonotic-maps.pk3dir/maps/$mapfile.map"
17                         if [ ! -f "$mapfile" ] ; then
18                                 msg "ERROR, $mapfile not found!"
19                         else
20                                 verbose measure_time misc/tools/xonotic-map-compiler-optionsfile "$mapfile"
21                         fi
22                 done
23                 ;;
24         compile)
25                 cleand0=false
26                 cleandp=false
27                 cleanqcc=false
28                 cleanqc=false
29                 compiled0=false
30                 debug=debug
31                 if [ -z "$CC" ]; then
32                         export CC="gcc"
33                 fi
34                 export CC="$CC -DSUPPORTIPV6"
35                 while :; do
36                         case "$1" in
37                                 -0)
38                                         compiled0=true
39                                         shift
40                                         ;;
41                                 -c)
42                                         cleand0=true
43                                         cleandp=true
44                                         cleanqcc=true
45                                         cleanqc=true
46                                         shift
47                                         ;;
48                                 -r|-p)
49                                         case "$1" in
50                                                 -p)
51                                                         debug=profile
52                                                         ;;
53                                                 -r)
54                                                         debug=release
55                                                         ;;
56                                         esac
57                                         export CC="$CC -g"
58                                         case "`$CC -dumpversion`" in
59                                                 [5-9]*|[1-9][0-9]*|4.[3-9]*|4.[1-9][0-9]*)
60                                                         # gcc 4.3 or higher
61                                                         # -march=native is broken < 4.3
62                                                         if $CC -mtune=native -march=native misc/tools/conftest.c -o conftest >/dev/null 2>&1; then
63                                                                 export CC="$CC -mtune=native -march=native"
64                                                         fi
65                                                         ;;
66                                         esac
67                                         if [ -n "$WE_HATE_OUR_USERS" ]; then
68                                                 export CC="$CC -fno-common"
69                                         fi
70                                         shift
71                                         ;;
72                                 *)
73                                         break
74                                         ;;
75                         esac
76                 done
77
78                 if [ x"`uname`" = x"Darwin" ]; then
79                         TARGETS="sv-$debug sdl-$debug"
80                         export CC="$CC -fno-reorder-blocks"
81                 else
82                         TARGETS="sv-$debug cl-$debug sdl-$debug"
83                 fi
84
85                 if [ $# -gt 0 ] && [ x"$1" = x"" ]; then
86                         # if we give the command make the arg "", it will surely fail (invalid filename),
87                         # so better handle it as an empty client option
88                         BAD_TARGETS=" "
89                         shift
90                 elif [ -n "$1" ]; then
91                         BAD_TARGETS=
92                         TARGETS_SAVE=$TARGETS
93                         TARGETS=
94                         for X in $1; do
95                                 case "$X" in
96                                         sdl)
97                                                 TARGETS="$TARGETS sdl-$debug"
98                                                 ;;
99                                         glx|wgl)
100                                                 TARGETS="$TARGETS cl-$debug"
101                                                 ;;
102                                         dedicated)
103                                                 TARGETS="$TARGETS sv-$debug"
104                                                 ;;
105                                         *)
106                                                 BAD_TARGETS="$BAD_TARGETS $X"
107                                                 ;;
108                                 esac
109                         done
110                         if [ -n "$TARGETS" ]; then # at least a valid client
111                                 shift
112                         else # no valid client, let's assume this option is not meant to be a client then
113                                 TARGETS=$TARGETS_SAVE
114                                 BAD_TARGETS=
115                         fi
116                 fi
117
118                 if [ -z "$MAKEFLAGS" ]; then
119                         ncpus=`getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null || echo 1`
120                         if [ $ncpus -gt 1 ]; then
121                                 MAKEFLAGS=-j$ncpus
122                         fi
123                         if [ -n "$WE_HATE_OUR_USERS" ]; then
124                                 MAKEFLAGS="$MAKEFLAGS DP_MAKE_TARGET=mingw"
125                         fi
126                 fi
127
128                 if [ -n "$WE_HATE_OUR_USERS" ]; then
129                         # win32: use SDL2
130                         MAKEFLAGS="$MAKEFLAGS SDL_CONFIG=$d0/misc/builddeps/win32/sdl/bin/sdl2-config"
131
132                         # win32: don't rely on jpeg includes
133                         MAKEFLAGS="$MAKEFLAGS LIB_JPEG= CFLAGS_LIBJPEG="
134                 fi
135
136                 if [ x"`uname`" = x"Darwin" ]; then
137                         # osx: use SDL2
138                         f=$d0/misc/buildfiles/osx/Xonotic.app/Contents/Frameworks
139                         MAKEFLAGS="$MAKEFLAGS SDLCONFIG_MACOSXCFLAGS=-I$f/SDL2.framework/Headers SDLCONFIG_MACOSXLIBS=-F$f SDLCONFIG_MACOSXLIBS+=-framework SDLCONFIG_MACOSXLIBS+=SDL2 SDLCONFIG_MACOSXLIBS+=-framework SDLCONFIG_MACOSXLIBS+=Cocoa SDLCONFIG_MACOSXLIBS+=-I$f/SDL2.framework/Headers SDLCONFIG_MACOSXSTATICLIBS=-F$f SDLCONFIG_MACOSXSTATICLIBS+=-framework SDLCONFIG_MACOSXSTATICLIBS+=SDL2 SDLCONFIG_MACOSXSTATICLIBS+=-framework SDLCONFIG_MACOSXSTATICLIBS+=Cocoa SDLCONFIG_MACOSXSTATICLIBS+=-I$f/SDL2.framework/Headers"
140                 fi
141
142                 # workaround ARM issue in DP's makefile.inc
143                 case `uname -m` in
144                         x86_64|*86)
145                                 ;;
146                         *)
147                                 MAKEFLAGS="$MAKEFLAGS CFLAGS_SSE= CFLAGS_SSE2="
148                                 ;;
149                 esac
150                 
151                 if ! verbose $CC misc/tools/conftest.c -o conftest; then
152                         msg ""
153                         msg "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
154                         msg "~~~~~~~~~~ COMPILER ~~~~~~~~~~"
155                         msg "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
156                         msg "~~~~~~~~~~~~~~_...._~~~~~~~~~~"
157                         msg "~~~~~~~~~~~,-'     \\\`-._~~~~~~"
158                         msg "~~~~~~~~~~/     --. ><  \\~~~~~"
159                         msg "~~~~~~~~~/      (*)> -<: \\~~~~"
160                         msg "~~~~~~~~~(     ^~-'  (*) )~~~~"
161                         msg "~~~~~~~~~\\        ^+-_/  |~~~~"
162                         msg "~~~~~~~~~~\\       {vvv}  |~~~~"
163                         msg "~~~~~~~~~~,\\    , {^^^},/~~~~~"
164                         msg "~~~~~~~~,/  \`---.....-'~~W~~~~"
165                         msg "~~~~~~,/   \\_____/_\\_W~~/~~~~~"
166                         msg "~~~~~/          /~~~\\__/~~~~~~"
167                         msg "~~~~/          /~~~~~~~~~~~~~~"
168                         msg "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
169                         msg "~~~~~~~ Y U NO COMPILE ~~~~~~~"
170                         msg "~~~~~~~~~~~~ CODE ~~~~~~~~~~~~"
171                         msg "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
172                         msg ""
173                         exit 1
174                 fi
175                 rm -f conftest
176
177                 verbose cd "$d0/d0_blind_id"
178                 if ! $compiled0; then
179                         # compilation of crypto library failed
180                         # use binaries then, if we can...
181                         mkdir -p .libs
182                         if [ -n "$WE_HATE_OUR_USERS" ]; then
183                                 verbose cp "$d0/misc/buildfiles/win32/libd0_blind_id"-* .libs/
184                                 verbose cp "$d0/misc/buildfiles/win32/libd0_rijndael"-* .libs/
185                                 verbose cp "$d0/misc/buildfiles/win32/libgmp"-* .libs/
186                         else
187                                 case "`uname`" in
188                                         Linux)
189                                                 case `uname -m` in
190                                                         x86_64)
191                                                                 # No cp commands, we want to use static linking instead.
192                                                                 export CC="$CC -I../../../../misc/builddeps/linux64/d0_blind_id/include"
193                                                                 export CC="$CC -L../../../../misc/builddeps/linux64/d0_blind_id/lib"
194                                                                 export CC="$CC -Wl,-rpath,../../../../misc/builddeps/linux64/d0_blind_id/lib"
195                                                                 export CC="$CC -I../../../../misc/builddeps/linux64/gmp/include"
196                                                                 export CC="$CC -L../../../../misc/builddeps/linux64/gmp/lib"
197                                                                 export CC="$CC -Wl,-rpath,../../../../misc/builddeps/linux64/gmp/lib"
198                                                                 MAKEFLAGS="$MAKEFLAGS DP_LINK_CRYPTO=shared DP_LINK_CRYPTO_RIJNDAEL=shared LIB_CRYPTO=../../../../misc/builddeps/linux64/d0_blind_id/lib/libd0_blind_id.a LIB_CRYPTO+=../../../../misc/builddeps/linux64/gmp/lib/libgmp.a LIB_CRYPTO_RIJNDAEL=../../../../misc/builddeps/linux64/d0_blind_id/lib/libd0_rijndael.a"
199                                                                 ;;
200                                                         *86)
201                                                                 # No cp commands, we want to use static linking instead.
202                                                                 export CC="$CC -I../../../../misc/builddeps/linux32/d0_blind_id/include"
203                                                                 export CC="$CC -L../../../../misc/builddeps/linux32/d0_blind_id/lib"
204                                                                 export CC="$CC -Wl,-rpath,../../../../misc/builddeps/linux32/d0_blind_id/lib"
205                                                                 export CC="$CC -I../../../../misc/builddeps/linux32/gmp/include"
206                                                                 export CC="$CC -L../../../../misc/builddeps/linux32/gmp/lib"
207                                                                 export CC="$CC -Wl,-rpath,../../../../misc/builddeps/linux32/gmp/lib"
208                                                                 MAKEFLAGS="$MAKEFLAGS DP_LINK_CRYPTO=shared DP_LINK_CRYPTO_RIJNDAEL=shared LIB_CRYPTO=../../../../misc/builddeps/linux32/d0_blind_id/lib/libd0_blind_id.a LIB_CRYPTO+=../../../../misc/builddeps/linux32/gmp/lib/libgmp.a LIB_CRYPTO_RIJNDAEL=../../../../misc/builddeps/linux32/d0_blind_id/lib/libd0_rijndael.a"
209                                                                 ;;
210                                                         *)
211                                                                 compiled0=true
212                                                                 ;;
213                                                 esac
214                                                 ;;
215                                         Darwin)
216                                                 verbose cp "$d0/misc/buildfiles/osx/Xonotic.app/Contents/MacOS/libd0_blind_id".* .libs/
217                                                 verbose cp "$d0/misc/buildfiles/osx/Xonotic.app/Contents/MacOS/libd0_rijndael".* .libs/
218                                                 ;;
219                                         *)
220                                                 compiled0=true
221                                                 ;;
222                                 esac
223                         fi
224                 fi
225                 if $compiled0; then
226                         if $cleand0; then
227                                 if [ -f Makefile ]; then
228                                         verbose make $MAKEFLAGS distclean
229                                 fi
230                         fi
231                         if ! [ -f Makefile ]; then
232                                 verbose sh autogen.sh
233                                 verbose ./configure
234                         fi
235                         verbose make $MAKEFLAGS
236                 fi
237
238                 verbose cd "$d0/gmqcc"
239                 if $cleanqcc; then
240                         verbose make $MAKEFLAGS clean
241                 fi
242                 if [ -n "$WE_HATE_OUR_USERS" ]; then
243                         verbose make $MAKEFLAGS gmqcc.exe
244                 else
245                         verbose make $MAKEFLAGS gmqcc
246                 fi
247
248                 verbose cd "$d0/data/xonotic-data.pk3dir"
249                 if $cleanqc; then
250                         verbose make QCC="../../../../gmqcc/gmqcc" "$@" $MAKEFLAGS clean
251                 fi
252                 verbose make QCC="../../../../gmqcc/gmqcc" "$@" $MAKEFLAGS
253                 # 4 levels up: data, xonotic-data, qcsrc, server
254
255                 verbose cd "$d0/darkplaces"
256                 if [ x"$BAD_TARGETS" = x" " ]; then
257                         $ECHO "Warning: invalid empty client, default clients will be used."
258                 fi
259                 if $cleandp; then
260                         verbose make $MAKEFLAGS clean
261                 fi
262                 for T in $TARGETS; do
263                         verbose make $MAKEFLAGS STRIP=: "$@" "$T"
264                 done
265                 for T in $BAD_TARGETS; do
266                         $ECHO "Warning: discarded invalid client $T."
267                 done
268
269                 verbose "$SELF" update-maps
270                 ;;
271         run)
272                 if [ -n "$WE_HATE_OUR_USERS" ]; then
273                         client=
274                         export PATH="$d0/misc/buildfiles/win32:$d0/d0_blind_id/.libs:$PATH"
275                 elif [ x"`uname`" = x"Darwin" ]; then
276                         export DYLD_LIBRARY_PATH="$d0/misc/buildfiles/osx/Xonotic.app/Contents/MacOS:$d0/d0_blind_id/.libs"
277                         export DYLD_FRAMEWORK_PATH="$d0/misc/buildfiles/osx/Xonotic.app/Contents/Frameworks"
278                         client=-sdl
279                 else
280                         export LD_LIBRARY_PATH="$d0/d0_blind_id/.libs"
281                         client=-sdl
282                 fi
283                 case "$1" in
284                         dedicated)
285                                 client=-$1
286                                 shift
287                                 ;;
288                         sdl|glx)
289                                 USE_RLWRAP=no
290                                 client=-$1
291                                 shift
292                                 ;;
293                         wgl)
294                                 USE_RLWRAP=no
295                                 client=
296                                 shift
297                                 ;;
298                 esac
299                 if ! [ -x "darkplaces/darkplaces$client" ]; then
300                         if [ -x "darkplaces/darkplaces$client.exe" ]; then
301                                 client=$client.exe
302                         else
303                                 $ECHO "Client darkplaces/darkplaces$client not found, aborting"
304                                 exit 1
305                         fi
306                 fi
307                 set -- "darkplaces/darkplaces$client" -xonotic "$@"
308
309                 # if pulseaudio is running: USE IT
310                 if [ -z "$SDL_AUDIODRIVER" ] && ! [ -n "$WE_HATE_OUR_USERS" ] && ! [ x"`uname`" = x"Darwin" ]; then
311                         if ps -C pulseaudio >/dev/null; then
312                                 if ldd /usr/lib/libSDL.so 2>/dev/null | grep pulse >/dev/null; then
313                                         export SDL_AUDIODRIVER=pulse
314                                 fi
315                         fi
316                 fi
317
318                 binary=$1
319
320                 if [ x"$USE_GDB" = x"yes" ]; then
321                         set -- gdb --args "$@"
322                         USE_RLWRAP=no
323                 elif [ x"$USE_GDB" = x"core" ]; then
324                         set -- gdb --batch -x savecore.gdb --args "$@"
325                         USE_RLWRAP=no
326                 elif which catchsegv >/dev/null 2>&1; then
327                         set -- catchsegv "$@"
328                 fi
329                 if [ x"$USE_RLWRAP" != x"no" ] && which rlwrap >/dev/null 2>&1; then
330                         set -- rlwrap -A -g '^quit' -q "\"" -s 10000 -S ']' -w 100 "$@"
331                 fi
332                 rm -f xonotic.core
333                 verbose measure_time "$@" || true
334                 if [ -f xonotic.core ]; then
335                         if yesno "The program has CRASHED. Do you want to examine the core dump?"; then
336                                 gdb "$binary" xonotic.core
337                         #elif yesno "You did not want to examine the core dump. Do you want to provide it - including your DarkPlaces checkout - to the Xonotic developers?"; then
338                         #       tar cvzf xonotic.core.tar.gz xonotic.core darkplaces/*.c darkplaces/*.h
339                         #       # somehow send it
340                         #       rm -f xonotic.core.tar.gz
341                         else
342                                 $ECHO "The core dump can be examined later by"
343                                 $ECHO "  gdb $binary xonotic.core"
344                         fi
345                         exit 1
346                 fi
347                 ;;
348         help)
349                 $ECHO "  $SELF compile [-c] [-r|-p] [-0] [sdl|glx|wgl|dedicated]"
350                 $ECHO "  $SELF update-maps"
351                 $ECHO "  $SELF run [sdl|glx|wgl|dedicated] options..."
352                 $ECHO "  $SELF compile-map mapname1 mapname2... (e.g. \"./all compile-map dance drain fuse\")"
353                 handled=false
354                 ;;
355         *)
356                 handled=false
357                 ;;
358 esac