Commit | Line | Data |
---|---|---|
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 |
7 | set -exu |
8 | ||
ae855ba1 MJ |
9 | # Version compare functions |
10 | vercomp () { | |
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 | ||
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 | ||
2c34ea14 MJ |
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 | ||
51c9c62d MJ |
83 | failed_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 |
93 | print_header "Librseq build script starting" |
94 | ||
1d56e325 MJ |
95 | # Required variables |
96 | WORKSPACE=${WORKSPACE:-} | |
97 | ||
2c34ea14 | 98 | # Axis |
9a07183c | 99 | platform=${platform:-} |
ae855ba1 MJ |
100 | conf=${conf:-} |
101 | build=${build:-} | |
102 | cc=${cc:-} | |
103 | ||
2c34ea14 MJ |
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}" | |
ae855ba1 MJ |
109 | |
110 | SRCDIR="$WORKSPACE/src/librseq" | |
111 | TMPDIR="$WORKSPACE/tmp" | |
1d56e325 | 112 | PREFIX="/build" |
2c34ea14 MJ |
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 | |
ae855ba1 | 132 | |
1d56e325 MJ |
133 | # Create tmp directory |
134 | rm -rf "$TMPDIR" | |
135 | mkdir -p "$TMPDIR" | |
ae855ba1 MJ |
136 | |
137 | export TMPDIR | |
138 | export CFLAGS="-g -O2" | |
2c34ea14 | 139 | export CXXFLAGS="-g -O2" |
1d56e325 MJ |
140 | |
141 | # Add the convenience headers in extra to the | |
142 | # include path. | |
ae855ba1 MJ |
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 | ;; | |
9a07183c MJ |
151 | gcc-*) |
152 | export CC=gcc-${cc#gcc-} | |
153 | export CXX=g++-${cc#gcc-} | |
ae855ba1 MJ |
154 | ;; |
155 | clang) | |
156 | export CC=clang | |
157 | export CXX=clang++ | |
158 | ;; | |
9a07183c MJ |
159 | clang-*) |
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 | ;; | |
168 | esac | |
169 | ||
170 | # Set platform variables | |
9a07183c | 171 | case "$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 | ;; |
179 | esac | |
180 | ||
51c9c62d | 181 | # Print build env details |
2c34ea14 | 182 | print_header "Build environment details" |
51c9c62d MJ |
183 | print_os || true |
184 | print_tooling || true | |
185 | ||
ae855ba1 MJ |
186 | # Enter the source directory |
187 | cd "$SRCDIR" | |
188 | ||
189 | # Run bootstrap in the source directory prior to configure | |
2c34ea14 | 190 | print_header "Bootstrap autotools" |
ae855ba1 MJ |
191 | ./bootstrap |
192 | ||
193 | # Get source version from configure script | |
194 | eval "$(grep '^PACKAGE_VERSION=' ./configure)" | |
1d56e325 | 195 | PACKAGE_VERSION=${PACKAGE_VERSION//\-pre*/} |
ae855ba1 | 196 | |
ae855ba1 MJ |
197 | # Set configure options and environment variables for each build |
198 | # configuration. | |
2c34ea14 | 199 | CONF_OPTS=("--prefix=$PREFIX" "--libdir=$PREFIX/$LIBDIR_ARCH" "--disable-maintainer-mode") |
ae855ba1 MJ |
200 | case "$conf" in |
201 | static) | |
2c34ea14 | 202 | print_header "Conf: Static lib only" |
1d56e325 MJ |
203 | |
204 | CONF_OPTS+=("--enable-static" "--disable-shared") | |
ae855ba1 MJ |
205 | ;; |
206 | ||
207 | *) | |
2c34ea14 | 208 | print_header "Conf: Standard" |
ae855ba1 MJ |
209 | ;; |
210 | esac | |
211 | ||
212 | # Build type | |
1d56e325 MJ |
213 | # oot : out-of-tree build |
214 | # dist : build via make dist | |
215 | # oot-dist: build via make dist out-of-tree | |
216 | # * : normal tree build | |
ae855ba1 | 217 | # |
1d56e325 MJ |
218 | # Make sure to move to the build directory and run configure |
219 | # before continuing. | |
ae855ba1 MJ |
220 | case "$build" in |
221 | oot) | |
2c34ea14 | 222 | print_header "Build: Out of tree" |
1d56e325 MJ |
223 | |
224 | # Create and enter a temporary build directory | |
225 | builddir=$(mktemp -d) | |
226 | cd "$builddir" | |
227 | ||
51c9c62d | 228 | "$SRCDIR/configure" "${CONF_OPTS[@]}" || failed_configure |
ae855ba1 MJ |
229 | ;; |
230 | ||
231 | dist) | |
2c34ea14 | 232 | print_header "Build: Distribution In-tree" |
1d56e325 MJ |
233 | |
234 | # Run configure and generate the tar file | |
235 | # in the source directory | |
51c9c62d | 236 | ./configure || failed_configure |
1d56e325 MJ |
237 | $MAKE dist |
238 | ||
239 | # Create and enter a temporary build directory | |
240 | builddir=$(mktemp -d) | |
241 | cd "$builddir" | |
242 | ||
243 | # Extract the distribution tar in the build directory, | |
244 | # ignore the first directory level | |
245 | $TAR xvf "$SRCDIR"/*.tar.* --strip 1 | |
246 | ||
247 | # Build in extracted source tree | |
51c9c62d | 248 | ./configure "${CONF_OPTS[@]}" || failed_configure |
1d56e325 MJ |
249 | ;; |
250 | ||
251 | oot-dist) | |
2c34ea14 | 252 | print_header "Build: Distribution Out of tree" |
ae855ba1 | 253 | |
1d56e325 MJ |
254 | # Create and enter a temporary build directory |
255 | builddir=$(mktemp -d) | |
256 | cd "$builddir" | |
257 | ||
258 | # Run configure out of tree and generate the tar file | |
51c9c62d | 259 | "$SRCDIR/configure" || failed_configure |
ae855ba1 MJ |
260 | $MAKE dist |
261 | ||
1d56e325 MJ |
262 | dist_srcdir="$(mktemp -d)" |
263 | cd "$dist_srcdir" | |
ae855ba1 | 264 | |
1d56e325 MJ |
265 | # Extract the distribution tar in the new source directory, |
266 | # ignore the first directory level | |
267 | $TAR xvf "$builddir"/*.tar.* --strip 1 | |
ae855ba1 | 268 | |
1d56e325 MJ |
269 | # Create and enter a second temporary build directory |
270 | builddir="$(mktemp -d)" | |
271 | cd "$builddir" | |
272 | ||
273 | # Run configure from the extracted distribution tar, | |
274 | # out of the source tree | |
51c9c62d | 275 | "$dist_srcdir/configure" "${CONF_OPTS[@]}" || failed_configure |
ae855ba1 MJ |
276 | ;; |
277 | ||
278 | *) | |
2c34ea14 MJ |
279 | print_header "Build: Standard In-tree" |
280 | ||
51c9c62d | 281 | ./configure "${CONF_OPTS[@]}" || failed_configure |
ae855ba1 MJ |
282 | ;; |
283 | esac | |
284 | ||
1d56e325 MJ |
285 | # We are now inside a configured build directory |
286 | ||
ae855ba1 | 287 | # BUILD! |
2c34ea14 MJ |
288 | print_header "BUILD!" |
289 | $BEAR ${BEAR:+--} $MAKE -j "$($NPROC)" V=1 | |
ae855ba1 | 290 | |
2c34ea14 MJ |
291 | # Install in the workspace if enabled |
292 | if [ "$LIBRSEQ_MAKE_INSTALL" = "yes" ]; then | |
293 | print_header "Install" | |
1d56e325 | 294 | |
2c34ea14 | 295 | $MAKE install V=1 DESTDIR="$WORKSPACE" |
ae855ba1 | 296 | |
2c34ea14 MJ |
297 | # Cleanup rpath in executables and shared libraries |
298 | #find "$WORKSPACE/$PREFIX/bin" -type f -perm -0500 -exec chrpath --delete {} \; | |
299 | find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.so" -exec chrpath --delete {} \; | |
1d56e325 | 300 | |
2c34ea14 MJ |
301 | # Remove libtool .la files |
302 | find "$WORKSPACE/$PREFIX/$LIBDIR_ARCH" -name "*.la" -delete | |
303 | fi | |
4174b905 | 304 | |
2c34ea14 MJ |
305 | # Run tests if enabled |
306 | if [ "$LIBRSEQ_RUN_TESTS" = "yes" ]; then | |
307 | print_header "Run test suite" | |
ae855ba1 | 308 | |
2c34ea14 MJ |
309 | # Run tests, don't fail now, we want to run the archiving steps |
310 | $MAKE --keep-going check || exit_status=1 | |
311 | ||
312 | # Copy tap logs for the jenkins tap parser before cleaning the build dir | |
313 | rsync -a --exclude 'test-suite.log' --include '*/' --include '*.log' --exclude='*' tests/ "$WORKSPACE/tap" | |
314 | ||
315 | # Copy the test suites top-level log which includes all tests failures | |
316 | rsync -a --include 'test-suite.log' --include '*/' --exclude='*' tests/ "$WORKSPACE/log" | |
317 | fi | |
318 | ||
319 | # Clean the build directory | |
320 | if [ "$LIBRSEQ_MAKE_CLEAN" = "yes" ]; then | |
321 | print_header "Clean" | |
322 | $MAKE clean | |
323 | fi | |
ae855ba1 | 324 | |
2c34ea14 | 325 | print_header "Librseq build script ended with: $(test $exit_status == 0 && echo SUCCESS || echo FAILURE)" |
ae855ba1 | 326 | |
2c34ea14 MJ |
327 | # Exit with failure if any of the tests failed |
328 | exit $exit_status | |
ae855ba1 MJ |
329 | |
330 | # EOF | |
2c34ea14 | 331 | # vim: expandtab tabstop=4 shiftwidth=4 |