blob: b41fc0596eb3dd2fe39bfd0eb421230380232de3 [file] [log] [blame]
Neels Hofmeyr53e758a2017-05-29 22:53:34 +02001#!source_this_file
2
3# Common parts for osmo-gsm-tester jenkins build scripts. Use like in below example:
4#
5#--------------
6# #!/bin/sh
7# set -e -x
8# base="$PWD"
9# name="osmo-name"
10# . "$(dirname "$0")/jenkins-build-common.sh"
11#
12# build_repo libosmocore --configure --opts
13# build_repo libosmo-foo special_branch --configure --opts
14# build_repo osmo-bar
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +010015# build_repo_dir openbsc ./openbsc
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020016#
17# create_bin_tgz
18#--------------
19#
20# Some explanations:
21#
22# To allow calling from arbitrary working directories, other scripts should
23# source this file like shown above.
24#
25# Sourcing scripts must provide some variables/functions, see above.
26# In addition, these values can optionally be passed to override:
27# git_url, prefix, prefix_real, BUILD_NUMBER
28#
29# CONFIGURE_FLAGS may contain flags that should be passed to all builds'
30# ./configure steps (useful e.g. for building in the sysmobts SDK).
31#
32# For each built repository, a specific git branch or hash can be provided by
33# environment variable: OSMO_GSM_TESTER_BUILD_$repo="<git-hash>"
34# NOTE: convert $repo's dashes to underscore. For example:
35# OSMO_GSM_TESTER_BUILD_osmo_hlr="f001234abc"
36# OSMO_GSM_TESTER_BUILD_libosmocore="my/branch"
37# ("origin/" is prepended to branch names automatically)
38
39if [ -z "$name" -o -z "$base" ]; then
40 set +x
41 echo "Some environment variables are not provided as required by jenkins-build-common.sh. Error."
42 exit 1
43fi
44
Harald Welte882b8ae2022-06-18 12:05:50 +020045git_url="${git_url:-https://git.osmocom.org}"
Pau Espin Pedrol70c01ec2020-02-17 17:42:05 +010046prefix="${prefix:-$base/inst-$name}"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020047# prefix_real is usually identical with prefix, except when installing to a
48# different $DESTDIR than /, which is the case for example when building
49# osmo-bts within the sysmoBTS SDK
Pau Espin Pedrol70c01ec2020-02-17 17:42:05 +010050prefix_real="${prefix_real:-$prefix}"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020051
Holger Hans Peter Freyther90b05072018-09-23 15:14:00 +010052# Flag to be used to enable ASAN in builds. Defaults to enable ASAN builds and
53# it can be disabled by passing SANITIZE_FLAGS="" to the build.
Pau Espin Pedrol70c01ec2020-02-17 17:42:05 +010054SANITIZE_FLAGS="${SANITIZE_FLAGS:---enable-sanitize}"
Holger Hans Peter Freyther90b05072018-09-23 15:14:00 +010055
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020056export PKG_CONFIG_PATH="$prefix_real/lib/pkgconfig:$PKG_CONFIG_PATH"
57export LD_LIBRARY_PATH="$prefix_real/lib:$LD_LIBRARY_PATH"
58
59# Show current environment. Sometimes the LESS_ vars have ansi colors making a
60# mess, so exclude those.
61env | grep -v "^LESS" | sort
62
63# clean the workspace
Neels Hofmeyra8647f32017-09-13 19:17:08 +020064rm -f "$base"/*.build-*.tgz
65rm -f "$base"/*.build-*.md5
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020066rm -rf "$prefix_real"
67mkdir -p "$prefix_real"
68
69have_repo() {
70 repo="$1"
71 branch="${2-master}"
72
73 # Evaluate environment for instructions to build a specific git hash.
74 # Using a hash as $branch above unfortunately doesn't work.
75 branch_override_var="$(echo "OSMO_GSM_TESTER_BUILD_$repo" | sed 's/-/_/g')"
76 branch_override="$(eval "echo \$$branch_override_var")"
77 if [ -n "$branch_override" ]; then
78 branch="$branch_override"
79 fi
80
81 cd "$base"
Holger Hans Peter Freyther9eafb132018-09-18 22:55:05 +010082 if [ -d "$repo" ]; then
83 cd "$repo"
84 git fetch
85 else
86 git clone "$git_url/$repo" "$repo"
87 cd "$repo"
88 fi
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020089
Neels Hofmeyrb398b522017-06-23 04:13:30 +020090
91 # Figure out whether we need to prepend origin/ to find branches in upstream.
92 # Doing this allows using git hashes instead of a branch name.
Neels Hofmeyr851802b2017-06-23 03:52:26 +020093 if git rev-parse "origin/$branch"; then
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020094 branch="origin/$branch"
95 fi
96
Holger Hans Peter Freyther9eafb132018-09-18 22:55:05 +010097 git checkout -B build_branch "$branch"
Neels Hofmeyr2581b502017-06-23 04:07:40 +020098 rm -rf *
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020099 git reset --hard "$branch"
100
101 git rev-parse HEAD
102
Pau Espin Pedrolcbbe5d82018-09-18 15:39:17 +0200103 echo "$(git rev-parse HEAD) $repo" >> "$prefix_real/${name}_git_hashes.txt"
Pau Espin Pedrol70439962018-05-17 17:09:38 +0200104
Oliver Smithd087b6c2022-12-23 15:36:15 +0100105 git submodule update --init
106
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200107 cd "$base"
108}
109
110build_repo() {
111 # usage: build_repo <name> [<branch>] [--configure-opts [...]]
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100112 dir="$1"
113 shift
114 build_repo_dir "${dir}" "./" $@
115}
116
117build_repo_dir() {
118 # usage: build_repo_dir <name> <dir> [<branch>] [--configure-opts [...]]
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200119 dep="$1"
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100120 dir="$2"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200121 branch="master"
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100122 if [ -z "$(echo "$3" | grep '^-')" ]; then
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200123 # second arg does not start with a dash, it's empty or a branch
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100124 branch="$3"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200125 if [ -n "$branch" ]; then
126 # we had a branch arg, need to shift once more to get config options
127 shift
128 else
129 branch="master"
130 fi
131 fi
132 shift
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100133 shift
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200134 configure_opts="$@"
135
136 set +x; echo "
137
138====================== $dep
139
140"; set -x
141
142
143 have_repo "$dep" "$branch"
144
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100145 cd "$dep/${dir}"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200146
Pau Espin Pedroladdf78e2020-02-03 15:47:01 +0100147 if [ -f configure.ac ]; then
148 set +x; echo; echo; set -x
149 autoreconf -fi
150 set +x; echo; echo; set -x
151 ./configure --prefix="$prefix" --with-systemdsystemunitdir=no $CONFIGURE_FLAGS $configure_opts
152 elif [ -f CMakeLists.txt ]; then
153 rm -rf build && mkdir build && cd build || exit 1
154 set +x; echo; echo; set -x
Andre Puschmann8e1ba792020-04-20 15:12:55 +0200155 cmake -DCMAKE_INSTALL_PREFIX=$prefix $configure_opts ../
Pau Espin Pedrol6231c3c2021-03-15 11:27:15 +0100156 elif [ -f meson.build ]; then
157 rm -rf build && mkdir build && cd build || exit 1
158 set +x; echo; echo; set -x
Pau Espin Pedrol0696c602021-03-16 14:25:37 +0100159 meson ../ --prefix=$prefix --libdir="lib" $configure_opts
Pau Espin Pedrol6231c3c2021-03-15 11:27:15 +0100160 ninja -j8
161 ninja install
162 return
Pau Espin Pedroladdf78e2020-02-03 15:47:01 +0100163 else
164 echo "Unknwown build system" && exit 1
165 fi
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200166 set +x; echo; echo; set -x
167 make -j8 || make # libsmpp34 can't build in parallel
168 set +x; echo; echo; set -x
169 make install
170}
171
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200172prune_files() {
173 bindir="$1"
174 wanted_binaries="$2"
175
176 if [ ! -d "$prefix_real/$bindir" ]; then return; fi
177 # remove binaries not intended to originate from this build
178 cd "$prefix_real/$bindir"
179 for f in * ; do
180 if [ -z "$(echo "_ $wanted_binaries _" | grep " $f ")" ]; then
181 rm "$f"
182 fi
183 done
184
185 # ensure requested binaries indeed exist
186 for b in $wanted_binaries ; do
187 if [ ! -f "$b" ]; then
188 set +x; echo "ERROR: no such binary: $b in $prefix_real/$bindir/"; set -x
189 ls -1 "$prefix_real/$bindir"
190 exit 1
191 fi
192 done
193}
194
Holger Hans Peter Freyther9cf1c8a2018-11-04 14:43:17 +0000195add_rpath() {
196 # Adds an RPATH to executables in bin/ or sbin/ to search for the
197 # (Osmocom) libraries in `dirname /proc/self/exe`/../lib/. Adds an
198 # RPATH to a library to search in the same directory as the library.
199 #
200 # NOTE: Binaries should not have the SUID bit set and should run as the
201 # user executing the binary.
202 #
203 # NOTE: $ORIGIN is not a shell variable but a feature of the dynamic
204 # linker that will be expanded at runtime. For details see:
205 # http://man7.org/linux/man-pages/man8/ld.so.8.html
206 #
207 # Add an rpath relative to the binary and library if the directory
208 # exists.
Alejandro Leal Conejose3f6a3b2021-06-07 09:55:32 +0200209
210 rpath_args='--set-rpath'
211 rpath_dir='$ORIGIN/../lib/'
212 if [ -n "$patchelf_rapth_extra_args" ]; then
213 rpath_args="$patchelf_rapth_extra_args $rpath_args"
214 fi
215
216 if [ -n "$patchelf_rpath_dir" ]; then
217 rpath_dir="$rpath_dir:$patchelf_rpath_dir"
218 fi
219
Holger Hans Peter Freyther9cf1c8a2018-11-04 14:43:17 +0000220 if [ -d bin/ ]; then
Alejandro Leal Conejose3f6a3b2021-06-07 09:55:32 +0200221 find bin -depth -type f -exec patchelf $rpath_args "$rpath_dir" {} \;
Holger Hans Peter Freyther9cf1c8a2018-11-04 14:43:17 +0000222 fi
223 if [ -d sbin/ ]; then
Alejandro Leal Conejose3f6a3b2021-06-07 09:55:32 +0200224 find sbin -depth -type f -exec patchelf $rpath_args "$rpath_dir" {} \;
Holger Hans Peter Freyther9cf1c8a2018-11-04 14:43:17 +0000225 fi
226 if [ -d lib/ ]; then
227 find lib -depth -type f -name "lib*.so.*" -exec patchelf --set-rpath '$ORIGIN/' {} \;
228 fi
229}
230
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200231create_bin_tgz() {
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200232 # build the archive that is going to be copied to the tester
Neels Hofmeyr1921c0f2017-09-04 16:34:18 +0200233
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200234 wanted_binaries_bin="$1"
235 wanted_binaries_sbin="$2"
Neels Hofmeyr1921c0f2017-09-04 16:34:18 +0200236
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200237 if [ -z "$wanted_binaries_bin" ] && [ -z "$wanted_binaries_sbin" ]; then
Neels Hofmeyr1921c0f2017-09-04 16:34:18 +0200238 set +x; echo "ERROR: create_bin_tgz needs a list of permitted binaries"; set -x
239 exit 1
240 fi
241
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200242 prune_files bin "$wanted_binaries_bin"
243 prune_files sbin "$wanted_binaries_sbin"
Pau Espin Pedroladdf78e2020-02-03 15:47:01 +0100244 # Drop all static libraries if exist:
245 rm -f $prefix_real/lib/*.a
246 rm -f $prefix_real/lib/*.la
Neels Hofmeyr713a1202017-09-07 00:57:46 +0200247
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200248 cd "$prefix_real"
Holger Hans Peter Freyther9cf1c8a2018-11-04 14:43:17 +0000249 add_rpath
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200250 this="$name.build-${BUILD_NUMBER-$(date +%Y-%m-%d_%H_%M_%S)}"
251 tar="${this}.tgz"
252 tar czf "$base/$tar" *
253 cd "$base"
254 md5sum "$tar" > "${this}.md5"
255}