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