From 7a85ff768385e2f833c575b9825af9be83eb637b Mon Sep 17 00:00:00 2001 From: Guennadi Liakhovetski Date: Mon, 14 Mar 2022 17:26:18 +0100 Subject: [PATCH] sparse: add sparse support With this adding "-DSPARSE=y" to the "west build" command line performs a sparse check of the project build. So far only gcc-based builds are supported. Signed-off-by: Guennadi Liakhovetski --- CMakeLists.txt | 4 ++++ cmake/compiler/gcc/target.cmake | 9 ++++++++- cmake/linker/ld/target.cmake | 8 +++++++- doc/develop/test/index.rst | 1 + doc/develop/test/sparse.rst | 25 +++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 doc/develop/test/sparse.rst diff --git a/CMakeLists.txt b/CMakeLists.txt index fa66f592ca9..bbb5fd2f573 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -267,6 +267,10 @@ if(CONFIG_COMPILER_COLOR_DIAGNOSTICS) zephyr_compile_options($) endif() +if("${SPARSE}" STREQUAL "y") + list(APPEND TOOLCHAIN_C_FLAGS -D__CHECKER__) +endif() + zephyr_compile_options( ${TOOLCHAIN_C_FLAGS} ) diff --git a/cmake/compiler/gcc/target.cmake b/cmake/compiler/gcc/target.cmake index 6c114355ac5..1cb68cd60dc 100644 --- a/cmake/compiler/gcc/target.cmake +++ b/cmake/compiler/gcc/target.cmake @@ -5,7 +5,14 @@ set_ifndef(C++ g++) # Configures CMake for using GCC, this script is re-used by several # GCC-based toolchains -find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}${CC} PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) +if("${SPARSE}" STREQUAL "y") + find_program(CMAKE_C_COMPILER cgcc) + find_program(REAL_CC ${CROSS_COMPILE}${CC} PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) + set(ENV{REAL_CC} ${REAL_CC}) +else() + find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}${CC} PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) +endif() + if(${CMAKE_C_COMPILER} STREQUAL CMAKE_C_COMPILER-NOTFOUND) message(FATAL_ERROR "C compiler ${CROSS_COMPILE}${CC} not found - Please check your toolchain installation") endif() diff --git a/cmake/linker/ld/target.cmake b/cmake/linker/ld/target.cmake index 9d1d2357915..4957b5fd2e0 100644 --- a/cmake/linker/ld/target.cmake +++ b/cmake/linker/ld/target.cmake @@ -66,6 +66,12 @@ macro(configure_linker_script linker_script_gen linker_pass_define) zephyr_get_include_directories_for_lang(C current_includes) get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES) + if("${SPARSE}" STREQUAL "y") + set(ld_command ${REAL_CC}) + else() + set(ld_command ${CMAKE_C_COMPILER}) + endif() + add_custom_command( OUTPUT ${linker_script_gen} DEPENDS @@ -74,7 +80,7 @@ macro(configure_linker_script linker_script_gen linker_pass_define) ${extra_dependencies} # NB: 'linker_script_dep' will use a keyword that ends 'DEPENDS' ${linker_script_dep} - COMMAND ${CMAKE_C_COMPILER} + COMMAND ${ld_command} -x assembler-with-cpp ${NOSYSDEF_CFLAG} -MD -MF ${linker_script_gen}.dep -MT ${linker_script_gen} diff --git a/doc/develop/test/index.rst b/doc/develop/test/index.rst index b7debc460b8..2383832a027 100644 --- a/doc/develop/test/index.rst +++ b/doc/develop/test/index.rst @@ -9,3 +9,4 @@ Testing ztest twister coverage + sparse diff --git a/doc/develop/test/sparse.rst b/doc/develop/test/sparse.rst new file mode 100644 index 00000000000..bf4a138ffaf --- /dev/null +++ b/doc/develop/test/sparse.rst @@ -0,0 +1,25 @@ +.. _sparse: + +Sparse support +############## + +`Sparse `__ is a static code analysis tool. +Apart from performing common code analysis tasks it also supports an +``address_space`` attribute, which allows introduction of distinct address +spaces in C code with subsequent verification that pointers to different +address spaces do not get confused. Additionally it supports a ``force`` +attribute which should be used to cast pointers between different address +spaces. At the moment Zephyr introduces a single custom address space +``__cache`` used to identify pointers from the cached address range on the +Xtensa architecture. This helps identify cases where cached and uncached +addresses are confused. + +Running with sparse +******************* + +To run a sparse verification build :ref:`west build ` should be +called with a `-DSPARSE=y` parameter, e.g. + +.. code-block:: shell + + west build -d hello -b intel_adsp_cavs25 zephyr/samples/hello_world -- -DSPARSE=y