1#!/bin/bash
2#
3# NetSurf continuous integration build script for jenkins
4#
5# Copyright © 2013 Vincent Sanders <vince@netsurf-browser.org>
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14#   * The above copyright notice and this permission notice shall be included in
15#     all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23# THE SOFTWARE.
24
25# This script may be executed by jenkins jobs that use the core buildsystem
26#
27# Usage: jenkins-build.sh [install|test-install|coverage|static|docs]
28#
29# install - build and install
30# test-install - build, test and install
31# coverage - run coverage
32# static - perform a static analysis
33# coverity - perform a coverity scan
34# docs - build doxygen docs
35# sanitize - Run the test suite under ASAN and UBSAN
36
37# HOST must be in the environment and set correctly
38if [ "x${HOST}" = "x" ];then
39    echo "HOST unset"
40    exit 1
41fi
42
43# The target for built artifacts
44#
45# This requires the artifacts of a target must all be built on the same
46#  jenkins slave instance.
47ARTIFACT_HOME=${JENKINS_HOME}/artifacts-${HOST}
48
49# Obtain the native build triplet if unset
50if [ "x${BUILD}" = "x" ];then
51    # This assumes the cc on the PATH is the native one.
52    BUILD=$(cc -dumpmachine)
53fi
54
55
56# target defaults
57TARGET_TEST=
58TARGET_INSTALL=
59TARGET_COVERAGE=
60TARGET_STATIC=
61TARGET_COVERITY=
62TARGET_DOCS=
63
64# Which variant is being generated
65VARIANT="release"
66
67# change target build according to parameter
68case "$1" in
69    "install")
70	TARGET_INSTALL=${HOST}
71	;;
72
73    "coverage")
74	TARGET_COVERAGE=${HOST}
75	VARIANT="debug"
76	# need to disable ccache on coverage builds
77	export CCACHE=
78	;;
79
80    "static")
81	TARGET_STATIC=${HOST}
82	VARIANT="debug"
83	# need to disable ccache on static builds
84	export CCACHE=
85	;;
86
87    "coverity")
88	TARGET_COVERITY=${HOST}
89	VARIANT="debug"
90	# need to disable ccache on coverity builds
91	export CCACHE=
92	;;
93
94    "test-install")
95	# Perfom test if being executed on native target
96	TARGET_TEST=${BUILD}
97	TARGET_INSTALL=${HOST}
98	;;
99
100    "docs")
101	TARGET_DOCS=${HOST}
102	;;
103
104    "sanitize")
105	TARGET_SANITIZE=${HOST}
106	VARIANT="debug"
107	;;
108
109    "")
110	# default is test only on Linux and install
111	# Currently most tests do not work on targets except for Linux
112	TARGET_TEST="x86_64-linux-gnu"
113	TARGET_INSTALL=${HOST}
114	;;
115
116    *)
117	cat <<EOF
118Usage: jenkins-build.sh [install|test-install|coverage|static|docs|sanitize]
119
120       install        build and install
121       test-install   build, test and install
122       coverage       run coverage
123       static         perform a static anaysis
124       coverity       perform a coverity scan
125       docs           generate doxygen docs
126       sanitize       run the tests under ASAN/UBSAN
127EOF
128	exit 1
129	;;
130esac
131
132
133# adjust tools based on build
134case ${BUILD} in
135
136    amd64-unknown-openbsd*)
137	MAKE=gmake
138	;;
139
140    x86_64-unknown-openbsd*)
141	MAKE=gmake
142	;;
143
144    x86_64-unknown-freebsd*)
145	MAKE=gmake
146	;;
147
148    x86_64-apple-darwin14.5.0*)
149        PATH="/opt/local/bin:/opt/local/sbin:$PATH"
150	MAKE=make
151        ;;
152
153    *)
154	MAKE=make
155	;;
156esac
157
158
159# Ensure the artifact target directory exists
160mkdir -p ${ARTIFACT_HOME}
161
162
163# Configure all build paths relative to prefix
164export PREFIX=${ARTIFACT_HOME}
165export PKG_CONFIG_PATH=${PREFIX}/lib/pkgconfig
166export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${PREFIX}/lib
167export PATH=${PATH}:${PREFIX}/bin
168
169
170# execute the build steps
171
172# clean target is always first
173${MAKE} Q= clean HOST=${HOST} VARIANT=${VARIANT}
174
175
176# build as per type requested
177if [ "x${HOST}" = "x${TARGET_COVERAGE}" ]; then
178    # Coverage Build
179    CFLAGS=-Wno-error ${MAKE} Q= HOST=${HOST} VARIANT=${VARIANT} coverage
180    gcovr -v -x -e 'test/.*' -r . -o coverage.xml
181
182elif [ "x${HOST}" = "x${TARGET_STATIC}" ]; then
183    # static build
184    rm -rf clangScanBuildReports
185
186    # build with static analysis without warnings causing errors
187    CFLAGS=-Wno-error scan-build -o clangScanBuildReports -v -v --use-cc clang --use-analyzer=/usr/bin/clang ${MAKE} Q= VARIANT=${VARIANT}
188
189    # clean up after
190    ${MAKE} Q= clean HOST=${HOST} VARIANT=${VARIANT}
191
192
193elif [ "x${HOST}" = "x${TARGET_COVERITY}" ]; then
194    # coverity build
195
196    # Check thses are set
197    #
198    # COVERITY_PROJECT
199    # COVERITY_TOKEN
200    # COVERITY_USER
201    # COVERITY_PREFIX
202
203    if [ -z "${COVERITY_PROJECT}" -o -z "${COVERITY_TOKEN}" -o -z "${COVERITY_USER}" -o -z "${COVERITY_PREFIX}" ]; then
204	echo "Coverity parameters not set"
205	exit 1
206    fi
207
208    # Coverity tools location
209    COVERITY_PREFIX=${COVERITY_PREFIX:-/opt/coverity/cov-analysis-linux64-6.6.1}
210    COVERITY_VERSION=$(git rev-parse HEAD)
211
212    export PATH=${PATH}:${COVERITY_PREFIX}/bin
213
214    # cleanup before we start
215    rm -rf cov-int/ coverity-scan.tar.gz coverity-scan.tar
216
217    cov-build --dir cov-int ${MAKE} Q= HOST=${HOST} VARIANT=${VARIANT}
218
219    tar cf coverity-scan.tar cov-int
220
221    gzip -9 coverity-scan.tar
222
223    curl --form "project=${COVERITY_PROJECT}" --form "token=${COVERITY_TOKEN}" --form "email=${COVERITY_USER}" --form "file=@coverity-scan.tar.gz" --form "version=${COVERITY_VERSION}" --form "description=Git Head build" "https://scan.coverity.com/builds?project=${COVERITY_PROJECT}"
224
225
226elif [ "x${HOST}" = "x${TARGET_DOCS}" ]; then
227    # Documentation build
228    ${MAKE} Q= HOST=${HOST} VARIANT=${VARIANT} docs
229
230elif [ "x${HOST}" = "x${TARGET_SANITIZE}" ]; then
231    # Sanitize build
232    env UBSAN_OPTIONS=print_stacktrace=1 ${MAKE} Q= HOST=${HOST} VARIANT=${VARIANT} sanitize
233
234else
235    # Normal build
236    ${MAKE} Q= HOST=${HOST} VARIANT=${VARIANT}
237
238
239fi
240
241
242# run tests if appropriate
243if [ "x${HOST}" = "x${TARGET_TEST}" ]; then
244    ${MAKE} Q= HOST=${HOST} VARIANT=${VARIANT} test
245fi
246
247
248# install the output
249if [ "x${HOST}" = "x${TARGET_INSTALL}" ]; then
250    ${MAKE} Q= HOST=${HOST} VARIANT=${VARIANT} install
251fi
252