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 cmake_minimum_required(VERSION 3.7)
0009 
0010 project(clazy)
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 11.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 set(CMAKE_CXX_STANDARD 17)
0083 set(CXX_STANDARD_REQUIRED ON)
0084 
0085 # Look for std::regex support
0086 if(NOT DEFINED REGEX_RUN_RESULT)
0087   message("Looking for std::regex support...")
0088   try_compile(REGEX_RUN_RESULT SOURCES ${CMAKE_CURRENT_LIST_DIR}/.cmake_has_regex_test.cpp)
0089 endif()
0090 
0091 if(NOT DEFINED REGEX_RUN_RESULT)
0092   message(FATAL_ERROR "std::regex support is mndatory")
0093 endif()
0094 
0095 # Compiler might support c++17 but not have std::filesystem in the standard library
0096 if (NOT FILESYSTEM_RUN_RESULT)
0097   try_compile(FILESYSTEM_RUN_RESULT SOURCES ${CMAKE_CURRENT_LIST_DIR}/.cmake_has_filesystem_test.cpp OUTPUT_VARIABLE foo CXX_STANDARD 17 CXX_STANDARD_REQUIRED true)
0098 endif()
0099 
0100 if (NOT FILESYSTEM_RUN_RESULT)
0101   message(FATAL_ERROR "std::filesystem support is mndatory")
0102 endif()
0103 
0104 include(ClazySources.cmake)
0105 
0106 include_directories(${CMAKE_BINARY_DIR})
0107 include_directories(${CLANG_INCLUDE_DIRS} ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/src)
0108 link_directories("${LLVM_INSTALL_PREFIX}/lib" ${LLVM_LIBRARY_DIRS})
0109 
0110 set(clang_tooling_refactoring_lib clangToolingRefactoring)
0111 
0112 if (${LLVM_VERSION} VERSION_GREATER_EQUAL "15.0.0")
0113   set(clang_support_lib clangSupport)
0114 endif()
0115 
0116 macro(link_to_llvm name is_standalone)
0117   if (CLAZY_LINK_CLANG_DYLIB)
0118     target_link_libraries(${name} clang-cpp)
0119   else()
0120     foreach(clang_lib ${CLANG_LIBS})
0121       if(MSVC)
0122         get_filename_component(LIB_FILENAME ${clang_lib} NAME)
0123         if(LIB_FILENAME STREQUAL "clangFrontend.lib")
0124           # On MSVC we don't link against clangFrontend.lib, instead we link against clang.exe (via clang.lib)
0125           # Otherwise the clazy plugin would have it's own plugin registry and clang wouldn't see it.
0126           # This way clazy registers with clang.
0127           continue()
0128         endif()
0129       endif()
0130 
0131       target_link_libraries(${name} ${clang_lib})
0132     endforeach()
0133     target_link_libraries(${name} ${clang_support_lib})
0134     target_link_libraries(${name} clangTooling)
0135     target_link_libraries(${name} clangToolingCore)
0136     target_link_libraries(${name} ${clang_tooling_refactoring_lib})
0137   endif()
0138 
0139   foreach(llvm_lib ${LLVM_LIBS})
0140     if(NOT ${is_standalone} AND NOT APPLE AND NOT MINGW AND NOT MSVC)
0141         ## Don't link against LLVMSupport, causes: CommandLine Error: Option 'view-background' registered more than once!
0142         if (NOT llvm_lib MATCHES ".*LLVMSupport.*")
0143             target_link_libraries(${name} ${llvm_lib})
0144         endif()
0145     else()
0146         target_link_libraries(${name} ${llvm_lib})
0147     endif()
0148   endforeach()
0149 
0150   foreach(user_lib ${USER_LIBS})
0151     target_link_libraries(${name} ${user_lib})
0152   endforeach()
0153 
0154   foreach(llvm_system_lib ${LLVM_SYSTEM_LIBS})
0155     target_link_libraries(${name} ${llvm_system_lib})
0156   endforeach()
0157 
0158   if(WIN32)
0159     target_link_libraries(${name} version.lib)
0160   endif()
0161 endmacro()
0162 
0163 macro(add_clang_plugin name)
0164   set(srcs ${ARGN})
0165 
0166   add_library(${name} SHARED ${srcs})
0167 
0168   if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
0169       # 30% speedup
0170       target_precompile_headers(${name} PRIVATE src/checkbase.h)
0171   endif()
0172 
0173   if(SYMBOL_FILE)
0174     set_target_properties(${name} PROPERTIES LINK_FlAGS "-exported_symbols_list ${SYMBOL_FILE}")
0175   endif()
0176 
0177   if (LINK_CLAZY_TO_LLVM)
0178     link_to_llvm(${name} FALSE)
0179   else()
0180     if (APPIMAGE_HACK)
0181         # Hack to build on old CentOS 6.8
0182         target_link_libraries(${name} clangTooling)
0183         target_link_libraries(${name} clangToolingCore)
0184         target_link_libraries(${name} ${clang_tooling_refactoring_lib})
0185     endif()
0186   endif()
0187 
0188   if(MSVC)
0189     target_link_libraries(${name} ${CLANG_LIBRARY_IMPORT}) # Link against clang.exe to share the plugin registry
0190   endif()
0191 
0192 endmacro()
0193 
0194 set(SYMBOL_FILE Lazy.exports)
0195 
0196 if (NOT CLAZY_BUILD_WITH_CLANG)
0197   set(CLAZY_MINI_AST_DUMPER_SRCS src/MiniAstDumper.cpp)
0198   add_clang_plugin(ClazyPlugin ${CLAZY_PLUGIN_SRCS} ${CLAZY_MINI_AST_DUMPER_SRCS})
0199   set_target_properties(ClazyPlugin PROPERTIES
0200     LINKER_LANGUAGE CXX
0201     PREFIX ""
0202   )
0203 
0204   install(TARGETS ClazyPlugin
0205     RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
0206     LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
0207     ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
0208   )
0209 
0210   if(NOT WIN32)
0211     if(APPLE)
0212       find_program(READLINK_CMD greadlink)
0213     else()
0214       find_program(READLINK_CMD readlink)
0215     endif()
0216     if(NOT READLINK_CMD)
0217       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")
0218     endif()
0219     file(RELATIVE_PATH BIN_RELATIVE_LIBDIR "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_FULL_LIBDIR}")
0220     file(RELATIVE_PATH BIN_RELATIVE_SHAREDIR "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_FULL_DATAROOTDIR}")
0221     configure_file(${CMAKE_CURRENT_LIST_DIR}/clazy.cmake ${CMAKE_BINARY_DIR}/clazy @ONLY)
0222     install(PROGRAMS ${CMAKE_BINARY_DIR}/clazy DESTINATION bin)
0223   else()
0224     install(PROGRAMS ${CMAKE_CURRENT_LIST_DIR}/clazy.bat DESTINATION ${CMAKE_INSTALL_BINDIR})
0225     if(MSVC)
0226       install(PROGRAMS ${CMAKE_CURRENT_LIST_DIR}/clazy-cl.bat DESTINATION ${CMAKE_INSTALL_BINDIR})
0227     endif()
0228   endif()
0229 
0230   # Install the explanation README's
0231   include(${CMAKE_CURRENT_LIST_DIR}/readmes.cmake)
0232 
0233   install(FILES ${README_LEVEL0_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/level0)
0234   install(FILES ${README_LEVEL1_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/level1)
0235   install(FILES ${README_LEVEL2_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/level2)
0236   install(FILES ${README_manuallevel_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/manuallevel)
0237 
0238   # Install more doc files
0239   install(FILES README.md LICENSES/LGPL-2.0-or-later.txt checks.json DESTINATION ${CMAKE_INSTALL_DOCDIR})
0240 
0241   # Build docs
0242   if (CLAZY_MAN_PAGE)
0243     add_subdirectory(docs)
0244   endif()
0245 
0246   # rpath
0247   set(CMAKE_SKIP_BUILD_RPATH FALSE)
0248   set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
0249   set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
0250   set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
0251   list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" isSystemDir)
0252   if("${isSystemDir}" STREQUAL "-1")
0253     set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
0254   endif("${isSystemDir}" STREQUAL "-1")
0255 
0256   # Build clazy-standalone
0257   add_executable(clazy-standalone ${CLAZY_STANDALONE_SRCS})
0258 
0259   if(MSVC)
0260     # On MSVC clang-standalone crashes with a meaningless backtrace if linked to ClazyPlugin.dll
0261     target_link_libraries(clazy-standalone clangFrontend)
0262   else()
0263     target_link_libraries(clazy-standalone ClazyPlugin)
0264   endif()
0265 
0266   link_to_llvm(clazy-standalone TRUE)
0267 
0268   install(TARGETS clazy-standalone DESTINATION bin PERMISSIONS OWNER_WRITE OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE)
0269 
0270   set(CPACK_PACKAGE_VERSION_MAJOR ${CLAZY_VERSION_MAJOR})
0271   set(CPACK_PACKAGE_VERSION_MINOR ${CLAZY_VERSION_MINOR})
0272   set(CPACK_PACKAGE_VERSION_PATCH ${CLAZY_VERSION_PATCH})
0273   include(CPack)
0274 else()
0275   set(LLVM_LINK_COMPONENTS
0276     Support
0277     )
0278   add_clang_library(clazyPlugin
0279     ${CLAZY_PLUGIN_SRCS}
0280 
0281     LINK_LIBS
0282     clangToolingCore
0283     clangToolingInclusions
0284     ${clang_tooling_refactoring_lib}
0285     clangFrontend
0286     clangDriver
0287     clangCodeGen
0288     clangSema
0289     clangAnalysis
0290     clangRewriteFrontend
0291     clangRewrite
0292     clangAST
0293     clangASTMatchers
0294     clangParse
0295     clangLex
0296     clangBasic
0297     clangARCMigrate
0298     clangEdit
0299     clangFrontendTool
0300     clangRewrite
0301     clangSerialization
0302     ${clang_support_lib}
0303     clangTooling
0304     clangStaticAnalyzerCheckers
0305     clangStaticAnalyzerCore
0306     clangStaticAnalyzerFrontend
0307     )
0308   add_executable(clazy-standalone ${CLAZY_STANDALONE_SRCS})
0309 
0310   target_link_libraries(clazy-standalone clazyPlugin)
0311 
0312   install(TARGETS clazy-standalone DESTINATION bin PERMISSIONS OWNER_WRITE OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE)
0313 endif()
0314 
0315 function(to_raw_string_literal input_string output_string)
0316     if (MSVC)
0317         # Work around "C2026: string too big, trailing characters truncated"
0318         #   https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2026?view=vs-2019
0319         # The limit is 16380 single-byte characters, so split up the string as
0320         # suggested on the site.
0321         set(str ${input_string})
0322         set(chunk_size 1000)
0323         set(result "\n")
0324         string(LENGTH ${str} str_size)
0325         while (${str_size} GREATER ${chunk_size})
0326             string(SUBSTRING ${str} 0 ${chunk_size} chunk)
0327             string(SUBSTRING ${str} ${chunk_size} -1 str)
0328             set(chunk "R\"meta(${chunk})meta\"\n")
0329             string(APPEND result ${chunk})
0330             string(LENGTH ${str} str_size)
0331         endwhile()
0332         if (str_size GREATER 0)
0333             string(APPEND result "R\"meta(${str})meta\"\n")
0334         endif()
0335         set(${output_string} ${result} PARENT_SCOPE)
0336     else()
0337         set(result "\nR\"meta(${input_string})meta\"\n")
0338         set(${output_string} ${result} PARENT_SCOPE)
0339     endif()
0340 endfunction()
0341 
0342 file(READ checks.json SUPPORTED_CHECKS_JSON_STR)
0343 to_raw_string_literal(${SUPPORTED_CHECKS_JSON_STR} SUPPORTED_CHECKS_JSON_STR)
0344 configure_file(checks.json.h.in checks.json.h)
0345 
0346 enable_testing()
0347 include(ClazyTests.generated.cmake)
0348 
0349 install(FILES org.kde.clazy.metainfo.xml DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/metainfo)