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