Skip to content

Commit

Permalink
Update LLVM/Clang support to 14.0+, add LLVM 19 handling
Browse files Browse the repository at this point in the history
  • Loading branch information
phyBrackets authored and alex1701c committed Nov 19, 2024
1 parent d0b033a commit 5a86780
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 25 deletions.
12 changes: 4 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ set(CLAZY_PRINT_VERSION "${CLAZY_VERSION_MAJOR}.${CLAZY_VERSION_MINOR}")

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_LIST_DIR}/cmake)
if (NOT CLAZY_BUILD_WITH_CLANG)
find_package(Clang 11.0 MODULE REQUIRED)
find_package(Clang 14.0 MODULE REQUIRED)

if (CLANG_CLANG-CPP_LIB AND NOT APPLE)
set(default_use_clang_cpp ON)
Expand All @@ -44,7 +44,6 @@ add_definitions(-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS)
add_definitions(-D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H)
add_definitions(-DCLAZY_VERSION="${CLAZY_PRINT_VERSION}")

option(CLAZY_AST_MATCHERS_CRASH_WORKAROUND "Disable AST Matchers if being built with clang. See bug #392223" ON)
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)
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)
option(CLAZY_MAN_PAGE "Builds the man page." ON)
Expand All @@ -57,11 +56,6 @@ if (CLAZY_ENABLE_SANITIZERS)
endif()
endif()

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)
message("Enabling AST Matchers workaround. Consider updating clang or building with gcc instead. See bug #392223.")
add_definitions(-DCLAZY_DISABLE_AST_MATCHERS)
endif()

if(NOT CLAZY_BUILD_WITH_CLANG AND MSVC AND NOT CLANG_LIBRARY_IMPORT)
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")
endif()
Expand Down Expand Up @@ -110,7 +104,9 @@ link_directories("${LLVM_INSTALL_PREFIX}/lib" ${LLVM_LIBRARY_DIRS})

set(clang_tooling_refactoring_lib clangToolingRefactoring)

if (${LLVM_VERSION} VERSION_GREATER_EQUAL "15.0.0")
if (${LLVM_VERSION} VERSION_GREATER_EQUAL "19.0.0")
set(clang_support_lib clangSupport)
elseif (${LLVM_VERSION} VERSION_GREATER_EQUAL "15.0.0")
set(clang_support_lib clangSupport)
endif()

Expand Down
26 changes: 17 additions & 9 deletions cmake/FindClang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#=============================================================================
# SPDX-FileCopyrightText: 2014-2015 Kevin Funk <[email protected]>
# SPDX-FileCopyrightText: 2024 Shivan Kunwar <[email protected]>
#
# SPDX-License-Identifier: BSD-2-Clause
#=============================================================================
Expand All @@ -32,10 +33,6 @@ endif ()
set(CLANG_FOUND FALSE)

if (LLVM_FOUND AND LLVM_LIBRARY_DIRS)
macro(FIND_CLANG_LIB _libname_)
string(TOUPPER ${_libname_} _prettylibname_)
find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} HINTS ${LLVM_LIBRARY_DIRS})
endmacro(FIND_CLANG_LIB)
macro(FIND_AND_ADD_CLANG_LIB _libname_)
string(TOUPPER ${_libname_} _prettylibname_)
find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} HINTS ${LLVM_LIBRARY_DIRS})
Expand All @@ -47,12 +44,11 @@ if (LLVM_FOUND AND LLVM_LIBRARY_DIRS)
# note: On Windows there's 'libclang.dll' instead of 'clang.dll' -> search for 'libclang', too
find_library(CLANG_LIBCLANG_LIB NAMES clang libclang HINTS ${LLVM_LIBRARY_DIRS}) # LibClang: high-level C interface

FIND_CLANG_LIB(clang-cpp)
FIND_AND_ADD_CLANG_LIB(clang-cpp)
FIND_AND_ADD_CLANG_LIB(clangFrontend)
FIND_AND_ADD_CLANG_LIB(clangDriver)
FIND_AND_ADD_CLANG_LIB(clangCodeGen)
FIND_AND_ADD_CLANG_LIB(clangSema)
FIND_AND_ADD_CLANG_LIB(clangChecker)
FIND_AND_ADD_CLANG_LIB(clangAnalysis)
FIND_AND_ADD_CLANG_LIB(clangRewriteFrontend)
FIND_AND_ADD_CLANG_LIB(clangRewrite)
Expand All @@ -64,15 +60,27 @@ if (LLVM_FOUND AND LLVM_LIBRARY_DIRS)
FIND_AND_ADD_CLANG_LIB(clangARCMigrate)
FIND_AND_ADD_CLANG_LIB(clangEdit)
FIND_AND_ADD_CLANG_LIB(clangFrontendTool)
FIND_AND_ADD_CLANG_LIB(clangRewrite)
FIND_AND_ADD_CLANG_LIB(clangSerialization)
FIND_AND_ADD_CLANG_LIB(clangTooling)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCheckers)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCore)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerFrontend)
FIND_AND_ADD_CLANG_LIB(clangSema)
FIND_AND_ADD_CLANG_LIB(clangRewriteCore)
FIND_AND_ADD_CLANG_LIB(clangAPINotes)

if (LLVM_VERSION VERSION_GREATER_EQUAL "15.0")
FIND_AND_ADD_CLANG_LIB(clangSupport)
endif()

# LLVM 19 specific libraries
if (LLVM_VERSION VERSION_GREATER_EQUAL "19.0")
FIND_AND_ADD_CLANG_LIB(clangASTMatchers)
FIND_AND_ADD_CLANG_LIB(clangTransformer)
FIND_AND_ADD_CLANG_LIB(clangToolingCore)
FIND_AND_ADD_CLANG_LIB(clangToolingInclusions)
FIND_AND_ADD_CLANG_LIB(clangToolingRefactoring)
FIND_AND_ADD_CLANG_LIB(clangToolingSyntax)
FIND_AND_ADD_CLANG_LIB(clangDependencyScanning)
endif()
endif()

if(CLANG_LIBS OR CLANG_LIBCLANG_LIB OR CLANG_CLANG-CPP_LIB)
Expand Down
58 changes: 50 additions & 8 deletions cmake/FindLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#=============================================================================
# SPDX-FileCopyrightText: 2014 Kevin Funk <[email protected]>
# SPDX-FileCopyrightText: 2024 Shivan Kunwar <[email protected]>
#
# SPDX-License-Identifier: BSD-2-Clause
#=============================================================================
Expand All @@ -30,14 +31,10 @@ endif()
if (LLVM_ROOT)
find_program(LLVM_CONFIG_EXECUTABLE NAMES llvm-config HINTS ${LLVM_ROOT}/bin DOC "llvm-config executable" NO_DEFAULT_PATH)
else()
# find llvm-config, prefer the one with a version suffix, e.g. llvm-config-3.5
# note: FreeBSD installs llvm-config as llvm-config35 and so on
# note: on some distributions, only 'llvm-config' is shipped, so let's always try to fallback on that
# find llvm-config, prefer the one with a version suffix, e.g. llvm-config-14
string(REPLACE "." "" LLVM_FIND_VERSION_CONCAT ${LLVM_FIND_VERSION})
find_program(LLVM_CONFIG_EXECUTABLE NAMES llvm-config-${LLVM_FIND_VERSION} llvm-config${LLVM_FIND_VERSION_CONCAT} llvm-config DOC "llvm-config executable")
find_program(LLVM_CONFIG_EXECUTABLE NAMES llvm-config-${LLVM_FIND_VERSION} llvm-config${LLVM_FIND_VERSION_CONCAT} llvm-config-14 llvm-config-15 llvm-config-16 llvm-config-17 llvm-config-18 llvm-config-19 llvm-config DOC "llvm-config executable")

# other distributions don't ship llvm-config, but only some llvm-config-VERSION binary
# try to deduce installed LLVM version by looking up llvm-nm in PATH and *then* find llvm-config-VERSION via that
if (NOT LLVM_CONFIG_EXECUTABLE)
find_program(_llvmNmExecutable llvm-nm)
if (_llvmNmExecutable)
Expand All @@ -58,7 +55,7 @@ if (LLVM_CONFIG_EXECUTABLE)

if (NOT LLVM_VERSION)
set(_LLVM_ERROR_MESSAGE "Failed to parse version from llvm-config")
elseif (LLVM_FIND_VERSION VERSION_GREATER LLVM_VERSION)
elseif (LLVM_VERSION VERSION_LESS "14.0")
set(_LLVM_ERROR_MESSAGE "LLVM version too old: ${LLVM_VERSION}")
else()
set(LLVM_FOUND TRUE)
Expand Down Expand Up @@ -103,7 +100,7 @@ if (LLVM_FOUND)
OUTPUT_VARIABLE LLVM_LIBS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REPLACE " " ";" LLVM_LIBS ${LLVM_LIBS}) # Make it consistent with --libs
string(REPLACE " " ";" LLVM_LIBS ${LLVM_LIBS})

execute_process(
COMMAND ${LLVM_CONFIG_EXECUTABLE} --system-libs
Expand Down Expand Up @@ -144,6 +141,17 @@ if (LLVM_FOUND)
if (NOT _llvmIsInstalled)
list(APPEND LLVM_INCLUDE_DIRS "${LLVM_INSTALL_PREFIX}/include")
endif()

# LLVM 19 specific handling
if (LLVM_VERSION VERSION_GREATER_EQUAL "19.0")
execute_process(
COMMAND ${LLVM_CONFIG_EXECUTABLE} --libs all
OUTPUT_VARIABLE LLVM_LIBS_19
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REPLACE " " ";" LLVM_LIBS_19 ${LLVM_LIBS_19})
list(APPEND LLVM_LIBS ${LLVM_LIBS_19})
endif()
endif()

if (LLVM_FIND_REQUIRED AND NOT LLVM_FOUND)
Expand All @@ -157,4 +165,38 @@ if (LLVM_FOUND)
message(STATUS " Include dirs: ${LLVM_INCLUDE_DIRS}")
message(STATUS " LLVM libraries: ${LLVM_LIBS}")
message(STATUS " LLVM System libraries: ${LLVM_SYSTEM_LIBS}")

if (LLVM_VERSION VERSION_GREATER_EQUAL "19.0")
message(STATUS " LLVM 19+ detected, additional libraries may be available")
endif()
endif()

execute_process(
COMMAND ${LLVM_CONFIG_EXECUTABLE} --cxxflags
OUTPUT_VARIABLE LLVM_DEFINITIONS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX REPLACE "[ \t]+" ";" LLVM_DEFINITIONS "${LLVM_DEFINITIONS}")

if(LLVM_FIND_COMPONENTS)
foreach(component ${LLVM_FIND_COMPONENTS})
string(TOUPPER ${component} component_upcase)
set(LLVM_${component_upcase}_FOUND TRUE)
set(LLVM_${component_upcase}_LIBRARY ${component})
endforeach()
endif()

if (LLVM_VERSION VERSION_GREATER_EQUAL "14.0")
execute_process(
COMMAND ${LLVM_CONFIG_EXECUTABLE} --has-rtti
OUTPUT_VARIABLE LLVM_HAS_RTTI
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (LLVM_HAS_RTTI)
message(STATUS " LLVM built with RTTI: ${LLVM_HAS_RTTI}")
endif()
endif()

set(LLVM_VERSION "${LLVM_VERSION}" CACHE STRING "LLVM version")

mark_as_advanced(LLVM_INCLUDE_DIRS LLVM_LIBRARY_DIRS LLVM_CFLAGS LLVM_LFLAGS LLVM_MODULE_LIBS LLVM_LIBS LLVM_DEFINITIONS)

0 comments on commit 5a86780

Please sign in to comment.