/
cmake-easyinstall
executable file
·96 lines (84 loc) · 2.22 KB
/
cmake-easyinstall
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env bash
#
# Copyright 2020-2021 Axel Huebl
#
# License: BSD-2-Clause
#
# dependencies: bash, cmake 3.15.0+, git
set -eu -o pipefail
# usage
function print_usage_and_exit() {
echo "Usage: $0 [--prefix=<dir>] git+<https-url>.git[@branch] [cmake-options]"
exit 1
}
# environment options
CEI_CMAKE=${CEI_CMAKE:-"cmake"}
CEI_CONFIG=${CEI_CONFIG:-"RelWithDebInfo"}
CEI_PARALLEL=${CEI_PARALLEL:-"2"}
CEI_PREFIX=${CEI_PREFIX:-""}
CEI_SUDO=${CEI_SUDO:-""}
CEI_TMP=${CEI_TMP:-""}
# command line options (take precedence over environment)
if [ "$#" -lt 1 ]; then
print_usage_and_exit
fi
if [ "${1}" == "--prefix" ] && [ "$#" -lt 3 ]; then
print_usage_and_exit
fi
if [ "${1::9}" == "--prefix=" ] && [ "$#" -lt 2 ]; then
print_usage_and_exit
fi
if [ "${1}" == "--prefix" ]; then
CEI_PREFIX="${2}"
uri=${3}
cmake_opts=${@:4}
elif [ "${1::9}" == "--prefix=" ]; then
CEI_PREFIX="${1:9}"
uri=${2}
cmake_opts=${@:3}
else
uri=${1}
cmake_opts=${@:2}
fi
# create a temporary build directory
if [ -z "${CEI_TMP}" ]; then
tmp_dir=$(mktemp --help >/dev/null 2>&1 && mktemp -d -t ci-XXXXXXXXXX || mktemp -d "${TMPDIR:-/tmp}"/ci-XXXXXXXXXX)
if [ $? -ne 0 ]; then
echo "Cannot create a temporary directory"
exit 2
fi
else
tmp_dir=${CEI_TMP}
fi
rm -rf ${tmp_dir}/build
mkdir -p ${tmp_dir}/build
# download
git_url=${uri:4} # crop-off "git+" prefix
# branch provided?
git_branch=""
IFS='@' read -ra git_url_tokens <<< "$git_url"
if [ ${#git_url_tokens[@]} -gt 1 ]; then
git_url="${git_url_tokens[0]}"
git_branch="--branch ${git_url_tokens[1]}"
fi
git clone \
--depth 1 --shallow-submodules \
${git_url} ${git_branch} ${tmp_dir}/src
# configure
cd ${tmp_dir}/build
if [ -z "${CEI_PREFIX}" ]; then
${CEI_CMAKE} ../src -DCMAKE_BUILD_TYPE=${CEI_CONFIG} ${cmake_opts}
else
${CEI_CMAKE} ../src -DCMAKE_BUILD_TYPE=${CEI_CONFIG} -DCMAKE_INSTALL_PREFIX="${CEI_PREFIX}" ${cmake_opts}
fi
# build
${CEI_CMAKE} --build . --config ${CEI_CONFIG} --parallel ${CEI_PARALLEL}
# install
if [ -z "${CEI_SUDO}" ]; then
${CEI_CMAKE} --install . --config ${CEI_CONFIG}
else
${CEI_SUDO} ${CEI_CMAKE} --install . --config ${CEI_CONFIG}
fi
# remove temporary directory
cd -
rm -rf ${tmp_dir}