jjb: babeltrace: use clang-format-16
[lttng-ci.git] / scripts / librseq / 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 "Librseq 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 LIBRSEQ_MAKE_INSTALL="${LIBRSEQ_MAKE_INSTALL:-yes}"
106 LIBRSEQ_MAKE_CLEAN="${LIBRSEQ_MAKE_CLEAN:-yes}"
107 LIBRSEQ_GEN_COMPILE_COMMANDS="${LIBRSEQ_GEN_COMPILE_COMMANDS:-no}"
108 LIBRSEQ_RUN_TESTS="${LIBRSEQ_RUN_TESTS:-yes}"
109
110 SRCDIR="$WORKSPACE/src/librseq"
111 TMPDIR="$WORKSPACE/tmp"
112 PREFIX="/build"
113 LIBDIR="lib"
114 LIBDIR_ARCH="$LIBDIR"
115
116 # RHEL and SLES both use lib64 but don't bother shipping a default autoconf
117 # site config that matches this.
118 if [[ ( -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
123 fi
124
125 exit_status=0
126
127 # Use bear to generate compile_commands.json when enabled
128 BEAR=""
129 if [ "$LIBRSEQ_GEN_COMPILE_COMMANDS" = "yes" ]; then
130 BEAR="bear"
131 fi
132
133 # Create tmp directory
134 rm -rf "$TMPDIR"
135 mkdir -p "$TMPDIR"
136
137 export TMPDIR
138 export CFLAGS="-g -O2"
139 export CXXFLAGS="-g -O2"
140
141 # Add the convenience headers in extra to the
142 # include path.
143 export CPPFLAGS="-I$SRCDIR/extra"
144
145 # Set compiler variables
146 case "$cc" in
147 gcc)
148 export CC=gcc
149 export CXX=g++
150 ;;
151 gcc-*)
152 export CC=gcc-${cc#gcc-}
153 export CXX=g++-${cc#gcc-}
154 ;;
155 clang)
156 export CC=clang
157 export CXX=clang++
158 ;;
159 clang-*)
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 ;;
168 esac
169
170 # Set platform variables
171 case "$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 ;;
179 esac
180
181 # Print build env details
182 print_header "Build environment details"
183 print_hardware || true
184 print_os || true
185 print_tooling || true
186
187 # Enter the source directory
188 cd "$SRCDIR"
189
190 # Run bootstrap in the source directory prior to configure
191 print_header "Bootstrap autotools"
192 ./bootstrap
193
194 # Get source version from configure script
195 eval "$(grep '^PACKAGE_VERSION=' ./configure)"
196 PACKAGE_VERSION=${PACKAGE_VERSION//\-pre*/}
197
198 # Set configure options and environment variables for each build
199 # configuration.
200 CONF_OPTS=("--prefix=$PREFIX" "--libdir=$PREFIX/$LIBDIR_ARCH" "--disable-maintainer-mode")
201 case "$conf" in
202 static)
203 print_header "Conf: Static lib only"
204
205 CONF_OPTS+=("--enable-static" "--disable-shared")
206 ;;
207
208 *)
209 print_header "Conf: Standard"
210 ;;
211 esac
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.
221 case "$build" in
222 oot)
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
232 dist)
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
252 oot-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 ;;
284 esac
285
286 # We are now inside a configured build directory
287
288 # BUILD!
289 print_header "BUILD!"
290 $BEAR ${BEAR:+--} $MAKE -j "$($NPROC)" V=1
291
292 # Install in the workspace if enabled
293 if [ "$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
304 fi
305
306 # Run tests if enabled
307 if [ "$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"
318 fi
319
320 # Clean the build directory
321 if [ "$LIBRSEQ_MAKE_CLEAN" = "yes" ]; then
322 print_header "Clean"
323 $MAKE clean
324 fi
325
326 print_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
329 exit $exit_status
330
331 # EOF
332 # vim: expandtab tabstop=4 shiftwidth=4
This page took 0.035421 seconds and 4 git commands to generate.