lava: Install vlttng inside a venv
[lttng-ci.git] / scripts / babeltrace / build.sh
CommitLineData
51c9c62d 1#!/bin/bash
890bff23 2#
1ad4c3d0
MJ
3# SPDX-FileCopyrightText: 2015 Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
4# SPDX-FileCopyrightText: 2016-2023 Michael Jeanson <mjeanson@efficios.com>
5# SPDX-License-Identifier: GPL-2.0-or-later
890bff23 6
51c9c62d
MJ
7set -exu
8
a0b535b2
MJ
9# Version compare functions
10vercomp () {
11 set +u
12 if [[ "$1" == "$2" ]]; then
13 return 0
14 fi
15 local IFS=.
6946ebc0
SM
16 # Ignore the shellcheck warning, we want splitting to happen based on IFS.
17 # shellcheck disable=SC2206
a0b535b2
MJ
18 local i ver1=($1) ver2=($2)
19 # fill empty fields in ver1 with zeros
20 for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
21 ver1[i]=0
22 done
23 for ((i=0; i<${#ver1[@]}; i++)); do
24 if [[ -z ${ver2[i]} ]]; then
25 # fill empty fields in ver2 with zeros
26 ver2[i]=0
27 fi
28 if ((10#${ver1[i]} > 10#${ver2[i]})); then
29 return 1
30 fi
31 if ((10#${ver1[i]} < 10#${ver2[i]})); then
32 return 2
33 fi
34 done
35 set -u
36 return 0
37}
38
6946ebc0
SM
39# Shellcheck flags the following functions that are unused as "unreachable",
40# ignore that.
41
42# shellcheck disable=SC2317
a0b535b2 43verlte() {
6946ebc0
SM
44 vercomp "$1" "$2"
45 local res="$?"
a0b535b2
MJ
46 [ "$res" -eq "0" ] || [ "$res" -eq "2" ]
47}
48
6946ebc0 49# shellcheck disable=SC2317
a0b535b2
MJ
50verlt() {
51 vercomp "$1" "$2"; local res="$?"
52 [ "$res" -eq "2" ]
53}
54
6946ebc0 55# shellcheck disable=SC2317
a0b535b2
MJ
56vergte() {
57 vercomp "$1" "$2"; local res="$?"
58 [ "$res" -eq "0" ] || [ "$res" -eq "1" ]
59}
60
6946ebc0 61# shellcheck disable=SC2317
a0b535b2
MJ
62vergt() {
63 vercomp "$1" "$2"; local res="$?"
64 [ "$res" -eq "1" ]
65}
66
6946ebc0 67# shellcheck disable=SC2317
a0b535b2
MJ
68verne() {
69 vercomp "$1" "$2"; local res="$?"
70 [ "$res" -ne "0" ]
71}
72
1ad4c3d0
MJ
73print_header() {
74 set +x
75
76 local message=" $1 "
77 local message_len
78 local padding_len
79
80 message_len="${#message}"
81 padding_len=$(( (80 - (message_len)) / 2 ))
82
83 printf '\n'; printf -- '#%.0s' {1..80}; printf '\n'
84 printf -- '-%.0s' {1..80}; printf '\n'
85 printf -- '#%.0s' $(seq 1 $padding_len); printf '%s' "$message"; printf -- '#%.0s' $(seq 1 $padding_len); printf '\n'
86 printf -- '-%.0s' {1..80}; printf '\n'
87 printf -- '#%.0s' {1..80}; printf '\n\n'
88
89 set -x
90}
91
51c9c62d
MJ
92failed_configure() {
93 # Assume we are in the configured build directory
1ad4c3d0 94 print_header "BEGIN config.log"
51c9c62d 95 cat config.log
1ad4c3d0 96 print_header "END config.log"
51c9c62d
MJ
97 exit 1
98}
99
1ad4c3d0 100print_header "Babeltrace build script starting"
51c9c62d 101
07dafe32
MJ
102# Required variables
103WORKSPACE=${WORKSPACE:-}
104
1ad4c3d0 105# Axis
776b0d3d 106platform=${platform:-}
a57a60d9
MJ
107conf=${conf:-}
108build=${build:-}
6476f917 109cc=${cc:-}
a57a60d9 110
1ad4c3d0
MJ
111# Build steps that can be overriden by the environment
112BABELTRACE_MAKE_INSTALL="${BABELTRACE_MAKE_INSTALL:-yes}"
113BABELTRACE_MAKE_CLEAN="${BABELTRACE_MAKE_CLEAN:-yes}"
114BABELTRACE_GEN_COMPILE_COMMANDS="${BABELTRACE_GEN_COMPILE_COMMANDS:-no}"
115BABELTRACE_RUN_TESTS="${BABELTRACE_RUN_TESTS:-yes}"
116BABELTRACE_CLANG_TIDY="${BABELTRACE_CLANG_TIDY:-no}"
c56b9301 117
e6be9fb0
MJ
118SRCDIR="$WORKSPACE/src/babeltrace"
119TMPDIR="$WORKSPACE/tmp"
07dafe32 120PREFIX="/build"
4afa623f 121LIBDIR="lib"
32dde2a3 122LIBDIR_ARCH="$LIBDIR"
4afa623f
MJ
123
124# RHEL and SLES both use lib64 but don't bother shipping a default autoconf
125# site config that matches this.
47ca4354 126if [[ ( -f /etc/redhat-release || -f /etc/products.d/SLES.prod || -f /etc/yocto-release ) ]]; then
85322e5d
MJ
127 # Detect the userspace bitness in a distro agnostic way
128 if file -L /bin/bash | grep '64-bit' >/dev/null 2>&1; then
129 LIBDIR_ARCH="${LIBDIR}64"
85322e5d 130 fi
4afa623f 131fi
275b59d2 132
1ad4c3d0
MJ
133exit_status=0
134
135# Use bear to generate compile_commands.json when enabled
136BEAR=""
137if [ "$BABELTRACE_GEN_COMPILE_COMMANDS" = "yes" ]; then
138 BEAR="bear"
139fi
140
07dafe32
MJ
141# Create tmp directory
142rm -rf "$TMPDIR"
143mkdir -p "$TMPDIR"
e6be9fb0 144
0e9967db 145export TMPDIR
72d087d4 146export CFLAGS="-g -O2"
1ad4c3d0 147export CXXFLAGS="-g -O2"
0e9967db 148
6476f917
MJ
149# Set compiler variables
150case "$cc" in
151gcc)
152 export CC=gcc
153 export CXX=g++
154 ;;
0f505d21
MJ
155gcc-*)
156 export CC=gcc-${cc#gcc-}
157 export CXX=g++-${cc#gcc-}
6476f917
MJ
158 ;;
159clang)
160 export CC=clang
161 export CXX=clang++
162 ;;
0f505d21
MJ
163clang-*)
164 export CC=clang-${cc#clang-}
165 export CXX=clang++-${cc#clang-}
6476f917
MJ
166 ;;
167*)
168 if [ "x$cc" != "x" ]; then
1ad4c3d0
MJ
169 echo ""
170 exit 1
6476f917
MJ
171 fi
172 ;;
173esac
174
c56b9301 175# Set platform variables
776b0d3d 176case "$platform" in
f0d7e5b1 177macos*)
a0b535b2
MJ
178 export MAKE=make
179 export TAR=tar
180 export NPROC="getconf _NPROCESSORS_ONLN"
221450b6 181 export PATH="/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
8b15f123 182 export CFLAGS="$CFLAGS -Wno-\#pragma-messages" # Fix warnings with clang14
07dafe32 183 export CPPFLAGS="-I/opt/local/include"
221450b6 184 export LDFLAGS="-L/opt/local/lib"
00f7bb3f
MJ
185 export PYTHON="python3"
186 export PYTHON_CONFIG="python3-config"
221450b6 187 ;;
07dafe32 188
0f505d21 189freebsd*)
894db5f1
MJ
190 export MAKE=gmake
191 export TAR=tar
192 export NPROC="getconf _NPROCESSORS_ONLN"
193 export CPPFLAGS="-I/usr/local/include"
194 export LDFLAGS="-L/usr/local/lib"
195 export PYTHON="python3"
196 export PYTHON_CONFIG="python3-config"
197
198 # For bt 1.5
199 export YACC="bison -y"
200 ;;
201
87e41bca 202*)
a0b535b2
MJ
203 export MAKE=make
204 export TAR=tar
205 export NPROC=nproc
1f620ba0
MJ
206 export PYTHON="python3"
207 export PYTHON_CONFIG="python3-config"
87e41bca
MJ
208 ;;
209esac
210
51c9c62d 211# Print build env details
1ad4c3d0 212print_header "Build environment details"
8c956c4b 213print_hardware || true
51c9c62d
MJ
214print_os || true
215print_tooling || true
216
a0b535b2
MJ
217# Enter the source directory
218cd "$SRCDIR"
219
220# Run bootstrap in the source directory prior to configure
1ad4c3d0 221print_header "Bootstrap autotools"
a0b535b2
MJ
222./bootstrap
223
224# Get source version from configure script
225eval "$(grep '^PACKAGE_VERSION=' ./configure)"
07dafe32 226PACKAGE_VERSION=${PACKAGE_VERSION//\-pre*/}
a0b535b2 227
11ba7264 228# Enable dev mode by default for BT 2.0 builds
9201e00d
MJ
229export BABELTRACE_DEBUG_MODE=1
230export BABELTRACE_DEV_MODE=1
8130d845 231export BABELTRACE_MINIMAL_LOG_LEVEL=TRACE
11ba7264 232
07dafe32
MJ
233# Set configure options and environment variables for each build
234# configuration.
1ad4c3d0 235CONF_OPTS=("--prefix=$PREFIX" "--libdir=$PREFIX/$LIBDIR_ARCH" "--disable-maintainer-mode")
f0d7e5b1
MJ
236
237# -Werror is enabled by default in stable-2.0 but won't be in 2.1
238# Explicitly disable it for consistency.
239if vergte "$PACKAGE_VERSION" "2.0"; then
1ad4c3d0 240 CONF_OPTS+=("--disable-Werror")
f0d7e5b1
MJ
241fi
242
275b59d2
JRJ
243case "$conf" in
244static)
1ad4c3d0 245 print_header "Conf: Static lib only"
1f620ba0
MJ
246
247 CONF_OPTS+=("--enable-static" "--disable-shared")
248
a0b535b2 249 if vergte "$PACKAGE_VERSION" "2.0"; then
1f620ba0 250 CONF_OPTS+=("--enable-built-in-plugins")
a0b535b2 251 fi
275b59d2 252 ;;
1f620ba0 253
221450b6 254python-bindings)
1ad4c3d0 255 print_header "Conf: Python bindings"
1f620ba0
MJ
256
257 CONF_OPTS+=("--enable-python-bindings")
a0b535b2
MJ
258
259 if vergte "$PACKAGE_VERSION" "2.0"; then
1f620ba0 260 CONF_OPTS+=("--enable-python-bindings-doc" "--enable-python-plugins")
a0b535b2 261 fi
275b59d2 262 ;;
1f620ba0 263
0d30552d 264prod)
1ad4c3d0 265 print_header "Conf: Production"
9201e00d
MJ
266
267 # Unset the developper variables
268 unset BABELTRACE_DEBUG_MODE
269 unset BABELTRACE_DEV_MODE
270 unset BABELTRACE_MINIMAL_LOG_LEVEL
271
272 # Enable the python bindings
301b8fc9
MJ
273 CONF_OPTS+=("--enable-python-bindings" "--enable-python-plugins")
274 ;;
275
276doc)
1ad4c3d0 277 print_header "Conf: Documentation"
301b8fc9
MJ
278
279 CONF_OPTS+=("--enable-python-bindings" "--enable-python-bindings-doc" "--enable-python-plugins" "--enable-api-doc")
9201e00d 280 ;;
1f620ba0 281
1b232bc6 282asan)
1ad4c3d0 283 print_header "Conf: Address Sanitizer"
1b232bc6
SM
284
285 # --enable-asan was introduced after 2.0 but don't check the version, we
286 # want this configuration to fail if ASAN is unavailable.
287 CONF_OPTS+=("--enable-asan" "--enable-python-bindings" "--enable-python-plugins")
288 ;;
289
0d30552d 290min)
1ad4c3d0 291 print_header "Conf: Minimal"
0d30552d 292 ;;
1f620ba0 293
275b59d2 294*)
1ad4c3d0 295 print_header "Conf: Standard"
0d30552d 296
1f620ba0 297 # Enable the python bindings / plugins by default with babeltrace2,
0d30552d
MJ
298 # the test suite is mostly useless without it.
299 if vergte "$PACKAGE_VERSION" "2.0"; then
1f620ba0 300 CONF_OPTS+=("--enable-python-bindings" "--enable-python-plugins")
0d30552d 301 fi
6871000c
MJ
302
303 # Something is broken in docbook-xml on yocto
304 if [[ "$platform" = yocto* ]]; then
305 CONF_OPTS+=("--disable-man-pages")
306 fi
275b59d2
JRJ
307 ;;
308esac
309
aad6ac90 310# Build type
07dafe32
MJ
311# oot : out-of-tree build
312# dist : build via make dist
313# oot-dist: build via make dist out-of-tree
314# * : normal tree build
aad6ac90 315#
07dafe32 316# Make sure to move to the build directory and run configure
1f620ba0 317# before continuing.
aad6ac90 318case "$build" in
b8475d72 319oot)
1ad4c3d0 320 print_header "Build: Out of tree"
b8475d72
MJ
321
322 # Create and enter a temporary build directory
323 builddir=$(mktemp -d)
324 cd "$builddir"
325
51c9c62d 326 "$SRCDIR/configure" "${CONF_OPTS[@]}" || failed_configure
b8475d72 327 ;;
1f620ba0 328
b8475d72 329dist)
1ad4c3d0 330 print_header "Build: Distribution In-tree"
1f620ba0 331
b8475d72
MJ
332 # Run configure and generate the tar file
333 # in the source directory
51c9c62d 334 ./configure || failed_configure
b8475d72 335 $MAKE dist
c56b9301 336
b8475d72
MJ
337 # Create and enter a temporary build directory
338 builddir=$(mktemp -d)
339 cd "$builddir"
340
341 # Extract the distribution tar in the build directory,
342 # ignore the first directory level
343 $TAR xvf "$SRCDIR"/*.tar.* --strip 1
344
07dafe32 345 # Build in extracted source tree
51c9c62d 346 ./configure "${CONF_OPTS[@]}" || failed_configure
b8475d72 347 ;;
c56b9301 348
b8475d72 349oot-dist)
1ad4c3d0 350 print_header "Build: Distribution Out of tree"
c56b9301 351
b8475d72
MJ
352 # Create and enter a temporary build directory
353 builddir=$(mktemp -d)
354 cd "$builddir"
c56b9301 355
b8475d72 356 # Run configure out of tree and generate the tar file
51c9c62d 357 "$SRCDIR/configure" || failed_configure
b8475d72 358 $MAKE dist
c56b9301 359
b8475d72
MJ
360 dist_srcdir="$(mktemp -d)"
361 cd "$dist_srcdir"
c56b9301 362
b8475d72
MJ
363 # Extract the distribution tar in the new source directory,
364 # ignore the first directory level
365 $TAR xvf "$builddir"/*.tar.* --strip 1
366
367 # Create and enter a second temporary build directory
368 builddir="$(mktemp -d)"
369 cd "$builddir"
370
371 # Run configure from the extracted distribution tar,
372 # out of the source tree
51c9c62d 373 "$dist_srcdir/configure" "${CONF_OPTS[@]}" || failed_configure
b8475d72
MJ
374 ;;
375
376*)
1ad4c3d0 377 print_header "Build: Standard In-tree"
51c9c62d 378 ./configure "${CONF_OPTS[@]}" || failed_configure
b8475d72 379 ;;
aad6ac90
JR
380esac
381
1f620ba0
MJ
382# We are now inside a configured build directory
383
c56b9301 384# BUILD!
1ad4c3d0
MJ
385print_header "BUILD!"
386$BEAR ${BEAR:+--} $MAKE -j "$($NPROC)" V=1
387
388# Install in the workspace if enabled
389if [ "$BABELTRACE_MAKE_INSTALL" = "yes" ]; then
390 print_header "Install"
391
392 $MAKE install V=1 DESTDIR="$WORKSPACE"
393
394 # Cleanup rpath in executables and shared libraries
395 find "$WORKSPACE/$PREFIX/bin" -type f -perm -0500 -exec chrpath --delete {} \;
396 find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.so" -exec chrpath --delete {} \;
397
398 # Remove libtool .la files
399 find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.la" -delete
400fi
401
402# Run clang-tidy on the topmost commit
403if [ "$BABELTRACE_CLANG_TIDY" = "yes" ]; then
404 print_header "Run clang-tidy"
07dafe32 405
1ad4c3d0
MJ
406 # This would be better by linting only the lines touched by a patch but it
407 # doesn't seem to work, the lines are always filtered and no error is
408 # reported.
409 #git diff -U0 HEAD^ | clang-tidy-diff -p1 -j "$($NPROC)" -timeout 60 -fix
c56b9301 410
1ad4c3d0
MJ
411 # Instead, run clan-tidy on all the files touched by the patch.
412 while read -r filepath; do
413 if [[ "$filepath" =~ (\.cpp|\.hhp|\.c|\.h)$ ]]; then
414 clang-tidy --fix-errors "$(realpath "$filepath")"
415 fi
416 done < <(git diff-tree --no-commit-id --diff-filter=d --name-only -r HEAD)
417
418 # If the tree has local changes, the formatting was incorrect
419 GIT_DIFF_OUTPUT=$(git diff)
420 if [ -n "$GIT_DIFF_OUTPUT" ]; then
421 echo "Saving clang-tidy proposed fixes in clang-tidy-fixes.diff"
422 git diff > "$WORKSPACE/clang-tidy-fixes.diff"
423
424 # Restore the unfixed files so they can be viewed in the warnings web
425 # interface
426 git checkout .
427 exit_status=1
428 fi
429fi
430
431# Run tests if enabled
e3c32202 432if [ "$BABELTRACE_RUN_TESTS" = "yes" ]; then
1ad4c3d0 433 print_header "Run test suite"
e3c32202 434
1ad4c3d0
MJ
435 # Run tests, don't fail now, we want to run the archiving steps
436 $MAKE --keep-going check || exit_status=1
e3c32202 437
1ad4c3d0
MJ
438 # Copy tap logs for the jenkins tap parser before cleaning the build dir
439 rsync -a --exclude 'test-suite.log' --include '*/' --include '*.log' --exclude='*' tests/ "$WORKSPACE/tap"
4174b905 440
1ad4c3d0
MJ
441 # Copy the test suites top-level log which includes all tests failures
442 rsync -a --include 'test-suite.log' --include '*/' --exclude='*' tests/ "$WORKSPACE/log"
649c39a6
MJ
443fi
444
e6be9fb0 445# Clean the build directory
1ad4c3d0
MJ
446if [ "$BABELTRACE_MAKE_CLEAN" = "yes" ]; then
447 print_header "Clean"
448 $MAKE clean
449fi
c56b9301 450
1ad4c3d0 451print_header "Babeltrace build script ended with: $(test $exit_status == 0 && echo SUCCESS || echo FAILURE)"
87e41bca 452
1d56e325 453# Exit with failure if any of the tests failed
1ad4c3d0 454exit $exit_status
9d56171a 455
87e41bca 456# EOF
1ad4c3d0 457# vim: expandtab tabstop=4 shiftwidth=4
This page took 0.054308 seconds and 4 git commands to generate.