Warning, /sdk/clazy/CMakeLists.txt is written in an unsupported language. File is not indexed.

0001 # This is the top-level CMakeLists.txt file for the Clazy project.
0002 #
0003 # To build the man page from POD, run 'make man' after CMake (assumes perl is available)
0004 # To install the resulting man page, run 'make install'
0005 # The man page is not available on Windows.
0006 #
0007 
0008 project(clazy)
0009 
0010 cmake_minimum_required(VERSION 3.7)
0011 
0012 if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0")
0013     cmake_policy(SET CMP0074 NEW)
0014 endif()
0015 
0016 include(FeatureSummary)
0017 include(GenerateExportHeader)
0018 include(GNUInstallDirs)
0019 
0020 # Version setup
0021 set(CLAZY_VERSION_MAJOR "1")
0022 set(CLAZY_VERSION_MINOR "12")
0023 set(CLAZY_VERSION_PATCH "0")
0024 set(CLAZY_VERSION "${CLAZY_VERSION_MAJOR}.${CLAZY_VERSION_MINOR}.${CLAZY_VERSION_PATCH}")
0025 set(CLAZY_PRINT_VERSION "${CLAZY_VERSION_MAJOR}.${CLAZY_VERSION_MINOR}")
0026 
0027 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_LIST_DIR}/cmake)
0028 if (NOT CLAZY_BUILD_WITH_CLANG)
0029   find_package(Clang 8.0 MODULE REQUIRED)
0030 
0031   if (CLANG_CLANG-CPP_LIB AND NOT APPLE)
0032     set(default_use_clang_cpp ON)
0033   else()
0034     set(default_use_clang_cpp OFF)
0035   endif()
0036   option(CLAZY_LINK_CLANG_DYLIB "Link clazy to dynamic Clang C++ library clang-cpp." ${default_use_clang_cpp})
0037 endif()
0038 
0039 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
0040 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
0041 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
0042 
0043 add_definitions(-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS)
0044 add_definitions(-D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H)
0045 
0046 option(CLAZY_AST_MATCHERS_CRASH_WORKAROUND "Disable AST Matchers if being built with clang. See bug #392223" ON)
0047 option(LINK_CLAZY_TO_LLVM "Links the clazy plugin to LLVM. Switch to OFF if your clang binary has all symbols already. Might need to be OFF if your LLVM is static." ON)
0048 option(APPIMAGE_HACK "Links the clazy plugin to the clang tooling libs only. For some reason this is needed when building on our old CentOS 6.8 to create the AppImage." OFF)
0049 option(CLAZY_MAN_PAGE "Builds the man page." ON)
0050 option(CLAZY_ENABLE_SANITIZERS "Builds clazy with ASAN/UBSAN. For clazy's own development only." OFF)
0051 
0052 if (CLAZY_ENABLE_SANITIZERS)
0053     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
0054     if (NOT MSVC)
0055         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
0056     endif()
0057 endif()
0058 
0059 if (CLAZY_AST_MATCHERS_CRASH_WORKAROUND AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.0 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)
0060     message("Enabling AST Matchers workaround. Consider updating clang or building with gcc instead. See bug #392223.")
0061     add_definitions(-DCLAZY_DISABLE_AST_MATCHERS)
0062 endif()
0063 
0064 if(NOT CLAZY_BUILD_WITH_CLANG AND MSVC AND NOT CLANG_LIBRARY_IMPORT)
0065   message(FATAL_ERROR "\nOn MSVC you need to pass -DCLANG_LIBRARY_IMPORT=C:/path/to/llvm-build/lib/clang.lib to cmake when building Clazy.\nAlso make sure you've built LLVM with -DLLVM_EXPORT_SYMBOLS_FOR_PLUGINS=ON")
0066 endif()
0067 
0068 if(MSVC)
0069   # disable trigger-happy warnings from Clang/LLVM headers
0070   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4267 /wd4244 /wd4291 /wd4800 /wd4141 /wd4146 /wd4251")
0071 elseif(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
0072   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing -pedantic -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings -fno-exceptions -fno-rtti")
0073 endif()
0074 
0075 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
0076 if(WIN32)
0077   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
0078 else()
0079   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
0080 endif()
0081 
0082 # Look for std::regex support
0083 message("Looking for std::regex support...")
0084 try_run(REGEX_RUN_RESULT COMPILE_RESULT ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/.cmake_has_regex_test.cpp CXX_STANDARD 17 CXX_STANDARD_REQUIRED true)
0085 
0086 if(NOT REGEX_RUN_RESULT EQUAL 0)
0087   message("Using boost::regex instead of std::regex")
0088   set(CLAZY_USES_BOOST_REGEX TRUE)
0089   add_definitions(-DCLAZY_USES_BOOST_REGEX)
0090   find_package(Boost REQUIRED COMPONENTS regex)
0091   include_directories(${Boost_INCLUDE_DIRS})
0092 endif()
0093 
0094 set(CMAKE_CXX_STANDARD 17)
0095 set(CXX_STANDARD_REQUIRED ON)
0096 
0097 # Compiler might support c++17 but not have std::filesystem in the standard library
0098 message("Looking for std::filesystem support...")
0099 try_run(FILESYSTEM_RUN_RESULT COMPILE_RESULT ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/.cmake_has_filesystem_test.cpp OUTPUT_VARIABLE foo CXX_STANDARD 17 CXX_STANDARD_REQUIRED true)
0100 
0101 if (FILESYSTEM_RUN_RESULT EQUAL 0 AND COMPILE_RESULT)
0102     add_definitions(-DHAS_STD_FILESYSTEM)
0103 endif()
0104 
0105 include(ClazySources.cmake)
0106 
0107 include_directories(${CMAKE_BINARY_DIR})
0108 include_directories(${CLANG_INCLUDE_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/src)
0109 link_directories("${LLVM_INSTALL_PREFIX}/lib" ${LLVM_LIBRARY_DIRS})
0110 
0111 if (${LLVM_VERSION} VERSION_GREATER_EQUAL "9.0.0")
0112     set(clang_tooling_refactoring_lib clangToolingRefactoring)
0113 else()
0114     set(clang_tooling_refactoring_lib clangToolingRefactor)
0115 endif()
0116 
0117 if (${LLVM_VERSION} VERSION_GREATER_EQUAL "15.0.0")
0118   set(clang_support_lib clangSupport)
0119 endif()
0120 
0121 macro(link_to_llvm name is_standalone)
0122   if (CLAZY_LINK_CLANG_DYLIB)
0123     target_link_libraries(${name} clang-cpp)
0124   else()
0125     foreach(clang_lib ${CLANG_LIBS})
0126       if(MSVC)
0127         get_filename_component(LIB_FILENAME ${clang_lib} NAME)
0128         if(LIB_FILENAME STREQUAL "clangFrontend.lib")
0129           # On MSVC we don't link against clangFrontend.lib, instead we link against clang.exe (via clang.lib)
0130           # Otherwise the clazy plugin would have it's own plugin registry and clang wouldn't see it.
0131           # This way clazy registers with clang.
0132           continue()
0133         endif()
0134       endif()
0135 
0136       target_link_libraries(${name} ${clang_lib})
0137     endforeach()
0138     target_link_libraries(${name} ${clang_support_lib})
0139     target_link_libraries(${name} clangTooling)
0140     target_link_libraries(${name} clangToolingCore)
0141     target_link_libraries(${name} ${clang_tooling_refactoring_lib})
0142   endif()
0143 
0144   foreach(llvm_lib ${LLVM_LIBS})
0145     if(NOT ${is_standalone} AND NOT APPLE AND NOT MINGW AND NOT MSVC)
0146         ## Don't link against LLVMSupport, causes: CommandLine Error: Option 'view-background' registered more than once!
0147         if (NOT llvm_lib MATCHES ".*LLVMSupport.*")
0148             target_link_libraries(${name} ${llvm_lib})
0149         endif()
0150     else()
0151         target_link_libraries(${name} ${llvm_lib})
0152     endif()
0153   endforeach()
0154 
0155   foreach(user_lib ${USER_LIBS})
0156     target_link_libraries(${name} ${user_lib})
0157   endforeach()
0158 
0159   foreach(llvm_system_lib ${LLVM_SYSTEM_LIBS})
0160     target_link_libraries(${name} ${llvm_system_lib})
0161   endforeach()
0162 
0163   if(WIN32)
0164     target_link_libraries(${name} version.lib)
0165   endif()
0166   if (CLAZY_USES_BOOST_REGEX)
0167     target_link_libraries(${name} ${Boost_LIBRARIES})
0168   endif()
0169 endmacro()
0170 
0171 macro(add_clang_plugin name)
0172   set(srcs ${ARGN})
0173 
0174   add_library(${name} SHARED ${srcs})
0175 
0176   if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
0177       # 30% speedup
0178       target_precompile_headers(${name} PRIVATE src/checkbase.h)
0179   endif()
0180 
0181   if(SYMBOL_FILE)
0182     set_target_properties(${name} PROPERTIES LINK_FlAGS "-exported_symbols_list ${SYMBOL_FILE}")
0183   endif()
0184 
0185   if (LINK_CLAZY_TO_LLVM)
0186     link_to_llvm(${name} FALSE)
0187   else()
0188     if (APPIMAGE_HACK)
0189         # Hack to build on old CentOS 6.8
0190         target_link_libraries(${name} clangTooling)
0191         target_link_libraries(${name} clangToolingCore)
0192         target_link_libraries(${name} ${clang_tooling_refactoring_lib})
0193     endif()
0194   endif()
0195 
0196   if(MSVC)
0197     target_link_libraries(${name} ${CLANG_LIBRARY_IMPORT}) # Link against clang.exe to share the plugin registry
0198   endif()
0199 
0200 endmacro()
0201 
0202 set(SYMBOL_FILE Lazy.exports)
0203 
0204 if (NOT CLAZY_BUILD_WITH_CLANG)
0205   set(CLAZY_MINI_AST_DUMPER_SRCS src/MiniAstDumper.cpp)
0206   add_clang_plugin(ClazyPlugin ${CLAZY_PLUGIN_SRCS} ${CLAZY_MINI_AST_DUMPER_SRCS})
0207   set_target_properties(ClazyPlugin PROPERTIES
0208     LINKER_LANGUAGE CXX
0209     PREFIX ""
0210   )
0211 
0212   install(TARGETS ClazyPlugin
0213     RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
0214     LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
0215     ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
0216   )
0217 
0218   if(NOT WIN32)
0219     if(APPLE)
0220       find_program(READLINK_CMD greadlink)
0221     else()
0222       find_program(READLINK_CMD readlink)
0223     endif()
0224     if(NOT READLINK_CMD)
0225       message(FATAL_ERROR "Could not find a proper readlink.  On Mac OSX you should install coreutils using homebrew in order to use the GNU readlink")
0226     endif()
0227     file(RELATIVE_PATH BIN_RELATIVE_LIBDIR "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_FULL_LIBDIR}")
0228     file(RELATIVE_PATH BIN_RELATIVE_SHAREDIR "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_FULL_DATAROOTDIR}")
0229     configure_file(${CMAKE_CURRENT_LIST_DIR}/clazy.cmake ${CMAKE_BINARY_DIR}/clazy @ONLY)
0230     install(PROGRAMS ${CMAKE_BINARY_DIR}/clazy DESTINATION bin)
0231   else()
0232     install(PROGRAMS ${CMAKE_CURRENT_LIST_DIR}/clazy.bat DESTINATION ${CMAKE_INSTALL_BINDIR})
0233     if(MSVC)
0234       install(PROGRAMS ${CMAKE_CURRENT_LIST_DIR}/clazy-cl.bat DESTINATION ${CMAKE_INSTALL_BINDIR})
0235     endif()
0236   endif()
0237 
0238   # Install the explanation README's
0239   include(${CMAKE_CURRENT_LIST_DIR}/readmes.cmake)
0240 
0241   install(FILES ${README_LEVEL0_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/level0)
0242   install(FILES ${README_LEVEL1_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/level1)
0243   install(FILES ${README_LEVEL2_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/level2)
0244   install(FILES ${README_manuallevel_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/manuallevel)
0245 
0246   # Install more doc files
0247   install(FILES README.md COPYING-LGPL2.txt checks.json DESTINATION ${CMAKE_INSTALL_DOCDIR})
0248 
0249   # Build docs
0250   if (CLAZY_MAN_PAGE)
0251     add_subdirectory(docs)
0252   endif()
0253 
0254   # rpath
0255   set(CMAKE_SKIP_BUILD_RPATH FALSE)
0256   set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
0257   set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
0258   set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
0259   list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" isSystemDir)
0260   if("${isSystemDir}" STREQUAL "-1")
0261     set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
0262   endif("${isSystemDir}" STREQUAL "-1")
0263 
0264   # Build clazy-standalone
0265   add_executable(clazy-standalone ${CLAZY_STANDALONE_SRCS})
0266 
0267   if(MSVC)
0268     # On MSVC clang-standalone crashes with a meaningless backtrace if linked to ClazyPlugin.dll
0269     target_link_libraries(clazy-standalone clangFrontend)
0270   else()
0271     target_link_libraries(clazy-standalone ClazyPlugin)
0272   endif()
0273 
0274   link_to_llvm(clazy-standalone TRUE)
0275 
0276   install(TARGETS clazy-standalone DESTINATION bin PERMISSIONS OWNER_WRITE OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE)
0277 
0278   set(CPACK_PACKAGE_VERSION_MAJOR ${CLAZY_VERSION_MAJOR})
0279   set(CPACK_PACKAGE_VERSION_MINOR ${CLAZY_VERSION_MINOR})
0280   set(CPACK_PACKAGE_VERSION_PATCH ${CLAZY_VERSION_PATCH})
0281   include(CPack)
0282 else()
0283   set(LLVM_LINK_COMPONENTS
0284     Support
0285     )
0286   add_clang_library(clazyPlugin
0287     ${CLAZY_PLUGIN_SRCS}
0288 
0289     LINK_LIBS
0290     clangToolingCore
0291     clangToolingInclusions
0292     ${clang_tooling_refactoring_lib}
0293     clangFrontend
0294     clangDriver
0295     clangCodeGen
0296     clangSema
0297     clangAnalysis
0298     clangRewriteFrontend
0299     clangRewrite
0300     clangAST
0301     clangASTMatchers
0302     clangParse
0303     clangLex
0304     clangBasic
0305     clangARCMigrate
0306     clangEdit
0307     clangFrontendTool
0308     clangRewrite
0309     clangSerialization
0310     ${clang_support_lib}
0311     clangTooling
0312     clangStaticAnalyzerCheckers
0313     clangStaticAnalyzerCore
0314     clangStaticAnalyzerFrontend
0315     )
0316   add_executable(clazy-standalone ${CLAZY_STANDALONE_SRCS})
0317 
0318   target_link_libraries(clazy-standalone clazyPlugin)
0319 
0320   install(TARGETS clazy-standalone DESTINATION bin PERMISSIONS OWNER_WRITE OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE)
0321 endif()
0322 
0323 function(to_raw_string_literal input_string output_string)
0324     if (MSVC)
0325         # Work around "C2026: string too big, trailing characters truncated"
0326         #   https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2026?view=vs-2019
0327         # The limit is 16380 single-byte characters, so split up the string as
0328         # suggested on the site.
0329         set(str ${input_string})
0330         set(chunk_size 1000)
0331         set(result "\n")
0332         string(LENGTH ${str} str_size)
0333         while (${str_size} GREATER ${chunk_size})
0334             string(SUBSTRING ${str} 0 ${chunk_size} chunk)
0335             string(SUBSTRING ${str} ${chunk_size} -1 str)
0336             set(chunk "R\"meta(${chunk})meta\"\n")
0337             string(APPEND result ${chunk})
0338             string(LENGTH ${str} str_size)
0339         endwhile()
0340         if (str_size GREATER 0)
0341             string(APPEND result "R\"meta(${str})meta\"\n")
0342         endif()
0343         set(${output_string} ${result} PARENT_SCOPE)
0344     else()
0345         set(result "\nR\"meta(${input_string})meta\"\n")
0346         set(${output_string} ${result} PARENT_SCOPE)
0347     endif()
0348 endfunction()
0349 
0350 file(READ checks.json SUPPORTED_CHECKS_JSON_STR)
0351 to_raw_string_literal(${SUPPORTED_CHECKS_JSON_STR} SUPPORTED_CHECKS_JSON_STR)
0352 configure_file(checks.json.h.in checks.json.h)
0353 
0354 enable_testing()
0355 include(ClazyTests.cmake)
0356 
0357 install(FILES org.kde.clazy.metainfo.xml DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/metainfo)