blob: bfe8d7345dc60febfc212f4e30e1db6a19e9d4f4 [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
45git_url="${git_url-"git://git.osmocom.org"}"
46prefix="${prefix-"$base/inst-$name"}"
47# 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
50prefix_real="${prefix_real-"$prefix"}"
51
52export PKG_CONFIG_PATH="$prefix_real/lib/pkgconfig:$PKG_CONFIG_PATH"
53export LD_LIBRARY_PATH="$prefix_real/lib:$LD_LIBRARY_PATH"
54
55# Show current environment. Sometimes the LESS_ vars have ansi colors making a
56# mess, so exclude those.
57env | grep -v "^LESS" | sort
58
59# clean the workspace
Neels Hofmeyra8647f32017-09-13 19:17:08 +020060rm -f "$base"/*.build-*.tgz
61rm -f "$base"/*.build-*.md5
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020062rm -rf "$prefix_real"
63mkdir -p "$prefix_real"
64
65have_repo() {
66 repo="$1"
67 branch="${2-master}"
68
69 # Evaluate environment for instructions to build a specific git hash.
70 # Using a hash as $branch above unfortunately doesn't work.
71 branch_override_var="$(echo "OSMO_GSM_TESTER_BUILD_$repo" | sed 's/-/_/g')"
72 branch_override="$(eval "echo \$$branch_override_var")"
73 if [ -n "$branch_override" ]; then
74 branch="$branch_override"
75 fi
76
77 cd "$base"
Neels Hofmeyrb398b522017-06-23 04:13:30 +020078 rm -rf "$repo"
79 git clone "$git_url/$repo" "$repo"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020080
Neels Hofmeyrb398b522017-06-23 04:13:30 +020081 cd "$repo"
82
83 # Figure out whether we need to prepend origin/ to find branches in upstream.
84 # Doing this allows using git hashes instead of a branch name.
Neels Hofmeyr851802b2017-06-23 03:52:26 +020085 if git rev-parse "origin/$branch"; then
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020086 branch="origin/$branch"
87 fi
88
Neels Hofmeyr851802b2017-06-23 03:52:26 +020089 git checkout -b build_branch "$branch"
Neels Hofmeyr2581b502017-06-23 04:07:40 +020090 rm -rf *
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020091 git reset --hard "$branch"
92
93 git rev-parse HEAD
94
Pau Espin Pedrolcbbe5d82018-09-18 15:39:17 +020095 echo "$(git rev-parse HEAD) $repo" >> "$prefix_real/${name}_git_hashes.txt"
Pau Espin Pedrol70439962018-05-17 17:09:38 +020096
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020097 cd "$base"
98}
99
100build_repo() {
101 # usage: build_repo <name> [<branch>] [--configure-opts [...]]
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100102 dir="$1"
103 shift
104 build_repo_dir "${dir}" "./" $@
105}
106
107build_repo_dir() {
108 # usage: build_repo_dir <name> <dir> [<branch>] [--configure-opts [...]]
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200109 dep="$1"
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100110 dir="$2"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200111 branch="master"
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100112 if [ -z "$(echo "$3" | grep '^-')" ]; then
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200113 # second arg does not start with a dash, it's empty or a branch
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100114 branch="$3"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200115 if [ -n "$branch" ]; then
116 # we had a branch arg, need to shift once more to get config options
117 shift
118 else
119 branch="master"
120 fi
121 fi
122 shift
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100123 shift
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200124 configure_opts="$@"
125
126 set +x; echo "
127
128====================== $dep
129
130"; set -x
131
132
133 have_repo "$dep" "$branch"
134
Holger Hans Peter Freythercb132bd2018-09-15 10:21:40 +0100135 cd "$dep/${dir}"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200136
137 set +x; echo; echo; set -x
138 autoreconf -fi
139 set +x; echo; echo; set -x
Pau Espin Pedrol7a216e52018-09-12 14:46:12 +0200140 ./configure --prefix="$prefix" --with-systemdsystemunitdir=no $CONFIGURE_FLAGS $configure_opts
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200141 set +x; echo; echo; set -x
142 make -j8 || make # libsmpp34 can't build in parallel
143 set +x; echo; echo; set -x
144 make install
145}
146
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200147prune_files() {
148 bindir="$1"
149 wanted_binaries="$2"
150
151 if [ ! -d "$prefix_real/$bindir" ]; then return; fi
152 # remove binaries not intended to originate from this build
153 cd "$prefix_real/$bindir"
154 for f in * ; do
155 if [ -z "$(echo "_ $wanted_binaries _" | grep " $f ")" ]; then
156 rm "$f"
157 fi
158 done
159
160 # ensure requested binaries indeed exist
161 for b in $wanted_binaries ; do
162 if [ ! -f "$b" ]; then
163 set +x; echo "ERROR: no such binary: $b in $prefix_real/$bindir/"; set -x
164 ls -1 "$prefix_real/$bindir"
165 exit 1
166 fi
167 done
168}
169
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200170create_bin_tgz() {
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200171 # build the archive that is going to be copied to the tester
Neels Hofmeyr1921c0f2017-09-04 16:34:18 +0200172
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200173 wanted_binaries_bin="$1"
174 wanted_binaries_sbin="$2"
Neels Hofmeyr1921c0f2017-09-04 16:34:18 +0200175
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200176 if [ -z "$wanted_binaries_bin" ] && [ -z "$wanted_binaries_sbin" ]; then
Neels Hofmeyr1921c0f2017-09-04 16:34:18 +0200177 set +x; echo "ERROR: create_bin_tgz needs a list of permitted binaries"; set -x
178 exit 1
179 fi
180
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200181 prune_files bin "$wanted_binaries_bin"
182 prune_files sbin "$wanted_binaries_sbin"
Neels Hofmeyr713a1202017-09-07 00:57:46 +0200183
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200184 cd "$prefix_real"
185 this="$name.build-${BUILD_NUMBER-$(date +%Y-%m-%d_%H_%M_%S)}"
186 tar="${this}.tgz"
187 tar czf "$base/$tar" *
188 cd "$base"
189 md5sum "$tar" > "${this}.md5"
190}