xref: /freebsd/contrib/ldns/makewin.sh (revision d44c9549ef31884ac069b0306bad248a74bb2825)
1#!/usr/bin/env bash
2
3# compile ldns for windows
4cdir="$(echo ldns.win.$$)"
5tmpdir=$(pwd)
6mkdir "$cdir"
7cd "$cdir"
8#configure="mingw32-configure"
9#strip="i686-w64-mingw32-strip"
10#warch="i686"
11configure="mingw64-configure"
12strip="x86_64-w64-mingw32-strip"
13warch="x86_64"
14WINSSL="$HOME/Downloads/openssl-1.1.0h.tar.gz"
15cross_flag=""
16cross_flag_nonstatic=""
17RC="no"
18SNAPSHOT="no"
19CHECKOUT=""
20GITREPO=""
21# the destination is a zipfile in the start directory ldns-a.b.c.zip
22# the start directory is a git repository, and it is copied to build from.
23
24info () {
25	echo "info: $1"
26}
27
28error_cleanup () {
29	echo "$1"
30	cd "$tmpdir"
31	rm -rf "$cdir"
32	exit 1
33}
34
35replace_text () {
36    (cp "$1" "$1".orig && \
37        sed -e "s/$2/$3/g" < "$1".orig > "$1" && \
38        rm "$1".orig) || error_cleanup "Replacement for $1 failed."
39}
40
41# Parse command line arguments
42while [ "$1" ]; do
43    case "$1" in
44	"-h")
45		echo "Compile a zip file with static executables, and"
46		echo "dynamic library, static library, include dir and"
47		echo "manual pages."
48		echo ""
49		echo "	-h		This usage information."
50		echo "	-s		snapshot, current date appended to version"
51		echo "	-rc <nr>	release candidate, the number is added to version"
52		echo "			ldns-<version>rc<nr>."
53		echo "	-u git_url	Retrieve the source from the specified repository url."
54		echo "			Detected from the working copy if not specified."
55		echo "	-c <tag/br>	Checkout this tag or branch, (defaults to current"
56		echo "			branch)."
57		echo "	-wssl <file>	Pass openssl.tar.gz file, use absolute path."
58		echo ""
59		exit 1
60		;;
61	"-c")
62		CHECKOUT="$2"
63		shift
64		;;
65	"-s")
66		SNAPSHOT="yes"
67		;;
68	"-rc")
69		RC="$2"
70		shift
71		;;
72	"-u")
73		GITREPO="$2"
74		shift
75		;;
76	"-wssl")
77		WINSSL="$2"
78		shift
79		;;
80	*)
81		error_cleanup "Unrecognized argument -- $1"
82		;;
83    esac
84    shift
85done
86if [ -z "$CHECKOUT" ]
87then
88        if [ "$RC" = "no" ]
89        then
90                CHECKOUT=$( (git status | head -n 1 | awk '{print$3}') || echo master)
91        else
92                CHECKOUT=$( (git status | head -n 1 | awk '{print$3}') || echo develop)
93        fi
94fi
95if [ -z "$GITREPO" ]
96then
97	GITREPO=`git config remote.origin.url`
98fi
99
100# this script creates a temp directory $cdir.
101# this directory contains subdirectories:
102# ldns/ : ldns source compiled
103# openssl-a.b.c/ : the openSSL source compiled
104# ldnsinstall/ : install of ldns here.
105# sslinstall/ : install of ssl here.
106# file/ : directory to gather the components of the zipfile distribution
107# ldns-nonstatic/ : ldns source compiled nonstatic
108# ldnsinstall-nonstatic/ : install of ldns nonstatic compile
109# openssl-nonstatic/ : nonstatic openssl source compiled
110# sslinstall-nonstatic/ : install of nonstatic openssl compile
111
112info "exporting source into $cdir/ldns"
113git clone "$GITREPO" ldns || error_cleanup "git command failed"
114(cd ldns; git checkout "$CHECKOUT") || error_cleanup "Could not checkout $CHECKOUT"
115#svn export . $cdir/ldns
116info "exporting source into $cdir/ldns-nonstatic"
117git clone "$GITREPO" ldns-nonstatic || error_cleanup "git command failed"
118(cd ldns-nonstatic; git checkout "$CHECKOUT") || error_cleanup "Could not checkout $CHECKOUT"
119#svn export . $cdir/ldns-nonstatic
120
121# Fix up the version number if necessary
122(cd ldns; if test ! -f install-sh -a -f ../../install-sh; then cp ../../install-sh . ; fi; libtoolize -ci; autoreconf -fi)
123version=$(./ldns/configure --version | head -1 | awk '{ print $3 }') || \
124    error_cleanup "Cannot determine version number."
125info "LDNS version: $version"
126if [ "$RC" != "no" ]; then
127	info "Building LDNS release candidate $RC."
128	version2="${version}-rc$RC"
129	info "Version number: $version2"
130	replace_text "ldns/configure.ac" "AC_INIT(ldns, $version" "AC_INIT(ldns, $version2"
131	replace_text "ldns-nonstatic/configure.ac" "AC_INIT(ldns, $version" "AC_INIT(ldns, $version2"
132	version="$version2"
133fi
134if [ "$SNAPSHOT" = "yes" ]; then
135	info "Building LDNS snapshot."
136	version2="${version}_$(date +%Y%m%d)"
137	info "Snapshot version number: $version2"
138	replace_text "ldns/configure.ac" "AC_INIT(ldns, $version" "AC_INIT(ldns, $version2"
139	replace_text "ldns-nonstatic/configure.ac" "AC_INIT(ldns, $version" "AC_INIT(ldns, $version2"
140	version="$version2"
141fi
142
143# Build OpenSSL
144gzip -cd "$WINSSL" | tar xf - || error_cleanup "tar unpack of $WINSSL failed"
145sslinstall="$(pwd)/sslinstall"
146cd openssl-* || error_cleanup "no openssl-X dir in tarball"
147if test $configure = "mingw64-configure"; then
148	sslflags="no-shared no-asm -DOPENSSL_NO_CAPIENG mingw64"
149else
150	sslflags="no-shared no-asm -DOPENSSL_NO_CAPIENG mingw"
151fi
152info "winssl: Configure $sslflags"
153if test -f "/usr/${warch}-w64-mingw32/sys-root/mingw/bin/libssp-0.dll" ; then
154	export __CNF_LDLIBS="-l:libssp.a"
155fi
156CC="${warch}-w64-mingw32-gcc" AR="${warch}-w64-mingw32-ar" RANLIB="${warch}-w64-mingw32-ranlib" WINDRES="${warch}-w64-mingw32-windres" ./Configure --prefix="$sslinstall" $sslflags || error_cleanup "OpenSSL Configure failed"
157info "winssl: make"
158make || error_cleanup "make failed for $WINSSL"
159info "winssl: make install_sw"
160make install_sw || error_cleanup "OpenSSL install failed"
161cross_flag="$cross_flag --with-ssl=$sslinstall"
162cd ..
163
164# Build ldns
165ldnsinstall="$(pwd)/ldnsinstall"
166cd ldns
167info "ldns: autoconf"
168# cp install-sh because one at ../.. means libtoolize won't install it for us.
169if test ! -f install-sh -a -f ../../install-sh; then cp ../../install-sh . ; fi
170libtoolize -ci
171autoreconf -fi
172ldns_flag="--with-examples --with-drill"
173if test -f "/usr/${warch}-w64-mingw32/sys-root/mingw/bin/libssp-0.dll" ; then
174	info "ldns: Configure $cross_flag $ldns_flag LDFLAGS=\"-fstack-protector\" LIBS=\"-l:libssp.a\""
175	$configure $cross_flag $ldns_flag LDFLAGS="-fstack-protector" LIBS="-l:libssp.a" || error_cleanup "ldns configure failed"
176else
177	info "ldns: Configure $cross_flag $ldns_flag"
178	$configure $cross_flag $ldns_flag || error_cleanup "ldns configure failed"
179fi
180info "ldns: make"
181make || error_cleanup "ldns make failed"
182# do not strip debug symbols, could be useful for stack traces
183# $strip lib/*.dll || error_cleanup "cannot strip ldns dll"
184make doc || error_cleanup "ldns make doc failed"
185DESTDIR=$ldnsinstall make install || error_cleanup "ldns make install failed"
186cd ..
187
188# Build OpenSSL nonstatic
189sslinstallnonstatic="$(pwd)/sslinstallnonstatic"
190mkdir openssl-nonstatic
191cd openssl-nonstatic
192# remove openssl-a.b.c/ and put in openssl-nonstatic directory
193gzip -cd "$WINSSL" | tar xf - --strip-components=1 || error_cleanup "tar unpack of $WINSSL failed"
194if test "$configure" = "mingw64-configure"; then
195	sslflags_nonstatic="shared no-asm -DOPENSSL_NO_CAPIENG mingw64"
196else
197	sslflags_nonstatic="shared no-asm -DOPENSSL_NO_CAPIENG mingw"
198fi
199info "winsslnonstatic: Configure $sslflags_nonstatic"
200CC="${warch}-w64-mingw32-gcc" AR="${warch}-w64-mingw32-ar" RANLIB="${warch}-w64-mingw32-ranlib" WINDRES="${warch}-w64-mingw32-windres" ./Configure --prefix="$sslinstallnonstatic" $sslflags_nonstatic || error_cleanup "OpenSSL Configure failed"
201info "winsslnonstatic: make"
202make || error_cleanup "make failed for $WINSSL"
203info "winsslnonstatic: make install_sw"
204make install_sw || error_cleanup "OpenSSL install failed"
205cross_flag_nonstatic="$cross_flag_nonstatic --with-ssl=$sslinstallnonstatic"
206cd ..
207
208# Build ldns nonstatic
209ldnsinstallnonstatic="$(pwd)/ldnsinstall-nonstatic"
210cd ldns-nonstatic
211info "ldnsnonstatic: autoconf"
212# cp install-sh because one at ../.. means libtoolize won't install it for us.
213if test ! -f install-sh -a -f ../../install-sh; then cp ../../install-sh . ; fi
214libtoolize -ci
215autoreconf -fi
216ldns_flag_nonstatic="--with-examples --with-drill"
217if test -f "/usr/${warch}-w64-mingw32/sys-root/mingw/bin/libssp-0.dll" ; then
218	info "ldnsnonstatic: Configure $cross_flag_nonstatic $ldns_flag_nonstatic LDFLAGS=\"-fstack-protector\" LIBS=\"-lssp\""
219	$configure $cross_flag_nonstatic $ldns_flag_nonstatic LDFLAGS="-fstack-protector" LIBS="-lssp" || error_cleanup "ldns configure failed"
220else
221	info "ldnsnonstatic: Configure $cross_flag_nonstatic $ldns_flag_nonstatic"
222	$configure $cross_flag_nonstatic $ldns_flag_nonstatic || error_cleanup "ldns configure failed"
223fi
224info "ldnsnonstatic: make"
225make || error_cleanup "ldns make failed"
226# do not strip debug symbols, could be useful for stack traces
227# $strip lib/*.dll || error_cleanup "cannot strip ldns dll"
228make doc || error_cleanup "ldns make doc failed"
229DESTDIR=$ldnsinstallnonstatic make install || error_cleanup "ldns make install failed"
230cd ..
231
232# create zipfile
233file="ldns-$version.zip"
234rm -f "$file"
235info "Creating $file"
236mkdir file
237cd file
238installplace="$ldnsinstall/usr/$warch-w64-mingw32/sys-root/mingw"
239installplacenonstatic="$ldnsinstallnonstatic/usr/$warch-w64-mingw32/sys-root/mingw"
240cp "$installplace"/lib/libldns.a .
241cp "$installplacenonstatic"/lib/libldns.dll.a .
242cp "$installplacenonstatic"/bin/*.dll .
243if test -d "$sslinstallnonstatic"/lib64; then
244	cp "$sslinstallnonstatic"/lib64/*.dll.a .
245else
246	cp "$sslinstallnonstatic"/lib/*.dll.a .
247fi
248cp "$sslinstallnonstatic"/bin/*.dll .
249if test -d "$sslinstallnonstatic"/lib64; then
250	cp "$sslinstallnonstatic"/lib64/engines-*/*.dll .
251else
252	cp "$sslinstallnonstatic"/lib/engines-*/*.dll .
253fi
254if test -f "/usr/${warch}-w64-mingw32/sys-root/mingw/bin/libssp-0.dll" ; then
255	cp "/usr/${warch}-w64-mingw32/sys-root/mingw/bin/libssp-0.dll" .
256fi
257cp ../ldns/LICENSE .
258cp ../ldns/README .
259cp ../ldns/Changelog .
260info "copy static exe"
261for x in "$installplace"/bin/* ; do
262	cp "$x" .
263done
264# but the shell script stays a script file
265if test -f ldns-config.exe; then
266	mv ldns-config.exe ldns-config
267fi
268info "copy include"
269mkdir include
270mkdir include/ldns
271cp "$installplace"/include/ldns/*.h include/ldns/.
272info "copy man1"
273mkdir man1
274cp "$installplace"/share/man/man1/* man1/.
275info "copy man3"
276mkdir man3
277cp "$installplace"/share/man/man3/* man3/.
278info "create cat1"
279mkdir cat1
280for x in man1/*.1; do groff -man -Tascii -Z "$x" | grotty -cbu > cat1/"$(basename "$x" .1).txt"; done
281info "create cat3"
282mkdir cat3
283for x in man3/*.3; do groff -man -Tascii -Z "$x" | grotty -cbu > cat3/"$(basename "$x" .3).txt"; done
284add_files=""
285if test -f ldns-config; then add_files="$add_files ldns-config"; fi
286rm -f "../../$file"
287info "$file contents"
288# show contents of directory we are zipping up.
289du -s ./*
290# zip it
291info "zip $file"
292zip -r ../../"$file" LICENSE README libldns.a *.dll *.dll.a Changelog *.exe $add_files include man1 man3 cat1 cat3
293info "Testing $file"
294(cd ../.. ; zip -T "$file" ) || error_cleanup "errors in zipfile $file"
295cd ..
296
297# cleanup before exit
298cd "$tmpdir"
299rm -rf "$cdir"
300echo "done"
301# display
302ls -lG "$file"
303