Update build script for Lua
[liba.git] / cmake / TargetAnalyzer.cmake
blobda43d96321fea42732ee8bf1286949daff8f8506
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} index)
8     if(${index} 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 CXX index)
20     if(${index} GREATER -1)
21       check_cxx_compiler_flag(${arg} xx${var})
22       if(xx${var})
23         list_append(ANALYZER ${arg})
24       endif()
25     endif()
26     list(FIND ENABLED_LANGUAGES C index)
27     if(${index} GREATER -1)
28       check_c_compiler_flag(${arg} cc${var})
29       if(cc${var})
30         list_append(ANALYZER ${arg})
31       endif()
32     endif()
33     set(var)
34   endforeach()
35   set(index)
36 endmacro()
38 if(CMAKE_C_COMPILER_ID MATCHES "MSVC" OR CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
39   # https://learn.microsoft.com/en-us/cpp/build/reference/analyze-code-analysis
40   analyzer_flag(/analyze)
41 elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
42   # https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/Static-Analyzer-Options.html
43   analyzer_flag(-fanalyzer) # 10.1+
44 endif()
46 function(target_compile_analyzer)
47   foreach(target ${ARGN})
48     target_compile_options(${target} PRIVATE ${ANALYZER})
49   endforeach()
50 endfunction()