jjb: babeltrace: use clang-format-16
[lttng-ci.git] / scripts / librseq / build.sh
CommitLineData
51c9c62d 1#!/bin/bash
ae855ba1 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
ae855ba1 6
51c9c62d
MJ
7set -exu
8
ae855ba1
MJ
9# Version compare functions
10vercomp () {
11 set +u
12 if [[ "$1" == "$2" ]]; then
13 return 0
14 fi
15 local IFS=.
2c34ea14
MJ
16 # Ignore the shellcheck warning, we want splitting to happen based on IFS.
17 # shellcheck disable=SC2206
ae855ba1
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
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
2c34ea14
MJ
64print_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
51c9c62d
MJ
83failed_configure() {
84 # Assume we are in the configured build directory
2c34ea14 85 print_header "BEGIN config.log"
51c9c62d 86 cat config.log
2c34ea14 87 print_header "END config.log"
51c9c62d
MJ
88
89 # End the build with failure
90 exit 1
91}
92
2c34ea14
MJ
93print_header "Librseq build script starting"
94
1d56e325
MJ
95# Required variables
96WORKSPACE=${WORKSPACE:-}
97
2c34ea14 98# Axis
9a07183c 99platform=${platform:-}
ae855ba1
MJ
100conf=${conf:-}
101build=${build:-}
102cc=${cc:-}
103
2c34ea14
MJ
104# Build steps that can be overriden by the environment
105LIBRSEQ_MAKE_INSTALL="${LIBRSEQ_MAKE_INSTALL:-yes}"
106LIBRSEQ_MAKE_CLEAN="${LIBRSEQ_MAKE_CLEAN:-yes}"
107LIBRSEQ_GEN_COMPILE_COMMANDS="${LIBRSEQ_GEN_COMPILE_COMMANDS:-no}"
108LIBRSEQ_RUN_TESTS="${LIBRSEQ_RUN_TESTS:-yes}"
ae855ba1
MJ
109
110SRCDIR="$WORKSPACE/src/librseq"
111TMPDIR="$WORKSPACE/tmp"
1d56e325 112PREFIX="/build"
2c34ea14
MJ
113LIBDIR="lib"
114LIBDIR_ARCH="$LIBDIR"
115
116# RHEL and SLES both use lib64 but don't bother shipping a default autoconf
117# site config that matches this.
118if [[ ( -f /etc/redhat-release || -f /etc/products.d/SLES.prod || -f /etc/yocto-release ) ]]; then
119 # Detect the userspace bitness in a distro agnostic way
120 if file -L /bin/bash | grep '64-bit' >/dev/null 2>&1; then
121 LIBDIR_ARCH="${LIBDIR}64"
122 fi
123fi
124
125exit_status=0
126
127# Use bear to generate compile_commands.json when enabled
128BEAR=""
129if [ "$LIBRSEQ_GEN_COMPILE_COMMANDS" = "yes" ]; then
130 BEAR="bear"
131fi
ae855ba1 132
1d56e325
MJ
133# Create tmp directory
134rm -rf "$TMPDIR"
135mkdir -p "$TMPDIR"
ae855ba1
MJ
136
137export TMPDIR
138export CFLAGS="-g -O2"
2c34ea14 139export CXXFLAGS="-g -O2"
1d56e325
MJ
140
141# Add the convenience headers in extra to the
142# include path.
ae855ba1
MJ
143export CPPFLAGS="-I$SRCDIR/extra"
144
145# Set compiler variables
146case "$cc" in
147gcc)
148 export CC=gcc
149 export CXX=g++
150 ;;
9a07183c
MJ
151gcc-*)
152 export CC=gcc-${cc#gcc-}
153 export CXX=g++-${cc#gcc-}
ae855ba1
MJ
154 ;;
155clang)
156 export CC=clang
157 export CXX=clang++
158 ;;
9a07183c
MJ
159clang-*)
160 export CC=clang-${cc#clang-}
161 export CXX=clang++-${cc#clang-}
ae855ba1
MJ
162 ;;
163*)
164 if [ "x$cc" != "x" ]; then
165 export CC="$cc"
166 fi
167 ;;
168esac
169
170# Set platform variables
9a07183c 171case "$platform" in
ae855ba1
MJ
172*)
173 export MAKE=make
174 export TAR=tar
175 export NPROC=nproc
1d56e325
MJ
176 export PYTHON="python3"
177 export PYTHON_CONFIG="python3-config"
ae855ba1
MJ
178 ;;
179esac
180
51c9c62d 181# Print build env details
2c34ea14 182print_header "Build environment details"
8c956c4b 183print_hardware || true
51c9c62d
MJ
184print_os || true
185print_tooling || true
186
ae855ba1
MJ
187# Enter the source directory
188cd "$SRCDIR"
189
190# Run bootstrap in the source directory prior to configure
2c34ea14 191print_header "Bootstrap autotools"
ae855ba1
MJ
192./bootstrap
193
194# Get source version from configure script
195eval "$(grep '^PACKAGE_VERSION=' ./configure)"
1d56e325 196PACKAGE_VERSION=${PACKAGE_VERSION//\-pre*/}
ae855ba1 197
ae855ba1
MJ
198# Set configure options and environment variables for each build
199# configuration.
2c34ea14 200CONF_OPTS=("--prefix=$PREFIX" "--libdir=$PREFIX/$LIBDIR_ARCH" "--disable-maintainer-mode")
ae855ba1
MJ
201case "$conf" in
202static)
2c34ea14 203 print_header "Conf: Static lib only"
1d56e325
MJ
204
205 CONF_OPTS+=("--enable-static" "--disable-shared")
ae855ba1
MJ
206 ;;
207
208*)
2c34ea14 209 print_header "Conf: Standard"
ae855ba1
MJ
210 ;;
211esac
212
213# Build type
1d56e325
MJ
214# oot : out-of-tree build
215# dist : build via make dist
216# oot-dist: build via make dist out-of-tree
217# * : normal tree build
ae855ba1 218#
1d56e325
MJ
219# Make sure to move to the build directory and run configure
220# before continuing.
ae855ba1
MJ
221case "$build" in
222oot)
2c34ea14 223 print_header "Build: Out of tree"
1d56e325
MJ
224
225 # Create and enter a temporary build directory
226 builddir=$(mktemp -d)
227 cd "$builddir"
228
51c9c62d 229 "$SRCDIR/configure" "${CONF_OPTS[@]}" || failed_configure
ae855ba1
MJ
230 ;;
231
232dist)
2c34ea14 233 print_header "Build: Distribution In-tree"
1d56e325
MJ
234
235 # Run configure and generate the tar file
236 # in the source directory
51c9c62d 237 ./configure || failed_configure
1d56e325
MJ
238 $MAKE dist
239
240 # Create and enter a temporary build directory
241 builddir=$(mktemp -d)
242 cd "$builddir"
243
244 # Extract the distribution tar in the build directory,
245 # ignore the first directory level
246 $TAR xvf "$SRCDIR"/*.tar.* --strip 1
247
248 # Build in extracted source tree
51c9c62d 249 ./configure "${CONF_OPTS[@]}" || failed_configure
1d56e325
MJ
250 ;;
251
252oot-dist)
2c34ea14 253 print_header "Build: Distribution Out of tree"
ae855ba1 254
1d56e325
MJ
255 # Create and enter a temporary build directory
256 builddir=$(mktemp -d)
257 cd "$builddir"
258
259 # Run configure out of tree and generate the tar file
51c9c62d 260 "$SRCDIR/configure" || failed_configure
ae855ba1
MJ
261 $MAKE dist
262
1d56e325
MJ
263 dist_srcdir="$(mktemp -d)"
264 cd "$dist_srcdir"
ae855ba1 265
1d56e325
MJ
266 # Extract the distribution tar in the new source directory,
267 # ignore the first directory level
268 $TAR xvf "$builddir"/*.tar.* --strip 1
ae855ba1 269
1d56e325
MJ
270 # Create and enter a second temporary build directory
271 builddir="$(mktemp -d)"
272 cd "$builddir"
273
274 # Run configure from the extracted distribution tar,
275 # out of the source tree
51c9c62d 276 "$dist_srcdir/configure" "${CONF_OPTS[@]}" || failed_configure
ae855ba1
MJ
277 ;;
278
279*)
2c34ea14
MJ
280 print_header "Build: Standard In-tree"
281
51c9c62d 282 ./configure "${CONF_OPTS[@]}" || failed_configure
ae855ba1
MJ
283 ;;
284esac
285
1d56e325
MJ
286# We are now inside a configured build directory
287
ae855ba1 288# BUILD!
2c34ea14
MJ
289print_header "BUILD!"
290$BEAR ${BEAR:+--} $MAKE -j "$($NPROC)" V=1
ae855ba1 291
2c34ea14
MJ
292# Install in the workspace if enabled
293if [ "$LIBRSEQ_MAKE_INSTALL" = "yes" ]; then
294 print_header "Install"
1d56e325 295
2c34ea14 296 $MAKE install V=1 DESTDIR="$WORKSPACE"
ae855ba1 297
2c34ea14
MJ
298 # Cleanup rpath in executables and shared libraries
299 #find "$WORKSPACE/$PREFIX/bin" -type f -perm -0500 -exec chrpath --delete {} \;
300 find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.so" -exec chrpath --delete {} \;
1d56e325 301
2c34ea14
MJ
302 # Remove libtool .la files
303 find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.la" -delete
304fi
4174b905 305
2c34ea14
MJ
306# Run tests if enabled
307if [ "$LIBRSEQ_RUN_TESTS" = "yes" ]; then
308 print_header "Run test suite"
ae855ba1 309
2c34ea14
MJ
310 # Run tests, don't fail now, we want to run the archiving steps
311 $MAKE --keep-going check || exit_status=1
312
313 # Copy tap logs for the jenkins tap parser before cleaning the build dir
314 rsync -a --exclude 'test-suite.log' --include '*/' --include '*.log' --exclude='*' tests/ "$WORKSPACE/tap"
315
316 # Copy the test suites top-level log which includes all tests failures
317 rsync -a --include 'test-suite.log' --include '*/' --exclude='*' tests/ "$WORKSPACE/log"
318fi
319
320# Clean the build directory
321if [ "$LIBRSEQ_MAKE_CLEAN" = "yes" ]; then
322 print_header "Clean"
323 $MAKE clean
324fi
ae855ba1 325
2c34ea14 326print_header "Librseq build script ended with: $(test $exit_status == 0 && echo SUCCESS || echo FAILURE)"
ae855ba1 327
2c34ea14
MJ
328# Exit with failure if any of the tests failed
329exit $exit_status
ae855ba1
MJ
330
331# EOF
2c34ea14 332# vim: expandtab tabstop=4 shiftwidth=4
This page took 0.042991 seconds and 4 git commands to generate.