-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigure_cmake.sh
More file actions
executable file
·157 lines (131 loc) · 2.98 KB
/
configure_cmake.sh
File metadata and controls
executable file
·157 lines (131 loc) · 2.98 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
set -eo pipefail
IFS=$'\n\t'
me="$(basename "$0")"
sourcedir="$(cd "$(dirname "$0")" && pwd)"
builddir="build"
prefix="/usr/local"
CMakeCacheEntries=()
CMakeGenerator=""
build_type=""
install_after_build=false
cores=""
cmake_args=()
command -v cmake >/dev/null 2>&1 || {
echo "cmake is required but not installed"
exit 1
}
append_cache_entry() {
CMakeCacheEntries+=("-D$1:$2=$3")
}
detect_cores() {
if [ -n "$cores" ]; then
echo "$cores"
return
fi
if command -v nproc >/dev/null 2>&1; then
nproc
elif sysctl -n hw.ncpu >/dev/null 2>&1; then
sysctl -n hw.ncpu
else
echo 4
fi
}
while [ $# -gt 0 ]; do
case "$1" in
--builddir=*)
builddir="${1#*=}"
;;
--generator=*)
CMakeGenerator="${1#*=}"
;;
--prefix=*)
prefix="${1#*=}"
append_cache_entry "CMAKE_INSTALL_PREFIX" "PATH" "$prefix"
;;
--build-type=*)
build_type="${1#*=}"
;;
--install)
install_after_build=true
;;
--cores=*)
cores="${1#*=}"
;;
--define=*)
CMakeCacheEntries+=("-D${1#*=}")
;;
--)
shift
while [ $# -gt 0 ]; do
cmake_args+=("-D$1")
shift
done
break
;;
*)
if [[ "$1" == *=* ]]; then
cmake_args+=("-D$1")
else
echo "Invalid option: $1"
exit 1
fi
;;
esac
shift
done
if [ -z "$build_type" ]; then
if [ -z "${CFLAGS-}" ] && [ -z "${CXXFLAGS-}" ]; then
build_type="RelWithDebInfo"
else
build_type="Debug"
fi
fi
case "$build_type" in
Debug|Release|RelWithDebInfo|MinSizeRel) ;;
*)
echo "Invalid build type: $build_type"
exit 1
;;
esac
mkdir -p "$builddir"
cd "$builddir"
rm -f CMakeCache.txt
gen_args=()
if [ -n "$CMakeGenerator" ]; then
gen_args=(-G "$CMakeGenerator")
elif command -v ninja >/dev/null 2>&1; then
gen_args=(-G Ninja)
fi
cmake_cmd=(cmake)
if [ ${#gen_args[@]} -gt 0 ]; then
cmake_cmd+=("${gen_args[@]}")
fi
cmake_cmd+=(
"-DCMAKE_BUILD_TYPE=$build_type"
"-DCMAKE_INSTALL_PREFIX=$prefix"
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
"-DCMAKE_C_FLAGS=${CFLAGS-} ${CPPFLAGS-}"
"-DCMAKE_CXX_FLAGS=${CXXFLAGS-} ${CPPFLAGS-}"
)
if [ ${#CMakeCacheEntries[@]} -gt 0 ]; then
cmake_cmd+=("${CMakeCacheEntries[@]}")
fi
if [ ${#cmake_args[@]} -gt 0 ]; then
cmake_cmd+=("${cmake_args[@]}")
fi
cmake_cmd+=("$sourcedir")
echo "Configuring..."
"${cmake_cmd[@]}"
cat > config.status <<EOF
$me $*
EOF
chmod u+x config.status || true
echo "Building..."
num_cores="$(detect_cores)"
cmake --build . -- -j"$num_cores"
if [ "$install_after_build" = true ]; then
echo "Installing..."
cmake --install .
fi
echo "Done. Build directory: $builddir"