-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbuild.sh
executable file
·232 lines (194 loc) · 5.37 KB
/
bbuild.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
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
#!/usr/bin/env bash
# Global flags
global_flag_f_format=0
global_flag_b_build=0
global_flag_r_rebuild=0
global_flag_e_execute=0
global_flag_c_clean=0
global_flag_v_verbose=0
global_value_target=""
#################################################################################
#
# Print usage.
#
function print_help()
{
cat << EOF
Use like this:
./do.sh <flags> <target>
flags:
-f, --format [f]ormat all source files with clang-formatter
-b, --build [b]uild
-r, --rebuild [r]euild
-e, --execute [e]xecute
-c, --clean [c]lean
-v, --verbose [v]erbose
targets:
<target> is a positional argument. Either "app" of "test"
EOF
return 0
}
################################################################################
#
# Print a fancy banner to separate sections visually.
#
function print_banner()
{
if [[ global_flag_v_verbose -eq 1 ]]; then
printf "\n================================================================================\n"
printf "%s\n" "$*"
printf "================================================================================\n\n"
fi
}
################################################################################
#
# Print text a simple header to make it easier to spot.
#
function print_header()
{
if [[ global_flag_v_verbose -eq 1 ]]; then
printf "==-- %s --==\n" "$*"
fi
}
################################################################################
#
# Format with clang-format
#
function func_format()
{
print_banner "Formatting code"
# -iname pattern: Returns true if the file’s name matches the provided shell
# pattern. The matching here is case insensitive.
# xargs is a great command that reads streams of data from standard input,
# then generates and executes command lines; meaning it can take output of a
# command and passes it as argument of another command. If no command is specified,
# xargs executes echo by default. You many also instruct it to read data from a file
# instead of stdin.
find . -iname '*.c' | grep --invert-match './build' | grep --invert-match './external' | xargs clang-format -i -style=file
find . -iname '*.h' | grep --invert-match './build' | grep --invert-match './external' | xargs clang-format -i -style=file
}
################################################################################
#
# Build with make/cmake
#
function func_build()
{
print_banner "Building code"
cmake -S . -B build -D TARGET_GROUP=$1
cmake --build build --parallel `nproc`
}
################################################################################
#
# Rebuild with make/cmake
#
function func_rebuild()
{
print_banner "Deleting build directory"
rm -rf build
func_build $1
}
################################################################################
#
# Execute the binary
#
function func_execute()
{
print_banner "Executing code"
./build/main
}
################################################################################
#
# Clean workspace
#
function func_clean()
{
print_banner "Cleaning workspace"
rm -rf build
rm -rf *.bin
rm -rf *.hex
}
################################################################################
#
# Gather all params passed to the script
#
function gather_params()
{
while [[ $# -gt 0 ]]; do
case $1 in
-f | --format)
global_flag_f_format=1
shift
;;
-b | --build)
global_flag_b_build=1
shift
;;
-r | --rebuild)
global_flag_r_rebuild=1
shift
;;
-e | --execute)
global_flag_e_execute=1
shift
;;
-c | --clean)
global_flag_c_clean=1
shift
;;
-v | --verbose)
global_flag_v_verbose=1
shift
;;
-h | --help)
print_help
exit
;;
*)
global_value_target=$1
shift
;;
esac
done
# Allow only format or clean without specifying <target>
#if [ global_flag_f_format -eq 0 ] && [ global_flag_c_clean -eq 0 ]; then
if [ "$global_flag_f_format" -eq 0 ] && [ "$global_flag_c_clean" -eq 0 ]; then
if [[ -z "$global_value_target" ]]; then
echo "Please, define a <target>"
print_help
exit
fi
fi
}
################################################################################
#
# Execute script logic based on parameters
#
function execute_logic()
{
if [[ global_flag_c_clean -eq 1 ]]; then
func_clean
fi
if [[ global_flag_f_format -eq 1 ]]; then
func_format
fi
if [[ global_flag_b_build -eq 1 ]]; then
func_build "$global_value_target"
fi
if [[ global_flag_r_rebuild -eq 1 ]]; then
func_rebuild "$global_value_target"
fi
if [[ global_flag_e_execute -eq 1 ]]; then
func_execute
fi
}
################################################################################
#
# main function
#
function main()
{
# Gather params
gather_params "$@"
execute_logic "$global_value_target"
}
main "$@"