Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / cmake-build.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11
12 # Creates a build subdir then invokes cmake and make from that dir with the
13 # specified install prefix and options. Optionally runs make install and tars
14 # up all files from the install prefix, then uses sudo to extract those files
15 # to /usr/local and run ldconfig, leaving shared lib(s) ready for use.
16 # Prereqs:
17 # The build minion has cmake, make, gcc etc.
18 # Environment variables:
19 # WORKSPACE is a non-empty path (required)
20 # CMAKE_INSTALL_PREFIX is a non-empty path (required)
21 # PROJECT is a non-empty name (required)
22 # BUILD_DIR is a path (optional; has usable default)
23 # CMAKE_OPTS has options for cmake (optional, empty default)
24 # MAKE_OPTS has options for make (optional, empty default)
25 # INSTALL is "true" or "false" (optional, default true)
26
27 echo "---> cmake-build.sh"
28
29 # be careful and verbose
30 set -eux -o pipefail
31
32 build_dir="${BUILD_DIR:-$WORKSPACE/target}"
33 cmake_opts="${CMAKE_OPTS:-}"
34 make_opts="${MAKE_OPTS:-}"
35 install="${INSTALL:-true}"
36 # Not a misspelling as shellcheck reports.
37 # shellcheck disable=SC2153
38 project="${PROJECT//\//\-}"
39
40 mkdir -p "$build_dir"
41 cd "$build_dir" || exit
42 cmake -version
43 # $cmake_opts needs to wordsplit to pass options.
44 # shellcheck disable=SC2086
45 eval cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX" $cmake_opts ..
46 make -version
47 # $make_opts needs to wordsplit to pass options.
48 # shellcheck disable=SC2086
49 make $make_opts
50
51 if [[ $install == true ]]; then
52     make install
53     mkdir -p "$WORKSPACE/dist"
54     tar -cJvf "$WORKSPACE/dist/$project.tar.xz" -C "$INSTALL_PREFIX" .
55     sudo tar -xvf "$WORKSPACE/dist/$project.tar.xz" -C "/usr/local"
56     sudo ldconfig
57 fi
58
59 echo "---> cmake-build.sh ends"