Repository navigation
Expand file tree
/
Copy pathsbs
More file actions
executable file
·885 lines (742 loc) · 28.9 KB
/
Copy pathsbs
File metadata and controls
executable file
·885 lines (742 loc) · 28.9 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
#!/usr/bin/env bash
# Copyright (c) 2024 Skyward Experimental Rocketry
# Authors: Davide Basso, Niccolò Betto
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
################################################################################
############################## Constants ##############################
################################################################################
# Terminal colors
TTY_RESET="\033[0m"
TTY_BOLD="\033[1m"
TTY_FOUND="\033[35m"
TTY_SUCCESS="\033[32m"
TTY_ERROR="\033[31m"
TTY_STEP="\033[94m"
TTY_LOGO_1="\033[38;5;200m"
TTY_LOGO_2="\033[38;5;164m"
TTY_LOGO_3="\033[38;5;93m"
TTY_LOGO_4="\033[38;5;57m"
TTY_LOGO_5="\033[38;5;27m"
# Error message
ERR=""$TTY_ERROR""$TTY_BOLD"ERROR"$TTY_RESET""
# Error codes
EPERM=1 # Operation not permitted
ENOENT=2 # No such file or directory
ENOEXEC=8 # Exec format error
ENODEV=19 # No such device
EINVAL=22 # Invalid argument
ENOPKG=65 # Package not installed
ENOTSUP=95 # Operation not supported
# Filenames/Dirnames
CMAKE_FILENAME="CMakeCache.txt"
CTEST_FILENAME="CTestTestfile.cmake"
DEBUG_FILENAME=".sbs_debug"
VERBOSE_FILENAME=".sbs_verbose"
BUILD_DEFAULT_DIRNAME="build"
BUILD_HOST_DIRNAME="build-host"
# Logical cores count, cross platform
N_PROC=$(
nproc 2>/dev/null || # Linux
sysctl -n hw.logicalcpu 2>/dev/null || # macOS
getconf _NPROCESSORS_ONLN 2>/dev/null || # POSIX
echo 1 # Fallback to 1 core
)
OS=$(uname -s)
################################################################################
############################## Global States ##############################
################################################################################
sbs_base=
source_dir=
build_default_dir=
build_host_dir=
toolchain_file=
found_cmake=false
found_miosixgpp=false
found_ccache=false
found_ninja=false
found_python=false
found_cppcheck=false
found_clangtidy=false
found_clangformat=false
found_stflash=false
found_stlink=false
config_build_dir=
config_board=
config_host=false
config_debug=false
config_verbose=false
config_reset=false
config_jobs=$N_PROC
# Set to true if a target is required for the build command (e.g. flash, run)
target_required=false
################################################################################
############################## Banner ##############################
################################################################################
banner="\
$TTY_LOGO_1 _____ ____ _____
$TTY_LOGO_2 ___________ / ___// __ ) ___/ ________________________________
$TTY_LOGO_3 __________ \__ \/ __ \__ \ _________________________________
$TTY_LOGO_4 _________ ___/ / /_/ /__/ / ____________________________v4.0__
$TTY_LOGO_5 /____/_____/____/
$TTY_RESET"
print_configuration() {
step "Dependencies"
yf=""$TTY_SUCCESS""$TTY_BOLD"Yes"$TTY_RESET""
nf=""$TTY_ERROR""$TTY_BOLD"No"$TTY_RESET""
printf "cmake: "; [ "$found_cmake" = true ] && printf "$yf\n" || printf "$nf\n"
printf "arm-miosix-eabi-g++: "; [ "$found_miosixgpp" = true ] && printf "$yf\n" || printf "$nf\n"
printf "ccache: "; [ "$found_ccache" = true ] && printf "$yf\n" || printf "$nf\n"
printf "ninja: "; [ "$found_ninja" = true ] && printf "$yf\n" || printf "$nf\n"
printf "python: "; [ "$found_python" = true ] && printf "$yf\n" || printf "$nf\n"
printf "cppcheck: "; [ "$found_cppcheck" = true ] && printf "$yf\n" || printf "$nf\n"
printf "clang-tidy: "; [ "$found_clangtidy" = true ] && printf "$yf\n" || printf "$nf\n"
printf "clang-format: "; [ "$found_clangformat" = true ] && printf "$yf\n" || printf "$nf\n"
printf "flasher: ";
if [ "$found_stflash" = true ]; then
printf ""$TTY_FOUND""$TTY_BOLD"st-flash"$TTY_RESET"\n"
elif [ "$found_stlink" = true ]; then
printf ""$TTY_FOUND""$TTY_BOLD"st-link"$TTY_RESET"\n"
else
printf "$nf\n"
fi
}
# Print a step message
step() {
printf "\n"$TTY_STEP""$TTY_BOLD"$1"$TTY_RESET"\n"
# Print - for every letter in the pre string
for (( i=0; i<${#1}; i++ )); do printf -- "-"; done;
printf "\n";
}
# Print an error message
error() {
printf "\n"
printf "$ERR: $1\n" >&2
}
# Print an error message without top padding
error_nopad() {
printf "$ERR: $1\n" >&2
}
################################################################################
############################## Internals ##############################
################################################################################
# Find all the dependencies
find_deps() {
command -v cmake > /dev/null 2>&1 && found_cmake=true
command -v arm-miosix-eabi-g++ > /dev/null 2>&1 && found_miosixgpp=true
command -v ccache > /dev/null 2>&1 && found_ccache=true
command -v ninja > /dev/null 2>&1 && found_ninja=true
command -v python > /dev/null 2>&1 && found_python=true
command -v cppcheck > /dev/null 2>&1 && found_cppcheck=true
command -v clang-tidy > /dev/null 2>&1 && found_clangtidy=true
command -v clang-format > /dev/null 2>&1 && found_clangformat=true
command -v st-flash > /dev/null 2>&1 && found_stflash=true
command -v ST-LINK_CLI.exe > /dev/null 2>&1 && found_stlink=true
}
# Initialize all the directories
init_dirs() {
sbs_base="$(cd -- "$(dirname "$0")" > /dev/null 2>&1 && pwd -P)"
source_dir="$PWD"
build_default_dir="$source_dir/$BUILD_DEFAULT_DIRNAME"
build_host_dir="$source_dir/$BUILD_HOST_DIRNAME"
toolchain_file="$sbs_base/libs/miosix-kernel/miosix/cmake/Toolchains/gcc.cmake"
# Miosix 3 board list (stock boards) and the Boardcore registry files
kernel_arch_cmake="$sbs_base/libs/miosix-kernel/miosix/arch/CMakeLists.txt"
targets_registry_file="$source_dir/CMakeLists.txt"
boards_registry_cmake="$sbs_base/cmake/boards.cmake"
}
# Print the stock boards shipped by the Miosix 3 kernel
kernel_boards() {
awk '
/^set\(MIOSIX_BOARDS/ { in_list=1; next }
in_list && /^\)/ { in_list=0; next }
in_list {
gsub(/^[ \t]+|[ \t]+$/, "")
if ($0 != "") print
}
' "$kernel_arch_cmake"
}
# Print the boards referenced by the Boardcore target registry (custom boards)
boards_from_registry() {
awk '
/^sbs_declare_target\(/ {
line=$0
sub(/^sbs_declare_target\(/, "", line)
split(line, a, /[ )]/)
print a[2]
}
' "$targets_registry_file" | sort -u
}
# Print the custom Boardcore boards registered in cmake/boards.cmake
boardcore_registry_boards() {
awk '
/^set\(BOARDCORE_BOARDS/ { in_list=1; next }
in_list && /^\)/ { in_list=0; next }
in_list {
gsub(/^[ \t]+|[ \t]+$/, "")
if ($0 != "") print
}
' "$boards_registry_cmake"
}
# Return the board a target is defined for (empty if the target is unknown)
target_board() {
local target="$1"
awk -v t="$target" '
/^sbs_declare_target\(/ {
line=$0
sub(/^sbs_declare_target\(/, "", line)
split(line, a, /[ )]/)
if (a[1] == t) { print a[2]; found=1; exit }
}
END { exit found ? 0 : 1 }
' "$targets_registry_file"
}
# Check if a board can be configured with the current Miosix 3 kernel
board_available() {
local board="$1"
kernel_boards | grep -qx "$board"
}
# Boards from the target registry that the current kernel can actually build
available_boards() {
local board
for board in $(boards_from_registry); do
board_available "$board" && echo "$board"
done
}
# Linker script override for boards whose Miosix 3 kernel file ships a broken
# default (the default name is not present in MIOSIX_LINKER_SCRIPT_LIST, so
# configure fails unless the script is selected explicitly). Empty for boards
# whose kernel default is valid.
board_linker_script() {
case "$1" in
stm32f767zi_skyward_compute_unit) echo "unikernel-xram-sgm.ld" ;;
esac
}
# workaround: disable tests in excluded subdirectories
# see: https://gitlab.kitware.com/cmake/cmake/-/issues/20212
cmake_disable_excluded_tests() {
build_dir="$build_host_dir"
[ ! -f "$build_dir/$CTEST_FILENAME" ] || sed -i.bak 's/^subdirs/# subdirs/' "$build_dir/$CTEST_FILENAME"
}
check_build_env() {
[ "$found_cmake" = true ] || { error "CMake must be installed"; return $ENOPKG; }
if [ "$config_host" = false ]; then
[ "$found_miosixgpp" = true ] || { error "arm-miosix-eabi-g++ must be installed"; return $ENOPKG; }
fi
}
# Check if the project is configured
# Ensure the following variables are properly set before calling this:
# config_build_dir, config_debug, config_verbose, config_host, config_board
check_configured() {
to_reconfigure=false
# Ensure config_build_dir is set. If you see this, fix the subcommand
# by populating the variable before calling this function
if [ -z "$config_build_dir" ]; then
error "Build directory not set, this is an SBS bug"
return $ENOENT
fi
# Miosix 3 configures one board per build directory
if [ "$config_host" = false ] && [ -z "$config_board" ]; then
error "Board not set, this is an SBS bug"
return $ENOENT
fi
if [ ! -d "$config_build_dir" ]; then
to_reconfigure=true
elif [ ! -f "$config_build_dir/$CMAKE_FILENAME" ]; then
rm -rf "$config_build_dir"
to_reconfigure=true
else
[ -f "$config_build_dir/$DEBUG_FILENAME" ] && found_debug=true || found_debug=false
[ -f "$config_build_dir/$VERBOSE_FILENAME" ] && found_verbose=true || found_verbose=false
if [ "$config_debug" != "$found_debug" ] \
|| [ "$config_verbose" != "$found_verbose" ]; then
to_reconfigure=true
fi
fi
if [ "$to_reconfigure" = true ]; then
step "Configure"
printf "Reconfiguring project with the following options:\n"
printf " - Debug: %s\n" "$config_debug"
printf " - Verbose: %s\n" "$config_verbose"
printf " - Host: %s\n" "$config_host"
printf " - Board: %s\n" "${config_board:-<none>}"
linker_script="$(board_linker_script "$config_board")"
[ -n "$linker_script" ] && printf " - Linker: %s\n" "$linker_script"
[ -f "$toolchain_file" ] || { error "CMake toolchain file for Miosix was not found"; return $ENOPKG; }
declare -a defs=(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)
defs+=(-DCMAKE_C_FLAGS=-fdiagnostics-color=always -DCMAKE_CXX_FLAGS=-fdiagnostics-color=always)
[ "$config_host" = false ] && defs+=(-DCMAKE_TOOLCHAIN_FILE="$toolchain_file" -DBUILD_TESTING=OFF -DMIOSIX_BOARD="$config_board")
[ -n "$linker_script" ] && defs+=(-DMIOSIX_LINKER_SCRIPT="$linker_script")
[ "$found_ccache" = true ] && defs+=(-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache)
[ "$config_debug" = true ] && defs+=(-DCMAKE_BUILD_TYPE=Debug) || defs+=(-DCMAKE_BUILD_TYPE=Release)
[ "$config_verbose" = true ] && defs+=(-DCMAKE_VERBOSE_MAKEFILE=ON)
declare gen
[ "$found_ninja" = true ] && gen=-GNinja || gen=-G"Unix Makefiles"
cmake -B "$config_build_dir" "${defs[@]}" "$gen" "$source_dir" || return
{ [ "$config_debug" = true ] && touch "$config_build_dir/$DEBUG_FILENAME"; } || rm -f "$config_build_dir/$DEBUG_FILENAME"
{ [ "$config_verbose" = true ] && touch "$config_build_dir/$VERBOSE_FILENAME"; } || rm -f "$config_build_dir/$VERBOSE_FILENAME"
fi
}
lint_copyright() {
step "Lint copyright"
"$sbs_base/scripts/linter.py" --copyright "$source_dir/src"
}
lint_find() {
step "Lint find"
"$sbs_base/scripts/linter.py" --find "$source_dir/src"
}
lint_clangtidy() {
build_dir="$1"
step "Lint clang-tidy"
config_debug=false
config_verbose=false
defs=(--extra-arg=-D_MIOSIX=1 --extra-arg=-D_MIOSIX_GCC_PATCH_MINOR=1 \
--extra-arg=-D_MIOSIX_GCC_PATCH_MAJOR=3 --extra-arg=-D__LINT__)
IFS=$'\n' read -rd '' -a incs < \
<(arm-miosix-eabi-g++ -E -Wp,-v -xc++ /dev/null 2>&1 \
| sed -n "s/^ /--extra-arg=-isystem/p")
declare opts=()
[ "$to_edit" = true ] && opts+=(--fix-notes --fix-errors)
find "$source_dir/src" \
-type f \( -iname "*.cpp" -o -iname "*.h" -o -iname "*.c" \) \
-exec clang-tidy --header-filter=".*" -p="$build_dir" "${defs[@]}" \
"${incs[@]}" "${opts[@]}" {} \;
}
lint_cppcheck() {
step "Lint cppcheck"
echo "Running cppcheck..."
cppcheck --language=c++ --std=c++20 --enable=all --inline-suppr \
--suppress=unmatchedSuppression --suppress=unusedFunction \
--suppress=missingInclude --error-exitcode=1 -q \
-I$sbs_base/libs/socrate/include \
"$source_dir/src"
}
lint_clangformat() {
step "Lint clang-format"
echo "Running clang-format..."
opts=(--style=file --Werror --dry-run)
# Find all the source files
declare files=$(find "$source_dir/src" \
-type f \( -iname "*.cpp" -o -iname "*.h" -o -iname "*.c" \))
# Count files and spread them evenly across cores: files / nproc + 1
declare files_per_proc=$(
echo "$files" | wc -l | \
xargs -I {} bash -c "expr \( {} / $N_PROC \) + 1"
)
echo "$files" | xargs -n $files_per_proc -P 0 clang-format "${opts[@]}"
}
################################################################################
############################### Subcommands #################################
################################################################################
build() {
# Parse build arguments
target=
for arg in "$@"; do
case $arg in
-d|--debug) config_debug=true ;;
-v|--verbose) config_verbose=true ;;
-j|--jobs=*) config_jobs="${arg#*=}" ;;
-*) error "Unknown argument: $arg"; return $EINVAL ;;
*) target="$arg" ;;
esac
done
config_host=false
declare -a verbose_build_opts
[ "$config_verbose" = true ] && verbose_build_opts+=(--verbose)
# Use the default "all" target if none was specified
if [ -z "$target" ]; then
if [ "$target_required" = true ]; then
error "No target specified"
return $ENOENT
else
target="all"
fi
fi
check_build_env || return
if [ "$target" = "all" ]; then
# Miosix 3 builds exactly one board per configure, so "all" builds
# every board referenced by the target registry that the kernel ships
any_board_built=false
for board in $(available_boards); do
any_board_built=true
config_board="$board"
config_build_dir="$build_default_dir/$config_board"
check_configured || return
step "Build"
cmake --build "$config_build_dir" --target all "${verbose_build_opts[@]}" -- -j"$config_jobs" || return
done
# Report registry boards that the current kernel cannot build yet
for board in $(boards_from_registry); do
if ! board_available "$board"; then
echo "Skipping board '$board': not available in the Miosix 3 kernel (BSP not ported yet)"
fi
done
[ "$any_board_built" = true ] || { error "No buildable boards found"; return $ENOTSUP; }
else
config_board="$(target_board "$target")"
if [ -z "$config_board" ]; then
error "Unknown target '$target'"
return $ENOENT
fi
if [ "$config_board" = "host" ]; then
error "Target '$target' is a host-only target (e.g. the Catch2 test suite). Use 'sbs test' to build and run it"
return $ENOTSUP
fi
if ! board_available "$config_board"; then
error "Board '$config_board' required by '$target' is not available in the Miosix 3 kernel (BSP not ported yet)"
return $ENOTSUP
fi
config_build_dir="$build_default_dir/$config_board"
check_configured || return
step "Build"
# Also build the _bin artifact target: Miosix produces .bin/.hex from
# dedicated ${TARGET}_bin/${TARGET}_hex targets, which flash needs
cmake --build "$config_build_dir" --target "$target" --target "${target}_bin" "${verbose_build_opts[@]}" -- -j"$config_jobs"
fi
}
flash() {
# Parse flash arguments
unhandled_args=()
for arg in "$@"; do
case $arg in
-r|--reset) config_reset=true ;;
*) unhandled_args+=("$arg") ;;
esac
done
set -- "${unhandled_args[@]}"
# Require a target for flashing
target_required=true
build "$@" || return
# Check if the target is flashable
[ -f "$config_build_dir/$target.bin" ] || { error "Target '$target' is not flashable"; return $ENOEXEC; }
# Flash the target
step "Flash"
declare -a flash_opts
[ "$config_reset" = true ] && flash_opts+=("--connect-under-reset")
if [ "$found_stflash" = true ]; then
st-flash --reset "${flash_opts[@]}" write "$config_build_dir/$target.bin" 0x8000000
elif [ "$found_stlink" = true ]; then
ST-LINK_CLI.exe -P "$config_build_dir/$target.bin" 0x8000000 -V -Rst
else
error "No flashing software found"
return $ENOPKG
fi
}
run() {
# Parse run arguments
unhandled_args=()
while [ $# -gt 0 ]; do
case "$1" in
-D|--device) shift; [ -n "$1" ] && device="$1" && shift ;;
-D=*|--device=*) device="${1#*=}"; shift ;;
-B|--baudrate) shift; [ -n "$1" ] && baudrate="$1" && shift ;;
-B=*|--baudrate=*) baudrate="${1#*=}"; shift ;;
*) unhandled_args+=("$1"); shift ;;
esac
done
set -- "${unhandled_args[@]}"
# Try some heuristics to select a serial device if not specified
if [ -z "$device" ]; then
device_patterns=(
"/dev/ttyACM*"
"/dev/ttyUSB*"
"/dev/cu.usb*"
"/dev/tty.usb*"
)
# Enable nullglob to ensure globs that don't match expand to nothing
shopt -s nullglob
for pattern in "${device_patterns[@]}"; do
# Expand the current pattern into an array
matching_devices=( $pattern )
# Take the first matching device if any were found
if (( ${#matching_devices[@]} > 0 )); then
device="${matching_devices[0]}"
break
fi
done
# Disable nullglob
shopt -u nullglob
if [ -z "$device" ]; then
error "No serial device specified and none could be autodetected"
return $ENODEV
fi
fi
# Set Skyward's default baudrate, if none was specified
if [ -z "$baudrate" ]; then
baudrate="115200"
else
# Ensure baudrate is a valid number
if ! [[ "$baudrate" =~ ^[0-9]+$ ]]; then
error "Invalid baudrate: $baudrate"
return $EINVAL
fi
fi
# Set port settings early to reduce connection delay after flashing
# TTY devices use \r\n for newlines by default
# Disable newlines on \r with `-icrnl` to avoid double newlines
if [ "$OS" = "Linux" ]; then
stty --file "$device" "$baudrate" -icrnl || return
elif [ "$OS" = "Darwin" ]; then
# macOS restores port settings to default when the port is closed
# so we need to open it, set it up and then read from it
exec 3<>$device
stty -f "$device" "$baudrate" -icrnl || { exec 3<&-; return 1; }
else
error "Run command not supported on this OS: $OS"
return $ENOTSUP
fi
# Flash the target
flash "$@" || return
step "Run - $device @ $baudrate"
# OS specific behavior
if [ "$OS" = "Linux" ]; then
cat "$device"
elif [ "$OS" = "Darwin" ]; then
cat <&3
# Close the fd
exec 3<&-
fi
}
clean() {
# Parse build arguments
for arg in "$@"; do
case $arg in
*) error "Unknown argument: $arg"; return $EINVAL ;;
esac
done
step "Clean"
echo "Cleaning build directories..."
rm -rf "$build_default_dir"
rm -rf "$build_host_dir"
echo "Done."
}
test() {
# Parse test arguments
for arg in "$@"; do
case $arg in
-d|--debug) config_debug=true ;;
-v|--verbose) config_verbose=true ;;
-j|--jobs=*) config_jobs="${arg#*=}" ;;
-*) error "Unknown argument: $arg"; return $EINVAL ;;
*) target="$arg" ;;
esac
done
config_host=true
config_build_dir="$build_host_dir"
declare -a verbose_build_opts
[ "$config_verbose" = true ] && verbose_build_opts+=(--verbose)
# If target wasn't previously set, use the default one
if [ -z "$target" ]; then
target="catch-tests-boardcore"
fi
# Build the target
check_build_env || return
check_configured || return
step "Build (Host)"
cmake --build "$config_build_dir" --target "$target" "${verbose_build_opts[@]}" -- -j"$config_jobs" || return
# Run the tests
step "Test"
cmake_disable_excluded_tests
( cd "$config_build_dir" || return; ctest )
}
list() {
list_type="$1"
if [ -z "$list_type" ]; then
error_nopad "No list type specified"
return $EINVAL
fi
if [ "$list_type" = "targets" ]; then
# Read the declarative target registry in CMakeLists.txt
awk '
/^sbs_declare_target\(/ {
line=$0
sub(/^sbs_declare_target\(/, "", line)
split(line, a, /[ )]/)
print a[1]
}
' "$targets_registry_file"
elif [ "$list_type" = "boards" ]; then
# Stock boards from the Miosix 3 kernel + Boardcore custom registry
{
kernel_boards
boardcore_registry_boards
} | sort -u
else
error_nopad "Unknown list type: $list_type"
return $EINVAL
fi
}
lint() {
# Parse lint arguments
for arg in "$@"; do
case $arg in
*) error "Unknown argument: $arg"; return $EINVAL ;;
esac
done
declare ret=0
if [ "$found_python" = true ]; then
lint_copyright
[ $? -ne 0 ] && ret=-1
lint_find
[ $? -ne 0 ] && ret=-1
else
echo "Python not found, skipping copyright and find..."
fi
# Disable clang-tidy for now, as too many false positives
# if [ "$found_clangtidy" = true ]; then
# lint_clangtidy "$build_default_dir"
# else
# echo "Clang-tidy not found, skipping clang-tidy..."
# fi
if [ "$found_cppcheck" = true ]; then
lint_cppcheck
[ $? -ne 0 ] && ret=-1
else
echo "Cppcheck not found, skipping cppcheck..."
fi
if [ "$found_clangformat" = true ]; then
lint_clangformat
[ $? -ne 0 ] && ret=-1
else
echo "Clang-format not found, skipping clang-format..."
fi
return $ret
}
format() {
# Parse format arguments
for arg in "$@"; do
case $arg in
*) error "Unknown argument: $arg"; return $EINVAL ;;
esac
done
if [ "$found_clangformat" = false ]; then
error "clang-format must be installed"
return $ENOPKG
fi
step "Format"
echo "Running clang-format..."
opts=(--style=file --Werror -i)
# Find all the source files
declare files=$(find "$source_dir/src" \
-type f \( -iname "*.cpp" -o -iname "*.h" -o -iname "*.c" \))
# Count files and spread them evenly across cores: files / nproc + 1
declare files_per_proc=$(
echo "$files" | wc -l | \
xargs -I {} bash -c "expr \( {} / $N_PROC \) + 1"
)
echo "$files" | xargs -n $files_per_proc -P 0 clang-format "${opts[@]}"
}
help() {
echo "Usage: sbs (command) <args> [options] "
echo ""
echo "Commands:"
echo " build - Build the specified target. If none is specified, all targets are built"
echo " -> <target=all> [-d | --debug] [-v | --verbose] [-j | --jobs=<jobs>]"
echo " flash - Build and flash the specified target"
echo " -> <target> [-d | --debug] [-v | --verbose] [-r | --reset] [-j | --jobs=<jobs>]"
echo " run - Build and flash the specified target and connect to serial device"
echo " -> <target> [-d | --debug] [-v | --verbose] [-r | --reset] [-j | --jobs=<jobs>]"
echo " [-D | --device=<device>] [-B | --baudrate=<baudrate>]"
echo " test - Run the specified test. If none is specified, catch tests are run"
echo " -> <target> [-d | --debug] [-v | --verbose] [-j | --jobs=<jobs>]"
echo " list - List the available targets or boards"
echo " -> <targets | boards>"
echo " clean - Clean the build directory"
echo " lint - Lint the source code"
echo " format - Format the source code"
echo " install - Install autocomplete"
echo " uninstall - Uninstall autocomplete"
echo ""
echo "Options:"
echo " -d, --debug: Build in debug mode"
echo " -v, --verbose: Print verbose output (full compiler/linker commands) during build"
echo " -r, --reset: Force connect under reset when flashing"
echo " -j, --jobs: Number of jobs to run in parallel"
echo " -D, --device: Serial device to connect to (tries to autodetect if not specified)"
echo " -B, --baudrate: Baudrate for the serial device (default: 115200)"
}
################################################################################
############################## Python Commands ##############################
################################################################################
install() {
# Parse install arguments
for arg in "$@"; do
case $arg in
*) error "Unknown argument: $arg"; return $EINVAL ;;
esac
done
step "Install autocomplete"
if [ "$found_python" = false ]; then
error "Python is required to install autocomplete"
return $ENOPKG
fi
echo "Retrieving targets..."
targets=$(list "targets")
declare ret=$?
[ $ret -ne 0 ] && printf "$targets\n" && return $ret
# Split the targets on newlines
IFS=$'\n' read -rd '' -a targets <<< "$targets"
echo "Found ${#targets[@]} targets"
echo "Installing completion files..."
sudo python "$sbs_base/scripts/autocomplete.py" "--install" "${targets[@]}"
}
uninstall() {
# Parse uninstall arguments
for arg in "$@"; do
case $arg in
*) error "Unknown argument: $arg"; return $EINVAL ;;
esac
done
step "Uninstall autocomplete"
if [ "$found_python" = false ]; then
error "Python is required to uninstall autocomplete"
return $ENOPKG
fi
echo "Uninstalling completion files..."
sudo python "$sbs_base/scripts/autocomplete.py" "--uninstall"
}
################################################################################
############################## Main ##############################
################################################################################
# Exit immediately if no argument was provided
if [ "$#" -eq 0 ]; then
help
exit $EINVAL
fi
welcome() {
printf "$banner"
print_configuration
}
# Populate global variables for all commands
find_deps
init_dirs
case $1 in
build) welcome; shift; build "$@"; exit $? ;;
clean) welcome; shift; clean "$@"; exit $? ;;
flash) welcome; shift; flash "$@"; exit $? ;;
run) welcome; shift; run "$@"; exit $? ;;
test) welcome; shift; test "$@"; exit $? ;;
lint) welcome; shift; lint "$@"; exit $? ;;
format) welcome; shift; format "$@"; exit $? ;;
install) welcome; shift; install "$@"; exit $? ;;
uninstall) welcome; shift; uninstall "$@"; exit $? ;;
list) shift; list "$@"; exit $? ;;
help) help; exit 0 ;;
*) error_nopad "'$1' is not a SBS command. See 'sbs help'." ; exit $EINVAL ;;
esac