Repository navigation
Expand file tree
/
Copy pathprepare_cmake.sh
More file actions
executable file
·372 lines (271 loc) · 8.69 KB
/
Copy pathprepare_cmake.sh
File metadata and controls
executable file
·372 lines (271 loc) · 8.69 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#! /bin/bash
# ##########################################################
# functions - 1
sis_cmake_is_truey() {
case "$(printf '%s' "${1:-}" | tr '[:upper:]' '[:lower:]')" in
1|ok|on|true|yes|y)
return 0
;;
*)
return 1
;;
esac
}
# ##########################################################
# constants and variables
Basename=$(basename "$0")
Dir=$(cd "$(dirname "$0")" && pwd)
CMakeDir=${SIS_CMAKE_BUILD_DIR:-$Dir/_build}
ProjectNameFile="$Dir/.sis/project_name.txt"
ProjectName=$(tr -d '[:space:]' < "$ProjectNameFile")
ScriptPath=$0
AlwaysUseColours=${SIS_CMAKE_ALWAYS_USE_COLOURS:-${SIS_ALWAYS_USE_COLOURS:-0}}
BuildSharedLibs=0
Configuration=Release
ExamplesDisabled=0
MinGW=$(sis_cmake_is_truey "${SIS_CMAKE_MINGW:-}" && echo 1 || echo 0)
MSVC_MT=0
RunMake=0
SisUseColours=0
TestingDisabled=0
VerboseMakefile=0
# ##########################################################
# PROJECT-SPECIFIC FLAGS (customise per library; keep marker)
#
# Add project-only options here (e.g. --no-cpp, --stlsoft-root-dir,
# --no-shwild). The common body below must stay aligned with
# misc-dev-scripts/shell-scripts/cmake-helpers/prepare_cmake.sh.
CStandard=
CXXStandard=
NO_shwild=0
STLSoftDirGiven=
# ##########################################################
# colours
#
# Enable when tput is available and either:
# - AlwaysUseColours is set (overrides NO_COLOR; may set TERM if
# empty/dumb), or
# - NO_COLOR is unset, stdout is a TTY, and TERM is not dumb (union of
# collect-c's "TERM set + TTY" and cstring's "TTY" — empty TERM on a
# TTY is OK).
SisClr_Blue=
SisClr_Bold=
SisClr_Green=
SisClr_None=
SisClr_Red=
SisClr_Yellow=
for arg in "$@"; do
case $arg in
--always-use-colors|--always-use-colours|-A)
AlwaysUseColours=1
;;
esac
done
if command -v tput >/dev/null 2>&1; then
if [ $AlwaysUseColours -ne 0 ]; then
if [ -z "${TERM:-}" ] || [ "$TERM" = "dumb" ]; then
TERM=xterm-256color
fi
SisUseColours=1
elif [ -z "${NO_COLOR:-}" ] && [ -t 1 ] && [ "${TERM:-}" != "dumb" ]; then
SisUseColours=1
fi
fi
if [ $SisUseColours -ne 0 ]; then
SisClr_Blue=${FG_BLUE:-$(tput setaf 4)}
SisClr_Bold=${FD_BOLD:-$(tput bold)}
SisClr_Green=${FG_GREEN:-$(tput setaf 2)}
SisClr_None=${FD_NONE:-$(tput sgr0)}
SisClr_Red=${FG_RED:-$(tput setaf 1)}
SisClr_Yellow=${FG_YELLOW:-$(tput setaf 3)}
fi
CMakeDirClr="${SisClr_Blue}${SisClr_Bold}${CMakeDir}${SisClr_None}"
ProjectNameClr="${SisClr_Blue}${SisClr_Bold}${ProjectName}${SisClr_None}"
ScriptPathClr="${SisClr_Blue}${SisClr_Bold}${ScriptPath}${SisClr_None}"
# ##########################################################
# functions - 2
sis_cmake_build() {
local config="${SIS_CMAKE_CONFIG:-Release}"
local args=(--build "$CMakeDir")
if [ -f "$CMakeDir/CMakeCache.txt" ] && grep -q '^CMAKE_CONFIGURATION_TYPES:' "$CMakeDir/CMakeCache.txt" 2>/dev/null; then
args+=(--config "$config")
fi
if [ "$#" -gt 0 ]; then
local t
for t in "$@"; do
args+=(--target "$t")
done
fi
cmake "${args[@]}"
}
# ##########################################################
# command-line handling
while [[ $# -gt 0 ]]; do
case $1 in
--always-use-colors|--always-use-colours|-A)
# AlwaysUseColours=1 - this is handled by the for loop above
;;
--build-shared-libs)
BuildSharedLibs=1
;;
--c-standard)
shift
CStandard=$1
case $CStandard in
90|99|11|17|23)
;;
*)
>&2 echo "${ScriptPathClr}: invalid C standard '${SisClr_Red}${SisClr_Bold}$CStandard${SisClr_None}'; expected 90, 99, 11, 17, or 23"
exit 1
;;
esac
;;
--cmake-verbose-makefile|-v)
VerboseMakefile=1
;;
--cxx-standard)
shift
CXXStandard=$1
case $CXXStandard in
98|11|14|17|20|23)
;;
*)
>&2 echo "${ScriptPathClr}: invalid C++ standard '${SisClr_Red}${SisClr_Bold}$CXXStandard${SisClr_None}'; expected 98, 11, 14, 17, 20, or 23"
exit 1
;;
esac
;;
--debug-configuration|-d)
Configuration=Debug
;;
--disable-examples|-E)
ExamplesDisabled=1
;;
--disable-testing|-T)
TestingDisabled=1
;;
--mingw)
MinGW=1
;;
--msvc-mt)
MSVC_MT=1
;;
--no-shwild)
NO_shwild=1
;;
--run-make|-m)
RunMake=1
;;
--stlsoft-root-dir|-s)
shift
STLSoftDirGiven=$1
;;
--help)
[ -f "$Dir/.sis/script_info_lines.txt" ] && cat "$Dir/.sis/script_info_lines.txt"
cat << EOF
Creates/reinitialises the CMake build script(s)
${ScriptPath} [ ... flags/options ... ]
Flags/options:
behaviour:
-A
--always-use-colors
--always-use-colours
forces use of colours even when stdout is not a TTY
--build-shared-libs
builds ${ProjectName} as a shared library (BUILD_SHARED_LIBS=ON)
--c-standard {90|99|11|17|23}
sets CMAKE_C_STANDARD; when omitted, CMakeLists.txt defaults apply
-v
--cmake-verbose-makefile
configures CMake to run verbosely (CMAKE_VERBOSE_MAKEFILE=ON)
--cxx-standard {98|11|14|17|20|23}
sets CMAKE_CXX_STANDARD; when omitted, CMakeLists.txt defaults apply
-d
--debug-configuration
use Debug configuration (CMAKE_BUILD_TYPE=Debug). Default is Release
-E
--disable-examples
disables building of examples (BUILD_EXAMPLES=OFF)
-T
--disable-testing
disables building of tests (BUILD_TESTING=OFF)
--mingw
uses explicitly the "MinGW Makefiles" generator
--msvc-mt
when using Visual C++ (MSVC), select the static runtime library
--no-shwild
prevents recognising shwild library (NO_SHWILD=ON)
-m
--run-make
executes a build via cmake --build after a successful configure
-s <dir>
--stlsoft-root-dir <dir>
specifies the STLSoft root-directory, which will be passed to CMake
as the variable STLSOFT, and which will override the environment
variable STLSOFT (if present)
standard flags:
--help
displays this help and terminates
EOF
exit 0
;;
*)
>&2 echo "${ScriptPathClr}: unrecognised argument '${SisClr_Red}${SisClr_Bold}$1${SisClr_None}'; use --help for usage"
exit 1
;;
esac
shift
done
# ##########################################################
# main()
mkdir -p "$CMakeDir" || exit 1
echo
echo "Executing CMake for ${ProjectNameClr} (in ${CMakeDirClr})"
if [ $BuildSharedLibs -eq 0 ]; then CMakeBuildSharedLibsFlag="OFF" ; else CMakeBuildSharedLibsFlag="ON" ; fi
if [ $ExamplesDisabled -eq 0 ]; then CMakeBuildExamplesFlag="ON" ; else CMakeBuildExamplesFlag="OFF" ; fi
if [ $MSVC_MT -eq 0 ]; then CMakeMsvcMtFlag="OFF" ; else CMakeMsvcMtFlag="ON" ; fi
if [ -z "$CStandard" ]; then CMakeCStandardVariable="" ; else CMakeCStandardVariable="-DCMAKE_C_STANDARD=$CStandard" ; fi
if [ -z "$CXXStandard" ]; then CMakeCXXStandardVariable="" ; else CMakeCXXStandardVariable="-DCMAKE_CXX_STANDARD=$CXXStandard" ; fi
if [ $NO_shwild -eq 0 ]; then CMakeNoShwild="OFF" ; else CMakeNoShwild="ON" ; fi
if [ -z "$STLSoftDirGiven" ]; then CMakeSTLSoftVariable="" ; else CMakeSTLSoftVariable="-DSTLSOFT=$STLSoftDirGiven/" ; fi
if [ $TestingDisabled -eq 0 ]; then CMakeBuildTestingFlag="ON" ; else CMakeBuildTestingFlag="OFF" ; fi
if [ $VerboseMakefile -eq 0 ]; then CMakeVerboseMakefileFlag="OFF" ; else CMakeVerboseMakefileFlag="ON" ; fi
# NOTE: the generator is the *only* thing that may differ between the MinGW
# and the default paths; every -D option is passed in both cases, so that no
# flag can be silently ignored according to the generator selected.
CMakeGeneratorArgs=()
if [ -n "${SIS_CMAKE_GENERATOR:-}" ] && [ $MinGW -eq 0 ]; then
CMakeGeneratorArgs=(-G "$SIS_CMAKE_GENERATOR")
fi
if [ $MinGW -ne 0 ]; then
CMakeGeneratorArgs=(-G "MinGW Makefiles")
fi
cmake \
-DBUILD_EXAMPLES:BOOL=$CMakeBuildExamplesFlag \
-DBUILD_SHARED_LIBS:BOOL=$CMakeBuildSharedLibsFlag \
-DBUILD_TESTING:BOOL=$CMakeBuildTestingFlag \
-DCMAKE_BUILD_TYPE=$Configuration \
-DCMAKE_VERBOSE_MAKEFILE:BOOL=$CMakeVerboseMakefileFlag \
-DMSVC_USE_MT:BOOL=$CMakeMsvcMtFlag \
-DNO_SHWILD:BOOL=$CMakeNoShwild \
$CMakeCStandardVariable \
$CMakeCXXStandardVariable \
$CMakeSTLSoftVariable \
"${CMakeGeneratorArgs[@]}" \
-B "$CMakeDir" \
-S "$Dir" \
|| exit 1
status=0
if [ $RunMake -ne 0 ]; then
echo
echo "Executing build of ${ProjectNameClr} (via cmake --build)"
sis_cmake_build
status=$?
fi
if [ $VerboseMakefile -ne 0 ]; then
echo
echo -e "contents of ${CMakeDirClr}:"
ls -al "$CMakeDir"
fi
exit $status
# ############################## end of file ############################# #