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