Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 1 | #!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 Freyther | cb132bd | 2018-09-15 10:21:40 +0100 | [diff] [blame] | 15 | # build_repo_dir openbsc ./openbsc |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 16 | # |
| 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 | |
| 39 | if [ -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 |
| 43 | fi |
| 44 | |
Pau Espin Pedrol | 70c01ec | 2020-02-17 17:42:05 +0100 | [diff] [blame] | 45 | git_url="${git_url:-git://git.osmocom.org}" |
| 46 | prefix="${prefix:-$base/inst-$name}" |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 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 |
Pau Espin Pedrol | 70c01ec | 2020-02-17 17:42:05 +0100 | [diff] [blame] | 50 | prefix_real="${prefix_real:-$prefix}" |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 51 | |
Holger Hans Peter Freyther | 90b0507 | 2018-09-23 15:14:00 +0100 | [diff] [blame] | 52 | # 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 Pedrol | 70c01ec | 2020-02-17 17:42:05 +0100 | [diff] [blame] | 54 | SANITIZE_FLAGS="${SANITIZE_FLAGS:---enable-sanitize}" |
Holger Hans Peter Freyther | 90b0507 | 2018-09-23 15:14:00 +0100 | [diff] [blame] | 55 | |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 56 | export PKG_CONFIG_PATH="$prefix_real/lib/pkgconfig:$PKG_CONFIG_PATH" |
| 57 | export 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. |
| 61 | env | grep -v "^LESS" | sort |
| 62 | |
| 63 | # clean the workspace |
Neels Hofmeyr | a8647f3 | 2017-09-13 19:17:08 +0200 | [diff] [blame] | 64 | rm -f "$base"/*.build-*.tgz |
| 65 | rm -f "$base"/*.build-*.md5 |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 66 | rm -rf "$prefix_real" |
| 67 | mkdir -p "$prefix_real" |
| 68 | |
| 69 | have_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 Freyther | 9eafb13 | 2018-09-18 22:55:05 +0100 | [diff] [blame] | 82 | 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 Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 89 | |
Neels Hofmeyr | b398b52 | 2017-06-23 04:13:30 +0200 | [diff] [blame] | 90 | |
| 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 Hofmeyr | 851802b | 2017-06-23 03:52:26 +0200 | [diff] [blame] | 93 | if git rev-parse "origin/$branch"; then |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 94 | branch="origin/$branch" |
| 95 | fi |
| 96 | |
Holger Hans Peter Freyther | 9eafb13 | 2018-09-18 22:55:05 +0100 | [diff] [blame] | 97 | git checkout -B build_branch "$branch" |
Neels Hofmeyr | 2581b50 | 2017-06-23 04:07:40 +0200 | [diff] [blame] | 98 | rm -rf * |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 99 | git reset --hard "$branch" |
| 100 | |
| 101 | git rev-parse HEAD |
| 102 | |
Pau Espin Pedrol | cbbe5d8 | 2018-09-18 15:39:17 +0200 | [diff] [blame] | 103 | echo "$(git rev-parse HEAD) $repo" >> "$prefix_real/${name}_git_hashes.txt" |
Pau Espin Pedrol | 7043996 | 2018-05-17 17:09:38 +0200 | [diff] [blame] | 104 | |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 105 | cd "$base" |
| 106 | } |
| 107 | |
| 108 | build_repo() { |
| 109 | # usage: build_repo <name> [<branch>] [--configure-opts [...]] |
Holger Hans Peter Freyther | cb132bd | 2018-09-15 10:21:40 +0100 | [diff] [blame] | 110 | dir="$1" |
| 111 | shift |
| 112 | build_repo_dir "${dir}" "./" $@ |
| 113 | } |
| 114 | |
| 115 | build_repo_dir() { |
| 116 | # usage: build_repo_dir <name> <dir> [<branch>] [--configure-opts [...]] |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 117 | dep="$1" |
Holger Hans Peter Freyther | cb132bd | 2018-09-15 10:21:40 +0100 | [diff] [blame] | 118 | dir="$2" |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 119 | branch="master" |
Holger Hans Peter Freyther | cb132bd | 2018-09-15 10:21:40 +0100 | [diff] [blame] | 120 | if [ -z "$(echo "$3" | grep '^-')" ]; then |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 121 | # second arg does not start with a dash, it's empty or a branch |
Holger Hans Peter Freyther | cb132bd | 2018-09-15 10:21:40 +0100 | [diff] [blame] | 122 | branch="$3" |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 123 | if [ -n "$branch" ]; then |
| 124 | # we had a branch arg, need to shift once more to get config options |
| 125 | shift |
| 126 | else |
| 127 | branch="master" |
| 128 | fi |
| 129 | fi |
| 130 | shift |
Holger Hans Peter Freyther | cb132bd | 2018-09-15 10:21:40 +0100 | [diff] [blame] | 131 | shift |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 132 | configure_opts="$@" |
| 133 | |
| 134 | set +x; echo " |
| 135 | |
| 136 | ====================== $dep |
| 137 | |
| 138 | "; set -x |
| 139 | |
| 140 | |
| 141 | have_repo "$dep" "$branch" |
| 142 | |
Holger Hans Peter Freyther | cb132bd | 2018-09-15 10:21:40 +0100 | [diff] [blame] | 143 | cd "$dep/${dir}" |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 144 | |
Pau Espin Pedrol | addf78e | 2020-02-03 15:47:01 +0100 | [diff] [blame] | 145 | if [ -f configure.ac ]; then |
| 146 | set +x; echo; echo; set -x |
| 147 | autoreconf -fi |
| 148 | set +x; echo; echo; set -x |
| 149 | ./configure --prefix="$prefix" --with-systemdsystemunitdir=no $CONFIGURE_FLAGS $configure_opts |
| 150 | elif [ -f CMakeLists.txt ]; then |
| 151 | rm -rf build && mkdir build && cd build || exit 1 |
| 152 | set +x; echo; echo; set -x |
Andre Puschmann | 8e1ba79 | 2020-04-20 15:12:55 +0200 | [diff] [blame] | 153 | cmake -DCMAKE_INSTALL_PREFIX=$prefix $configure_opts ../ |
Pau Espin Pedrol | 6231c3c | 2021-03-15 11:27:15 +0100 | [diff] [blame] | 154 | elif [ -f meson.build ]; then |
| 155 | rm -rf build && mkdir build && cd build || exit 1 |
| 156 | set +x; echo; echo; set -x |
Pau Espin Pedrol | 0696c60 | 2021-03-16 14:25:37 +0100 | [diff] [blame] | 157 | meson ../ --prefix=$prefix --libdir="lib" $configure_opts |
Pau Espin Pedrol | 6231c3c | 2021-03-15 11:27:15 +0100 | [diff] [blame] | 158 | ninja -j8 |
| 159 | ninja install |
| 160 | return |
Pau Espin Pedrol | addf78e | 2020-02-03 15:47:01 +0100 | [diff] [blame] | 161 | else |
| 162 | echo "Unknwown build system" && exit 1 |
| 163 | fi |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 164 | set +x; echo; echo; set -x |
| 165 | make -j8 || make # libsmpp34 can't build in parallel |
| 166 | set +x; echo; echo; set -x |
| 167 | make install |
| 168 | } |
| 169 | |
Pau Espin Pedrol | 1828d35 | 2018-05-17 18:06:15 +0200 | [diff] [blame] | 170 | prune_files() { |
| 171 | bindir="$1" |
| 172 | wanted_binaries="$2" |
| 173 | |
| 174 | if [ ! -d "$prefix_real/$bindir" ]; then return; fi |
| 175 | # remove binaries not intended to originate from this build |
| 176 | cd "$prefix_real/$bindir" |
| 177 | for f in * ; do |
| 178 | if [ -z "$(echo "_ $wanted_binaries _" | grep " $f ")" ]; then |
| 179 | rm "$f" |
| 180 | fi |
| 181 | done |
| 182 | |
| 183 | # ensure requested binaries indeed exist |
| 184 | for b in $wanted_binaries ; do |
| 185 | if [ ! -f "$b" ]; then |
| 186 | set +x; echo "ERROR: no such binary: $b in $prefix_real/$bindir/"; set -x |
| 187 | ls -1 "$prefix_real/$bindir" |
| 188 | exit 1 |
| 189 | fi |
| 190 | done |
| 191 | } |
| 192 | |
Holger Hans Peter Freyther | 9cf1c8a | 2018-11-04 14:43:17 +0000 | [diff] [blame] | 193 | add_rpath() { |
| 194 | # Adds an RPATH to executables in bin/ or sbin/ to search for the |
| 195 | # (Osmocom) libraries in `dirname /proc/self/exe`/../lib/. Adds an |
| 196 | # RPATH to a library to search in the same directory as the library. |
| 197 | # |
| 198 | # NOTE: Binaries should not have the SUID bit set and should run as the |
| 199 | # user executing the binary. |
| 200 | # |
| 201 | # NOTE: $ORIGIN is not a shell variable but a feature of the dynamic |
| 202 | # linker that will be expanded at runtime. For details see: |
| 203 | # http://man7.org/linux/man-pages/man8/ld.so.8.html |
| 204 | # |
| 205 | # Add an rpath relative to the binary and library if the directory |
| 206 | # exists. |
| 207 | if [ -d bin/ ]; then |
| 208 | find bin -depth -type f -exec patchelf --set-rpath '$ORIGIN/../lib/' {} \; |
| 209 | fi |
| 210 | if [ -d sbin/ ]; then |
| 211 | find sbin -depth -type f -exec patchelf --set-rpath '$ORIGIN/../lib/' {} \; |
| 212 | fi |
| 213 | if [ -d lib/ ]; then |
| 214 | find lib -depth -type f -name "lib*.so.*" -exec patchelf --set-rpath '$ORIGIN/' {} \; |
| 215 | fi |
| 216 | } |
| 217 | |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 218 | create_bin_tgz() { |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 219 | # build the archive that is going to be copied to the tester |
Neels Hofmeyr | 1921c0f | 2017-09-04 16:34:18 +0200 | [diff] [blame] | 220 | |
Pau Espin Pedrol | 1828d35 | 2018-05-17 18:06:15 +0200 | [diff] [blame] | 221 | wanted_binaries_bin="$1" |
| 222 | wanted_binaries_sbin="$2" |
Neels Hofmeyr | 1921c0f | 2017-09-04 16:34:18 +0200 | [diff] [blame] | 223 | |
Pau Espin Pedrol | 1828d35 | 2018-05-17 18:06:15 +0200 | [diff] [blame] | 224 | if [ -z "$wanted_binaries_bin" ] && [ -z "$wanted_binaries_sbin" ]; then |
Neels Hofmeyr | 1921c0f | 2017-09-04 16:34:18 +0200 | [diff] [blame] | 225 | set +x; echo "ERROR: create_bin_tgz needs a list of permitted binaries"; set -x |
| 226 | exit 1 |
| 227 | fi |
| 228 | |
Pau Espin Pedrol | 1828d35 | 2018-05-17 18:06:15 +0200 | [diff] [blame] | 229 | prune_files bin "$wanted_binaries_bin" |
| 230 | prune_files sbin "$wanted_binaries_sbin" |
Pau Espin Pedrol | addf78e | 2020-02-03 15:47:01 +0100 | [diff] [blame] | 231 | # Drop all static libraries if exist: |
| 232 | rm -f $prefix_real/lib/*.a |
| 233 | rm -f $prefix_real/lib/*.la |
Neels Hofmeyr | 713a120 | 2017-09-07 00:57:46 +0200 | [diff] [blame] | 234 | |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 235 | cd "$prefix_real" |
Holger Hans Peter Freyther | 9cf1c8a | 2018-11-04 14:43:17 +0000 | [diff] [blame] | 236 | add_rpath |
Neels Hofmeyr | 53e758a | 2017-05-29 22:53:34 +0200 | [diff] [blame] | 237 | this="$name.build-${BUILD_NUMBER-$(date +%Y-%m-%d_%H_%M_%S)}" |
| 238 | tar="${this}.tgz" |
| 239 | tar czf "$base/$tar" * |
| 240 | cd "$base" |
| 241 | md5sum "$tar" > "${this}.md5" |
| 242 | } |