Skip to content

Instantly share code, notes, and snippets.

@samolisov
Last active December 20, 2021 11:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samolisov/eedc89dcc442cdcc808eb8c811fd0d09 to your computer and use it in GitHub Desktop.
Save samolisov/eedc89dcc442cdcc808eb8c811fd0d09 to your computer and use it in GitHub Desktop.
CMake: add target for invoking Clang Static Analyzer
# The solution is based upon this answer:
# https://stackoverflow.com/questions/19050461/cmake-add-target-for-invoking-clang-analyzer
function(add_clang_static_analysis target)
get_target_property(SRCs ${target} SOURCES)
add_library(${target}_analyze OBJECT EXCLUDE_FROM_ALL ${SRCs})
if (MSVC)
list(APPEND ANALYZER_OPTIONS --analyze)
list(APPEND ANALYZER_OPTIONS "SHELL:/clang:-Xanalyzer /clang:-analyzer-opt-analyze-headers")
list(APPEND ANALYZER_OPTIONS "SHELL:/clang:-Xanalyzer /clang:-analyzer-output=html")
# see output in <build/<file>.plist directories
else()
list(APPEND ANALYZER_OPTIONS --analyze)
list(APPEND ANALYZER_OPTIONS "SHELL:-Xanalyzer -analyzer-opt-analyze-headers")
list(APPEND ANALYZER_OPTIONS "SHELL:-Xanalyzer -analyzer-output=html")
# see output in <build>/<tree>/CMakeFiles/<target>.dir/<file>.obj directories
endif(MSVC)
set_target_properties(${target}_analyze PROPERTIES
COMPILE_OPTIONS "${ANALYZER_OPTIONS}"
EXCLUDE_FROM_DEFAULT_BUILD true)
endfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment