jenkins: use default XTARGET for testing with simpler setups
[gnupg-doc.git] / misc / jenkins / bin / build.bash
1 #!/bin/bash
2 # Script used jenkins to run builds for GnuPG and related packages.
3
4 # Stop on error and be nice to other processes.
5 set -xe
6 renice -n 10 -p $$
7
8 # Configuration.
9 MAKE=make
10
11 XTARGET="${XTARGET:-native}"
12
13 # Platform-specific configuration.
14 case "$(uname)" in
15     OpenBSD)
16         MAKE=gmake
17         ;;
18 esac
19
20 if [ "$XTARGET" = w32 ]; then
21     CC=i686-w64-mingw32-gcc
22     CXX=i686-w64-mingw32-g++
23 fi
24
25 # Setup ccache if installed.
26 if ccache --version >/dev/null; then
27     export CCACHE_DIR="$HOME/cache/$JOB_NAME"
28     mkdir -p "$CCACHE_DIR"
29     export CC="ccache ${CC:-gcc}"
30     export CXX="ccache ${CXX:-g++}"
31 fi
32
33 # Setup important envars
34 PREFIX=$HOME/prefix/$XTARGET
35 ORIGINAL_PREFIX=$HOME/prefix/$XTARGET
36 export PATH=$PREFIX/bin:$PATH
37
38 # Tweak the prefix we're installing this project into.  For gnupg-1.4
39 # and friends.
40 case "$JOB_NAME" in
41     *-1.4*)
42         PREFIX=$PREFIX-1.4
43         ;;
44     *-2.0*)
45         PREFIX=$PREFIX-2.0
46         ;;
47     *-2.2*)
48         PREFIX=$PREFIX-2.2
49         ;;
50 esac
51 mkdir -p $PREFIX
52
53
54 fix_permissions()
55 {
56   find $1 -type d -exec chmod +w {} + || true
57 }
58
59 fix_permissions .
60
61 # Clean everything
62 git clean -fdx
63
64 # Run out autogen - note that --force is not required due to the git clean.
65 ./autogen.sh
66
67 # Parallel jobs.
68 MAKEFLAGS="-j6"
69
70 # Parallel tests with our test suite.
71 export TESTFLAGS="--parallel"
72
73 SCANBUILD=
74 if [ "$(uname)" = Linux ] \
75        && [ "$ROOT_BUILD_CAUSE_TIMERTRIGGER" = true ]; then
76     # We only do scan-builds (which are really slow) on nightly
77     # builds.
78     SCANBUILD="scan-build -o ${WORKSPACE}/clangScanBuildReports -v"
79 fi
80 CONFIGUREFLAGS=
81 SANFLAGS=""
82 if [ "$(uname)" = Linux ]; then
83     # XXX: We should check if the sanitizers are available.
84     SANFLAGS="-fsanitize=undefined -fsanitize=address"
85 fi
86
87 if [ "$(uname)" = Darwin ]; then
88     # XXX until we properly set this somewhere else
89     cversion="_DARWIN_C_SOURCE=900000L"
90     CFLAGS="$CFLAGS -D$cversion"
91     CXXFLAGS="$CXXFLAGS -D$cversion"
92 fi
93
94 # Tweak the build depending on the package.
95 case "$JOB_NAME" in
96     *tgpg*)
97         MAKEFLAGS="$MAKEFLAGS GPG=/usr/bin/gpg2"
98         ;;
99     *gpgme*)
100         # using libasan for python broke again, so disable the python
101         # bindings for the sanitizer build
102         if [ "$XTARGET" = sanitizer ]; then
103             CONFIGUREFLAGS_0="--enable-languages=cpp qt"
104         fi
105
106         # Disable Python bindings on macOS.  Something is not working
107         # there.
108         if [ "$NODE_NAME" = zygalski ]; then
109             CONFIGUREFLAGS_0="--enable-languages=cpp qt"
110         fi
111         ;;
112     *gnupg*)
113         # Common configure options.
114         CONFIGUREFLAGS="--enable-wks-tools --enable-gpg2-is-gpg"
115
116         # For Windows builds...
117         if [ "$XTARGET" = w32 ]; then
118             # ... we need to tweak it a little and we leave out some
119             # stuff...
120             CONFIGUREFLAGS="$CONFIGUREFLAGS --with-zlib=$ORIGINAL_PREFIX --with-bzip2=$ORIGINAL_PREFIX"
121         else
122             # ... that we enable for all other builds.
123             CONFIGUREFLAGS="$CONFIGUREFLAGS --enable-g13 --enable-symcryptrun"
124         fi
125
126         if [ "$NODE_NAME" = zygalski ]; then
127             CONFIGUREFLAGS="$CONFIGUREFLAGS --with-libiconv-prefix=$HOME/pkg"
128         fi
129         if [ "$NODE_NAME" = openbsd60 ]; then
130             CONFIGUREFLAGS="$CONFIGUREFLAGS --with-libiconv-prefix=$HOME/compat --with-bzip2=$HOME/compat"
131         fi
132
133         # Disable NTBTLS for now until it is actually mature and used.
134         CONFIGUREFLAGS="$CONFIGUREFLAGS --disable-ntbtls"
135         ;;
136 esac
137
138 # See if we have a GPGME checkout for the tesets.
139 xtest_gpgme_srcdir="$HOME/src/gpgme-for-gnupgs-tests"
140 if [ -d "$xtest_gpgme_srcdir/obj-$XTARGET" ]; then
141     # Some targets, like the sanitizer target, require a custom
142     # version of GPGME.
143     export XTEST_GPGME_SRCDIR="$xtest_gpgme_srcdir"
144     export XTEST_GPGME_BUILDDIR="$xtest_gpgme_srcdir/obj-$XTARGET"
145 elif [ -d "$xtest_gpgme_srcdir/obj" ]; then
146     export XTEST_GPGME_SRCDIR="$xtest_gpgme_srcdir"
147     export XTEST_GPGME_BUILDDIR="$xtest_gpgme_srcdir/obj"
148 fi
149
150 # The libraries use RUNPATH when linking the tests, so they locate
151 # their dependencies that way.  GnuPG, however, does not.  Therefore,
152 # we set LD_LIBRARY_PATH.
153 test_environment="LD_LIBRARY_PATH=$ORIGINAL_PREFIX/lib"
154
155 # HACKHACKHACK:
156 #
157 # Because newer Debian toolchains prefer RUNPATH over RPATH, and
158 # RUNPATH has lower precedence than LD_LIBRARY_PATH, we need to
159 # explicitly add libtool's .libs directory:
160 case "$JOB_NAME" in
161   *gnupg*)
162     if [ "${XTEST_GPGME_BUILDDIR}" ]; then
163         test_environment="LD_LIBRARY_PATH=${XTEST_GPGME_BUILDDIR}/src/.libs:${XTEST_GPGME_BUILDDIR}/lang/cpp/src/.libs:${XTEST_GPGME_BUILDDIR}/lang/qt/src/.libs:$ORIGINAL_PREFIX/lib"
164     fi
165     ;;
166   *gpgme*)
167     test_environment="LD_LIBRARY_PATH=$(pwd)/obj/src/.libs:$(pwd)/obj/lang/cpp/src/.libs:$(pwd)/obj/lang/qt/src/.libs:$ORIGINAL_PREFIX/lib"
168     ;;
169   *)
170     test_environment="LD_LIBRARY_PATH=$(pwd)/obj/src/.libs:$ORIGINAL_PREFIX/lib"
171     ;;
172 esac
173 #
174 # If we don't do this, the version tests fail because the runtime
175 # linker will pick up the library from LD_LIBRARY_PATH.  Also, testing
176 # the installed version is not what we want ofc.
177 #
178 # KCAHKCAHKCAH
179
180 # We build on the "obj" subdir.
181 abs_configure="$(pwd)/configure"
182 mkdir -p obj
183 cd obj
184
185
186 # Print the environment.
187 env
188 ulimit -a
189 set +x
190 for f in /etc/gcrypt/hwf.deny /etc/gcrypt/fips_enabled ; do
191   if [ -f "$f" ]; then
192     echo "=== $f ==="
193     cat -n "$f"
194   fi
195 done
196 set -x
197
198
199 # Switch on the different targets.
200 case "$XTARGET" in
201     native)
202         ../configure --prefix=$PREFIX --enable-maintainer-mode \
203                    $CONFIGUREFLAGS \
204                    "$CONFIGUREFLAGS_0" \
205                    CFLAGS="$CFLAGS" \
206                    CXXFLAGS="$CXXFLAGS -std=c++11"
207         $MAKE $MAKEFLAGS
208
209         env $test_environment $MAKE -k check verbose=2 \
210             || echo "FAIL: make check failed with status $?"
211         # Jenkins looks for "FAIL:" to mark a build unstable,
212         # hence || ... here
213
214         $MAKE install
215         ;;
216     in-tree)
217         cd ..
218         ./configure --prefix=$PREFIX --enable-maintainer-mode \
219                    $CONFIGUREFLAGS \
220                    "$CONFIGUREFLAGS_0" \
221                    CFLAGS="$CFLAGS" \
222                    CXXFLAGS="$CXXFLAGS -std=c++11"
223         $MAKE $MAKEFLAGS
224
225         # HACKHACKHACK: Fix the test_environment hack.
226         test_environment="$(echo $test_environment | sed -e 's#obj/##g')"
227         # KCAHKCAHKCAH
228
229         env $test_environment $MAKE -k check verbose=2 \
230             || echo "FAIL: make check failed with status $?"
231         # Jenkins looks for "FAIL:" to mark a build unstable,
232         # hence || ... here
233
234         $MAKE install
235         ;;
236     sanitizer)
237         # asan breaks the configure tests, so we disable it here.
238         ASAN_OPTIONS=detect_leaks=0 \
239         $SCANBUILD \
240             ../configure --prefix=$PREFIX --enable-maintainer-mode \
241                    $CONFIGUREFLAGS \
242                    "$CONFIGUREFLAGS_0" \
243                    CFLAGS="$CFLAGS $SANFLAGS -fPIC" \
244                    CXXFLAGS="$CXXFLAGS $SANFLAGS -fPIC -std=c++11"
245         $SCANBUILD $MAKE $MAKEFLAGS
246
247         env $test_environment $MAKE -k check verbose=2 \
248             || echo "FAIL: make check failed with status $?"
249         # Jenkins looks for "FAIL:" to mark a build unstable,
250         # hence || ... here
251
252         $MAKE install
253         ;;
254     w32)
255         export w32root=$PREFIX
256
257         # autogen.rc adds --with-gpg-error-prefix=@SYSROOT@, so we cannot
258         # install to a prefix that doesn't also contain all the dependencies,
259         # patch that out, so that the gpg-error-config and friends are located
260         # using PATH
261         if [ -f "/home/jenkins/bin/$(dirname $JOB_NAME)-w32.patch" ]; then
262           ( cd .. && patch -p1 <"/home/jenkins/bin/$(dirname $JOB_NAME)-w32.patch" )
263         fi
264         # We need to point it to npth then...
265         case "$JOB_NAME" in
266             gnupg/XTARGET=w32|gnupg-2.2/XTARGET=w32)
267                 CONFIGUREFLAGS="${CONFIGUREFLAGS} --with-npth-prefix=$ORIGINAL_PREFIX"
268                 ;;
269             gnupg-2.0/XTARGET=w32)
270                 CONFIGUREFLAGS="${CONFIGUREFLAGS} --with-pth-prefix=$ORIGINAL_PREFIX --with-adns=$ORIGINAL_PREFIX"
271                 ;;
272         esac
273
274         # gpg1's autogen.sh does not add --enable-maintainer-mode, so
275         # version.texi is not generated.  we add it here to be sure.
276         # likewise for --prefix
277         ../autogen.sh --build-w32 --enable-maintainer-mode --prefix=$PREFIX \
278           $CONFIGUREFLAGS
279         $MAKE $MAKEFLAGS
280         $MAKE install
281
282         case "$JOB_NAME" in
283                 gnupg/*|gnupg-2.2/*)
284                         bash /home/jenkins/bin/make-windows-cd.sh
285                         # We need to pass the absolute path of the iso.
286                         bash $HOME/bin/run-tests-w32.bash "$(readlink -f gnupg-test.iso)" || echo "Warning: error running tests on Windows."
287                         ;;
288         esac
289         ;;
290     distcheck)
291         CONFIGUREFLAGS=
292         WORKDIR="$(mktemp -d)"
293         cleanup()
294         {
295           cd /tmp
296           fix_permissions "$WORKDIR"
297           rm -rf -- "$WORKDIR" || true
298         }
299         trap cleanup EXIT
300
301           # We use a different WORKDIR to avoid problems with too long
302           # file names
303           cd "$WORKDIR"
304           $abs_configure --prefix=$PREFIX --enable-maintainer-mode \
305                    $CONFIGUREFLAGS
306
307           # Extract the directory / tarname from the package
308           tarname=$(awk <config.h '
309                      /^#define PACKAGE_TARNAME/ {gsub(/"/,"",$3);name=$3};
310                      /^#define PACKAGE_VERSION/ {gsub(/"/,"",$3);vers=$3};
311                      END {print name "-" vers}')
312
313           # HACKHACKHACK: Because newer Debian toolchains prefer
314           # RUNPATH over RPATH, and RUNPATH has lower precedence than
315           # LD_LIBRARY_PATH, we need to explicitly add libtool's .libs
316           # directory:
317           case "$JOB_NAME" in
318             *gnupg*)
319               if [ "${XTEST_GPGME_BUILDDIR}" ]; then
320                   test_environment="LD_LIBRARY_PATH=${XTEST_GPGME_BUILDDIR}/src/.libs:${XTEST_GPGME_BUILDDIR}/lang/cpp/src/.libs:${XTEST_GPGME_BUILDDIR}/lang/qt/src/.libs:$ORIGINAL_PREFIX/lib"
321               fi
322               ;;
323             *gpgme*)
324               test_environment="LD_LIBRARY_PATH=$(pwd)/${tarname}/_build/sub/src/.libs:$(pwd)/${tarname}/_build/sub/lang/cpp/src/.libs:$(pwd)/${tarname}/_build/sub/lang/qt/src/.libs:$ORIGINAL_PREFIX/lib"
325               ;;
326             *)
327               test_environment="LD_LIBRARY_PATH=$(pwd)/${tarname}/_build/sub/src/.libs:$ORIGINAL_PREFIX/lib"
328               ;;
329           esac
330           # KCAHKCAHKCAH
331
332           if ! env $test_environment $MAKE $MAKEFLAGS distcheck ; then
333               # Jenkins looks for "FAIL:" to mark a build unstable,
334               # hence we ignore errors here.
335               echo "FAIL: make distcheck failed with status $?"
336               # Disable the cleanup so that we can investigate.
337               trap - EXIT
338               exit 0
339           fi
340
341           if [ -f "${tarname}.tar.bz2" ]; then
342              bzcat "${tarname}.tar.bz2" | tar xf -
343           elif [ -f "${tarname}.tar.gz" ]; then
344              zcat "${tarname}.tar.gz" | tar xf -
345           else
346              echo "No tarball named $tarname found - skipping installation" >&2
347              exit 0
348           fi
349           # And do a final build using the generated tarball
350           cd ${tarname}
351           ./configure --prefix=$PREFIX $CONFIGUREFLAGS
352           $MAKE $MAKEFLAGS
353           $MAKE $MAKEFLAGS install
354
355         ;;
356     *)
357         echo "Bad XTARGET: '$XTARGET'"
358         exit 2
359 esac