]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/buildsrc/dlls.sh
bec5df23743ef4a37c1fefd9b8953fe5506d191f
[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 cjobs=$(($(nproc) + 1))
8
9 require () {
10         if ! dpkg -s "$1" >/dev/null 2>&1 ; then
11                 echo "$1 package is required. Please install it."
12                 exit -1
13         fi
14 }
15
16 prepare () {
17         case $target_arch in
18                 win32)    ARCH="i686" ;;
19                 win64)    ARCH="x86_64" ;;
20                 *)        echo "Invalid arch (win32 or win64)." && exit -1 ;;
21         esac
22
23         # Set directories
24         src_dir="$buildpath/src"
25         work_dir="$buildpath/work/$target_arch"
26         pkg_dir="$buildpath/pkg/$target_arch"
27         out_dir="$buildpath/out/$target_arch"
28
29         # Set arch vars
30         CHOST="$ARCH-w64-mingw32"
31         toolchain_file="$d0/toolchain-$CHOST.cmake"
32
33         # Fix for Debian package missing the Windows resource file
34         zlib1rc_file="$d0/zlib1.rc"
35
36         export LDFLAGS="-L$pkg_dir/lib"
37         export CPPFLAGS="-I$pkg_dir/include"
38
39         # Check dependencies
40         require libtool
41         require mingw-w64
42         require automake
43         require cmake
44         require nasm
45
46         set -ex
47
48         mkdir -p "$src_dir"
49         mkdir -p "$work_dir"
50         mkdir -p "$pkg_dir"
51 }
52
53 get_this_src () {
54         dir=$(find . -maxdepth 1 -type d -print | grep -m1 "^\./$1") || return 1
55         this_src="$src_dir/$dir"
56         this_ver="${dir##*-}"
57 }
58
59 fetch_source () {
60         cd "$src_dir"
61
62         if get_this_src "$1"; then
63                 echo "Source for $1 already exists."
64                 return 1
65         else
66                 echo "Getting source for $1..."
67                 apt source "$1"
68                 get_this_src "$1"
69                 return 0
70         fi
71 }
72
73 verlte () {
74         printf '%s\n%s' "$1" "$2" | sort -C -V
75 }
76
77 verlt () {
78         ! verlte "$2" "$1"
79 }
80
81 mkcd () {
82         mkdir -p "$1"
83         cd "$1"
84 }
85
86 build_zlib () {
87         if fetch_source zlib ; then
88                 echo "Fixing zlib prefix..."
89                 sed -i '/zlib PROPERTIES SUFFIX/i set_target_properties(zlib PROPERTIES PREFIX "")' "$this_src/CMakeLists.txt"
90
91                 # Debian source package is missing the win32 resource file for some reason,
92                 # so we add it ourselves.
93                 echo "Fixing zlib1.rc..."
94                 mkdir -p "$this_src/win32"
95                 cp "$zlib1rc_file" "$this_src/win32"
96         fi
97
98         mkcd "$work_dir/zlib"
99         cmake -DCMAKE_TOOLCHAIN_FILE="$toolchain_file"\
100               -DBUILD_SHARED_LIBS=ON \
101               -DCMAKE_INSTALL_PREFIX="$pkg_dir" \
102               -G"Unix Makefiles" "$this_src"
103         make -j${cjobs}
104         make install
105 }       
106
107 build_gmp () {
108         fetch_source gmp || true
109
110         mkcd "$work_dir/gmp"
111         "$this_src/configure" --prefix="$pkg_dir" \
112                               --host="$CHOST" \
113                               --with-pic \
114                               --enable-fat \
115                               --disable-static \
116                               --enable-shared
117         make -j${cjobs}
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 -j${cjobs}
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 -j${cjobs}
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 -j${cjobs}
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 -j${cjobs}
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 -j${cjobs}
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 -j${cjobs}
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 -j${cjobs}
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 -j${cjobs}
261         make install
262 }
263
264 build_all () {
265         build_zlib
266         build_gmp
267         build_libd0
268         build_libogg
269         build_libvorbis
270         build_libtheora
271         build_freetype
272         build_libpng16
273         build_libjpeg
274         build_curl
275 }
276
277 install () {
278         mkdir -p "$out_dir"
279
280         cp -v "$pkg_dir/bin/libgmp-10.dll" "$out_dir"
281         cp -v "$pkg_dir/bin/libd0_blind_id-0.dll" "$out_dir"
282         cp -v "$pkg_dir/bin/libd0_rijndael-0.dll" "$out_dir"
283         cp -v "$pkg_dir/bin/libogg.dll" "$out_dir"
284         cp -v "$pkg_dir/bin/libvorbis.dll" "$out_dir"
285         cp -v "$pkg_dir/bin/libvorbisenc.dll" "$out_dir"
286         cp -v "$pkg_dir/bin/libvorbisfile.dll" "$out_dir"
287         cp -v "$pkg_dir/bin/libtheora-0.dll" "$out_dir"
288         cp -v "$pkg_dir/bin/libfreetype.dll" "$out_dir/libfreetype-6.dll"
289         cp -v "$pkg_dir/bin/zlib1.dll" "$out_dir"
290         cp -v "$pkg_dir/bin/libpng16.dll" "$out_dir"
291         cp -v "$pkg_dir/bin/libjpeg-62.dll" "$out_dir/libjpeg.dll"
292         cp -v "$pkg_dir/bin/libcurl.dll" "$out_dir/libcurl-4.dll"
293
294         # Required for win32 builds
295         if [ "$ARCH" = "i686" ]; then
296                 cp -v "/usr/lib/gcc/i686-w64-mingw32/10-win32/libgcc_s_dw2-1.dll" "$out_dir"
297         fi
298
299         cd "$out_dir"
300         ${CHOST}-strip -s *.dll
301 }
302
303 clean () {
304         rm -rf "$buildpath/src"
305         rm -rf "$buildpath/work"
306         rm -rf "$buildpath/pkg"
307         rm -rf "$buildpath/out"
308 }
309
310 list () {
311         echo "Compilable libraries:"
312         echo
313         echo gmp
314         echo libd0
315         echo libogg
316         echo libvorbis
317         echo libtheora
318         echo freetype
319         echo zlib
320         echo libpng16
321         echo libjpeg
322         echo curl
323 }
324
325 usage () {
326         echo "Experimental Windows DLL cross-compiling for Xonotic"
327         echo "by z411"
328         echo
329         echo "usage: $0 <step> [build path] [arch]"
330         echo
331         echo "available steps (require arch):"
332         echo "  <library name>: build specified library"
333         echo "  build_all: build all libraries"
334         echo "  install: copy built DLLs into output directory"
335         echo "  all: do all the previous steps in order"
336         echo
337         echo "steps without arch:"
338         echo "  list: list all compilable libraries"
339         echo "  clean: delete all work"
340         echo
341         echo "arch can be:"
342         echo "  win32"
343         echo "  win64"
344 }
345
346 step=$1
347 buildpath=$2
348 target_arch=$3
349
350 case $step in
351         gmp)           prepare && build_gmp ;;
352         libd0)         prepare && build_libd0 ;;
353         libogg)        prepare && build_libogg ;;
354         libvorbis)     prepare && build_libvorbis ;;
355         libtheora)     prepare && build_libtheora ;;
356         freetype)      prepare && build_freetype ;;
357         zlib)          prepare && build_zlib ;;
358         libpng16)      prepare && build_libpng16 ;;
359         libjpeg)       prepare && build_libjpeg ;;
360         curl)          prepare && build_curl ;;
361         build_all)     prepare && build_all ;;
362         install)       prepare && install ;;
363         all)           prepare && build_all && install ;;
364         clean)         clean ;;
365         list)          list ;;
366         *)             usage ;;
367 esac