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