-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev
executable file
·368 lines (308 loc) · 9.28 KB
/
dev
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
#!/bin/bash
C4M_DEV_CFG="--buildtype=debug -Duse_memcheck=on -Ddev_mode=true"
C4M_RELEASE_CFG="--buildtype=release"
C4M_DEBUG_EXTRA="-Dshow_preprocessor_config=enabled -Dforkless_tests=enabled -Duse_memcheck=on -Dexception_traces=enabled"
C4M_SANITIZE_EXTRA="-Duse_ubsan=enabled -Duse_asan=enabled -Dshow_preprocessor_config=enabled -Dforkless_tests=enabled"
C4M_TEST_CFG=${C4M_DEV_CFG}
C4M_MESON_STATE_FILE=.meson_last
C4M_TEST_EXE=c4test
function color {
case $1 in
black) CODE=0 ;;
red) CODE=1 ;; RED) CODE=9 ;;
green) CODE=2 ;; GREEN) CODE=10 ;;
yellow) CODE=3 ;; YELLOW) CODE=11 ;;
blue) CODE=4 ;; BLUE) CODE=12 ;;
magenta) CODE=5 ;; MAGENTA) CODE=13 ;;
cyan) CODE=6 ;; CYAN) CODE=14 ;;
white) CODE=7 ;; WHITE) CODE=15 ;;
grey) CODE=8 ;; *) CODE=$1 ;;
esac
shift
echo -n $(tput setaf ${CODE})$@$(tput op)
}
OS=$(uname -o 2>/dev/null)
ARCH=$(uname -m 2>/dev/null)
PWD=$(pwd)
if [[ $? -ne 0 ]] ; then
# Older macOS/OSX versions of uname don't support -o
OS=$(uname -s)
fi
function log {
echo $(color blue "[-- libcon4m --]" $@)
}
function ensure_env_setup {
export LIBRARY_PATH=${PWD}/deps/${OS}-${ARCH}/:${HOME}/.local/c0/libs/:${HOME}/:
log "OS="$(color green ${OS})
log "ARCH="$(color green ${ARCH})
log "LIBRARY_PATH="$(color green ${LIBRARY_PATH})
}
function cur_exe_loc {
echo ${C4M_BUILD_DIR}/${C4M_TEST_EXE}
}
function meson_hatrack {
if [[ ! -d ${1} ]]; then
if [[ -f ${1} ]]; then
rm -rf ${1}
fi
log Creating meson target ${1}
meson setup -Dbuild_hatrack=enable ${1}
fi
cd ${1}
log Compiling meson target ${1}
if [[ ${OS} = "Darwin" ]] ; then
meson compile hash
else
CC=musl-gcc meson compile hash
fi
CODE=$?
if [[ $CODE -eq 0 ]] ; then
log "Done!"
else
log "Build FAILED"
fi
cd ..
if [[ ${1} != "debug" ]]; then
exit $CODE
fi
if [[ $CODE -ne 0 ]]; then
exit $CODE
fi
}
function libcon4m_dev_usage {
echo $(color YELLOW "Usage: ./dev [ profile | build | run | debug | rebuild | clean | hash ]")
echo $(color CYAN " profile [arg]") " Sets the current profile, or shows without arguments."
echo " Valid profiles are: " $(color blue "dev debug release test")
echo $(color CYAN " build [meson_options]") " Builds the current profile, passing any additional options to" $(color blue "meson setup") "if provided."
echo $(color CYAN " run") " Ensures the current profile is built, and if so, runs the test suite. "
echo " If not, attempts to compile it first."
echo $(color CYAN " debug") " Runs the current profile's test binary in the debugger."
echo $(color CYAN " rebuild [meson_options]") "Same as " $(color blue "build") "except it forces a rebuild."
echo $(color CYAN " clean [all | profile]") " Completely wipes the specified profile(s), or the current profile if no argument."
echo $(color CYAN " hash") " Does a build of libhatrack only."
exit 1
}
function ensure_directory {
if [[ ! -d ${C4M_BUILD_DIR} ]]; then
mkdir ${C4M_BUILD_DIR}
fi
}
function ensure_project {
# Return 1 if the project was just setup, 0 if not.
meson introspect ${C4M_BUILD_DIR} > /dev/null
if [[ $? -ne 0 ]] ; then
shift
log Setting up target: $(color green ${C4M_BUILD_TARGET})
meson setup ${C4M_BUILD_DIR} ${C4M_MESON_CONF} ${C4M_PASTHROUGH_ARGS}
if [[ $? -ne 0 ]] ; then
log Setup for target ${C4M_BUILD_TARGET} $(color FAILED.)
exit -1
fi
log Setup for target ${C4M_BUILD_TARGET}: $(color GREEN SUCCESS)
fi
}
function configure_target {
if [[ $# -ne 0 ]] ; then
meson configure ${C4M_BUILD_DIR} ${C4M_PASSTHROUGH_ARGS}
fi
}
function build_target {
meson compile ${C4M_BUILD_DIR}
}
function rebuild_target {
meson compile --clean
}
function set_profile {
if [[ $# -eq 0 ]] ; then
if [[ -f ${C4M_MESON_STATE_FILE} ]]; then
WRITE_TARGET_FILE=0
export C4M_BUILD_TARGET=$(cat ${C4M_MESON_STATE_FILE})
else
WRITE_TARGET_FILE=1
export C4M_BUILD_TARGET=dev
fi
else
WRITE_TARGET_FILE=1
export C4M_BUILD_TARGET=${1}
shift
export C4M_PASSTHROUGH_ARGS=${@}
fi
case ${C4M_BUILD_TARGET} in
dev)
export C4M_MESON_CONF="${C4M_DEV_CFG}"
;;
sanitize)
export C4M_MESON_CONF="${C4M_DEV_CFG} ${C4M_SANITIZE_EXTRA}"
;;
debug)
export C4M_MESON_CONF="${C4M_DEV_CFG} ${C4M_DEBUG_EXTRA}"
;;
release)
export C4M_MESON_CONF="${C4M_RELEASE_CFG}"
;;
test)
export C4M_MESON_CONF="${C4M_TEST_CFG}"
;;
*)
echo $(color RED "[-- libcon4m --] ") Invalid profile: $(color RED ${C4M_BUILD_TARGET})
echo $(color blue "Valid targets are: dev, debug, release, sanitize, test")
return -1
;;
esac
if [[ ${WRITE_TARGET_FILE} -eq 1 ]]; then
log "Setting the current profile to:" $(color green ${C4M_BUILD_TARGET})
echo ${C4M_BUILD_TARGET} > ${C4M_MESON_STATE_FILE}
else
log "Current profile is: " $(color YELLOW ${C4M_BUILD_TARGET})
fi
export C4M_BUILD_DIR="build_${C4M_BUILD_TARGET}"
log "Current meson build directory is:" $(color YELLOW ${C4M_BUILD_DIR})
return 0
}
function clean_one_target {
TARGET=build_${1}
if [[ -d ${TARGET} ]] ; then
log Cleaning: ${1}
rm -rf ${TARGET}
else
log Nothing to clean for ${1}
fi
}
function libcon4m_clean {
if [[ "$1" == "all" ]] ; then
for TARGET in dev sanitize debug release test ; do
clean_one_target ${TARGET}
done
if [[ -f ${C4M_MESON_STATE_FILE} ]]; then
rm ${C4M_MESON_STATE_FILE}
fi
else
if [[ $# -ne 0 ]]; then
for arg in $@ ; do
set_profile ${arg}
if [[ $? -eq 0 ]] ; then
clean_one_target ${C4M_BUILD_TARGET}
fi
done
else
set_profile
clean_one_target ${C4M_BUILD_TARGET}
fi
fi
}
function libcon4m_set_profile {
if [[ $# -gt 1 ]]; then
libcon4m_dev_usage
fi
set_profile $@
if [[ $? -ne 0 ]]; then
exit -1
fi
log "Current profile is: " $(cat $C4M_MESON_STATE_FILE)
ensure_directory
}
function libcon4m_prebuild {
CONFIGURE=1
if [[ ${1} -eq 0 ]] ; then
shift
CONFIGURE=0
fi
if [[ $# -ne 0 ]]; then
case ${1} in
dev | debug | release | sanitize | test)
set_profile $@
shift
;;
*)
set_profile
export C4M_PASSTHROUGH_ARGS=$@
;;
esac
else
set_profile
fi
ensure_directory
ensure_project
if [[ CONFIGURE -ne 0 ]] ; then
configure_target
fi
}
function libcon4m_compile {
log "Compiling target: " $(color green ${C4M_BUILD_TARGET})
which musl-gcc > /dev/null
if [[ $? -eq 0 ]]; then
CC=musl-gcc meson compile -C ${C4M_BUILD_DIR}
else
meson compile -C ${C4M_BUILD_DIR}
fi
if [[ $? -ne 0 ]] ; then
log Compilation of target ${C4M_BUILD_TARGET} $(color RED "FAILED.")
exit -1
fi
if [[ ${OS} = "Darwin" ]] ; then
dsymutil $(cur_exe_loc)
fi
}
function libcon4m_run_tests {
log Running test exe for target: $(color yellow '[==' $C4M_BUILD_TARGET '==]') in directory: $(color YELLOW ${C4M_BUILD_DIR})
$(cur_exe_loc) ${C4M_PASSTHROUGH_ARGS}
EXIT=$?
log "Exit status: ${EXIT}"
exit ${EXIT}
}
function libcon4m_run {
libcon4m_prebuild 0 $@
libcon4m_compile
set -e
libcon4m_run_tests $@
CODE=$?
if [[ $CODE -eq 0 ]] ; then
log "CI/CD tests passed."
else
log "CI/CD Tests " $(color RED "FAILED.")
exit $CODE
fi
}
function debug_project {
libcon4m_prebuild $@
CUR_EXE=$(cur_exe_loc)
libcon4m_compile
DEBUGGER=$(which gdb)
DEBUGGER=${DEBUGGER:-$(which lldb)}
DEBUGGER=${DEBUGGER:-1}
if [[ ${DEBUGGER} -eq 1 ]] ; then
echo $(color RED ERROR: ) "no debugger found in your path."
exit -1
fi
log "Running debugger: ${DEBUGGER} for target:" $(color green '[==' $C4M_BUILD_TARGET '==]')
${DEBUGGER} ${CUR_EXE} ${C4M_PASSTHROUGH_ARGS}
}
ensure_env_setup
case ${1} in
clean) shift
libcon4m_clean $@
;;
build | compile) shift
libcon4m_prebuild $@
libcon4m_compile
;;
rebuild) shift
libcon4m_prebuild $@
libcon4m_compile --clean
;;
profile) shift
libcon4m_set_profile $@
;;
debug) shift
debug_project $@
;;
run) shift
libcon4m_run $@
;;
hash) shift
meson_hatrack hash $@
;;
*)
libcon4m_dev_usage
;;
esac