jjb: Use lttng-modules from gerrit when depends-on is set
[lttng-ci.git] / scripts / librseq / 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
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
83failed_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
93print_header "Librseq build script starting"
94
95# Required variables
96WORKSPACE=${WORKSPACE:-}
97
98# Axis
99platform=${platform:-}
100conf=${conf:-}
101build=${build:-}
102cc=${cc:-}
103
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}"
109
110SRCDIR="$WORKSPACE/src/librseq"
111TMPDIR="$WORKSPACE/tmp"
112PREFIX="/build"
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
132
133# Create tmp directory
134rm -rf "$TMPDIR"
135mkdir -p "$TMPDIR"
136
137export TMPDIR
138export CFLAGS="-g -O2"
139export CXXFLAGS="-g -O2"
140
141# Add the convenience headers in extra to the
142# include path.
143export CPPFLAGS="-I$SRCDIR/extra"
144
145# Set compiler variables
146case "$cc" in
147gcc)
148 export CC=gcc
149 export CXX=g++
150 ;;
151gcc-*)
152 export CC=gcc-${cc#gcc-}
153 export CXX=g++-${cc#gcc-}
154 ;;
155clang)
156 export CC=clang
157 export CXX=clang++
158 ;;
159clang-*)
160 export CC=clang-${cc#clang-}
161 export CXX=clang++-${cc#clang-}
162 ;;
163*)
164 if [ "x$cc" != "x" ]; then
165 export CC="$cc"
166 fi
167 ;;
168esac
169
170# Set platform variables
171case "$platform" in
172*)
173 export MAKE=make
174 export TAR=tar
175 export NPROC=nproc
176 export PYTHON="python3"
177 export PYTHON_CONFIG="python3-config"
178 ;;
179esac
180
181# Print build env details
182print_header "Build environment details"
183print_hardware || true
184print_os || true
185print_tooling || true
186
187# Enter the source directory
188cd "$SRCDIR"
189
190# Run bootstrap in the source directory prior to configure
191print_header "Bootstrap autotools"
192./bootstrap
193
194# Get source version from configure script
195eval "$(grep '^PACKAGE_VERSION=' ./configure)"
196PACKAGE_VERSION=${PACKAGE_VERSION//\-pre*/}
197
198# Set configure options and environment variables for each build
199# configuration.
200CONF_OPTS=("--prefix=$PREFIX" "--libdir=$PREFIX/$LIBDIR_ARCH" "--disable-maintainer-mode")
201case "$conf" in
202static)
203 print_header "Conf: Static lib only"
204
205 CONF_OPTS+=("--enable-static" "--disable-shared")
206 ;;
207
208*)
209 print_header "Conf: Standard"
210 ;;
211esac
212
213# Build type
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
218#
219# Make sure to move to the build directory and run configure
220# before continuing.
221case "$build" in
222oot)
223 print_header "Build: Out of tree"
224
225 # Create and enter a temporary build directory
226 builddir=$(mktemp -d)
227 cd "$builddir"
228
229 "$SRCDIR/configure" "${CONF_OPTS[@]}" || failed_configure
230 ;;
231
232dist)
233 print_header "Build: Distribution In-tree"
234
235 # Run configure and generate the tar file
236 # in the source directory
237 ./configure || failed_configure
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
249 ./configure "${CONF_OPTS[@]}" || failed_configure
250 ;;
251
252oot-dist)
253 print_header "Build: Distribution Out of tree"
254
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
260 "$SRCDIR/configure" || failed_configure
261 $MAKE dist
262
263 dist_srcdir="$(mktemp -d)"
264 cd "$dist_srcdir"
265
266 # Extract the distribution tar in the new source directory,
267 # ignore the first directory level
268 $TAR xvf "$builddir"/*.tar.* --strip 1
269
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
276 "$dist_srcdir/configure" "${CONF_OPTS[@]}" || failed_configure
277 ;;
278
279*)
280 print_header "Build: Standard In-tree"
281
282 ./configure "${CONF_OPTS[@]}" || failed_configure
283 ;;
284esac
285
286# We are now inside a configured build directory
287
288# BUILD!
289print_header "BUILD!"
290$BEAR ${BEAR:+--} $MAKE -j "$($NPROC)" V=1
291
292# Install in the workspace if enabled
293if [ "$LIBRSEQ_MAKE_INSTALL" = "yes" ]; then
294 print_header "Install"
295
296 $MAKE install V=1 DESTDIR="$WORKSPACE"
297
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 {} \;
301
302 # Remove libtool .la files
303 find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.la" -delete
304fi
305
306# Run tests if enabled
307if [ "$LIBRSEQ_RUN_TESTS" = "yes" ]; then
308 print_header "Run test suite"
309
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
325
326print_header "Librseq build script ended with: $(test $exit_status == 0 && echo SUCCESS || echo FAILURE)"
327
328# Exit with failure if any of the tests failed
329exit $exit_status
330
331# EOF
332# vim: expandtab tabstop=4 shiftwidth=4
This page took 0.023786 seconds and 5 git commands to generate.