Skip to content

Commit

Permalink
cmake: add sparse support to the new SCA infrastructure
Browse files Browse the repository at this point in the history
Sparse support was original introduced in #43776.

This commit introduces sparse support as part of Zephyr SCA tool
infrastructure.

The implementation in this commit has some benefits over existing
support:
- It does not required users to set `REAL_CC` in environment before
  invoking build command.
  This reduces risk of user mistakes, such as
  - REAL_CC being different from CMAKE_C_COMPILER.
  - User running CMake in one terminal / environment where REAL_CC is
    defined but invoking the build command in a different terminal /
    environment where REAL_CC is not defined or defined differently.
- It improve user experience as the user no longer has to define /
  re-define REAL_CC when building for different architecture, like
  switching from arm to xtensa, as this is now handled in CMake.
- CMAKE_C_COMPILER is not overwriting, this can be important for other
  tools which calls the C compiler for pre-processing purposes, such
  as devicetree and linker script generation.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
  • Loading branch information
tejlmand authored and stephanosio committed Jan 27, 2023
1 parent cb690ec commit 91902c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cmake/sca/sparse/sca.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022, Nordic Semiconductor ASA

find_program(SPARSE_COMPILER cgcc REQUIRED)
message(STATUS "Found sparse: ${SPARSE_COMPILER}")

# Create sparse.cmake which will be called as compiler launcher.
# sparse.cmake will ensure that REAL_CC is set correctly in environment before
# cgcc is called, thereby ensuring correct behavior of sparse without a need
# for REAL_CC to be set in environment.
configure_file(${CMAKE_CURRENT_LIST_DIR}/sparse.template ${CMAKE_BINARY_DIR}/sparse.cmake @ONLY)

set(launch_environment ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/sparse.cmake --)
set(CMAKE_C_COMPILER_LAUNCHER ${launch_environment} CACHE INTERNAL "")

list(APPEND TOOLCHAIN_C_FLAGS -D__CHECKER__)
# Avoid compiler "attribute directive ignored" warnings
list(APPEND TOOLCHAIN_C_FLAGS -Wno-attributes)
19 changes: 19 additions & 0 deletions cmake/sca/sparse/sparse.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022, Nordic Semiconductor ASA

# Everything before `--` are arguments for cmake invocation, those must be ignored.
# First argument after `--` is the real compiler, but that is defined in REAL_CC
# as environment variable for cgcc, hence that must also be ignored, thus first
# argument to be passed to sparse is 2nd argument after `--`.
foreach(i RANGE ${CMAKE_ARGC})
if("${CMAKE_ARGV${i}}" STREQUAL "--")
math(EXPR end_of_options "${i} + 2")
break()
endif()
endforeach()

foreach(i RANGE ${end_of_options} ${CMAKE_ARGC})
list(APPEND ARGS ${CMAKE_ARGV${i}})
endforeach()
execute_process(COMMAND @CMAKE_COMMAND@ -E env REAL_CC=@CMAKE_C_COMPILER@ @SPARSE_COMPILER@ ${ARGS})

0 comments on commit 91902c5

Please sign in to comment.