-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpp-build
executable file
·406 lines (334 loc) · 10.1 KB
/
cpp-build
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/bin/bash
# Save the script's name in order to report errors, warnings and messages under it
prog=$(basename "$0")
function log
{
if [ "$1" == ERROR ]
then
echo -e "$(tput setaf 3)$prog:~$(tput sgr0) $2"
elif [ "$1" == WARNING ]
then
echo -e "$(tput setaf 9)$prog:~$(tput sgr0) $2"
else
echo -e "$(tput setaf 6)$prog:~$(tput sgr0) $2"
fi
}
# The configuration file must be located in the current working directory
config="$(pwd)/.config.json"
# A python script used in order to read the configuration file
load_config=\
"import sys, json;
data = json.load(sys.stdin)
for field in data.keys():
if isinstance(data[field], list):
data[field] = \" \".join(data[field])
print(field, \"=\", '\"', data[field], '\"', sep='')"
declare -A meta
# Store the configuration file's contents into the associative array "meta"
# Check if all the expected fields have been mentioned and
# if the mentioned directories exist
if [ ! -f "$config" ]
then
log ERROR "Unable to locate \"$config\""
exit 1
else
while read line
do
if [[ $line =~ (.*)=\"(.*)\" ]]
then
key="${BASH_REMATCH[1]}"
val="${BASH_REMATCH[2]}"
meta["$key"]="$val"
fi
done <<< $(cat "$config" | python3 -c "$load_config")
for field in ""CC CCFLAGS LIBS PATH_INC PATH_SRC PATH_TEST PATH_BIN""
do
if [[ ! -v "meta[$field]" ]]
then
log ERROR "Field \"$field\" was not specified"
exit 1
fi
done
for field in ""PATH_INC PATH_SRC PATH_TEST""
do
path="${meta[$field]}"
if [ ! -d "$path" ]
then
log ERROR "No directory named \"$path\""
exit 1
fi
done
fi
# A function used in order to confirm whether or not to overwrite
# the file specified by "$1"
function confirm
{
if [ -e "$1" ]
then
read -p "$(log WARNING "Are you sure you want to overwrite \"$1\": ")" answer
if [[ "$answer" != [yY] ]] && [[ "$answer" != [yY][eE][sS] ]]
then
exit 1
fi
fi
}
# Recursively find all the dependencies of the file specified by "$1"
# For this to work properly the name of every header file must be
# of the format *.h or *.hpp
function grep_include_directives
{
local includes=$(grep -Eo '["<].*\.[hi]pp[">]' $1)
if [ -z "$includes" ]
then
return
fi
local dependencies=""
for include in ""$includes""
do
include=${include:1:${#include} - 2}
local entry="${visited[$include]}"
if [[ -n "$entry" ]]
then
continue
else
visited["$include"]=true
grep_include_directives "${meta[PATH_INC]}/$include"
fi
done
}
# Generate a makefile that contains:
# (1) An explicit rule for each .cpp file residing in the "$PATH_SRC" directory
# (2) A wildcard rule for every test unit residing in the "$PATH_TEST" directory
# (3) A "clean" rule in order to remove the "$PATH_BIN" directory and its contents
# (4) An "all" rule that results in the separate compilation of every .cpp file
# residing in the "$PATH_SRC" directory
function generate
{
echo
echo "CC = ${meta[CC]}"
echo "CCFLAGS = ${meta[CCFLAGS]}"
echo
echo "LIBS = ${meta[LIBS]}"
echo
echo "PATH_SRC = ${meta[PATH_SRC]}"
echo "PATH_INC = ${meta[PATH_INC]}"
echo "PATH_BIN = ${meta[PATH_BIN]}"
echo "PATH_TEST = ${meta[PATH_TEST]}"
echo
echo ".PHONY: all"
echo "all:"
echo -e "\tmkdir -p \$(PATH_BIN)"
echo -e "\t@echo"
echo -e "\t@echo \"*** Compiling object files ***\""
echo -e "\t@echo \"***\""
echo -e "\tmake \$(OBJS)"
echo -e "\t@echo \"***\""
echo
echo ".PHONY: clean"
echo "clean:"
echo -e "\t@echo"
echo -e "\t@echo \"*** Purging binaries ***\""
echo -e "\t@echo \"***\""
echo -e "\trm -rvf \$(PATH_BIN)"
echo -e "\t@echo \"***\"\n\n"
objs="OBJS = \$(addprefix \$(PATH_BIN)/, "
deps=""
rules=""
for file in ""$1""
do
declare -A visited
grep_include_directives "${meta[PATH_SRC]}/$file"; includes=${!visited[@]}
unset visited
file=${file%.cpp};
rule="\$(PATH_BIN)/$file.o:"
if [ -n "$includes" ]
then
deps_name=$(echo $file | tr [:lower:] [:upper:])_DEP
rule="$rule \$($deps_name)"
deps_list="\$(addprefix \$(PATH_INC)/, $includes) \$(PATH_SRC)/$file.cpp"
deps="$deps$deps_name = $deps_list\n\n"
else
deps_name=""
deps_list=""
fi
rule="$rule\n\t\$(CC) -I \$(PATH_INC) \$(DEFINED) \$(CCFLAGS) \$(PATH_SRC)/$file.cpp -c -o \$(PATH_BIN)/$file.o"
rules="$rules$rule\n\n"
objs="$objs $file.o"
done
objs="$objs)"
echo -e "$deps\n$rules\n$objs\n"
echo "\$(PATH_BIN)/%.exe: \$(PATH_TEST)/%.cpp \$(OBJS)"
echo -e "\t\$(CC) -I \$(PATH_INC) \$(DEFINED) \$(CCFLAGS) \$< \$(OBJS) \$(LIBS) -o \$@"
}
declare -A classes
# The macros must be of the format __.*__
# grep every global macro and extract its name
classes[-g]=$(grep -Evs '//' ${meta[PATH_INC]}/*.h*p ${meta[PATH_SRC]}/*.c*p | grep -E '__.*__' | cut -d : -f 2 | sed -nE 's/^.*\((__.*__)\).*$/\1/p')
# grep every unit specific macro and extract its name
classes[-u]=$(grep -Evs '//' ${meta[PATH_TEST]}/*.c*p | grep -E '__.*__' | cut -d : -f 2 | sed -nE 's/^.*\((__.*__)\).*$/\1/p')
declare -A shortcuts
# For every class of macros
for class in "${!classes[@]}"
do
# For each macro in the current class
for macro in ""${classes[$class]}""
do
if [[ -z "$macro" ]]
then
continue
fi
# Create a key corresponding to the macro at hand
key="-$(echo ${macro:2:1} | tr [:upper:] [:lower:])"
if [[ "$key" =~ (-[ugxr]) ]]
then
log ERROR "\"$macro\"'s shortcut shadows \"${BASH_REMATCH[1]}\" flag"
exit 1
fi
entry="${shortcuts[$key]}"
# If there is no entry matching the current key register it
# Otherwise
if [[ -n "$entry" ]]
then
# If they don't have different names
if [[ "$entry" =~ (-?)"$macro" ]]
then
# If the macro at hand is global
if [[ "$class" == -g ]]
then
# It overrides the existing entry
shortcuts["$key"]="$class $macro"
fi
# Otherwise move on to the next macro
continue
fi
# If they do have different names but same keys
# then report a macro collision that needs to be
# taken care of
log ERROR "Macro collision detected \"$macro\" \""$(echo "$entry" | cut -d ' ' -f 2)"\""
exit 1
else
shortcuts["$key"]="$class $macro"
fi
done
done
# Print helpful text
if [ "$1" == "--help" ]
then
echo "# Options:"
echo "# -u, --unit-define Define a macro in a test unit"
echo "# -g, --global-define Define a macro globally"
echo "# -x, --executable Compile the specified executable"
echo "# -r, --rebuild Recompile library / executable"
if [ ${#shortcuts[@]} -gt 0 ]
then
echo -e "\n# Shortcuts:"
for macro in "${!shortcuts[@]}"
do
printf "# %s, %s\n" "$macro" "${shortcuts[$macro]}"
done
fi
echo -e "\n# Usage:"
echo "# $prog -u [MACRO]"
echo "# $prog -g [MACRO]"
echo "# $prog -x [name]"
echo "# $prog -r"
echo -e "\n# Example: $prog -r -u __BENCHMARK__ -u __QUIET__ -g __CACHE_SIZE__=32768"
exit 0
fi
# If the "--makefile" flag was specified or there's no Makefile generate one
if [ "$1" == "--makefile" ] || [ ! -f $(pwd)/Makefile ]
then
files=$(ls "${meta[PATH_SRC]}");
if [ -n "$files" ]
then
confirm "Makefile"; generate "$files" > Makefile
else
log ERROR "Failed to generate a makefile due to directory \"${meta[PATH_SRC]}\" being empty"
exit 1
fi
exit 0
fi
# Preprocess the input in order to substitute any shortcuts with their true meaning
cmd="$*"
for key in ${!shortcuts[@]}
do
full="${shortcuts[$key]}"; cmd=${cmd/$key/$full}
done
set -- ""${cmd[@]}""
# Handle the different options mentioned by the program when run with the "--help" flag
while [ ! "$#" -eq 0 ]
do
case "$1" in
"-u" | "--unit-define")
shift
dexe="$dexe -D$1"
shift
;;
"-g" | "--global-define")
shift
dexe="$dexe -D$1"
dlib="$dlib -D$1"
shift
;;
"-x" | "--executable")
shift
fexe=$(echo -e "$1\n$fexe")
shift
;;
"-r" | "--rebuild")
rebuild=true
shift
;;
*)
log ERROR "Invalid syntax! \"$*\""
exit 1
;;
esac
done
# If any number of global macros have been specified or
# the "--rebuild" flag was specified and we were not refering to specific executables
# everything is recompiled
if ([ "$rebuild" ] && [ -z "$fexe" ]) || [ ! -z "$dlib" ]
then
make clean
fi
make "DEFINED=$dlib"
# If no executables have been specified compile every test unit
if [ -z "$fexe" ]
then
fexe=$(ls ${meta[PATH_TEST]})
fi
echo "-e" "\n*** Compiling exe files ***"
echo "***"
# When listing executables with the "--executable" flag the directory and the extension
# of the executable need not be specified
for name in ""$fexe""
do
if [ -z "$name" ]
then
continue
fi
if [[ "$name" =~ (\.?/?.+)/(.+) ]]
then
dir=${BASH_REMATCH[1]}
file=${BASH_REMATCH[2]}
if [ "$dir" == "${meta[PATH_BIN]}" ]
then
name="$file"
else
log WARNING "Directory mismatch! \"$dir\""
continue
fi
fi
name="${meta[PATH_BIN]}/${name//.*/}.exe"
# If the executable already exists but
# the "--rebuild" flag was specified or
# any test unit specific macros have been specified
# recompile it
if ([ "$rebuild" ] || [ ! -z "$dexe" ]) && [ -x "$name" ]
then
rm -f "$name"
fi
make "$name" "DEFINED=$dexe"
done
echo "***"