jjb: update SLES detection in build scripts
[lttng-ci.git] / scripts / babeltrace / build.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2015 Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
4 # Copyright (C) 2016-2020 Michael Jeanson <mjeanson@efficios.com>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 set -exu
20
21 # Version compare functions
22 vercomp () {
23 set +u
24 if [[ "$1" == "$2" ]]; then
25 return 0
26 fi
27 local IFS=.
28 # Ignore the shellcheck warning, we want splitting to happen based on IFS.
29 # shellcheck disable=SC2206
30 local i ver1=($1) ver2=($2)
31 # fill empty fields in ver1 with zeros
32 for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
33 ver1[i]=0
34 done
35 for ((i=0; i<${#ver1[@]}; i++)); do
36 if [[ -z ${ver2[i]} ]]; then
37 # fill empty fields in ver2 with zeros
38 ver2[i]=0
39 fi
40 if ((10#${ver1[i]} > 10#${ver2[i]})); then
41 return 1
42 fi
43 if ((10#${ver1[i]} < 10#${ver2[i]})); then
44 return 2
45 fi
46 done
47 set -u
48 return 0
49 }
50
51 # Shellcheck flags the following functions that are unused as "unreachable",
52 # ignore that.
53
54 # shellcheck disable=SC2317
55 verlte() {
56 vercomp "$1" "$2"
57 local res="$?"
58 [ "$res" -eq "0" ] || [ "$res" -eq "2" ]
59 }
60
61 # shellcheck disable=SC2317
62 verlt() {
63 vercomp "$1" "$2"; local res="$?"
64 [ "$res" -eq "2" ]
65 }
66
67 # shellcheck disable=SC2317
68 vergte() {
69 vercomp "$1" "$2"; local res="$?"
70 [ "$res" -eq "0" ] || [ "$res" -eq "1" ]
71 }
72
73 # shellcheck disable=SC2317
74 vergt() {
75 vercomp "$1" "$2"; local res="$?"
76 [ "$res" -eq "1" ]
77 }
78
79 # shellcheck disable=SC2317
80 verne() {
81 vercomp "$1" "$2"; local res="$?"
82 [ "$res" -ne "0" ]
83 }
84
85 failed_configure() {
86 # Assume we are in the configured build directory
87 echo "#################### BEGIN config.log ####################"
88 cat config.log
89 echo "#################### END config.log ####################"
90 exit 1
91 }
92
93
94 # Required variables
95 WORKSPACE=${WORKSPACE:-}
96
97 platform=${platform:-}
98 conf=${conf:-}
99 build=${build:-}
100 cc=${cc:-}
101
102 # Controls if the tests are run
103 BABELTRACE_RUN_TESTS="${BABELTRACE_RUN_TESTS:=yes}"
104
105 SRCDIR="$WORKSPACE/src/babeltrace"
106 TMPDIR="$WORKSPACE/tmp"
107 PREFIX="/build"
108 LIBDIR="lib"
109 LIBDIR_ARCH="$LIBDIR"
110
111 # RHEL and SLES both use lib64 but don't bother shipping a default autoconf
112 # site config that matches this.
113 if [[ ( -f /etc/redhat-release || -f /etc/products.d/SLES.prod || -f /etc/yocto-release ) ]]; then
114 # Detect the userspace bitness in a distro agnostic way
115 if file -L /bin/bash | grep '64-bit' >/dev/null 2>&1; then
116 LIBDIR_ARCH="${LIBDIR}64"
117 fi
118 fi
119
120 # Create tmp directory
121 rm -rf "$TMPDIR"
122 mkdir -p "$TMPDIR"
123
124 export TMPDIR
125 export CFLAGS="-g -O2"
126
127 # Set compiler variables
128 case "$cc" in
129 gcc)
130 export CC=gcc
131 export CXX=g++
132 ;;
133 gcc-*)
134 export CC=gcc-${cc#gcc-}
135 export CXX=g++-${cc#gcc-}
136 ;;
137 clang)
138 export CC=clang
139 export CXX=clang++
140 ;;
141 clang-*)
142 export CC=clang-${cc#clang-}
143 export CXX=clang++-${cc#clang-}
144 ;;
145 *)
146 if [ "x$cc" != "x" ]; then
147 export CC="$cc"
148 fi
149 ;;
150 esac
151
152 if [ "x${CC:-}" != "x" ]; then
153 echo "Selected compiler:"
154 "$CC" -v
155 fi
156
157 # Set platform variables
158 case "$platform" in
159 macos*)
160 export MAKE=make
161 export TAR=tar
162 export NPROC="getconf _NPROCESSORS_ONLN"
163 export PATH="/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
164 export CPPFLAGS="-I/opt/local/include"
165 export LDFLAGS="-L/opt/local/lib"
166 export PYTHON="python3"
167 export PYTHON_CONFIG="python3-config"
168 ;;
169
170 freebsd*)
171 export MAKE=gmake
172 export TAR=tar
173 export NPROC="getconf _NPROCESSORS_ONLN"
174 export CPPFLAGS="-I/usr/local/include"
175 export LDFLAGS="-L/usr/local/lib"
176 export PYTHON="python3"
177 export PYTHON_CONFIG="python3-config"
178
179 # For bt 1.5
180 export YACC="bison -y"
181 ;;
182
183 *)
184 export MAKE=make
185 export TAR=tar
186 export NPROC=nproc
187 export PYTHON="python3"
188 export PYTHON_CONFIG="python3-config"
189 ;;
190 esac
191
192 # Print build env details
193 print_os || true
194 print_tooling || true
195
196 # Enter the source directory
197 cd "$SRCDIR"
198
199 # Run bootstrap in the source directory prior to configure
200 ./bootstrap
201
202 # Get source version from configure script
203 eval "$(grep '^PACKAGE_VERSION=' ./configure)"
204 PACKAGE_VERSION=${PACKAGE_VERSION//\-pre*/}
205
206 # Enable dev mode by default for BT 2.0 builds
207 export BABELTRACE_DEBUG_MODE=1
208 export BABELTRACE_DEV_MODE=1
209 export BABELTRACE_MINIMAL_LOG_LEVEL=TRACE
210
211 # Set configure options and environment variables for each build
212 # configuration.
213 CONF_OPTS=("--prefix=$PREFIX" "--libdir=$PREFIX/$LIBDIR_ARCH")
214
215 # -Werror is enabled by default in stable-2.0 but won't be in 2.1
216 # Explicitly disable it for consistency.
217 if vergte "$PACKAGE_VERSION" "2.0"; then
218 CONF_OPTS+=("--disable-Werror")
219 fi
220
221 case "$conf" in
222 static)
223 echo "Static lib only configuration"
224
225 CONF_OPTS+=("--enable-static" "--disable-shared")
226
227 if vergte "$PACKAGE_VERSION" "2.0"; then
228 CONF_OPTS+=("--enable-built-in-plugins")
229 fi
230 ;;
231
232 python-bindings)
233 echo "Python bindings configuration"
234
235 CONF_OPTS+=("--enable-python-bindings")
236
237 if vergte "$PACKAGE_VERSION" "2.0"; then
238 CONF_OPTS+=("--enable-python-bindings-doc" "--enable-python-plugins")
239 fi
240 ;;
241
242 prod)
243 echo "Production configuration"
244
245 # Unset the developper variables
246 unset BABELTRACE_DEBUG_MODE
247 unset BABELTRACE_DEV_MODE
248 unset BABELTRACE_MINIMAL_LOG_LEVEL
249
250 # Enable the python bindings
251 CONF_OPTS+=("--enable-python-bindings" "--enable-python-plugins")
252 ;;
253
254 doc)
255 echo "Documentation configuration"
256
257 CONF_OPTS+=("--enable-python-bindings" "--enable-python-bindings-doc" "--enable-python-plugins" "--enable-api-doc")
258 ;;
259
260 asan)
261 echo "Address Sanitizer configuration"
262
263 # --enable-asan was introduced after 2.0 but don't check the version, we
264 # want this configuration to fail if ASAN is unavailable.
265 CONF_OPTS+=("--enable-asan" "--enable-python-bindings" "--enable-python-plugins")
266 ;;
267
268 min)
269 echo "Minimal configuration"
270 ;;
271
272 *)
273 echo "Standard configuration"
274
275 # Enable the python bindings / plugins by default with babeltrace2,
276 # the test suite is mostly useless without it.
277 if vergte "$PACKAGE_VERSION" "2.0"; then
278 CONF_OPTS+=("--enable-python-bindings" "--enable-python-plugins")
279 fi
280
281 # Something is broken in docbook-xml on yocto
282 if [[ "$platform" = yocto* ]]; then
283 CONF_OPTS+=("--disable-man-pages")
284 fi
285 ;;
286 esac
287
288 # Build type
289 # oot : out-of-tree build
290 # dist : build via make dist
291 # oot-dist: build via make dist out-of-tree
292 # * : normal tree build
293 #
294 # Make sure to move to the build directory and run configure
295 # before continuing.
296 case "$build" in
297 oot)
298 echo "Out of tree build"
299
300 # Create and enter a temporary build directory
301 builddir=$(mktemp -d)
302 cd "$builddir"
303
304 "$SRCDIR/configure" "${CONF_OPTS[@]}" || failed_configure
305 ;;
306
307 dist)
308 echo "Distribution in-tree build"
309
310 # Run configure and generate the tar file
311 # in the source directory
312 ./configure || failed_configure
313 $MAKE dist
314
315 # Create and enter a temporary build directory
316 builddir=$(mktemp -d)
317 cd "$builddir"
318
319 # Extract the distribution tar in the build directory,
320 # ignore the first directory level
321 $TAR xvf "$SRCDIR"/*.tar.* --strip 1
322
323 # Build in extracted source tree
324 ./configure "${CONF_OPTS[@]}" || failed_configure
325 ;;
326
327 oot-dist)
328 echo "Distribution out of tree build"
329
330 # Create and enter a temporary build directory
331 builddir=$(mktemp -d)
332 cd "$builddir"
333
334 # Run configure out of tree and generate the tar file
335 "$SRCDIR/configure" || failed_configure
336 $MAKE dist
337
338 dist_srcdir="$(mktemp -d)"
339 cd "$dist_srcdir"
340
341 # Extract the distribution tar in the new source directory,
342 # ignore the first directory level
343 $TAR xvf "$builddir"/*.tar.* --strip 1
344
345 # Create and enter a second temporary build directory
346 builddir="$(mktemp -d)"
347 cd "$builddir"
348
349 # Run configure from the extracted distribution tar,
350 # out of the source tree
351 "$dist_srcdir/configure" "${CONF_OPTS[@]}" || failed_configure
352 ;;
353
354 *)
355 echo "Standard in-tree build"
356 ./configure "${CONF_OPTS[@]}" || failed_configure
357 ;;
358 esac
359
360 # We are now inside a configured build directory
361
362 # BUILD!
363 $MAKE -j "$($NPROC)" V=1
364
365 # Install in the workspace
366 $MAKE install DESTDIR="$WORKSPACE"
367
368 # Run tests, don't fail now, we want to run the archiving steps
369 failed_tests=0
370 if [ "$BABELTRACE_RUN_TESTS" = "yes" ]; then
371 $MAKE --keep-going check || failed_tests=1
372
373 # Copy tap logs for the jenkins tap parser before cleaning the build dir
374 rsync -a --exclude 'test-suite.log' --include '*/' --include '*.log' --exclude='*' tests/ "$WORKSPACE/tap"
375
376 # Copy the test suites top-level log which includes all tests failures
377 rsync -a --include 'test-suite.log' --include '*/' --exclude='*' tests/ "$WORKSPACE/log"
378
379 # The test suite prior to 1.5 did not produce TAP logs
380 if verlt "$PACKAGE_VERSION" "1.5"; then
381 mkdir -p "$WORKSPACE/tap/no-log"
382 echo "1..1" > "$WORKSPACE/tap/no-log/tests.log"
383 echo "ok 1 - Test suite doesn't support logging" >> "$WORKSPACE/tap/no-log/tests.log"
384 fi
385 fi
386
387 # Clean the build directory
388 $MAKE clean
389
390 # Cleanup rpath in executables and shared libraries
391 find "$WORKSPACE/$PREFIX/bin" -type f -perm -0500 -exec chrpath --delete {} \;
392 find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.so" -exec chrpath --delete {} \;
393
394 # Remove libtool .la files
395 find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.la" -exec rm -f {} \;
396
397 # Exit with failure if any of the tests failed
398 exit $failed_tests
399
400 # EOF
This page took 0.037643 seconds and 5 git commands to generate.