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 "11")
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")
0060     message("Enabling AST Matchers workaround. Consider 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)
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 macro(link_to_llvm name is_standalone)
0118   if (CLAZY_LINK_CLANG_DYLIB)
0119     target_link_libraries(${name} clang-cpp)
0120   else()
0121     foreach(clang_lib ${CLANG_LIBS})
0122       if(MSVC)
0123         get_filename_component(LIB_FILENAME ${clang_lib} NAME)
0124         if(LIB_FILENAME STREQUAL "clangFrontend.lib")
0125           # On MSVC we don't link against clangFrontend.lib, instead we link against clang.exe (via clang.lib)
0126           # Otherwise the clazy plugin would have it's own plugin registry and clang wouldn't see it.
0127           # This way clazy registers with clang.
0128           continue()
0129         endif()
0130       endif()
0131 
0132       target_link_libraries(${name} ${clang_lib})
0133     endforeach()
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   if (CLAZY_USES_BOOST_REGEX)
0162     target_link_libraries(${name} ${Boost_LIBRARIES})
0163   endif()
0164 endmacro()
0165 
0166 macro(add_clang_plugin name)
0167   set(srcs ${ARGN})
0168 
0169   add_library(${name} SHARED ${srcs})
0170 
0171   if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
0172       # 30% speedup
0173       target_precompile_headers(${name} PRIVATE src/checkbase.h)
0174   endif()
0175 
0176   if(SYMBOL_FILE)
0177     set_target_properties(${name} PROPERTIES LINK_FlAGS "-exported_symbols_list ${SYMBOL_FILE}")
0178   endif()
0179 
0180   if (LINK_CLAZY_TO_LLVM)
0181     link_to_llvm(${name} FALSE)
0182   else()
0183     if (APPIMAGE_HACK)
0184         # Hack to build on old CentOS 6.8
0185         target_link_libraries(${name} clangTooling)
0186         target_link_libraries(${name} clangToolingCore)
0187         target_link_libraries(${name} ${clang_tooling_refactoring_lib})
0188     endif()
0189   endif()
0190 
0191   if(MSVC)
0192     target_link_libraries(${name} ${CLANG_LIBRARY_IMPORT}) # Link against clang.exe to share the plugin registry
0193   endif()
0194 
0195 endmacro()
0196 
0197 set(SYMBOL_FILE Lazy.exports)
0198 
0199 if (NOT CLAZY_BUILD_WITH_CLANG)
0200   set(CLAZY_MINI_AST_DUMPER_SRCS src/MiniAstDumper.cpp)
0201   add_clang_plugin(ClazyPlugin ${CLAZY_PLUGIN_SRCS} ${CLAZY_MINI_AST_DUMPER_SRCS})
0202   set_target_properties(ClazyPlugin PROPERTIES
0203     LINKER_LANGUAGE CXX
0204     PREFIX ""
0205   )
0206 
0207   install(TARGETS ClazyPlugin
0208     RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
0209     LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
0210     ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
0211   )
0212 
0213   if(NOT WIN32)
0214     if(APPLE)
0215       find_program(READLINK_CMD greadlink)
0216     else()
0217       find_program(READLINK_CMD readlink)
0218     endif()
0219     if(NOT READLINK_CMD)
0220       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")
0221     endif()
0222     file(RELATIVE_PATH BIN_RELATIVE_LIBDIR "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_FULL_LIBDIR}")
0223     file(RELATIVE_PATH BIN_RELATIVE_SHAREDIR "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_FULL_DATAROOTDIR}")
0224     configure_file(${CMAKE_CURRENT_LIST_DIR}/clazy.cmake ${CMAKE_BINARY_DIR}/clazy @ONLY)
0225     install(PROGRAMS ${CMAKE_BINARY_DIR}/clazy DESTINATION bin)
0226   else()
0227     install(PROGRAMS ${CMAKE_CURRENT_LIST_DIR}/clazy.bat DESTINATION ${CMAKE_INSTALL_BINDIR})
0228     if(MSVC)
0229       install(PROGRAMS ${CMAKE_CURRENT_LIST_DIR}/clazy-cl.bat DESTINATION ${CMAKE_INSTALL_BINDIR})
0230     endif()
0231   endif()
0232 
0233   # Install the explanation README's
0234   include(${CMAKE_CURRENT_LIST_DIR}/readmes.cmake)
0235 
0236   install(FILES ${README_LEVEL0_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/level0)
0237   install(FILES ${README_LEVEL1_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/level1)
0238   install(FILES ${README_LEVEL2_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/level2)
0239   install(FILES ${README_manuallevel_FILES} DESTINATION ${CMAKE_INSTALL_DOCDIR}/manuallevel)
0240 
0241   # Install more doc files
0242   install(FILES README.md COPYING-LGPL2.txt checks.json DESTINATION ${CMAKE_INSTALL_DOCDIR})
0243 
0244   # Build docs
0245   if (CLAZY_MAN_PAGE)
0246     add_subdirectory(docs)
0247   endif()
0248 
0249   # rpath
0250   set(CMAKE_SKIP_BUILD_RPATH FALSE)
0251   set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
0252   set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
0253   set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
0254   list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" isSystemDir)
0255   if("${isSystemDir}" STREQUAL "-1")
0256     set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
0257   endif("${isSystemDir}" STREQUAL "-1")
0258 
0259   # Build clazy-standalone
0260   add_executable(clazy-standalone ${CLAZY_STANDALONE_SRCS})
0261 
0262   if(MSVC)
0263     # On MSVC clang-standalone crashes with a meaningless backtrace if linked to ClazyPlugin.dll
0264     target_link_libraries(clazy-standalone clangFrontend)
0265   else()
0266     target_link_libraries(clazy-standalone ClazyPlugin)
0267   endif()
0268 
0269   link_to_llvm(clazy-standalone TRUE)
0270 
0271   install(TARGETS clazy-standalone DESTINATION bin PERMISSIONS OWNER_WRITE OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE)
0272 
0273   set(CPACK_PACKAGE_VERSION_MAJOR ${CLAZY_VERSION_MAJOR})
0274   set(CPACK_PACKAGE_VERSION_MINOR ${CLAZY_VERSION_MINOR})
0275   set(CPACK_PACKAGE_VERSION_PATCH ${CLAZY_VERSION_PATCH})
0276   include(CPack)
0277 else()
0278   set(LLVM_LINK_COMPONENTS
0279     Support
0280     )
0281   add_clang_library(clazyPlugin
0282     ${CLAZY_PLUGIN_SRCS}
0283 
0284     LINK_LIBS
0285     clangToolingCore
0286     clangToolingInclusions
0287     ${clang_tooling_refactoring_lib}
0288     clangFrontend
0289     clangDriver
0290     clangCodeGen
0291     clangSema
0292     clangAnalysis
0293     clangRewriteFrontend
0294     clangRewrite
0295     clangAST
0296     clangASTMatchers
0297     clangParse
0298     clangLex
0299     clangBasic
0300     clangARCMigrate
0301     clangEdit
0302     clangFrontendTool
0303     clangRewrite
0304     clangSerialization
0305     clangTooling
0306     clangStaticAnalyzerCheckers
0307     clangStaticAnalyzerCore
0308     clangStaticAnalyzerFrontend
0309     )
0310   add_executable(clazy-standalone ${CLAZY_STANDALONE_SRCS})
0311 
0312   target_link_libraries(clazy-standalone clazyPlugin)
0313 
0314   install(TARGETS clazy-standalone DESTINATION bin PERMISSIONS OWNER_WRITE OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE)
0315 endif()
0316 
0317 function(to_raw_string_literal input_string output_string)
0318     if (MSVC)
0319         # Work around "C2026: string too big, trailing characters truncated"
0320         #   https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2026?view=vs-2019
0321         # The limit is 16380 single-byte characters, so split up the string as
0322         # suggested on the site.
0323         set(str ${input_string})
0324         set(chunk_size 1000)
0325         set(result "\n")
0326         string(LENGTH ${str} str_size)
0327         while (${str_size} GREATER ${chunk_size})
0328             string(SUBSTRING ${str} 0 ${chunk_size} chunk)
0329             string(SUBSTRING ${str} ${chunk_size} -1 str)
0330             set(chunk "R\"meta(${chunk})meta\"\n")
0331             string(APPEND result ${chunk})
0332             string(LENGTH ${str} str_size)
0333         endwhile()
0334         if (str_size GREATER 0)
0335             string(APPEND result "R\"meta(${str})meta\"\n")
0336         endif()
0337         set(${output_string} ${result} PARENT_SCOPE)
0338     else()
0339         set(result "\nR\"meta(${input_string})meta\"\n")
0340         set(${output_string} ${result} PARENT_SCOPE)
0341     endif()
0342 endfunction()
0343 
0344 file(READ checks.json SUPPORTED_CHECKS_JSON_STR)
0345 to_raw_string_literal(${SUPPORTED_CHECKS_JSON_STR} SUPPORTED_CHECKS_JSON_STR)
0346 configure_file(checks.json.h.in checks.json.h)
0347 
0348 enable_testing()
0349 include(ClazyTests.cmake)
0350 
0351 install(FILES org.kde.clazy.metainfo.xml DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/metainfo)