From 4dbe76b9ecba0dedd971e9e667a020e3ca6192e0 Mon Sep 17 00:00:00 2001 From: Szymon Gizler Date: Tue, 24 Sep 2024 17:38:10 +0200 Subject: [PATCH 1/3] Add Makefile for running linters and tests --- .ci-scripts/cpp-fmt-check | 7 ------- .ci.yml | 3 +-- .github/workflows/test.yml | 6 ++---- README.md | 19 +++++++++++++++++-- check.mk | 26 ++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 15 deletions(-) delete mode 100755 .ci-scripts/cpp-fmt-check create mode 100644 check.mk diff --git a/.ci-scripts/cpp-fmt-check b/.ci-scripts/cpp-fmt-check deleted file mode 100755 index a89c79e..0000000 --- a/.ci-scripts/cpp-fmt-check +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -EXIT_CODE=0 -for i in "$@"; do - diff "$i" <(clang-format-14 "$i") --label "original $i" --label "formatted $i" --color=always -u || EXIT_CODE=1 -done - -exit $EXIT_CODE diff --git a/.ci.yml b/.ci.yml index 3f60508..92869c7 100644 --- a/.ci.yml +++ b/.ci.yml @@ -11,5 +11,4 @@ test-and-lint: - apt-get -qqy update && apt-get -qqy --no-install-recommends install cmake build-essential git ca-certificates python3 clang-format-14 mold ninja-build - cmake -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo -G Ninja -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=mold" -DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=mold" - cmake --build build -j"$(nproc)" - - PATH="$PWD/build:$PWD/scripts:$PATH" && make -j"$(nproc)" -O -k -C tests/ --no-print-directory - - .ci-scripts/cpp-fmt-check source/*.cpp source/*.hpp + - PATH="$PWD/build:$PWD/scripts:$PATH" && make -j"$(nproc)" -O -k --no-print-directory -f check.mk diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e65bd92..d55fc42 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,9 +17,7 @@ jobs: run: | cmake -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo cmake --build build -j"$(nproc)" - - name: Run tests + - name: Run tests and linters run: | PATH="$PWD/build:$PWD/scripts:$PATH" - make -j"$(nproc)" -O -k -C tests/ --no-print-directory - - name: Check formatting - run: .ci-scripts/cpp-fmt-check source/*.cpp source/*.hpp + make -j"$(nproc)" -O -k --no-print-directory -f check.mk diff --git a/README.md b/README.md index bd50777..0db2464 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,21 @@ The script attempts to: This script works on a best-effort basis, and it is expected that the result will require some manual adjustments. -## Tests +## Testing and linting -In order to launch tests, go to the `tests/` directory and run `make`. +`make`, `clang-format`, `shellcheck` and `verilator` are prerequisites for testing and linting. + +To run all tests, linters and format-checkers, invoke: +``` +make -f check.mk +``` + +in project root. To auto-apply linter/formatter fixes, run: +``` +make -f check.mk autofix +``` + +Golden files used in tests can be regenerated using: +``` +GOLDEN=1 make -f check.mk test +``` diff --git a/check.mk b/check.mk new file mode 100644 index 0000000..28c03b2 --- /dev/null +++ b/check.mk @@ -0,0 +1,26 @@ +SHELL := /bin/bash + +.PHONY: test_and_lint_dont_abort_after_err +test_and_lint_dont_abort_after_err: + @$(MAKE) -f check.mk -k test_and_lint + +.PHONY: test_and_lint +test_and_lint: test lint + +.PHONY: autofmt +autofmt: + clang-format-14 -i source/*.cpp source/*.hpp + +.PHONY: test +test: + @$(MAKE) -C tests + +.PHONY: lint +lint: fmt_check + +.PHONY: fmt_check +fmt_check: + for i in source/*.cpp source/*.hpp; do \ + diff "$$i" <(clang-format-14 "$$i") --label "original $$i" --label "formatted $$i" --color=always -u; \ + done + @echo # blank line for consistency From 900c1c4202a4e54fe7f2bb5d3e48d27fc7679645 Mon Sep 17 00:00:00 2001 From: Szymon Gizler Date: Tue, 24 Sep 2024 19:22:56 +0200 Subject: [PATCH 2/3] Add shellcheck linter --- .ci.yml | 2 +- .github/workflows/test.yml | 2 +- check.mk | 19 ++++++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.ci.yml b/.ci.yml index 92869c7..7545807 100644 --- a/.ci.yml +++ b/.ci.yml @@ -8,7 +8,7 @@ test-and-lint: image: "verilator/verilator:v5.016" tags: ['ace-x86_64'] script: - - apt-get -qqy update && apt-get -qqy --no-install-recommends install cmake build-essential git ca-certificates python3 clang-format-14 mold ninja-build + - apt-get -qqy update && apt-get -qqy --no-install-recommends install cmake build-essential git ca-certificates python3 clang-format-14 mold ninja-build shellcheck - cmake -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo -G Ninja -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=mold" -DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=mold" - cmake --build build -j"$(nproc)" - PATH="$PWD/build:$PWD/scripts:$PATH" && make -j"$(nproc)" -O -k --no-print-directory -f check.mk diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d55fc42..0e79cf3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Install deps - run: apt-get -qqy update && apt-get -qqy --no-install-recommends install cmake build-essential git ca-certificates python3 clang-format-14 + run: apt-get -qqy update && apt-get -qqy --no-install-recommends install cmake build-essential git ca-certificates python3 clang-format-14 shellcheck - name: Build run: | cmake -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo diff --git a/check.mk b/check.mk index 28c03b2..6f468f7 100644 --- a/check.mk +++ b/check.mk @@ -7,6 +7,9 @@ test_and_lint_dont_abort_after_err: .PHONY: test_and_lint test_and_lint: test lint +.PHONY: autofix +autofix: autofmt shellcheck_autofix + .PHONY: autofmt autofmt: clang-format-14 -i source/*.cpp source/*.hpp @@ -16,7 +19,7 @@ test: @$(MAKE) -C tests .PHONY: lint -lint: fmt_check +lint: fmt_check shellcheck .PHONY: fmt_check fmt_check: @@ -24,3 +27,17 @@ fmt_check: diff "$$i" <(clang-format-14 "$$i") --label "original $$i" --label "formatted $$i" --color=always -u; \ done @echo # blank line for consistency + + +SCRIPTS=scripts/* tests/*.sh tests/run_test \ + examples/caliptra_vcd/sv-bugpoint-check.sh \ + examples/caliptra_verilation_err/sv-bugpoint-check.sh + +.PHONY: shellcheck +shellcheck: + shellcheck $(SCRIPTS) --color + +.PHONY: shellcheck +shellcheck_autofix: + shellcheck $(SCRIPTS) -f diff --color || exit 0 + shellcheck $(SCRIPTS) -f diff | git apply From 520fdbffea96499f794841857737b52bd19a1a06 Mon Sep 17 00:00:00 2001 From: Szymon Gizler Date: Tue, 24 Sep 2024 18:45:40 +0200 Subject: [PATCH 3/3] Fix shellcheck warnings --- examples/caliptra_vcd/sv-bugpoint-check.sh | 1 + tests/run_test | 13 +++++-------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/examples/caliptra_vcd/sv-bugpoint-check.sh b/examples/caliptra_vcd/sv-bugpoint-check.sh index 6c36b60..9f94a24 100755 --- a/examples/caliptra_vcd/sv-bugpoint-check.sh +++ b/examples/caliptra_vcd/sv-bugpoint-check.sh @@ -28,6 +28,7 @@ mkfifo sim.vcd trap cleanup EXIT INT HUP TERM timeout 3 ./obj_dir/Vcaliptra_top_tb & +# shellcheck disable=SC2016 # extract all changes to timer1_timeout_period[0] and timer1_timeout_period[1] before timestamp "40" timeout 3 awk ' /var wire .* timer1_timeout_period\[/ { ID_TO_NAME[$4]=$5 } diff --git a/tests/run_test b/tests/run_test index af2f6fd..8380c54 100755 --- a/tests/run_test +++ b/tests/run_test @@ -3,22 +3,19 @@ printf "TEST: %s\n" "$1" actual_file=out/"$1"/sv-bugpoint-minimized.sv -[ -e $actual_file ] && rm $actual_file # to not get fake PASS if bugpoint did not overwrite old result +[ -e "$actual_file" ] && rm "$actual_file" # to not get fake PASS if bugpoint did not overwrite old result golden_file=golden/"$1"/sv-bugpoint-minimized.sv -bugpoint_msg=$(sv-bugpoint out/"$1" "$2" "$3" --force 2>&1) -if [ $? -ne 0 ]; then - printf "%s\n" "$bugpoint_msg" >&2; exit 1 -fi +bugpoint_msg=$(sv-bugpoint out/"$1" "$2" "$3" --force 2>&1) || (printf "%s\n" "$bugpoint_msg" >&2; exit 1) -diff $golden_file $actual_file --color=always >&2 +diff "$golden_file" "$actual_file" --color=always >&2 EXIT_CODE=$? [ "$EXIT_CODE" -eq 0 ] && printf "PASSED\n\n" || printf "FAILED\n\n" if [ -n "$GOLDEN" ]; then - mkdir -p "$(dirname $golden_file)" - cp $actual_file $golden_file + mkdir -p "$(dirname "$golden_file")" + cp "$actual_file" "$golden_file" fi exit $EXIT_CODE