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