jjb: babeltrace: use clang-format-16
[lttng-ci.git] / scripts / liburcu / build.sh
CommitLineData
51c9c62d 1#!/bin/bash
e3022ad9 2#
2c34ea14
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
e3022ad9 6
51c9c62d
MJ
7set -exu
8
5fd8db76 9# Version compare functions
0a6e708b
MJ
10vercomp () {
11 set +u
12 if [[ "$1" == "$2" ]]; then
13 return 0
14 fi
15 local IFS=.
4afa623f
MJ
16 # Ignore the shellcheck warning, we want splitting to happen based on IFS.
17 # shellcheck disable=SC2206
0a6e708b
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
5fd8db76 39verlte() {
0a6e708b
MJ
40 vercomp "$1" "$2"; local res="$?"
41 [ "$res" -eq "0" ] || [ "$res" -eq "2" ]
5fd8db76
MJ
42}
43
44verlt() {
0a6e708b
MJ
45 vercomp "$1" "$2"; local res="$?"
46 [ "$res" -eq "2" ]
5fd8db76
MJ
47}
48
49vergte() {
0a6e708b
MJ
50 vercomp "$1" "$2"; local res="$?"
51 [ "$res" -eq "0" ] || [ "$res" -eq "1" ]
5fd8db76
MJ
52}
53
54vergt() {
0a6e708b
MJ
55 vercomp "$1" "$2"; local res="$?"
56 [ "$res" -eq "1" ]
57}
58
59verne() {
60 vercomp "$1" "$2"; local res="$?"
61 [ "$res" -ne "0" ]
5fd8db76
MJ
62}
63
52be5739
KS
64mktemp_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
2c34ea14
MJ
83print_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
51c9c62d
MJ
102failed_configure() {
103 # Assume we are in the configured build directory
2c34ea14 104 print_header "BEGIN config.log"
51c9c62d 105 cat config.log
2c34ea14 106 print_header "END config.log"
51c9c62d
MJ
107
108 # End the build with failure
109 exit 1
110}
111
2c34ea14
MJ
112print_header "Liburcu build script starting"
113
1d56e325
MJ
114# Required variables
115WORKSPACE=${WORKSPACE:-}
116
2c34ea14 117# Axis
1794bc79 118platform=${platform:-}
a57a60d9
MJ
119conf=${conf:-}
120build=${build:-}
1d56e325 121cc=${cc:-}
a57a60d9 122
2c34ea14
MJ
123# Build steps that can be overriden by the environment
124USERSPACE_RCU_MAKE_INSTALL="${USERSPACE_RCU_MAKE_INSTALL:-yes}"
125USERSPACE_RCU_MAKE_CLEAN="${USERSPACE_RCU_MAKE_CLEAN:-yes}"
126USERSPACE_RCU_GEN_COMPILE_COMMANDS="${USERSPACE_RCU_GEN_COMPILE_COMMANDS:-no}"
127USERSPACE_RCU_RUN_TESTS="${USERSPACE_RCU_RUN_TESTS:-yes}"
128USERSPACE_RCU_CLANG_TIDY="${USERSPACE_RCU_CLANG_TIDY:-no}"
e3022ad9 129
6d35c326
MJ
130SRCDIR="$WORKSPACE/src/liburcu"
131TMPDIR="$WORKSPACE/tmp"
1d56e325 132PREFIX="/build"
4afa623f 133LIBDIR="lib"
32dde2a3 134LIBDIR_ARCH="$LIBDIR"
4afa623f
MJ
135
136# RHEL and SLES both use lib64 but don't bother shipping a default autoconf
137# site config that matches this.
47ca4354 138if [[ ( -f /etc/redhat-release || -f /etc/products.d/SLES.prod || -f /etc/yocto-release ) ]]; then
85322e5d
MJ
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"
85322e5d 142 fi
4afa623f 143fi
69d7af71 144
2c34ea14
MJ
145exit_status=0
146
147# Use bear to generate compile_commands.json when enabled
148BEAR=""
149if [ "$USERSPACE_RCU_GEN_COMPILE_COMMANDS" = "yes" ]; then
150 BEAR="bear"
151fi
152
1d56e325
MJ
153# Create tmp directory
154rm -rf "$TMPDIR"
155mkdir -p "$TMPDIR"
6d35c326
MJ
156
157export TMPDIR
72d087d4 158export CFLAGS="-g -O2"
2c34ea14 159export CXXFLAGS="-g -O2"
6d35c326 160
1d56e325
MJ
161# Set compiler variables
162case "$cc" in
163gcc)
164 export CC=gcc
165 export CXX=g++
166 ;;
d954b6a8
MJ
167gcc-*)
168 export CC=gcc-${cc#gcc-}
169 export CXX=g++-${cc#gcc-}
1d56e325
MJ
170 ;;
171clang)
172 export CC=clang
173 export CXX=clang++
174 ;;
d954b6a8
MJ
175clang-*)
176 export CC=clang-${cc#clang-}
177 export CXX=clang++-${cc#clang-}
1d56e325
MJ
178 ;;
179*)
180 if [ "x$cc" != "x" ]; then
181 export CC="$cc"
182 fi
183 ;;
184esac
185
72f4f0c1 186# Set platform variables
1794bc79 187case "$platform" in
f0d7e5b1 188macos*)
995ac8f2
MJ
189 export MAKE=make
190 export TAR=tar
191 export NPROC="getconf _NPROCESSORS_ONLN"
f7bf4d7a 192 export PATH="/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
1d56e325
MJ
193 export CPPFLAGS="-I/opt/local/include"
194 export LDFLAGS="-L/opt/local/lib"
00f7bb3f
MJ
195 export PYTHON="python3"
196 export PYTHON_CONFIG="python3-config"
f7bf4d7a
MJ
197 ;;
198
d954b6a8 199freebsd*)
6ad0e7e6
MJ
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
7491c28d 209*)
995ac8f2
MJ
210 export MAKE=make
211 export TAR=tar
212 export NPROC=nproc
1d56e325
MJ
213 export PYTHON="python3"
214 export PYTHON_CONFIG="python3-config"
7491c28d
MJ
215 ;;
216esac
217
51c9c62d 218# Print build env details
2c34ea14 219print_header "Build environment details"
8c956c4b 220print_hardware || true
51c9c62d
MJ
221print_os || true
222print_tooling || true
223
cd9733d5
JR
224# Enter the source directory
225cd "$SRCDIR"
226
227# Run bootstrap in the source directory prior to configure
2c34ea14 228print_header "Bootstrap autotools"
cd9733d5
JR
229./bootstrap
230
231# Get source version from configure script
232eval "$(grep '^PACKAGE_VERSION=' ./configure)"
1d56e325 233PACKAGE_VERSION=${PACKAGE_VERSION//\-pre*/}
cd9733d5
JR
234
235# Set configure options and environment variables for each build
236# configuration.
2c34ea14 237CONF_OPTS=("--prefix=$PREFIX" "--libdir=$PREFIX/$LIBDIR_ARCH" "--disable-maintainer-mode")
56162e2a
JRJ
238case "$conf" in
239static)
2c34ea14 240 print_header "Conf: Static lib only"
1d56e325
MJ
241
242 CONF_OPTS+=("--enable-static" "--disable-shared")
56162e2a 243 ;;
72f4f0c1 244
a57a60d9 245tls_fallback)
2c34ea14
MJ
246 print_header "Conf: Use pthread_getspecific() to emulate TLS"
247
1d56e325 248 CONF_OPTS+=("--disable-compiler-tls")
56162e2a 249 ;;
72f4f0c1 250
cd9733d5 251debug-rcu)
2c34ea14
MJ
252 print_header "Conf: Enable RCU sanity checks for debugging"
253
a0493692 254 if vergte "$PACKAGE_VERSION" "0.10"; then
1d56e325 255 CONF_OPTS+=("--enable-rcu-debug")
cd9733d5 256 else
72d087d4 257 export CFLAGS="$CFLAGS -DDEBUG_RCU"
cd9733d5 258 fi
25e13783
JR
259
260 echo "Enable iterator sanity validator"
261 if vergte "$PACKAGE_VERSION" "0.11"; then
1d56e325 262 CONF_OPTS+=("--enable-cds-lfht-iter-debug")
25e13783 263 fi
cd9733d5
JR
264 ;;
265
5e1966cf
MJ
266atomic-builtins)
267 print_header "Conf: Enable the use of compiler atomic builtins."
268
269 CONF_OPTS+=("--enable-compiler-atomic-builtins")
270 ;;
271
56162e2a 272*)
2c34ea14 273 print_header "Conf: Standard"
56162e2a
JRJ
274 ;;
275esac
276
595a34c7 277# Build type
1d56e325
MJ
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
595a34c7 282#
1d56e325 283# Make sure to move to the build directory and run configure
69d7af71 284# before continuing.
595a34c7 285case "$build" in
72f4f0c1 286oot)
2c34ea14 287 print_header "Build: Out of tree"
69d7af71 288
1d56e325 289 # Create and enter a temporary build directory
52be5739 290 builddir=$(mktemp_compat -d)
1d56e325 291 cd "$builddir"
69d7af71 292
51c9c62d 293 "$SRCDIR/configure" "${CONF_OPTS[@]}" || failed_configure
72f4f0c1
MJ
294 ;;
295
296dist)
2c34ea14 297 print_header "Build: Distribution In-tree"
1d56e325
MJ
298
299 # Run configure and generate the tar file
300 # in the source directory
51c9c62d 301 ./configure || failed_configure
1d56e325
MJ
302 $MAKE dist
303
304 # Create and enter a temporary build directory
52be5739 305 builddir=$(mktemp_compat -d)
1d56e325
MJ
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
51c9c62d 313 ./configure "${CONF_OPTS[@]}" || failed_configure
1d56e325
MJ
314 ;;
315
316oot-dist)
2c34ea14 317 print_header "Build: Distribution Out of tree"
72f4f0c1 318
1d56e325 319 # Create and enter a temporary build directory
52be5739 320 builddir=$(mktemp_compat -d)
1d56e325 321 cd "$builddir"
69d7af71 322
1d56e325 323 # Run configure out of tree and generate the tar file
51c9c62d 324 "$SRCDIR/configure" || failed_configure
72f4f0c1
MJ
325 $MAKE dist
326
52be5739 327 dist_srcdir="$(mktemp_compat -d)"
1d56e325 328 cd "$dist_srcdir"
72f4f0c1 329
1d56e325
MJ
330 # Extract the distribution tar in the new source directory,
331 # ignore the first directory level
332 $TAR xvf "$builddir"/*.tar.* --strip 1
72f4f0c1 333
1d56e325 334 # Create and enter a second temporary build directory
52be5739 335 builddir="$(mktemp_compat -d)"
1d56e325
MJ
336 cd "$builddir"
337
338 # Run configure from the extracted distribution tar,
339 # out of the source tree
51c9c62d 340 "$dist_srcdir/configure" "${CONF_OPTS[@]}" || failed_configure
72f4f0c1 341 ;;
1d56e325 342
72f4f0c1 343*)
2c34ea14
MJ
344 print_header "Build: Standard In-tree"
345
51c9c62d 346 ./configure "${CONF_OPTS[@]}" || failed_configure
72f4f0c1 347 ;;
595a34c7
JR
348esac
349
1d56e325
MJ
350# We are now inside a configured build directory
351
72f4f0c1 352# BUILD!
2c34ea14
MJ
353print_header "BUILD!"
354$BEAR ${BEAR:+--} $MAKE -j "$($NPROC)" V=1
355
356# Install in the workspace if enabled
357if [ "$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
368fi
369
370# Run clang-tidy on the topmost commit
371if [ "$USERSPACE_RCU_CLANG_TIDY" = "yes" ]; then
372 print_header "Run clang-tidy"
72f4f0c1 373
2c34ea14
MJ
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
1d56e325 378
2c34ea14
MJ
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
397fi
398
399# Run tests if enabled
aff4e3d1 400if [ "$USERSPACE_RCU_RUN_TESTS" = "yes" ]; then
2c34ea14
MJ
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
aff4e3d1
JR
406 # Only run regtest for 0.9 and up
407 if vergte "$PACKAGE_VERSION" "0.9"; then
2c34ea14 408 $MAKE --keep-going regtest || exit_status=1
aff4e3d1 409 fi
72f4f0c1 410
aff4e3d1
JR
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"
69d7af71 413
4174b905
MJ
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
aff4e3d1
JR
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
e5d878d8
MJ
423fi
424
1d56e325 425# Clean the build directory
2c34ea14
MJ
426if [ "$USERSPACE_RCU_MAKE_CLEAN" = "yes" ]; then
427 print_header "Clean"
428 $MAKE clean
429fi
72f4f0c1 430
2c34ea14 431print_header "Liburcu build script ended with: $(test $exit_status == 0 && echo SUCCESS || echo FAILURE)"
595a34c7 432
1d56e325 433# Exit with failure if any of the tests failed
2c34ea14 434exit $exit_status
7491c28d
MJ
435
436# EOF
2c34ea14 437# vim: expandtab tabstop=4 shiftwidth=4
This page took 0.057876 seconds and 4 git commands to generate.