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