|
| 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 "") |
0 commit comments