]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/buildsrc/dlls.sh
demotc-race-record-extractor.sh: remove useless rc case (gametype name is always...
[xonotic/xonotic.git] / misc / buildsrc / dlls.sh
1 #!/bin/bash
2 #
3 # Xonotic DLL automatic cross-compile script
4 # by z411
5
6 d0=$(pwd)
7
8 require () {
9         if ! dpkg -s "$1" >/dev/null 2>&1 ; then
10                 echo "$1 package is required. Please install it."
11                 exit -1
12         fi
13 }
14
15 prepare () {
16         case $target_arch in
17                 win32)    ARCH="i686" ;;
18                 win64)    ARCH="x86_64" ;;
19                 *)        echo "Invalid arch (win32 or win64)." && exit -1 ;;
20         esac
21
22         # Set directories
23         src_dir="$buildpath/src"
24         work_dir="$buildpath/work/$target_arch"
25         pkg_dir="$buildpath/pkg/$target_arch"
26         out_dir="$buildpath/out/$target_arch"
27
28         # Set arch vars
29         CHOST="$ARCH-w64-mingw32"
30         toolchain_file="$d0/toolchain-$CHOST.cmake"
31
32         # Fix for Debian package missing the Windows resource file
33         zlib1rc_file="$d0/zlib1.rc"
34
35         export LDFLAGS="-L$pkg_dir/lib"
36         export CPPFLAGS="-I$pkg_dir/include"
37
38         # Check dependencies
39         require libtool
40         require mingw-w64
41         require automake
42         require cmake
43         require nasm
44
45         set -ex
46
47         mkdir -p "$src_dir"
48         mkdir -p "$work_dir"
49         mkdir -p "$pkg_dir"
50 }
51
52 get_this_src () {
53         dir=$(find . -maxdepth 1 -type d -print | grep -m1 "^\./$1") || return 1
54         this_src="$src_dir/$dir"
55         this_ver="${dir##*-}"
56 }
57
58 fetch_source () {
59         cd "$src_dir"
60
61         if get_this_src "$1"; then
62                 echo "Source for $1 already exists."
63                 return 1
64         else
65                 echo "Getting source for $1..."
66                 apt-get source -t=stable "$1"
67                 get_this_src "$1"
68                 return 0
69         fi
70 }
71
72 verlte () {
73         printf '%s\n%s' "$1" "$2" | sort -C -V
74 }
75
76 verlt () {
77         ! verlte "$2" "$1"
78 }
79
80 mkcd () {
81         mkdir -p "$1"
82         cd "$1"
83 }
84
85 build_zlib () {
86         if fetch_source zlib ; then
87                 echo "Fixing zlib prefix..."
88                 sed -i '/zlib PROPERTIES SUFFIX/i set_target_properties(zlib PROPERTIES PREFIX "")' "$this_src/CMakeLists.txt"
89
90                 # Debian source package is missing the win32 resource file for some reason,
91                 # so we add it ourselves.
92                 echo "Fixing zlib1.rc..."
93                 mkdir -p "$this_src/win32"
94                 cp "$zlib1rc_file" "$this_src/win32"
95         fi
96
97         mkcd "$work_dir/zlib"
98         cmake -DCMAKE_TOOLCHAIN_FILE="$toolchain_file"\
99               -DBUILD_SHARED_LIBS=ON \
100               -DCMAKE_INSTALL_PREFIX="$pkg_dir" \
101               -G"Unix Makefiles" "$this_src"
102         make
103         make install
104 }
105
106 build_gmp () {
107         fetch_source gmp || true
108
109         mkcd "$work_dir/gmp"
110         autoreconf -i "$this_src"
111         "$this_src/configure" --prefix="$pkg_dir" \
112                               --host="$CHOST" \
113                               --with-pic \
114                               --enable-fat \
115                               --disable-static \
116                               --enable-shared
117         make
118         make install
119 }
120
121 build_libd0 () {
122         if [[ -d "$src_dir/d0_blind_id" ]] ; then
123                 echo "Sources already exist."
124         else
125                 echo "Getting git sources for libd0..."
126                 cd "$src_dir"
127                 git clone https://gitlab.com/xonotic/d0_blind_id.git
128                 cd d0_blind_id
129                 ./autogen.sh
130                 sed -i '/libd0_blind_id_la_LDFLAGS/ s/$/ -no-undefined/' Makefile.am
131                 sed -i '/libd0_rijndael_la_LDFLAGS/ s/$/ -no-undefined/' Makefile.am
132         fi
133
134         mkcd "$work_dir/libd0"
135         "$src_dir/d0_blind_id/configure" --with-pic \
136                                          --prefix="$pkg_dir" \
137                                          --host="$CHOST"
138         make
139         make install
140 }
141
142 build_libogg() {
143         if fetch_source libogg ; then
144                 echo "Fixing win32 def files..."
145                 sed -i 's/^LIBRARY ogg$/LIBRARY libogg/' "$this_src/win32/ogg.def"
146         fi
147
148         mkcd "$work_dir/libogg"
149         cmake -DCMAKE_TOOLCHAIN_FILE="$toolchain_file" \
150               -DBUILD_SHARED_LIBS=ON \
151               -DCMAKE_INSTALL_PREFIX="$pkg_dir" \
152               -G"Unix Makefiles" "$this_src"
153         make
154         make install
155 }
156
157 build_libvorbis () {
158         if fetch_source libvorbis ; then
159                 echo "Fixing win32 def files..."
160                 sed -i 's/^LIBRARY$/LIBRARY libvorbis/' "$this_src/win32/vorbis.def"
161                 sed -i 's/^LIBRARY$/LIBRARY libvorbisenc/' "$this_src/win32/vorbisenc.def"
162                 sed -i 's/^LIBRARY$/LIBRARY libvorbisfile/' "$this_src/win32/vorbisfile.def"
163         fi
164
165         mkcd "$work_dir/libvorbis"
166         cmake -DCMAKE_TOOLCHAIN_FILE="$toolchain_file" \
167               -DBUILD_SHARED_LIBS=ON \
168               -DOGG_INCLUDE_DIR="$pkg_dir/include" \
169               -DOGG_LIBRARY="$pkg_dir/lib/libogg.dll.a" \
170               -DCMAKE_INSTALL_PREFIX="$pkg_dir" \
171               -G"Unix Makefiles" "$this_src"
172         make
173         make install
174 }
175
176 build_libtheora () {
177         if fetch_source libtheora ; then
178                 echo "Fixing mingw32 defs..."
179                 sed -i '1iLIBRARY libtheoradec' "$this_src/win32/xmingw32/libtheoradec-all.def"
180                 sed -i '1iLIBRARY libtheoraenc' "$this_src/win32/xmingw32/libtheoraenc-all.def"
181                 sed -i '/TH_VP31_QUANT_INFO/d' "$this_src/win32/xmingw32/libtheoraenc-all.def"
182                 sed -i '/TH_VP31_HUFF_CODES/d' "$this_src/win32/xmingw32/libtheoraenc-all.def"
183         fi
184
185         mkcd "$work_dir/libtheora"
186         "$this_src/autogen.sh"
187         "$this_src/configure" --host="$CHOST" \
188                               --prefix="$pkg_dir" \
189                               --with-ogg="$pkg_dir" \
190                               --with-vorbis="$pkg_dir" \
191                               --enable-shared \
192                               --disable-examples \
193                               --disable-sdltest \
194                               --disable-vorbistest \
195                               --disable-oggtest
196         make
197         make install
198 }
199
200 build_freetype () {
201         fetch_source freetype || true
202
203         mkcd "$work_dir/freetype"
204         cmake -DCMAKE_TOOLCHAIN_FILE="$toolchain_file" \
205               -DBUILD_SHARED_LIBS=ON \
206               -DCMAKE_BUILD_TYPE=Release \
207               -DCMAKE_INSTALL_PREFIX="$pkg_dir" \
208               -G"Unix Makefiles" "$this_src"
209         make
210         make install
211 }
212
213 build_libpng16 () {
214         fetch_source "libpng1.6" || true
215
216         mkcd "$work_dir/libpng1.6"
217         cmake -DCMAKE_TOOLCHAIN_FILE="$toolchain_file" \
218               -DPNG_STATIC=OFF \
219               -DPNG_TESTS=OFF \
220               -DZLIB_INCLUDE_DIR="$pkg_dir/include" \
221               -DZLIB_LIBRARY="$pkg_dir/lib/libzlib.dll.a" \
222               -DCMAKE_INSTALL_PREFIX="$pkg_dir" \
223               -G"Unix Makefiles" "$this_src"
224         make
225         make install
226 }
227
228 build_libjpeg () {
229         fetch_source libjpeg-turbo || true
230
231         mkcd "$work_dir/libjpeg"
232         cmake -DCMAKE_TOOLCHAIN_FILE="$toolchain_file" \
233               -DCMAKE_SYSTEM_PROCESSOR="$ARCH" \
234               -DCMAKE_INSTALL_PREFIX="$pkg_dir" \
235               -DENABLE_SHARED=ON \
236               -DENABLE_STATIC=OFF \
237               -DWITH_TURBOJPEG=OFF \
238               -G"Unix Makefiles" "$this_src"
239         make
240         make install
241 }
242
243 build_curl () {
244         fetch_source curl || true
245
246         # curl versions older than 7.81.0 used CMAKE instead of CURL for
247         # private USE identifiers
248         verlt $this_ver 7.81.0 && PARAM="CMAKE" || PARAM="CURL"
249
250         mkcd "$work_dir/curl"
251         cmake -DCMAKE_TOOLCHAIN_FILE="$toolchain_file" \
252               -DCMAKE_INSTALL_PREFIX="$pkg_dir" \
253               -DZLIB_INCLUDE_DIR="$pkg_dir/include" \
254               -DZLIB_LIBRARY="$pkg_dir/lib/libzlib.dll.a" \
255               -D${PARAM}_USE_SCHANNEL=ON \
256               -DBUILD_SHARED_LIBS=ON \
257               -DBUILD_CURL_EXE=OFF \
258               -DHTTP_ONLY=ON \
259               -G"Unix Makefiles" "$this_src"
260         make
261         make install
262 }
263
264 build_libsdl2 ()
265 {
266         fetch_source libsdl2 || true
267
268         # this subdir will be made available to DP's linker
269         mkdir -p "$pkg_dir/sdl"
270
271         mkcd "$work_dir/libsdl2"
272         cmake -DCMAKE_TOOLCHAIN_FILE="$toolchain_file" \
273               -DCMAKE_SYSTEM_PROCESSOR="$ARCH" \
274               -DCMAKE_INSTALL_PREFIX="$pkg_dir/sdl" \
275               -G"Unix Makefiles" "$this_src"
276         make
277         make install
278 }
279
280 build_all () {
281         build_zlib
282         build_gmp
283         build_libd0
284         build_libogg
285         build_libvorbis
286         build_libtheora
287         build_freetype
288         build_libpng16
289         build_libjpeg
290         build_curl
291         build_libsdl2
292 }
293
294 install () {
295         mkdir -p "$out_dir"
296
297         cp -v "$pkg_dir/bin/libgmp-10.dll" "$out_dir"
298         cp -v "$pkg_dir/bin/libd0_blind_id-0.dll" "$out_dir"
299         cp -v "$pkg_dir/bin/libd0_rijndael-0.dll" "$out_dir"
300         cp -v "$pkg_dir/bin/libogg.dll" "$out_dir"
301         cp -v "$pkg_dir/bin/libvorbis.dll" "$out_dir"
302         cp -v "$pkg_dir/bin/libvorbisenc.dll" "$out_dir"
303         cp -v "$pkg_dir/bin/libvorbisfile.dll" "$out_dir"
304         cp -v "$pkg_dir/bin/libtheora-0.dll" "$out_dir"
305         cp -v "$pkg_dir/bin/libfreetype.dll" "$out_dir/libfreetype-6.dll"
306         cp -v "$pkg_dir/bin/zlib1.dll" "$out_dir"
307         cp -v "$pkg_dir/bin/libpng16.dll" "$out_dir"
308         cp -v "$pkg_dir/bin/libjpeg-62.dll" "$out_dir/libjpeg.dll"
309         cp -v "$pkg_dir/bin/libcurl.dll" "$out_dir/libcurl-4.dll"
310 #       cp -v "$pkg_dir/sdl/bin/SDL2.dll" "$out_dir"
311
312         # Required for win32 builds
313         if [ "$ARCH" = "i686" ]; then
314                 cp -v /usr/lib/gcc/i686-w64-mingw32/[0-9][0-9]-win32/libgcc_s_dw2-1.dll "$out_dir"
315         fi
316
317         cd "$out_dir"
318         ${CHOST}-strip -s *.dll
319 }
320
321 clean () {
322         rm -rf "$buildpath/src"
323         rm -rf "$buildpath/work"
324         rm -rf "$buildpath/pkg"
325         rm -rf "$buildpath/out"
326 }
327
328 list () {
329         echo "Compilable libraries:"
330         echo
331         echo gmp
332         echo libd0
333         echo libogg
334         echo libvorbis
335         echo libtheora
336         echo freetype
337         echo zlib
338         echo libpng16
339         echo libjpeg
340         echo curl
341         echo libsdl2
342 }
343
344 usage () {
345         echo "Experimental Windows DLL cross-compiling for Xonotic"
346         echo "by z411"
347         echo
348         echo "usage: $0 <step> [build path] [arch]"
349         echo
350         echo "available steps (require arch):"
351         echo "  <library name>: build specified library"
352         echo "  build_all: build all libraries"
353         echo "  install: copy built DLLs into output directory"
354         echo "  all: do all the previous steps in order"
355         echo
356         echo "steps without arch:"
357         echo "  list: list all compilable libraries"
358         echo "  clean: delete all work"
359         echo
360         echo "arch can be:"
361         echo "  win32"
362         echo "  win64"
363 }
364
365 step=$1
366 buildpath=$2
367 target_arch=$3
368
369 case $step in
370         gmp)           prepare && build_gmp ;;
371         libd0)         prepare && build_libd0 ;;
372         libogg)        prepare && build_libogg ;;
373         libvorbis)     prepare && build_libvorbis ;;
374         libtheora)     prepare && build_libtheora ;;
375         freetype)      prepare && build_freetype ;;
376         zlib)          prepare && build_zlib ;;
377         libpng16)      prepare && build_libpng16 ;;
378         libjpeg)       prepare && build_libjpeg ;;
379         curl)          prepare && build_curl ;;
380         libsdl2)       prepare && build_libsdl2 ;;
381         build_all)     prepare && build_all ;;
382         install)       prepare && install ;;
383         all)           prepare && build_all && install ;;
384         clean)         clean ;;
385         list)          list ;;
386         *)             usage ;;
387 esac