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