243e647090cc5c992687723ea0ea5778d1a137ca
[lttng-ci.git] / scripts / lttng-tools / build.sh
1 #!/bin/bash
2 #
3 # SPDX-FileCopyrightText: 2016 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 #
7 # shellcheck disable=SC2103
8
9 set -exu
10
11 # Version compare functions
12 vercomp () {
13 set +u
14 if [[ "$1" == "$2" ]]; then
15 return 0
16 fi
17 local IFS=.
18 # Ignore the shellcheck warning, we want splitting to happen based on IFS.
19 # shellcheck disable=SC2206
20 local i ver1=($1) ver2=($2)
21 # fill empty fields in ver1 with zeros
22 for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
23 ver1[i]=0
24 done
25 for ((i=0; i<${#ver1[@]}; i++)); do
26 if [[ -z ${ver2[i]} ]]; then
27 # fill empty fields in ver2 with zeros
28 ver2[i]=0
29 fi
30 if ((10#${ver1[i]} > 10#${ver2[i]})); then
31 return 1
32 fi
33 if ((10#${ver1[i]} < 10#${ver2[i]})); then
34 return 2
35 fi
36 done
37 set -u
38 return 0
39 }
40
41 verlte() {
42 vercomp "$1" "$2"; local res="$?"
43 [ "$res" -eq "0" ] || [ "$res" -eq "2" ]
44 }
45
46 verlt() {
47 vercomp "$1" "$2"; local res="$?"
48 [ "$res" -eq "2" ]
49 }
50
51 vergte() {
52 vercomp "$1" "$2"; local res="$?"
53 [ "$res" -eq "0" ] || [ "$res" -eq "1" ]
54 }
55
56 vergt() {
57 vercomp "$1" "$2"; local res="$?"
58 [ "$res" -eq "1" ]
59 }
60
61 verne() {
62 vercomp "$1" "$2"; local res="$?"
63 [ "$res" -ne "0" ]
64 }
65
66 print_header() {
67 set +x
68
69 local message=" $1 "
70 local message_len
71 local padding_len
72
73 message_len="${#message}"
74 padding_len=$(( (80 - (message_len)) / 2 ))
75
76
77 printf '\n'; printf -- '#%.0s' {1..80}; printf '\n'
78 printf -- '-%.0s' {1..80}; printf '\n'
79 printf -- '#%.0s' $(seq 1 $padding_len); printf '%s' "$message"; printf -- '#%.0s' $(seq 1 $padding_len); printf '\n'
80 printf -- '-%.0s' {1..80}; printf '\n'
81 printf -- '#%.0s' {1..80}; printf '\n\n'
82
83 set -x
84 }
85
86 failed_configure() {
87 # Assume we are in the configured build directory
88 print_header "BEGIN config.log"
89 cat config.log
90 print_header "END config.log"
91
92 # End the build with failure
93 exit 1
94 }
95
96 set_execute_traversal_bit()
97 {
98 path=$1
99
100 level="$path"
101 if [ ! -d "$path" ]; then
102 fail "Path is not a directory"
103 fi
104 while level="$(dirname "$level")"
105 do
106 if [ "$level" = / ]; then
107 break
108 fi
109 chmod a+x "$level"
110 done
111 chmod a+x "$path"
112 }
113
114 print_header "LTTng-tools build script starting"
115
116 # Required variables
117 WORKSPACE=${WORKSPACE:-}
118
119 # Axis
120 platform=${platform:-}
121 conf=${conf:-}
122 build=${build:-}
123 cc=${cc:-}
124
125 # Build steps that can be overriden by the environment
126 LTTNG_TOOLS_MAKE_INSTALL="${LTTNG_TOOLS_MAKE_INSTALL:-yes}"
127 LTTNG_TOOLS_MAKE_CLEAN="${LTTNG_TOOLS_MAKE_CLEAN:-yes}"
128 LTTNG_TOOLS_GEN_COMPILE_COMMANDS="${LTTNG_TOOLS_GEN_COMPILE_COMMANDS:-no}"
129 LTTNG_TOOLS_RUN_TESTS="${LTTNG_TOOLS_RUN_TESTS:-yes}"
130 LTTNG_TOOLS_RUN_TESTS_LONG_REGRESSION="${LTTNG_TOOLS_RUN_TESTS_LONG_REGRESSION:-no}"
131 LTTNG_TOOLS_CLANG_TIDY="${LTTNG_TOOLS_CLANG_TIDY:-no}"
132
133 SRCDIR="$WORKSPACE/src/lttng-tools"
134 TAPDIR="$WORKSPACE/tap"
135 PREFIX="/build"
136 LIBDIR="lib"
137 LIBDIR_ARCH="$LIBDIR"
138
139 # RHEL and SLES both use lib64 but don't bother shipping a default autoconf
140 # site config that matches this.
141 if [[ ( -f /etc/redhat-release || -f /etc/products.d/SLES.prod || -f /etc/yocto-release ) ]]; then
142 # Detect the userspace bitness in a distro agnostic way
143 if file -L /bin/bash | grep '64-bit' >/dev/null 2>&1; then
144 LIBDIR_ARCH="${LIBDIR}64"
145 fi
146 fi
147
148 DEPS_INC="$WORKSPACE/deps/build/include"
149 DEPS_LIB="$WORKSPACE/deps/build/$LIBDIR_ARCH"
150 DEPS_PKGCONFIG="$DEPS_LIB/pkgconfig"
151 DEPS_BIN="$WORKSPACE/deps/build/bin"
152 DEPS_JAVA="$WORKSPACE/deps/build/share/java"
153
154 export PATH="$DEPS_BIN:$PATH"
155 export LD_LIBRARY_PATH="$DEPS_LIB:${LD_LIBRARY_PATH:-}"
156 export PKG_CONFIG_PATH="$DEPS_PKGCONFIG"
157 export CPPFLAGS="-I$DEPS_INC"
158 export LDFLAGS="-L$DEPS_LIB"
159
160 exit_status=0
161
162 # Use bear to generate compile_commands.json when enabled
163 BEAR=""
164 if [ "$LTTNG_TOOLS_GEN_COMPILE_COMMANDS" = "yes" ]; then
165 BEAR="bear"
166 fi
167
168 # Create tmp directory
169 TMPDIR="$WORKSPACE/tmp"
170 mkdir -p "$TMPDIR"
171
172 # Use a symlink in /tmp to point to the the tmp directory
173 # inside the workspace, this is to work around the path length
174 # limit of unix sockets which are created by the test suite.
175 tmpdir="$(mktemp)"
176 ln -sf "$TMPDIR" "$tmpdir"
177 export TMPDIR="$tmpdir"
178
179 # Create a symlink to "babeltrace" when the "babeltrace2" executable is found.
180 # This is a temporary workaround until lttng-tools either allows the override of
181 # the trace reader in its test suite or that we move to only supporting
182 # babeltrace2
183 if [ -x "$DEPS_BIN/babeltrace2" ]; then
184 ln -s "$DEPS_BIN/babeltrace2" "$DEPS_BIN/babeltrace"
185 fi
186
187 # When using babeltrace2 make sure that it finds its plugins and
188 # plugin-providers.
189 export BABELTRACE_PLUGIN_PATH="$DEPS_LIB/babeltrace2/plugins/"
190 export LIBBABELTRACE2_PLUGIN_PROVIDER_DIR="$DEPS_LIB/babeltrace2/plugin-providers/"
191
192 export CFLAGS="-g -O2"
193 export CXXFLAGS="-g -O2"
194
195 # Set compiler variables
196 case "$cc" in
197 gcc)
198 export CC=gcc
199 export CXX=g++
200 ;;
201 gcc-*)
202 export CC=gcc-${cc#gcc-}
203 export CXX=g++-${cc#gcc-}
204 ;;
205 clang)
206 export CC=clang
207 export CXX=clang++
208 ;;
209 clang-*)
210 export CC=clang-${cc#clang-}
211 export CXX=clang++-${cc#clang-}
212 ;;
213 *)
214 if [ "x$cc" != "x" ]; then
215 export CC="$cc"
216 fi
217 ;;
218 esac
219
220 # Set platform variables
221 case "$platform" in
222 macos*)
223 export MAKE=make
224 export TAR=tar
225 export NPROC="getconf _NPROCESSORS_ONLN"
226 export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
227 export CPPFLAGS="-I/opt/local/include $CPPFLAGS"
228 export LDFLAGS="-L/opt/local/lib $LDFLAGS"
229 export PYTHON="python3"
230 export PYTHON_CONFIG="python3-config"
231 ;;
232
233 cygwin|cygwin64|msys32|msys64)
234 export MAKE=make
235 export TAR=tar
236 export NPROC=nproc
237 ;;
238
239 *)
240 export MAKE=make
241 export TAR=tar
242 export NPROC=nproc
243
244 PYTHON2=python2
245 PYTHON3=python3
246
247 if command -v $PYTHON2 >/dev/null 2>&1; then
248 P2_VERSION=$($PYTHON2 -c 'import sys;v = sys.version.split()[0].split("."); print("{}.{}".format(v[0], v[1]))')
249 DEPS_PYTHON2="$WORKSPACE/deps/build/$LIBDIR/python$P2_VERSION/site-packages"
250 if [ "$LIBDIR" != "$LIBDIR_ARCH" ]; then
251 DEPS_PYTHON2="$DEPS_PYTHON2:$WORKSPACE/deps/build/$LIBDIR_ARCH/python$P2_VERSION/site-packages"
252 fi
253 fi
254
255 P3_VERSION=$($PYTHON3 -c 'import sys;v = sys.version.split()[0].split("."); print("{}.{}".format(v[0], v[1]))')
256 DEPS_PYTHON3="$WORKSPACE/deps/build/$LIBDIR/python$P3_VERSION/site-packages"
257 if [ "$LIBDIR" != "$LIBDIR_ARCH" ]; then
258 DEPS_PYTHON3="$DEPS_PYTHON3:$WORKSPACE/deps/build/$LIBDIR_ARCH/python$P3_VERSION/site-packages"
259 fi
260
261 # Most build configs require access to the babeltrace 2 python bindings.
262 # This also makes the lttngust python agent available for `agents` builds.
263 export PYTHONPATH="${DEPS_PYTHON2:-}${DEPS_PYTHON2:+:}$DEPS_PYTHON3"
264 ;;
265 esac
266
267 # Some warning flags are very dumb in GCC 4.8 on SLES12 / EL7, disable them
268 # even if they are available.
269 if [[ $platform = sles12sp5* ]] || [[ $platform = el7* ]]; then
270 CFLAGS="$CFLAGS -Wno-missing-field-initializers -Wno-shadow"
271 CXXFLAGS="$CXXFLAGS -Wno-missing-field-initializers -Wno-shadow"
272 fi
273
274 # If we have modules, build them
275 if [ -d "$WORKSPACE/src/lttng-modules" ]; then
276 print_header "Build and install LTTng-modules"
277 cd "$WORKSPACE/src/lttng-modules"
278 $MAKE -j"$($NPROC)" V=1
279 $MAKE modules_install V=1
280 depmod
281 fi
282
283 # Print build env details
284 print_header "Build environment details"
285 print_os || true
286 print_tooling || true
287
288 # Enter the source directory
289 cd "$SRCDIR"
290
291 # Run bootstrap in the source directory prior to configure
292 print_header "Bootstrap autotools"
293 ./bootstrap
294
295 # Get source version from configure script
296 eval "$(grep '^PACKAGE_VERSION=' ./configure)"
297 PACKAGE_VERSION=${PACKAGE_VERSION//\-pre*/}
298
299 CONF_OPTS=("--prefix=$PREFIX" "--libdir=$PREFIX/$LIBDIR_ARCH" "--disable-maintainer-mode")
300 DIST_CONF_OPTS=("--disable-maintainer-mode")
301
302 # Set configure options and environment variables for each build
303 # configuration.
304 case "$conf" in
305 static)
306 print_header "Conf: Static lib only"
307
308 CONF_OPTS+=("--enable-static" "--disable-shared" "--enable-python-bindings")
309 ;;
310
311 no-ust)
312 print_header "Conf: Without UST support"
313
314 CONF_OPTS+=("--without-lttng-ust")
315 DIST_CONF_OPTS+=("--without-lttng-ust")
316 ;;
317
318 agents)
319 print_header "Conf: Java and Python agents"
320
321 export JAVA_HOME="/usr/lib/jvm/default-java"
322 export CLASSPATH="$DEPS_JAVA/lttng-ust-agent-all.jar:/usr/share/java/log4j-api.jar:/usr/share/java/log4j-core.jar:/usr/share/java/log4j-1.2.jar"
323
324 CONF_OPTS+=("--enable-python-bindings" "--enable-test-java-agent-all")
325
326 # Explicitly add '--enable-test-java-agent-log4j2', it's not part of '-all' in stable 2.12/2.13
327 if verlt "$PACKAGE_VERSION" "2.14"; then
328 CONF_OPTS+=("--enable-test-java-agent-log4j2")
329 fi
330
331 # Some distros don't ship python2 anymore
332 if command -v $PYTHON2 >/dev/null 2>&1; then
333 CONF_OPTS+=("--enable-test-python-agent-all")
334 else
335 CONF_OPTS+=("--enable-test-python3-agent")
336 fi
337 ;;
338
339 relayd-only)
340 print_header "Conf: Relayd only"
341
342 CONF_OPTS+=("--disable-bin-lttng" "--disable-bin-lttng-consumerd" "--disable-bin-lttng-crash" "--disable-bin-lttng-sessiond" "--disable-extras" "--disable-man-pages" "--without-lttng-ust")
343 ;;
344
345 debug-rcu)
346 print_header "Conf: RCU sanity checks for debugging"
347
348 CONF_OPTS+=("--enable-python-bindings")
349
350 export CPPFLAGS="$CPPFLAGS -DDEBUG_RCU"
351 ;;
352
353 *)
354 print_header "Conf: Standard"
355
356 CONF_OPTS+=("--enable-python-bindings")
357
358 # Something is broken in docbook-xml on yocto
359 if [[ "$platform" = yocto* ]]; then
360 CONF_OPTS+=("--disable-man-pages")
361 fi
362 ;;
363 esac
364
365 # Build type
366 # oot : out-of-tree build
367 # dist : build via make dist
368 # oot-dist: build via make dist out-of-tree
369 # * : normal tree build
370 #
371 # Make sure to move to the build directory and run configure
372 # before continuing.
373 case "$build" in
374 oot)
375 print_header "Build: Out of tree"
376
377 # Create and enter a temporary build directory
378 builddir=$(mktemp -d)
379 cd "$builddir"
380
381 "$SRCDIR/configure" "${CONF_OPTS[@]}" || failed_configure
382 ;;
383
384 dist)
385 print_header "Build: Distribution in-tree"
386
387 # Run configure and generate the tar file
388 # in the source directory
389
390 ./configure "${DIST_CONF_OPTS[@]}" || failed_configure
391 $MAKE dist
392
393 # Create and enter a temporary build directory
394 builddir=$(mktemp -d)
395 cd "$builddir"
396
397 # Extract the distribution tar in the build directory,
398 # ignore the first directory level
399 $TAR xvf "$SRCDIR"/*.tar.* --strip 1
400
401 # Build in extracted source tree
402 ./configure "${CONF_OPTS[@]}" || failed_configure
403 ;;
404
405 oot-dist)
406 print_header "Build: Distribution Out of tree"
407
408 # Create and enter a temporary build directory
409 builddir=$(mktemp -d)
410 cd "$builddir"
411
412 # Run configure out of tree and generate the tar file
413 "$SRCDIR/configure" "${DIST_CONF_OPTS[@]}" || failed_configure
414 $MAKE dist
415
416 dist_srcdir="$(mktemp -d)"
417 cd "$dist_srcdir"
418
419 # Extract the distribution tar in the new source directory,
420 # ignore the first directory level
421 $TAR xvf "$builddir"/*.tar.* --strip 1
422
423 # Create and enter a second temporary build directory
424 builddir="$(mktemp -d)"
425 cd "$builddir"
426
427 # Run configure from the extracted distribution tar,
428 # out of the source tree
429 "$dist_srcdir/configure" "${CONF_OPTS[@]}" || failed_configure
430 ;;
431
432 *)
433 print_header "Build: Standard In-tree"
434 ./configure "${CONF_OPTS[@]}" || failed_configure
435 ;;
436 esac
437
438 # We are now inside a configured build directory
439
440 # BUILD!
441 print_header "BUILD!"
442 $BEAR ${BEAR:+--} $MAKE -j "$($NPROC)" V=1
443
444 # Install in the workspace if enabled
445 if [ "$LTTNG_TOOLS_MAKE_INSTALL" = "yes" ]; then
446 print_header "Install"
447
448 $MAKE install V=1 DESTDIR="$WORKSPACE"
449
450 # Cleanup rpath in executables
451 find "$WORKSPACE/$PREFIX/bin" -type f -perm -0500 -exec chrpath --delete {} \;
452
453 # Some configs don't build liblttng-ctl
454 if [ -d "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" ]; then
455 # Cleanup rpath in shared libraries
456 find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.so" -exec chrpath --delete {} \;
457 # Remove libtool .la files
458 find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.la" -delete
459 fi
460 fi
461
462 # Run clang-tidy on the topmost commit
463 if [ "$LTTNG_TOOLS_CLANG_TIDY" = "yes" ]; then
464 print_header "Run clang-tidy"
465
466 # This would be better by linting only the lines touched by a patch but it
467 # doesn't seem to work, the lines are always filtered and no error is
468 # reported.
469 #git diff -U0 HEAD^ | clang-tidy-diff -p1 -j "$($NPROC)" -timeout 60 -fix
470
471 # Instead, run clan-tidy on all the files touched by the patch.
472 while read -r filepath; do
473 if [[ "$filepath" =~ (\.cpp|\.hhp|\.c|\.h)$ ]]; then
474 clang-tidy --fix-errors "$(realpath "$filepath")"
475 fi
476 done < <(git diff-tree --no-commit-id --diff-filter=d --name-only -r HEAD)
477
478 # If the tree has local changes, the formatting was incorrect
479 GIT_DIFF_OUTPUT=$(git diff)
480 if [ -n "$GIT_DIFF_OUTPUT" ]; then
481 echo "Saving clang-tidy proposed fixes in clang-tidy-fixes.diff"
482 git diff > "$WORKSPACE/clang-tidy-fixes.diff"
483
484 # Restore the unfixed files so they can be viewed in the warnings web
485 # interface
486 git checkout .
487 exit_status=1
488 fi
489 fi
490
491 # Run tests for all configs except 'no-ust' / 'relayd-only'
492 if [ "$LTTNG_TOOLS_RUN_TESTS" = "yes" ] && [[ ! "$conf" =~ (no-ust|relayd-only) ]]; then
493 print_header "Run test suite"
494
495 # Allow core dumps
496 ulimit -c unlimited
497
498 # Force the lttng-sessiond path to /bin/true to prevent the spawing of a
499 # lttng-sessiond --daemonize on "lttng create"
500 export LTTNG_SESSIOND_PATH="/bin/true"
501
502 # It is implied that tests depending on LTTNG_ENABLE_DESTRUCTIVE_TESTS
503 # only run for the root user. Note that here `destructive` means that
504 # operations are performed at the host level (add user etc.) that
505 # effectively modify the host. Running those tests are acceptable on our
506 # CI and root jobs since we always run root tests against a `snapshot`
507 # of the host.
508 if [ "$(id -u)" == "0" ]; then
509 # Allow the traversal of all directories leading to the
510 # DEPS_LIBS directory to enable test app run by temp users to
511 # access lttng-ust.
512 set_execute_traversal_bit "$DEPS_LIB"
513 # Allow `all` to interact with all deps libs.
514 chmod a+rwx -R "$DEPS_LIB"
515
516 export LTTNG_ENABLE_DESTRUCTIVE_TESTS="will-break-my-system"
517
518 # Some destructive tests play with the system clock, disable timesyncd
519 systemctl stop systemd-timesyncd.service || true
520 fi
521
522 make --keep-going check || exit_status=1
523
524 # Copy tap logs for the jenkins tap parser before cleaning the build dir
525 rsync -a --exclude 'test-suite.log' --include '*/' --include '*.log' --exclude='*' tests/ "$TAPDIR"
526
527 # Copy the test suites top-level log which includes all tests failures
528 rsync -a --include 'test-suite.log' --include '*/' --exclude='*' tests/ "$WORKSPACE/log"
529
530 if [ "$LTTNG_TOOLS_RUN_TESTS_LONG_REGRESSION" = "yes" ]; then
531 print_header "Run long regression tests"
532 cd tests
533 mkdir -p "$TAPDIR/long_regression"
534 prove --merge -v --exec '' - < long_regression --archive "$TAPDIR/long_regression/" || exit_status=1
535 cd ..
536 fi
537 fi
538
539 if [ "$LTTNG_TOOLS_RUN_TESTS" = "yes" ] && [[ "$conf" =~ (no-ust|relayd-only) ]]; then
540 # The TAP plugin will fail the job if no test logs are present
541 mkdir -p "$TAPDIR/no-tests"
542 echo "1..1" > "$TAPDIR/no-tests/tests.log"
543 echo "ok 1 - Test suite disabled" >> "$TAPDIR/no-tests/tests.log"
544 fi
545
546 # Clean the build directory
547 if [ "$LTTNG_TOOLS_MAKE_CLEAN" = "yes" ]; then
548 print_header "Clean"
549 $MAKE clean
550 fi
551
552 print_header "LTTng-tools build script ended with: $(test $exit_status == 0 && echo SUCCESS || echo FAILURE)"
553
554 # Exit with failure if any of the tests failed
555 exit $exit_status
556
557 # EOF
558 # vim: expandtab tabstop=4 shiftwidth=4
This page took 0.068728 seconds and 3 git commands to generate.