Skip to content

Commit

Permalink
cmake: implement build infrastructure for supporting SCA tools.
Browse files Browse the repository at this point in the history
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=<tool>` 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/<tool>/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 <Torsten.Rasmussen@nordicsemi.no>
  • Loading branch information
tejlmand authored and stephanosio committed Jan 27, 2023
1 parent ac95102 commit cb690ec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
31 changes: 31 additions & 0 deletions cmake/modules/FindScaTools.cmake
Original file line number Diff line number Diff line change
@@ -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}"
)
1 change: 1 addition & 0 deletions cmake/modules/kernel.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions cmake/modules/root.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion scripts/zephyr_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
module_ext_root:
required: false
type: str
sca_root:
required: false
type: str
tests:
required: false
type: seq
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cb690ec

Please sign in to comment.