Skip to content

Commit ab6a674

Browse files
authored
Merge pull request #445 from tropicsquare/ETR01SDK-551-Add-compilation-script-for-CodeChecker
ETR01SDK-551: Add compilation script for CodeChecker
2 parents f9987b7 + a9368e8 commit ab6a674

File tree

8 files changed

+140
-56
lines changed

8 files changed

+140
-56
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Using the CodeChecker
2+
The CodeChecker is a tool for performing static code analysis and generating reports.
3+
We provide scripts and custom configuration for running the CodeChecker.
4+
5+
We regularly run CodeChecker to check for any potential bugs or security issues. It is also
6+
recommended to run the CodeChecker yourself on any code you plan to contribute, as it can discover
7+
issues that other tools (compiler, ASAN, ...) can miss.
8+
9+
Note that the CodeChecker is supported on Linux and macOS only.
10+
11+
## Generating Reports
12+
You need to install the following dependencies:
13+
14+
- CodeChecker
15+
- Check out the [official repository](https://github.com/Ericsson/codechecker) for guidance.
16+
- Checkers for CodeChecker:
17+
- clang-tidy
18+
- clangsa
19+
- jq (used by our script for merging JSON reports)
20+
21+
We generate reports from multiple projects (examples and tests) to cover as much CALs and HALs
22+
as possible. The reports are then merged and exported to HTML.
23+
24+
To generate HTML report, you can use our convenience script. Reports will be
25+
generated to `.codechecker/reports_html` in the Libtropic repository.
26+
27+
!!! example "Generating HTML report"
28+
=== ":fontawesome-brands-linux: Linux"
29+
```bash { .copy }
30+
# Run from root directory of the Libtropic repository.
31+
scripts/codechecker/run_checks.sh
32+
```
33+
34+
=== ":fontawesome-brands-apple: macOS"
35+
TBA
36+
37+
??? note "Note: Running from a different directory"
38+
The script also supports running from a different directory, but you have to pass
39+
a path to the Libtropic repository as a first argument:
40+
41+
!!! example "Generating HTML report from any directory"
42+
=== ":fontawesome-brands-linux: Linux"
43+
```bash { .copy }
44+
scripts/codechecker/run_checks.sh <path_to_repo>
45+
```
46+
47+
=== ":fontawesome-brands-apple: macOS"
48+
TBA
49+
50+
If the script executes without any errors, exports will be ready and you can open
51+
`.codechecker/reports_html/index.html` in your favourite web browser.
52+
53+
## Remarks
54+
The current CodeChecker configuration is in YAML format, as it is more human-readable than JSON and also supports comments.
55+
56+
The configuration file enables some strict checkers, which may produce a lot of warnings. It is recommended to run the analysis using the full configuration at least once. After that, you can manually disable any checkers you find unnecessary.

docs/for_contributors/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
- [Contributing Guide](contributing_guide.md)
33
- [Building the Documentation](building_documentation.md)
44
- [Tests](tests/index.md)
5+
- [Using the CodeChecker](codechecker.md)
56
- [Adding a New Host Platform](adding_host_platform.md)
67
- [Adding a New Cryptographic Functionality Provider](adding_cfp.md)

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ nav:
125125
- Functional Tests: for_contributors/tests/functional_tests.md
126126
- Functional Mock Tests: for_contributors/tests/functional_mock_tests.md
127127
- Code Coverage: for_contributors/tests/code_coverage.md
128+
- Using the CodeChecker: for_contributors/codechecker.md
128129
- Adding a New Host Platform: for_contributors/adding_host_platform.md
129130
- Adding a New Cryptographic Functionality Provider: for_contributors/adding_cfp.md
130131
- FAQ: faq.md

scripts/codechecker/README.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

scripts/codechecker/codechecker.skip

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
+*/libtropic/cal/*
44
+*/libtropic/hal/*
55
+*/libtropic/include/*
6-
+*/libtropic/examples/*
6+
7+
# Disable checking of source codes of examples,
8+
# as we intentionally omit some checks and patterns for simplicity.
9+
# +*/libtropic/examples/*/main.c
710

811
# Skip everything else (vendor libs, CMake internal libs...)
912
-*

scripts/codechecker/codechecker_build.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

scripts/codechecker/codechecker_config.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
analyzer:
2-
################################################
3-
# Configure skipfile to select which sources
4-
# to check.
5-
################################################
6-
- --skip=scripts/codechecker/codechecker.skip
7-
82
################################################
93
# Enable / disable checks
104
################################################
@@ -48,11 +42,6 @@ analyzer:
4842
################################################
4943
- --ctu-all
5044

51-
################################################
52-
# Export setup
53-
################################################
54-
- --output=.codechecker/reports
55-
5645
################################################
5746
# Checker selection
5847
################################################

scripts/codechecker/run_checks.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env bash
2+
3+
# This script builds Libtropic in multiple configurations for CodeChecker evaluation.
4+
#
5+
# Majority of platforms are built using examples, as they are quicker to build,
6+
# the model is built using tests, as they allow to change CAL, which has
7+
# to be done for one target.
8+
9+
set -eo pipefail
10+
11+
LT_ROOT_DIR="."
12+
13+
if [ -z "$1" ]; then
14+
echo "Assuming Libtropic root directory is a current working directory."
15+
echo "To change the Libtropic root directory, pass it as the first argument:"
16+
echo " $0 <path_to_libtropic>"
17+
else
18+
LT_ROOT_DIR="${1%/}" # Remove last trailing slash (if any present)
19+
echo "Libtropic root directory set to: $LT_ROOT_DIR"
20+
fi
21+
22+
echo "Checking dependencies..."
23+
if ! command -v CodeChecker >/dev/null 2>&1; then
24+
echo "Missing CodeChecker! Install and try again."
25+
exit 1
26+
fi
27+
if ! command -v jq >/dev/null 2>&1; then
28+
echo "Missing jq! Install and try again."
29+
exit 1
30+
fi
31+
if ! command -v cmake >/dev/null 2>&1; then
32+
echo "Missing cmake! Install and try again."
33+
exit 1
34+
fi
35+
if ! command -v make >/dev/null 2>&1; then
36+
echo "Missing make! Install and try again."
37+
exit 1
38+
fi
39+
40+
# Recreating directories
41+
rm -fr "$LT_ROOT_DIR/.codechecker/"
42+
mkdir -p "$LT_ROOT_DIR/.codechecker/compile_commands"
43+
mkdir -p "$LT_ROOT_DIR/.codechecker/reports"
44+
mkdir -p "$LT_ROOT_DIR/.codechecker/reports_html"
45+
46+
# Linux USB DevKit + MbedTLSv4
47+
CodeChecker log -b "cd \"$LT_ROOT_DIR/examples/linux/usb_devkit/hello_world\" && rm -rf build && mkdir build && cd build && cmake .. && make -j" \
48+
-o "$LT_ROOT_DIR/.codechecker/compile_commands/usb_devkit_compile_commands.json"
49+
50+
# Linux SPI + MbedTLSv4
51+
CodeChecker log -b "cd \"$LT_ROOT_DIR/examples/linux/spi/hello_world\" && rm -rf build && mkdir build && cd build && cmake .. && make -j" \
52+
-o "$LT_ROOT_DIR/.codechecker/compile_commands/linux_spi_compile_commands.json"
53+
54+
# Model + all CALs
55+
CALS=("trezor_crypto" "mbedtls_v4" "openssl" "wolfcrypt")
56+
for CURRENT_CAL in "${CALS[@]}"; do
57+
CodeChecker log -b "cd \"$LT_ROOT_DIR/tests/functional/model\" && rm -rf build && mkdir build && cd build && cmake -DLT_CAL=$CURRENT_CAL .. && make -j" \
58+
-o "$LT_ROOT_DIR/.codechecker/compile_commands/model_${CURRENT_CAL}_compile_commands.json"
59+
done
60+
61+
# Merge compile_commands.json files
62+
# Change temporarily to the directory, so we support
63+
# also special symbols in the path specified by $LT_ROOT_DIR.
64+
(cd "$LT_ROOT_DIR/.codechecker/compile_commands" && \
65+
jq -s 'add' ./*_compile_commands.json \
66+
> "../merged_compile_commands.json")
67+
68+
set +e
69+
# Run analysis on merged compilation database
70+
CodeChecker analyze "$LT_ROOT_DIR/.codechecker/merged_compile_commands.json" \
71+
--config "$LT_ROOT_DIR/scripts/codechecker/codechecker_config.yml" \
72+
--skip "$LT_ROOT_DIR/scripts/codechecker/codechecker.skip" \
73+
-o "$LT_ROOT_DIR/.codechecker/reports"
74+
set -e
75+
76+
CodeChecker parse "$LT_ROOT_DIR/.codechecker/reports" \
77+
-e html \
78+
-o "$LT_ROOT_DIR/.codechecker/reports_html"

0 commit comments

Comments
 (0)