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