-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbuild.sh
executable file
·190 lines (165 loc) · 7.09 KB
/
cbuild.sh
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
#!/bin/bash
displayusage() {
echo " =================================================================================== "
echo "| Usage: |"
echo "| cbuild.sh OPTS |"
echo "| available options [OPTS]: |"
echo "| -b) --build) automatically updates the build when necessary |"
echo "| -c) --clean) removes build dirs |"
echo "| -d) --dry-run) creates the make file without building |"
echo "| -f) --force) forces an update of the build |"
echo "| -h) --help) print this help |"
echo "| -m) --make) performs make |"
echo "| -n) --nproc) sets the number of parallel processing (default nproc -1) |"
echo "| -o) --build-one) build a single test. Equivalent to \"-w -DBUILDSINGLE=NAME\"|"
echo "| -r) --recompile) continuously build when a file is saved |"
echo "| -s) --build-suite) build test suite. Equivalent to \"-w -DBUILDSUITE=ON\" |"
echo "| -t) --build-type) specifies a different cmake build type (e.g. \"-t Debug\") |"
echo "| -u) --no-unity-build) do not use unity build. Equivalent to \"-w -NOUNITYBUILD=ON\" |"
echo "| -w) --cmake-params) specifies cmake options in quoted (e.g. \"-DVAR=value\") |"
echo "| -z) --analyze) run scan-build |"
echo "| [no arguments] automatically updates the build when necessary |"
echo " =================================================================================== "
}
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) MACHINE=linux;;
Darwin*) MACHINE=macos;;
CYGWIN*) MACHINE=win;;
MINGW*) MACHINE=win;;
*) echo "unsupported architecture"; exit 1
esac
# Set all option to FALSE
ANALYZE="FALSE"
BUILD="FALSE"
CLEANBUILD="FALSE"
DRY_RUN="FALSE"
UPDATEMAKEFILES="FALSE"
EMAKE="FALSE"
CONTINUOUSCOMPILE="FALSE"
BASHSCRIPTDIR="$(cd "$(dirname "$0")" || exit; pwd)"
CURRENTDIR="$(pwd)"
SOURCEDIR="${CURRENTDIR}"
if [[ ! -f "${SOURCEDIR}/CMakeLists.txt" ]] ; then
echo "CMakeLists.txt not found. Exiting..."
exit 1
fi
update_makefiles(){
mkdir -p "${BUILDDIR}"
CURDIR="$(pwd)"
cd "${BUILDDIR}" || exit
if [ "${MACHINE}" == "macos" ]; then
cmake "${SOURCEDIR}" -DCMAKE_BUILD_TYPE="${BUILDTYPE:-Release}" ${CMAKEOPTS:+$CMAKEOPTS}
elif [ "${MACHINE}" == "linux" ]; then
cmake "${SOURCEDIR}" -DCMAKE_BUILD_TYPE="${BUILDTYPE:-Release}" ${CMAKEOPTS:+$CMAKEOPTS}
elif [ "${MACHINE}" == "win" ]; then
WINARCH="x64"
if [[ $PROCESSOR_IDENTIFIER == *"ARM"* ]]; then WINARCH="ARM64"; fi
cmake -A "${WINARCH}" "${SOURCEDIR}" -DCMAKE_BUILD_TYPE="${BUILDTYPE:-Release}" ${CMAKEOPTS:+$CMAKEOPTS}
fi
CMAKE_RET=$?
if [ $CMAKE_RET -ne 0 ] ; then exit ${CMAKE_RET}; fi
local BT="${BUILDTYPE:-Release}"
echo "$(tr '[:lower:]' '[:upper:]' <<< "${BT:0:1}")$(tr '[:upper:]' '[:lower:]' <<< "${BT:1}")" > "${BUILDTYPEFILE}"
cd "${CURDIR}" || exit
MAKEFILEUPDATED="TRUE"
}
build(){
if [[ ! -f "${NBFILES}" ]] ; then mkdir -p "${BUILDDIR}"; echo 0 > "${NBFILES}" ; fi
PREVFILES=$(<"${NBFILES}")
CURRFILES=$(find "${SOURCEDIR}" | wc -l)
if [[ "${MAKEFILEUPDATED}" == "TRUE" ]] || [[ "${CURRFILES}" -ne "${PREVFILES}" ]] ; then update_makefiles ; fi
if [ "${MACHINE}" == "win" ]; then
CMAKE_BUILD_PARALLEL_LEVEL=3 cmake --build "${BUILDDIR}" --config "${BUILDTYPE:-Release}"
else
cmake --build "${BUILDDIR}" --config "${BUILDTYPE:-Release}" -j "${NPROC}"
fi
CMAKE_RET=$?
echo "${CURRFILES}" > "${NBFILES}"
if [ $CMAKE_RET -ne 0 ] ; then exit ${CMAKE_RET}; fi
}
analyze(){
mkdir -p "${BUILDDIR}"
cd "${BUILDDIR}" || exit
if [ "${MACHINE}" == "macos" ]; then
scan-build cmake "${SOURCEDIR}" -DCMAKE_BUILD_TYPE="${BUILDTYPE:-Release}" ${CMAKEOPTS:+$CMAKEOPTS}
else
echo "unsupported architecture"; exit 1
fi
CMAKE_RET=$?
if [ $CMAKE_RET -ne 0 ] ; then exit ${CMAKE_RET}; fi
local BT="${BUILDTYPE:-Release}"
echo "$(tr '[:lower:]' '[:upper:]' <<< "${BT:0:1}")$(tr '[:upper:]' '[:lower:]' <<< "${BT:1}")" > "${BUILDTYPEFILE}"
cd - || exit
scan-build cmake --build "${BUILDDIR}" --config "${BUILDTYPE:-Release}" -j "${NPROC}"
CMAKE_RET=$?
if [ $CMAKE_RET -ne 0 ] ; then exit ${CMAKE_RET}; fi
}
for arg in "$@"; do
shift
case "$arg" in
"--build") set -- "$@" "-b" ;;
"--clean") set -- "$@" "-c" ;;
"--dry-run") set -- "$@" "-d" ;;
"--force") set -- "$@" "-f" ;;
"--help") set -- "$@" "-h" ;;
"--make") set -- "$@" "-m" ;;
"--nproc") set -- "$@" "-n" ;;
"--build-one") set -- "$@" "-o" ;;
"--recompile") set -- "$@" "-r" ;;
"--build-suite") set -- "$@" "-s" ;;
"--build-type") set -- "$@" "-t" ;;
"--no-unity-build") set -- "$@" "-u" ;;
"--cmake-params") set -- "$@" "-w" ;;
"--analyze") set -- "$@" "-z" ;;
*) set -- "$@" "$arg";;
esac
done
# Parse short options
OPTIND=1
while getopts "bcdfhmn:o:rst:uw:z?" opt
do
case "$opt" in
"b") BUILD="TRUE";;
"c") CLEANBUILD="TRUE";;
"d") UPDATEMAKEFILES="TRUE"; DRY_RUN="TRUE" ;;
"f") UPDATEMAKEFILES="TRUE";;
"h") displayusage; exit 0;;
"m") EMAKE="TRUE";;
"n") NPROC="${OPTARG}";;
"o") CMAKEOPTS+=" -DBUILDSINGLE=${OPTARG} "; UPDATEMAKEFILES="TRUE";;
"r") CONTINUOUSCOMPILE="TRUE";;
"s") CMAKEOPTS+=" -DBUILDSUITE=ON "; UPDATEMAKEFILES="TRUE";;
"t") BUILDTYPE=${OPTARG}; UPDATEMAKEFILES="TRUE";;
"u") CMAKEOPTS+=" -DNOUNITYBUILD=ON "; UPDATEMAKEFILES="TRUE";;
"w") CMAKEOPTS+="${OPTARG} "; UPDATEMAKEFILES="TRUE";;
"z") ANALYZE="TRUE" ;;
"?") displayusage; exit 0;;
esac
done
shift "$((OPTIND-1))"
CFG=${BUILDTYPE:-Release}
CFG=$(echo "${CFG}" | tr '[:upper:]' '[:lower:]' )
BUILDDIR="${CURRENTDIR}/build/master/${CFG}"
NBFILES="${BUILDDIR}/.nbfiles"
BUILDTYPEFILE="${BUILDDIR}/.buildtype"
if [[ -z "${NPROC}" ]]; then (( NPROC = $(nproc) - 1 )); fi
if [[ "${CLEANBUILD}" == "TRUE" ]] || [[ "${ANALYZE}" == "TRUE" ]] ; then
read -r -p "Are you sure? [y/N] " CLEANCONFIRM
case "${CLEANCONFIRM}" in
[yY][eE][sS]|[yY])
rm -rf "${ROOTDIR}"/_bin/test "${BUILDDIR}";;
*)
exit 0;;
esac
fi
if [[ "${BUILD}" == "TRUE" ]] ; then build; exit 0; fi
if [[ "${EMAKE}" == "TRUE" ]] ; then emake; exit 0; fi
if [[ "${ANALYZE}" == "TRUE" ]] ; then analyze; exit 0; fi
if [[ "${UPDATEMAKEFILES}" == "TRUE" ]] ; then update_makefiles; fi
if [[ "${DRY_RUN}" == "TRUE" ]] ; then exit 0; fi
if [[ "${CONTINUOUSCOMPILE}" == "TRUE" ]] ; then
find "${SOURCEDIR}" -not \( -path "${BUILDDIR}" -prune \) -type f | entr -s "${BASHSCRIPTDIR}/cbuild.sh"
else
build
fi