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