blob: b8cafcf50c2efe538ddc40ce41f46149c96b8f70 [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
15#
16# create_bin_tgz
17#--------------
18#
19# Some explanations:
20#
21# To allow calling from arbitrary working directories, other scripts should
22# source this file like shown above.
23#
24# Sourcing scripts must provide some variables/functions, see above.
25# In addition, these values can optionally be passed to override:
26# git_url, prefix, prefix_real, BUILD_NUMBER
27#
28# CONFIGURE_FLAGS may contain flags that should be passed to all builds'
29# ./configure steps (useful e.g. for building in the sysmobts SDK).
30#
31# For each built repository, a specific git branch or hash can be provided by
32# environment variable: OSMO_GSM_TESTER_BUILD_$repo="<git-hash>"
33# NOTE: convert $repo's dashes to underscore. For example:
34# OSMO_GSM_TESTER_BUILD_osmo_hlr="f001234abc"
35# OSMO_GSM_TESTER_BUILD_libosmocore="my/branch"
36# ("origin/" is prepended to branch names automatically)
37
38if [ -z "$name" -o -z "$base" ]; then
39 set +x
40 echo "Some environment variables are not provided as required by jenkins-build-common.sh. Error."
41 exit 1
42fi
43
44git_url="${git_url-"git://git.osmocom.org"}"
45prefix="${prefix-"$base/inst-$name"}"
46# prefix_real is usually identical with prefix, except when installing to a
47# different $DESTDIR than /, which is the case for example when building
48# osmo-bts within the sysmoBTS SDK
49prefix_real="${prefix_real-"$prefix"}"
50
51export PKG_CONFIG_PATH="$prefix_real/lib/pkgconfig:$PKG_CONFIG_PATH"
52export LD_LIBRARY_PATH="$prefix_real/lib:$LD_LIBRARY_PATH"
53
54# Show current environment. Sometimes the LESS_ vars have ansi colors making a
55# mess, so exclude those.
56env | grep -v "^LESS" | sort
57
58# clean the workspace
Neels Hofmeyra8647f32017-09-13 19:17:08 +020059rm -f "$base"/*.build-*.tgz
60rm -f "$base"/*.build-*.md5
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020061rm -rf "$prefix_real"
62mkdir -p "$prefix_real"
63
64have_repo() {
65 repo="$1"
66 branch="${2-master}"
67
68 # Evaluate environment for instructions to build a specific git hash.
69 # Using a hash as $branch above unfortunately doesn't work.
70 branch_override_var="$(echo "OSMO_GSM_TESTER_BUILD_$repo" | sed 's/-/_/g')"
71 branch_override="$(eval "echo \$$branch_override_var")"
72 if [ -n "$branch_override" ]; then
73 branch="$branch_override"
74 fi
75
76 cd "$base"
Neels Hofmeyrb398b522017-06-23 04:13:30 +020077 rm -rf "$repo"
78 git clone "$git_url/$repo" "$repo"
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020079
Neels Hofmeyrb398b522017-06-23 04:13:30 +020080 cd "$repo"
81
82 # Figure out whether we need to prepend origin/ to find branches in upstream.
83 # Doing this allows using git hashes instead of a branch name.
Neels Hofmeyr851802b2017-06-23 03:52:26 +020084 if git rev-parse "origin/$branch"; then
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020085 branch="origin/$branch"
86 fi
87
Neels Hofmeyr851802b2017-06-23 03:52:26 +020088 git checkout -b build_branch "$branch"
Neels Hofmeyr2581b502017-06-23 04:07:40 +020089 rm -rf *
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020090 git reset --hard "$branch"
91
92 git rev-parse HEAD
93
Pau Espin Pedrol70439962018-05-17 17:09:38 +020094 echo "$(git rev-parse HEAD) $dep" >> "$prefix_real/${name}_git_hashes.txt"
95
Neels Hofmeyr53e758a2017-05-29 22:53:34 +020096 cd "$base"
97}
98
99build_repo() {
100 # usage: build_repo <name> [<branch>] [--configure-opts [...]]
101 dep="$1"
102 branch="master"
103 if [ -z "$(echo "$2" | grep '^-')" ]; then
104 # second arg does not start with a dash, it's empty or a branch
105 branch="$2"
106 if [ -n "$branch" ]; then
107 # we had a branch arg, need to shift once more to get config options
108 shift
109 else
110 branch="master"
111 fi
112 fi
113 shift
114 configure_opts="$@"
115
116 set +x; echo "
117
118====================== $dep
119
120"; set -x
121
122
123 have_repo "$dep" "$branch"
124
125 cd "$dep"
126
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200127 # special shim: we know the openbsc.git needs to be built in the openbsc/ subdir.
128 if [ "$dep" = "openbsc" ]; then
129 cd openbsc
130 fi
131
132 set +x; echo; echo; set -x
133 autoreconf -fi
134 set +x; echo; echo; set -x
135 ./configure --prefix="$prefix" $CONFIGURE_FLAGS $configure_opts
136 set +x; echo; echo; set -x
137 make -j8 || make # libsmpp34 can't build in parallel
138 set +x; echo; echo; set -x
139 make install
140}
141
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200142prune_files() {
143 bindir="$1"
144 wanted_binaries="$2"
145
146 if [ ! -d "$prefix_real/$bindir" ]; then return; fi
147 # remove binaries not intended to originate from this build
148 cd "$prefix_real/$bindir"
149 for f in * ; do
150 if [ -z "$(echo "_ $wanted_binaries _" | grep " $f ")" ]; then
151 rm "$f"
152 fi
153 done
154
155 # ensure requested binaries indeed exist
156 for b in $wanted_binaries ; do
157 if [ ! -f "$b" ]; then
158 set +x; echo "ERROR: no such binary: $b in $prefix_real/$bindir/"; set -x
159 ls -1 "$prefix_real/$bindir"
160 exit 1
161 fi
162 done
163}
164
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200165create_bin_tgz() {
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200166 # build the archive that is going to be copied to the tester
Neels Hofmeyr1921c0f2017-09-04 16:34:18 +0200167
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200168 wanted_binaries_bin="$1"
169 wanted_binaries_sbin="$2"
Neels Hofmeyr1921c0f2017-09-04 16:34:18 +0200170
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200171 if [ -z "$wanted_binaries_bin" ] && [ -z "$wanted_binaries_sbin" ]; then
Neels Hofmeyr1921c0f2017-09-04 16:34:18 +0200172 set +x; echo "ERROR: create_bin_tgz needs a list of permitted binaries"; set -x
173 exit 1
174 fi
175
Pau Espin Pedrol1828d352018-05-17 18:06:15 +0200176 prune_files bin "$wanted_binaries_bin"
177 prune_files sbin "$wanted_binaries_sbin"
Neels Hofmeyr713a1202017-09-07 00:57:46 +0200178
Neels Hofmeyr53e758a2017-05-29 22:53:34 +0200179 cd "$prefix_real"
180 this="$name.build-${BUILD_NUMBER-$(date +%Y-%m-%d_%H_%M_%S)}"
181 tar="${this}.tgz"
182 tar czf "$base/$tar" *
183 cd "$base"
184 md5sum "$tar" > "${this}.md5"
185}