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