1# SPDX-License-Identifier: Apache-2.0 2# 3# Copyright (c) 2025 Alex Fabre 4 5find_program(CLANG_SCA_EXE NAMES analyze-build REQUIRED) 6message(STATUS "Found SCA: clang static analyzer (${CLANG_SCA_EXE})") 7 8# Get clang analyzer user options 9zephyr_get(CLANG_SCA_OPTS) 10zephyr_get(LLVM_TOOLCHAIN_PATH) 11 12# Check analyzer extra options 13if(DEFINED CLANG_SCA_OPTS) 14 foreach(analyzer_option IN LISTS CLANG_SCA_OPTS) 15 list(APPEND CLANG_SCA_EXTRA_OPTS ${analyzer_option}) 16 endforeach() 17endif() 18 19# clang analyzer uses the compile_commands.json as input 20set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 21 22# Create an output directory for clang analyzer results 23set(output_dir ${CMAKE_BINARY_DIR}/sca/clang) 24file(MAKE_DIRECTORY ${output_dir}) 25 26# Use a dummy file to let clang static analyzer know we can start analyzing 27set_property(GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND 28 ${CMAKE_COMMAND} -E touch ${output_dir}/clang-sca.ready) 29set_property(GLOBAL APPEND PROPERTY extra_post_build_byproducts 30 ${output_dir}/clang-sca.ready) 31 32# Add a cmake target to run the analyzer after the build is done 33add_custom_target(clang-sca ALL 34 COMMAND ${CLANG_SCA_EXE} --cdb ${CMAKE_BINARY_DIR}/compile_commands.json -o ${CMAKE_BINARY_DIR}/sca/clang/ --analyze-headers --use-analyzer ${LLVM_TOOLCHAIN_PATH}/bin/clang ${CLANG_SCA_EXTRA_OPTS} 35 DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json ${output_dir}/clang-sca.ready 36) 37 38# Cleanup dummy file 39add_custom_command( 40 TARGET clang-sca POST_BUILD 41 COMMAND ${CMAKE_COMMAND} -E rm ${output_dir}/clang-sca.ready 42) 43