Skip to content

Commit

Permalink
Fix detection of clang libraries for clang-9+ (#923)
Browse files Browse the repository at this point in the history
In some installation of recent clang libraries (probably 9 and later), the libraries are shipped as a single library on some platforms.
  • Loading branch information
havogt authored May 6, 2020
1 parent 64e1fe2 commit e65d66b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 52 deletions.
2 changes: 1 addition & 1 deletion docker/cuda10.1-ubuntu18.04.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN apt update && apt install -y --no-install-recommends \
build-essential openssh-client git \
kitware-archive-keyring cmake ninja-build \
gcc-8 g++-8 \
llvm-9-dev libclang-9-dev \
llvm-9-dev libclang-9-dev libclang-cpp9 \
python3 libpython3-dev python3-pip python3-setuptools python3-wheel \
libboost-dev && apt clean
RUN python3 -m pip install --upgrade pip
Expand Down
2 changes: 1 addition & 1 deletion docker/ubuntu19.10.dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM ubuntu:eoan
RUN apt update && apt install -y --no-install-recommends \
build-essential ninja-build cmake git openssh-client curl \
llvm-9-dev libclang-9-dev \
llvm-9-dev libclang-9-dev libclang-cpp9 \
python3 python3-pip libpython3-dev \
python3-setuptools python3-wheel \
libboost-dev && apt clean
Expand Down
114 changes: 64 additions & 50 deletions gtclang/cmake/FindClang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if(LLVM_FOUND)
OUTPUT_VARIABLE llvm_install_prefix
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(CLANG_RESSOURCE_INCLUDE_PATH "${llvm_install_prefix}/lib/clang/${LLVM_VERSION}/include")
set(CLANG_RESSOURCE_INCLUDE_PATH "${llvm_install_prefix}/lib/clang/${LLVM_VERSION}/include")

# potentially add include dir from binary dir for non-installed LLVM
execute_process(
Expand All @@ -49,65 +49,79 @@ set(CLANG_RESSOURCE_INCLUDE_PATH "${llvm_install_prefix}/lib/clang/${LLVM_VERSIO
message(STATUS
"Detected that llvm-config comes from a build-tree, adding more include directories for Clang")
list(APPEND CLANG_INCLUDE_DIRS
"${llvm_install_prefix}/tools/clang/include" # build dir
"${llvm_install_prefix}/tools/clang/include" # build dir
"${llvm_source_root}/tools/clang/include" # source dir
)
endif()

# TODO test this set with all supported LLVM versions
# 6: done
# 7: done
# 8: done
# 9
# 10 (master):
set(clang_libnames
clangFrontend
clangARCMigrate
clangASTMatchers
clangAnalysis
clangCodeGen
clangDriver
clangEdit
clangFormat
clangFrontendTool
clangIndex
clangParse
clangRewrite
clangRewriteFrontend
clangSema
clangAnalysis
clangSerialization
clangStaticAnalyzerCheckers
clangStaticAnalyzerCore
clangTooling
clangToolingCore
clangAST
clangLex
clangBasic
)
if(${LLVM_VERSION} VERSION_GREATER_EQUAL 7.0.0)
list(APPEND clang_libnames clangToolingInclusions)
endif()

add_library(Clang INTERFACE IMPORTED GLOBAL)
add_library(Clang::Clang ALIAS Clang)
target_include_directories(Clang INTERFACE ${CLANG_INCLUDE_DIRS})
target_compile_features(Clang INTERFACE cxx_std_11)
foreach(_lib ${clang_libnames})
find_library(${_lib}_LIBRARY ${_lib}
HINTS ${CLANG_LIBRARY_DIRS} ${LLVM_ROOT}
PATH_SUFFIXES lib
)
mark_as_advanced(${_lib}_LIBRARY)
list(APPEND CLANG_LIBS ${${_lib}_LIBRARY})
if(${_lib}_LIBRARY)
target_link_libraries(Clang INTERFACE ${${_lib}_LIBRARY})
else()
message(FATAL_ERROR "Could not find ${_lib} library: Try setting CLANG_LIBRARY_DIRS or LLVM_ROOT")

# Clang-9 includes a single-library version (libclang-cpp9 on Debian systems)
find_library(single_clang_lib clang-cpp
PATHS ${LLVM_LIBRARY_DIRS}
NO_DEFAULT_PATH
NO_PACKAGE_ROOT_PATH
NO_CMAKE_PATH
NO_CMAKE_ENVIRONMENT_PATH
NO_SYSTEM_ENVIRONMENT_PATH
NO_CMAKE_SYSTEM_PATH
)
mark_as_advanced(single_clang_lib)

if(single_clang_lib)
# Single library installation is available
list(APPEND CLANG_LIBS ${single_clang_lib})
target_link_libraries(Clang INTERFACE ${single_clang_lib})
else()
set(clang_libnames
clangFrontend
clangARCMigrate
clangASTMatchers
clangAnalysis
clangCodeGen
clangDriver
clangEdit
clangFormat
clangFrontendTool
clangIndex
clangParse
clangRewrite
clangRewriteFrontend
clangSema
clangAnalysis
clangSerialization
clangStaticAnalyzerCheckers
clangStaticAnalyzerCore
clangTooling
clangToolingCore
clangAST
clangLex
clangBasic
)
if(${LLVM_VERSION} VERSION_GREATER_EQUAL 7.0.0)
list(APPEND clang_libnames clangToolingInclusions)
endif()
endforeach()

foreach(_lib ${clang_libnames})
find_library(${_lib}_LIBRARY ${_lib}
HINTS ${CLANG_LIBRARY_DIRS} ${LLVM_ROOT}
PATH_SUFFIXES lib
)
mark_as_advanced(${_lib}_LIBRARY)
list(APPEND CLANG_LIBS ${${_lib}_LIBRARY})
if(${_lib}_LIBRARY)
target_link_libraries(Clang INTERFACE ${${_lib}_LIBRARY})
else()
message(FATAL_ERROR "Could not find ${_lib} library: Try setting CLANG_LIBRARY_DIRS or LLVM_ROOT")
endif()
endforeach()
endif()

else()
message(FATAL_ERROR "Could NOT find Clang")
message(FATAL_ERROR "LLVM NOT Found")
endif()

include(FindPackageHandleStandardArgs)
Expand Down

0 comments on commit e65d66b

Please sign in to comment.