release 0.1.15
[liba.git] / cmake / TargetAnalyzer.cmake
blobba21fbb2423a92e4edd13201ee3104b74cd90919
1 get_cmake_property(ENABLED_LANGUAGES ENABLED_LANGUAGES)
2 include(CheckCXXCompilerFlag)
3 include(CheckCCompilerFlag)
5 function(list_append var)
6   foreach(arg ${ARGN})
7     list(FIND ${var} ${arg} found)
8     if(${found} EQUAL -1)
9       list(APPEND ${var} ${arg})
10     endif()
11   endforeach()
12   set(${var} ${${var}} PARENT_SCOPE)
13 endfunction()
15 macro(analyzer_flag)
16   foreach(arg ${ARGN})
17     string(REPLACE "+" "x" var ${arg})
18     string(REGEX REPLACE "[^A-Za-z0-9_-]+" "-" var ${var})
19     list(FIND ENABLED_LANGUAGES C found)
20     if(${found} GREATER -1)
21       check_c_compiler_flag(${arg} cx${var})
22       if(cx${var})
23         list_append(ANALYZER_CX ${arg})
24       endif()
25     endif()
26     list(FIND ENABLED_LANGUAGES CXX found)
27     if(${found} GREATER -1)
28       check_cxx_compiler_flag(${arg} cx${var})
29       if(cx${var})
30         list_append(ANALYZER_CX ${arg})
31       endif()
32     endif()
33   endforeach()
34 endmacro()
36 if(CMAKE_C_COMPILER_ID MATCHES "MSVC" OR CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
37   # https://learn.microsoft.com/en-us/cpp/build/reference/analyze-code-analysis
38   analyzer_flag(/analyze)
39 elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
40   # https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/Static-Analyzer-Options.html
41   analyzer_flag(-fanalyzer) # 10.1+
42 endif()
44 function(target_compile_analyzer)
45   foreach(target ${ARGN})
46     target_compile_options(${target} PRIVATE ${ANALYZER_CX})
47   endforeach()
48 endfunction()