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