| 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 | verlte() { |
| 40 | vercomp "$1" "$2"; local res="$?" |
| 41 | [ "$res" -eq "0" ] || [ "$res" -eq "2" ] |
| 42 | } |
| 43 | |
| 44 | verlt() { |
| 45 | vercomp "$1" "$2"; local res="$?" |
| 46 | [ "$res" -eq "2" ] |
| 47 | } |
| 48 | |
| 49 | vergte() { |
| 50 | vercomp "$1" "$2"; local res="$?" |
| 51 | [ "$res" -eq "0" ] || [ "$res" -eq "1" ] |
| 52 | } |
| 53 | |
| 54 | vergt() { |
| 55 | vercomp "$1" "$2"; local res="$?" |
| 56 | [ "$res" -eq "1" ] |
| 57 | } |
| 58 | |
| 59 | verne() { |
| 60 | vercomp "$1" "$2"; local res="$?" |
| 61 | [ "$res" -ne "0" ] |
| 62 | } |
| 63 | |
| 64 | mktemp_compat() { |
| 65 | case "$platform" in |
| 66 | macos*) |
| 67 | # On MacOSX, mktemp doesn't respect TMPDIR in the same way as many |
| 68 | # other systems. Use the final positional argument to force the |
| 69 | # tempfile or tempdir to be created inside $TMPDIR, which must |
| 70 | # already exist. |
| 71 | if [ -n "${TMPDIR}" ] ; then |
| 72 | mktemp "${@}" "${TMPDIR}/tmp.XXXXXXXXXX" |
| 73 | else |
| 74 | mktemp "${@}" |
| 75 | fi |
| 76 | ;; |
| 77 | *) |
| 78 | mktemp "${@}" |
| 79 | ;; |
| 80 | esac |
| 81 | } |
| 82 | |
| 83 | print_header() { |
| 84 | set +x |
| 85 | |
| 86 | local message=" $1 " |
| 87 | local message_len |
| 88 | local padding_len |
| 89 | |
| 90 | message_len="${#message}" |
| 91 | padding_len=$(( (80 - (message_len)) / 2 )) |
| 92 | |
| 93 | printf '\n'; printf -- '#%.0s' {1..80}; printf '\n' |
| 94 | printf -- '-%.0s' {1..80}; printf '\n' |
| 95 | printf -- '#%.0s' $(seq 1 $padding_len); printf '%s' "$message"; printf -- '#%.0s' $(seq 1 $padding_len); printf '\n' |
| 96 | printf -- '-%.0s' {1..80}; printf '\n' |
| 97 | printf -- '#%.0s' {1..80}; printf '\n\n' |
| 98 | |
| 99 | set -x |
| 100 | } |
| 101 | |
| 102 | failed_configure() { |
| 103 | # Assume we are in the configured build directory |
| 104 | print_header "BEGIN config.log" |
| 105 | cat config.log |
| 106 | print_header "END config.log" |
| 107 | |
| 108 | # End the build with failure |
| 109 | exit 1 |
| 110 | } |
| 111 | |
| 112 | print_header "Liburcu build script starting" |
| 113 | |
| 114 | # Required variables |
| 115 | WORKSPACE=${WORKSPACE:-} |
| 116 | |
| 117 | # Axis |
| 118 | platform=${platform:-} |
| 119 | conf=${conf:-} |
| 120 | build=${build:-} |
| 121 | cc=${cc:-} |
| 122 | |
| 123 | # Build steps that can be overriden by the environment |
| 124 | USERSPACE_RCU_MAKE_INSTALL="${USERSPACE_RCU_MAKE_INSTALL:-yes}" |
| 125 | USERSPACE_RCU_MAKE_CLEAN="${USERSPACE_RCU_MAKE_CLEAN:-yes}" |
| 126 | USERSPACE_RCU_GEN_COMPILE_COMMANDS="${USERSPACE_RCU_GEN_COMPILE_COMMANDS:-no}" |
| 127 | USERSPACE_RCU_RUN_TESTS="${USERSPACE_RCU_RUN_TESTS:-yes}" |
| 128 | USERSPACE_RCU_CLANG_TIDY="${USERSPACE_RCU_CLANG_TIDY:-no}" |
| 129 | |
| 130 | SRCDIR="$WORKSPACE/src/liburcu" |
| 131 | TMPDIR="$WORKSPACE/tmp" |
| 132 | PREFIX="/build" |
| 133 | LIBDIR="lib" |
| 134 | LIBDIR_ARCH="$LIBDIR" |
| 135 | |
| 136 | # RHEL and SLES both use lib64 but don't bother shipping a default autoconf |
| 137 | # site config that matches this. |
| 138 | if [[ ( -f /etc/redhat-release || -f /etc/products.d/SLES.prod || -f /etc/yocto-release ) ]]; then |
| 139 | # Detect the userspace bitness in a distro agnostic way |
| 140 | if file -L /bin/bash | grep '64-bit' >/dev/null 2>&1; then |
| 141 | LIBDIR_ARCH="${LIBDIR}64" |
| 142 | fi |
| 143 | fi |
| 144 | |
| 145 | exit_status=0 |
| 146 | |
| 147 | # Use bear to generate compile_commands.json when enabled |
| 148 | BEAR="" |
| 149 | if [ "$USERSPACE_RCU_GEN_COMPILE_COMMANDS" = "yes" ]; then |
| 150 | BEAR="bear" |
| 151 | fi |
| 152 | |
| 153 | # Create tmp directory |
| 154 | rm -rf "$TMPDIR" |
| 155 | mkdir -p "$TMPDIR" |
| 156 | |
| 157 | export TMPDIR |
| 158 | export CFLAGS="-g -O2" |
| 159 | export CXXFLAGS="-g -O2" |
| 160 | |
| 161 | # Set compiler variables |
| 162 | case "$cc" in |
| 163 | gcc) |
| 164 | export CC=gcc |
| 165 | export CXX=g++ |
| 166 | ;; |
| 167 | gcc-*) |
| 168 | export CC=gcc-${cc#gcc-} |
| 169 | export CXX=g++-${cc#gcc-} |
| 170 | ;; |
| 171 | clang) |
| 172 | export CC=clang |
| 173 | export CXX=clang++ |
| 174 | ;; |
| 175 | clang-*) |
| 176 | export CC=clang-${cc#clang-} |
| 177 | export CXX=clang++-${cc#clang-} |
| 178 | ;; |
| 179 | *) |
| 180 | if [ "x$cc" != "x" ]; then |
| 181 | export CC="$cc" |
| 182 | fi |
| 183 | ;; |
| 184 | esac |
| 185 | |
| 186 | # Set platform variables |
| 187 | case "$platform" in |
| 188 | macos*) |
| 189 | export MAKE=make |
| 190 | export TAR=tar |
| 191 | export NPROC="getconf _NPROCESSORS_ONLN" |
| 192 | export PATH="/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" |
| 193 | export CPPFLAGS="-I/opt/local/include" |
| 194 | export LDFLAGS="-L/opt/local/lib" |
| 195 | export PYTHON="python3" |
| 196 | export PYTHON_CONFIG="python3-config" |
| 197 | ;; |
| 198 | |
| 199 | freebsd*) |
| 200 | export MAKE=gmake |
| 201 | export TAR=tar |
| 202 | export NPROC="getconf _NPROCESSORS_ONLN" |
| 203 | export CPPFLAGS="-I/usr/local/include" |
| 204 | export LDFLAGS="-L/usr/local/lib" |
| 205 | export PYTHON="python3" |
| 206 | export PYTHON_CONFIG="python3-config" |
| 207 | ;; |
| 208 | |
| 209 | *) |
| 210 | export MAKE=make |
| 211 | export TAR=tar |
| 212 | export NPROC=nproc |
| 213 | export PYTHON="python3" |
| 214 | export PYTHON_CONFIG="python3-config" |
| 215 | ;; |
| 216 | esac |
| 217 | |
| 218 | # Print build env details |
| 219 | print_header "Build environment details" |
| 220 | print_hardware || true |
| 221 | print_os || true |
| 222 | print_tooling || true |
| 223 | |
| 224 | # Enter the source directory |
| 225 | cd "$SRCDIR" |
| 226 | |
| 227 | # Run bootstrap in the source directory prior to configure |
| 228 | print_header "Bootstrap autotools" |
| 229 | ./bootstrap |
| 230 | |
| 231 | # Get source version from configure script |
| 232 | eval "$(grep '^PACKAGE_VERSION=' ./configure)" |
| 233 | PACKAGE_VERSION=${PACKAGE_VERSION//\-pre*/} |
| 234 | |
| 235 | # Set configure options and environment variables for each build |
| 236 | # configuration. |
| 237 | CONF_OPTS=("--prefix=$PREFIX" "--libdir=$PREFIX/$LIBDIR_ARCH" "--disable-maintainer-mode") |
| 238 | case "$conf" in |
| 239 | static) |
| 240 | print_header "Conf: Static lib only" |
| 241 | |
| 242 | CONF_OPTS+=("--enable-static" "--disable-shared") |
| 243 | ;; |
| 244 | |
| 245 | tls_fallback) |
| 246 | print_header "Conf: Use pthread_getspecific() to emulate TLS" |
| 247 | |
| 248 | CONF_OPTS+=("--disable-compiler-tls") |
| 249 | ;; |
| 250 | |
| 251 | debug-rcu) |
| 252 | print_header "Conf: Enable RCU sanity checks for debugging" |
| 253 | |
| 254 | if vergte "$PACKAGE_VERSION" "0.10"; then |
| 255 | CONF_OPTS+=("--enable-rcu-debug") |
| 256 | else |
| 257 | export CFLAGS="$CFLAGS -DDEBUG_RCU" |
| 258 | fi |
| 259 | |
| 260 | echo "Enable iterator sanity validator" |
| 261 | if vergte "$PACKAGE_VERSION" "0.11"; then |
| 262 | CONF_OPTS+=("--enable-cds-lfht-iter-debug") |
| 263 | fi |
| 264 | ;; |
| 265 | |
| 266 | atomic-builtins) |
| 267 | print_header "Conf: Enable the use of compiler atomic builtins." |
| 268 | |
| 269 | CONF_OPTS+=("--enable-compiler-atomic-builtins") |
| 270 | ;; |
| 271 | |
| 272 | *) |
| 273 | print_header "Conf: Standard" |
| 274 | ;; |
| 275 | esac |
| 276 | |
| 277 | # Build type |
| 278 | # oot : out-of-tree build |
| 279 | # dist : build via make dist |
| 280 | # oot-dist: build via make dist out-of-tree |
| 281 | # * : normal tree build |
| 282 | # |
| 283 | # Make sure to move to the build directory and run configure |
| 284 | # before continuing. |
| 285 | case "$build" in |
| 286 | oot) |
| 287 | print_header "Build: Out of tree" |
| 288 | |
| 289 | # Create and enter a temporary build directory |
| 290 | builddir=$(mktemp_compat -d) |
| 291 | cd "$builddir" |
| 292 | |
| 293 | "$SRCDIR/configure" "${CONF_OPTS[@]}" || failed_configure |
| 294 | ;; |
| 295 | |
| 296 | dist) |
| 297 | print_header "Build: Distribution In-tree" |
| 298 | |
| 299 | # Run configure and generate the tar file |
| 300 | # in the source directory |
| 301 | ./configure || failed_configure |
| 302 | $MAKE dist |
| 303 | |
| 304 | # Create and enter a temporary build directory |
| 305 | builddir=$(mktemp_compat -d) |
| 306 | cd "$builddir" |
| 307 | |
| 308 | # Extract the distribution tar in the build directory, |
| 309 | # ignore the first directory level |
| 310 | $TAR xvf "$SRCDIR"/*.tar.* --strip 1 |
| 311 | |
| 312 | # Build in extracted source tree |
| 313 | ./configure "${CONF_OPTS[@]}" || failed_configure |
| 314 | ;; |
| 315 | |
| 316 | oot-dist) |
| 317 | print_header "Build: Distribution Out of tree" |
| 318 | |
| 319 | # Create and enter a temporary build directory |
| 320 | builddir=$(mktemp_compat -d) |
| 321 | cd "$builddir" |
| 322 | |
| 323 | # Run configure out of tree and generate the tar file |
| 324 | "$SRCDIR/configure" || failed_configure |
| 325 | $MAKE dist |
| 326 | |
| 327 | dist_srcdir="$(mktemp_compat -d)" |
| 328 | cd "$dist_srcdir" |
| 329 | |
| 330 | # Extract the distribution tar in the new source directory, |
| 331 | # ignore the first directory level |
| 332 | $TAR xvf "$builddir"/*.tar.* --strip 1 |
| 333 | |
| 334 | # Create and enter a second temporary build directory |
| 335 | builddir="$(mktemp_compat -d)" |
| 336 | cd "$builddir" |
| 337 | |
| 338 | # Run configure from the extracted distribution tar, |
| 339 | # out of the source tree |
| 340 | "$dist_srcdir/configure" "${CONF_OPTS[@]}" || failed_configure |
| 341 | ;; |
| 342 | |
| 343 | *) |
| 344 | print_header "Build: Standard In-tree" |
| 345 | |
| 346 | ./configure "${CONF_OPTS[@]}" || failed_configure |
| 347 | ;; |
| 348 | esac |
| 349 | |
| 350 | # We are now inside a configured build directory |
| 351 | |
| 352 | # BUILD! |
| 353 | print_header "BUILD!" |
| 354 | $BEAR ${BEAR:+--} $MAKE -j "$($NPROC)" V=1 |
| 355 | |
| 356 | # Install in the workspace if enabled |
| 357 | if [ "$USERSPACE_RCU_MAKE_INSTALL" = "yes" ]; then |
| 358 | print_header "Install" |
| 359 | |
| 360 | $MAKE install V=1 DESTDIR="$WORKSPACE" |
| 361 | |
| 362 | # Cleanup rpath in executables and shared libraries |
| 363 | #find "$WORKSPACE/$PREFIX/bin" -type f -perm -0500 -exec chrpath --delete {} \; |
| 364 | find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.so" -exec chrpath --delete {} \; |
| 365 | |
| 366 | # Remove libtool .la files |
| 367 | find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.la" -delete |
| 368 | fi |
| 369 | |
| 370 | # Run clang-tidy on the topmost commit |
| 371 | if [ "$USERSPACE_RCU_CLANG_TIDY" = "yes" ]; then |
| 372 | print_header "Run clang-tidy" |
| 373 | |
| 374 | # This would be better by linting only the lines touched by a patch but it |
| 375 | # doesn't seem to work, the lines are always filtered and no error is |
| 376 | # reported. |
| 377 | #git diff -U0 HEAD^ | clang-tidy-diff -p1 -j "$($NPROC)" -timeout 60 -fix |
| 378 | |
| 379 | # Instead, run clan-tidy on all the files touched by the patch. |
| 380 | while read -r filepath; do |
| 381 | if [[ "$filepath" =~ (\.cpp|\.hhp|\.c|\.h)$ ]]; then |
| 382 | clang-tidy --fix-errors "$(realpath "$filepath")" |
| 383 | fi |
| 384 | done < <(git diff-tree --no-commit-id --diff-filter=d --name-only -r HEAD) |
| 385 | |
| 386 | # If the tree has local changes, the formatting was incorrect |
| 387 | GIT_DIFF_OUTPUT=$(git diff) |
| 388 | if [ -n "$GIT_DIFF_OUTPUT" ]; then |
| 389 | echo "Saving clang-tidy proposed fixes in clang-tidy-fixes.diff" |
| 390 | git diff > "$WORKSPACE/clang-tidy-fixes.diff" |
| 391 | |
| 392 | # Restore the unfixed files so they can be viewed in the warnings web |
| 393 | # interface |
| 394 | git checkout . |
| 395 | exit_status=1 |
| 396 | fi |
| 397 | fi |
| 398 | |
| 399 | # Run tests if enabled |
| 400 | if [ "$USERSPACE_RCU_RUN_TESTS" = "yes" ]; then |
| 401 | print_header "Run test suite" |
| 402 | |
| 403 | # Run tests, don't fail now, we want to run the archiving steps |
| 404 | $MAKE --keep-going check || exit_status=1 |
| 405 | |
| 406 | # Only run regtest for 0.9 and up |
| 407 | if vergte "$PACKAGE_VERSION" "0.9"; then |
| 408 | $MAKE --keep-going regtest || exit_status=1 |
| 409 | fi |
| 410 | |
| 411 | # Copy tap logs for the jenkins tap parser before cleaning the build dir |
| 412 | rsync -a --exclude 'test-suite.log' --include '*/' --include '*.log' --exclude='*' tests/ "$WORKSPACE/tap" |
| 413 | |
| 414 | # Copy the test suites top-level log which includes all tests failures |
| 415 | rsync -a --include 'test-suite.log' --include '*/' --exclude='*' tests/ "$WORKSPACE/log" |
| 416 | |
| 417 | # The test suite prior to 0.11 did not produce TAP logs |
| 418 | if verlt "$PACKAGE_VERSION" "0.11"; then |
| 419 | mkdir -p "$WORKSPACE/tap/no-log" |
| 420 | echo "1..1" > "$WORKSPACE/tap/no-log/tests.log" |
| 421 | echo "ok 1 - Test suite doesn't support logging" >> "$WORKSPACE/tap/no-log/tests.log" |
| 422 | fi |
| 423 | fi |
| 424 | |
| 425 | # Clean the build directory |
| 426 | if [ "$USERSPACE_RCU_MAKE_CLEAN" = "yes" ]; then |
| 427 | print_header "Clean" |
| 428 | $MAKE clean |
| 429 | fi |
| 430 | |
| 431 | print_header "Liburcu build script ended with: $(test $exit_status == 0 && echo SUCCESS || echo FAILURE)" |
| 432 | |
| 433 | # Exit with failure if any of the tests failed |
| 434 | exit $exit_status |
| 435 | |
| 436 | # EOF |
| 437 | # vim: expandtab tabstop=4 shiftwidth=4 |