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