Skip to content

Commit 4633132

Browse files
committed
cmake: sca: iar: Add IAR C-STAT
This commit adds support for IAR C-STAT Static Analysis in the Zephyr's SCA Framework. By specifying -DZEPHYR_SCA_VARIANT=iar in west, a SQLite database file will be generated under build/sca/iar. Signed-off-by: Felipe Torrezan <felipe.torrezan@iar.com>
1 parent d3d334c commit 4633132

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

cmake/sca/iar/sca.cmake

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# Copyright (c) 2025, IAR Systems AB.
4+
5+
include(extensions)
6+
include(west)
7+
8+
# Get IAR C-STAT
9+
cmake_path(GET CMAKE_C_COMPILER PARENT_PATH IAR_COMPILER_DIR)
10+
find_program(IAR_CSTAT icstat
11+
HINTS ${IAR_COMPILER_DIR}
12+
REQUIRED
13+
)
14+
message(STATUS "Found SCA: IAR C-STAT Static Analysis (${IAR_CSTAT})")
15+
find_program(IAR_CHECKS ichecks
16+
HINTS ${IAR_COMPILER_DIR}
17+
REQUIRED
18+
)
19+
20+
zephyr_get(CSTAT_RULESET)
21+
zephyr_get(CSTAT_ANALYZE_THREADS)
22+
zephyr_get(CSTAT_ANALYZE_OPTS)
23+
zephyr_get(CSTAT_DB)
24+
zephyr_get(CSTAT_CLEANUP)
25+
26+
# Create an output directory for IAR C-STAT
27+
set(output_dir ${CMAKE_BINARY_DIR}/sca/iar)
28+
file(MAKE_DIRECTORY ${output_dir})
29+
30+
# Set the IAR C-STAT ruleset
31+
set(iar_checks_arg --output=${output_dir}/cstat_sel_checks.txt)
32+
if(CSTAT_RULESET MATCHES "^(cert|security|misrac2004|misrac\\+\\+2008|misrac2012)")
33+
set(iar_checks_arg ${iar_checks_arg} --default=${CSTAT_RULESET})
34+
elseif(CSTAT_RULESET MATCHES "^all")
35+
set(iar_checks_arg ${iar_checks_arg} --all)
36+
else()
37+
set(iar_checks_arg ${iar_checks_arg} --default=stdchecks)
38+
endif()
39+
execute_process(COMMAND ${IAR_CHECKS} ${iar_checks_arg})
40+
41+
# Forwards the ruleset manifest file to icstat
42+
set(output_arg --checks=${output_dir}/cstat_sel_checks.txt)
43+
44+
# Analsys parallelization
45+
if(CSTAT_ANALYZE_THREADS)
46+
set(output_arg ${output_arg};--parallel=${CSTAT_ANALYZE_THREADS})
47+
endif()
48+
49+
# Entrypoint for additional C-STAT options
50+
if(CSTAT_ANALYZE_OPTS)
51+
set(output_arg ${output_arg};${CSTAT_ANALYZE_OPTS})
52+
endif()
53+
54+
# Full path to the C-STAT SQLite database
55+
if(CSTAT_DB)
56+
set(CSTAT_DB_PATH ${CSTAT_DB})
57+
else()
58+
set(CSTAT_DB_PATH ${output_dir}/cstat.db)
59+
endif()
60+
set(output_arg ${output_arg};--db=${CSTAT_DB_PATH})
61+
62+
# Clean-up C-STAT SQLite database
63+
if(CSTAT_CLEANUP)
64+
execute_process(COMMAND ${IAR_CSTAT} clear --db=${CSTAT_DB_PATH})
65+
endif()
66+
67+
# Enable IAR C-STAT Static Analysis (requires CMake v4.1+)
68+
set(CMAKE_C_ICSTAT ${IAR_CSTAT};${output_arg} CACHE INTERNAL "")
69+
set(CMAKE_CXX_ICSTAT ${IAR_CSTAT};${output_arg} CACHE INTERNAL "")

doc/develop/sca/iar.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.. _icstat:
2+
3+
IAR C-STAT support
4+
##################
5+
6+
`IAR C-STAT <https://iar.com/cstat>`__ is a comprehensive static analysis tool for
7+
C/C++ source code. It can find errors and vulnerabilities supporting a number of
8+
coding standards such as MISRA C, MISRA C++, CERT C/C++ and CWE.
9+
10+
Installing IAR C-STAT
11+
*********************
12+
13+
IAR C-STAT comes pre-installed with the IAR Build Tools and with the IAR Embedded
14+
Workbench. Refer to your respective product's documentation for details.
15+
16+
Building with IAR C-STAT
17+
************************
18+
19+
To run IAR C-STAT, :ref:`west build <west-building>` should be called with
20+
a ``-DZEPHYR_SCA_VARIANT=iar`` parameter, e.g.
21+
22+
.. code-block:: shell
23+
24+
west build -b stm32f429ii_aca samples/basic/blinky -- -DZEPHYR_SCA_VARIANT=iar
25+
26+
Configuring IAR C-STAT
27+
***********************
28+
29+
The IAR C-STAT accepts parameters for customizing the analysis.
30+
The following table lists the supported options.
31+
32+
.. list-table::
33+
:header-rows: 1
34+
35+
* - Parameter
36+
- Description
37+
* - ``CSTAT_RULESET``
38+
- The pre-defined ruleset to be used. (default: ``stdchecks``, accepted values: ``all,cert,misrac2004,misrac2012,misrac++2008,stdchecks,security``)
39+
* - ``CSTAT_ANALYZE_THREADS``
40+
- The number of threads to use in analysis. (default: <CPU count>)
41+
* - ``CSTAT_ANALYZE_OPTS``
42+
- Arguments passed to the ``analyze`` command directly. (e.g. ``--timeout=900;--deterministic;--fpe``)
43+
* - ``CSTAT_DB``
44+
- Override the default location of the C-STAT SQLite database. (e.g. ``/home/user/cstat.db``)
45+
* - ``CSTAT_CLEANUP``
46+
- Perform a cleanup of the C-STAT SQLite database. (e.g. ``true``)
47+
48+
These parameters can be passed on the command line, or be set as environment variables.

doc/develop/sca/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ The following is a list of SCA tools natively supported by Zephyr build system.
6969
eclair
7070
polyspace
7171
coverity
72+
iar

0 commit comments

Comments
 (0)