From cb690ec56e9d66355c91a309a7bffa47aecb0671 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 30 Nov 2022 12:14:35 +0100 Subject: [PATCH] cmake: implement build infrastructure for supporting SCA tools. Static code analyser (SCA) tools are important in software development. CMake offers built-in support for some tools, such as cppcheck and clang-tidy. Other tools, such as sparse, are not directly supported. This commit provides a uniform way for users to specify a supported SCA using `ZEPHYR_SCA_VARIANT=` which is consistent with how toolchains are specified. ZEPHYR_SCA_VARIANT can be set using `-D` or in environment. Support for an SCA tool is done in `cmake/sca//sca.cmake`. SCA_ROOT can be used to specify additional search paths when looking up implementation for a tool. SCA_ROOT can also be specified in `zephyr/module.yml` as setting. This makes it possible to provide SCA tool implementation as part of a Zephyr module. Signed-off-by: Torsten Rasmussen --- cmake/modules/FindScaTools.cmake | 31 +++++++++++++++++++++++++++++++ cmake/modules/kernel.cmake | 1 + cmake/modules/root.cmake | 3 +++ scripts/zephyr_module.py | 5 ++++- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 cmake/modules/FindScaTools.cmake diff --git a/cmake/modules/FindScaTools.cmake b/cmake/modules/FindScaTools.cmake new file mode 100644 index 0000000000..2e567cfd3d --- /dev/null +++ b/cmake/modules/FindScaTools.cmake @@ -0,0 +1,31 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (c) 2022, Nordic Semiconductor ASA + +# 'SCA_ROOT' is a prioritized list of directories where SCA tools may +# be found. It always includes ${ZEPHYR_BASE} at the lowest priority. +list(APPEND SCA_ROOT ${ZEPHYR_BASE}) + +zephyr_get(ZEPHYR_SCA_VARIANT) + +if(ScaTools_FIND_REQUIRED AND NOT DEFINED ZEPHYR_SCA_VARIANT) + message(FATAL_ERROR "ScaTools required but 'ZEPHYR_SCA_VARIANT' is not set. " + "Please set 'ZEPHYR_SCA_VARIANT' to desired tool." + ) +endif() + +if(NOT DEFINED ZEPHYR_SCA_VARIANT) + return() +endif() + +foreach(root ${SCA_ROOT}) + if(EXISTS ${root}/cmake/sca/${ZEPHYR_SCA_VARIANT}/sca.cmake) + include(${root}/cmake/sca/${ZEPHYR_SCA_VARIANT}/sca.cmake) + return() + endif() +endforeach() + +message(FATAL_ERROR "ZEPHYR_SCA_VARIANT set to '${ZEPHYR_SCA_VARIANT}' but no " + "implementation for '${ZEPHYR_SCA_VARIANT}' found. " + "SCA_ROOTs searched: ${SCA_ROOT}" +) diff --git a/cmake/modules/kernel.cmake b/cmake/modules/kernel.cmake index 10a4cdd828..7e65c9cd18 100644 --- a/cmake/modules/kernel.cmake +++ b/cmake/modules/kernel.cmake @@ -23,6 +23,7 @@ include_guard(GLOBAL) find_package(TargetTools) +find_package(ScaTools) # As this module is not intended for direct loading, but should be loaded through # find_package(Zephyr) then it won't be loading any Zephyr CMake modules by itself. diff --git a/cmake/modules/root.cmake b/cmake/modules/root.cmake index 24ff3884ce..4c6ab0ac5e 100644 --- a/cmake/modules/root.cmake +++ b/cmake/modules/root.cmake @@ -33,6 +33,9 @@ zephyr_file(APPLICATION_ROOT SOC_ROOT) # Convert paths to absolute, relative from APPLICATION_SOURCE_DIR zephyr_file(APPLICATION_ROOT ARCH_ROOT) +# Convert paths to absolute, relative from APPLICATION_SOURCE_DIR +zephyr_file(APPLICATION_ROOT SCA_ROOT) + if(unittest IN_LIST Zephyr_FIND_COMPONENTS) # Zephyr used in unittest mode, use dedicated unittest root. set(BOARD_ROOT ${ZEPHYR_BASE}/subsys/testsuite) diff --git a/scripts/zephyr_module.py b/scripts/zephyr_module.py index c9b297beb5..65fb7c5c74 100755 --- a/scripts/zephyr_module.py +++ b/scripts/zephyr_module.py @@ -81,6 +81,9 @@ mapping: module_ext_root: required: false type: str + sca_root: + required: false + type: str tests: required: false type: seq @@ -219,7 +222,7 @@ def process_settings(module, meta): out_text = "" if build_settings is not None: - for root in ['board', 'dts', 'soc', 'arch', 'module_ext']: + for root in ['board', 'dts', 'soc', 'arch', 'module_ext', 'sca']: setting = build_settings.get(root+'_root', None) if setting is not None: root_path = PurePath(module) / setting