-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbuild.sh
executable file
·195 lines (160 loc) · 4.41 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
#!/usr/bin/env bash
# Global flags
global_flag_f_format=0
global_flag_b_build=0
global_flag_r_rebuild=0
global_flag_l_flash=0
global_flag_v_verbose=0
#################################################################################
#
# Print usage.
#
function print_help()
{
cat << EOF
Use like this:
./bbuild.sh <flags>
flags:
-f, --format [f]ormat all source files with clang-formatter
-b, --build [b]uild
-r, --rebuild [r]euild
-l, --flash f[l]ash binary to target
-v, --verbose [v]erbose
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
if find . -iname '*.h' | grep --invert-match './build' | grep --invert-match 'external'; then
find . -iname '*.h' | grep --invert-match './build' | grep --invert-match 'external' | xargs clang-format -i -style=file
fi
python -m autopep8 -i -a -a -a scripts/stream_sin_wave.py
}
################################################################################
#
# Build with west
#
function func_build()
{
print_banner "Building code"
west build --pristine
}
################################################################################
#
# Rebuild with make/cmake
#
function func_rebuild()
{
print_banner "Deleting build directory"
rm -rf build
func_build
}
################################################################################
#
# Flash the binary to the target
#
function func_flash()
{
print_banner "Flashing code"
west flash
}
################################################################################
#
# 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
;;
-l | --flash)
global_flag_l_flash=1
shift
;;
-v | --verbose)
global_flag_v_verbose=1
shift
;;
-h | --help)
print_help
exit
;;
esac
done
}
################################################################################
#
# Execute script logic based on parameters
#
function execute_logic()
{
if [[ global_flag_f_format -eq 1 ]]; then
func_format
fi
if [[ global_flag_b_build -eq 1 ]]; then
func_build
fi
if [[ global_flag_r_rebuild -eq 1 ]]; then
func_rebuild
fi
if [[ global_flag_l_flash -eq 1 ]]; then
func_flash
fi
}
################################################################################
#
# main function
#
function main()
{
# Gather params
gather_params "$@"
execute_logic
}
main "$@"