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