2018-03-17 19:03:41 +00:00
|
|
|
#.rst
|
|
|
|
# FindClang
|
|
|
|
# ---------
|
|
|
|
#
|
2018-04-06 07:26:53 +00:00
|
|
|
# Find Clang and LLVM libraries required by ccls
|
2018-03-17 19:03:41 +00:00
|
|
|
#
|
|
|
|
# Results are reported in the following variables::
|
|
|
|
#
|
|
|
|
# Clang_FOUND - True if headers and requested libraries were found
|
|
|
|
# Clang_EXECUTABLE - Clang executable
|
2018-04-06 07:26:53 +00:00
|
|
|
# Clang_FORMAT - Clang-format executable
|
2018-03-17 19:03:41 +00:00
|
|
|
# Clang_RESOURCE_DIR - Clang resource directory
|
|
|
|
# Clang_VERSION - Clang version as reported by Clang executable
|
|
|
|
#
|
|
|
|
# The following :prop_tgt:`IMPORTED` targets are also defined::
|
|
|
|
#
|
|
|
|
# Clang::Clang - Target for all required Clang libraries and headers
|
|
|
|
#
|
|
|
|
# This module reads hints about which libraries to look for and where to find
|
|
|
|
# them from the following variables::
|
|
|
|
#
|
|
|
|
# CLANG_CXX - Search for and add Clang C++ libraries
|
|
|
|
# CLANG_ROOT - If set, only look for Clang components in CLANG_ROOT
|
|
|
|
#
|
|
|
|
# Example to link against Clang target::
|
|
|
|
#
|
|
|
|
# target_link_libraries(<target> PRIVATE Clang::Clang)
|
|
|
|
|
|
|
|
### Definitions
|
|
|
|
|
|
|
|
# Wrapper macro's around the find_* macro's from CMake that only search in
|
|
|
|
# CLANG_ROOT if it is defined
|
|
|
|
|
|
|
|
macro(_Clang_find_library VAR NAME)
|
|
|
|
# Windows needs lib prefix
|
2018-04-06 07:26:53 +00:00
|
|
|
if (CLANG_ROOT)
|
|
|
|
find_library(${VAR} NAMES ${NAME} lib${NAME}
|
2018-03-17 19:03:41 +00:00
|
|
|
NO_DEFAULT_PATH PATHS ${CLANG_ROOT} PATH_SUFFIXES lib)
|
|
|
|
else()
|
|
|
|
find_library(${VAR} NAMES ${NAME} lib${NAME})
|
|
|
|
endif()
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
macro(_Clang_find_path VAR INCLUDE_FILE)
|
|
|
|
if (CLANG_ROOT)
|
2018-04-06 07:26:53 +00:00
|
|
|
find_path(${VAR} ${INCLUDE_FILE}
|
2018-03-17 19:03:41 +00:00
|
|
|
NO_DEFAULT_PATH PATHS ${CLANG_ROOT} PATH_SUFFIXES include)
|
|
|
|
else()
|
|
|
|
find_path(${VAR} ${INCLUDE_FILE})
|
|
|
|
endif()
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
macro(_Clang_find_program VAR NAME)
|
|
|
|
if (CLANG_ROOT)
|
2018-04-06 07:26:53 +00:00
|
|
|
find_program(${VAR} ${NAME}
|
2018-03-17 19:03:41 +00:00
|
|
|
NO_DEFAULT_PATH PATHS ${CLANG_ROOT} PATH_SUFFIXES bin)
|
|
|
|
else()
|
|
|
|
find_program(${VAR} ${NAME})
|
|
|
|
endif()
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
# Macro to avoid duplicating logic for each Clang C++ library
|
|
|
|
macro(_Clang_find_and_add_cxx_lib NAME INCLUDE_FILE)
|
|
|
|
# Find library
|
|
|
|
_Clang_find_library(Clang_${NAME}_LIBRARY ${NAME})
|
|
|
|
list(APPEND _Clang_REQUIRED_VARS Clang_${NAME}_LIBRARY)
|
|
|
|
list(APPEND _Clang_CXX_LIBRARIES ${Clang_${NAME}_LIBRARY})
|
|
|
|
|
|
|
|
# Find corresponding include directory
|
|
|
|
_Clang_find_path(Clang_${NAME}_INCLUDE_DIR ${INCLUDE_FILE})
|
|
|
|
list(APPEND _Clang_REQUIRED_VARS Clang_${NAME}_INCLUDE_DIR)
|
|
|
|
list(APPEND _Clang_CXX_INCLUDE_DIRS ${Clang_${NAME}_INCLUDE_DIR})
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
### Start
|
|
|
|
|
2018-04-06 07:26:53 +00:00
|
|
|
set(_Clang_REQUIRED_VARS Clang_LIBRARY Clang_INCLUDE_DIR Clang_EXECUTABLE
|
2018-03-17 19:03:41 +00:00
|
|
|
Clang_RESOURCE_DIR Clang_VERSION)
|
|
|
|
|
|
|
|
_Clang_find_library(Clang_LIBRARY clang)
|
|
|
|
_Clang_find_path(Clang_INCLUDE_DIR clang-c/Index.h)
|
|
|
|
|
|
|
|
if(CLANG_CXX)
|
2018-04-06 07:26:53 +00:00
|
|
|
# The order is derived by topological sorting LINK_LIBS in
|
2018-03-17 19:03:41 +00:00
|
|
|
# clang/lib/*/CMakeLists.txt
|
|
|
|
_Clang_find_and_add_cxx_lib(clangFormat clang/Format/Format.h)
|
|
|
|
_Clang_find_and_add_cxx_lib(clangToolingCore clang/Tooling/Core/Diagnostic.h)
|
|
|
|
_Clang_find_and_add_cxx_lib(clangRewrite clang/Rewrite/Core/Rewriter.h)
|
|
|
|
_Clang_find_and_add_cxx_lib(clangAST clang/AST/AST.h)
|
|
|
|
_Clang_find_and_add_cxx_lib(clangLex clang/Lex/Lexer.h)
|
|
|
|
_Clang_find_and_add_cxx_lib(clangBasic clang/Basic/ABI.h)
|
|
|
|
|
|
|
|
# The order is derived from llvm-config --libs core
|
|
|
|
_Clang_find_and_add_cxx_lib(LLVMCore llvm/Pass.h)
|
|
|
|
_Clang_find_and_add_cxx_lib(LLVMBinaryFormat llvm/BinaryFormat/Dwarf.h)
|
|
|
|
_Clang_find_and_add_cxx_lib(LLVMSupport llvm/Support/Error.h)
|
|
|
|
_Clang_find_and_add_cxx_lib(LLVMDemangle llvm/Demangle/Demangle.h)
|
|
|
|
endif()
|
|
|
|
|
2018-04-06 07:26:53 +00:00
|
|
|
_Clang_find_program(Clang_FORMAT clang-format)
|
2018-03-17 19:03:41 +00:00
|
|
|
_Clang_find_program(Clang_EXECUTABLE clang)
|
|
|
|
if(Clang_EXECUTABLE)
|
|
|
|
# Find Clang resource directory with Clang executable
|
2018-04-06 07:26:53 +00:00
|
|
|
execute_process(COMMAND ${Clang_EXECUTABLE} -print-resource-dir
|
|
|
|
RESULT_VARIABLE _Clang_FIND_RESOURCE_DIR_RESULT
|
2018-03-26 14:24:05 +00:00
|
|
|
OUTPUT_VARIABLE Clang_RESOURCE_DIR
|
2018-04-06 07:26:53 +00:00
|
|
|
ERROR_VARIABLE _Clang_FIND_RESOURCE_DIR_ERROR
|
2018-03-26 14:24:05 +00:00
|
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
2018-03-17 19:03:41 +00:00
|
|
|
|
2018-04-06 07:26:53 +00:00
|
|
|
if(_Clang_FIND_RESOURCE_DIR_RESULT)
|
|
|
|
message(FATAL_ERROR "Error retrieving Clang resource directory with Clang \
|
|
|
|
executable. Output:\n ${_Clang_FIND_RESOURCE_DIR_ERROR}")
|
|
|
|
endif()
|
|
|
|
|
2018-03-17 19:03:41 +00:00
|
|
|
# Find Clang version
|
|
|
|
set(_Clang_VERSION_REGEX "([0-9]+)\\.([0-9]+)\\.([0-9]+)")
|
2018-04-06 07:26:53 +00:00
|
|
|
execute_process(COMMAND ${Clang_EXECUTABLE} --version
|
2018-03-17 19:03:41 +00:00
|
|
|
OUTPUT_VARIABLE Clang_VERSION)
|
|
|
|
string(REGEX MATCH ${_Clang_VERSION_REGEX} Clang_VERSION ${Clang_VERSION})
|
|
|
|
endif()
|
|
|
|
|
|
|
|
include(FindPackageHandleStandardArgs)
|
|
|
|
find_package_handle_standard_args(Clang
|
|
|
|
FOUND_VAR Clang_FOUND
|
|
|
|
REQUIRED_VARS ${_Clang_REQUIRED_VARS}
|
|
|
|
VERSION_VAR Clang_VERSION
|
|
|
|
)
|
|
|
|
|
|
|
|
if(Clang_FOUND AND NOT TARGET Clang::Clang)
|
|
|
|
set(_Clang_LIBRARIES ${Clang_LIBRARY} ${_Clang_CXX_LIBRARIES})
|
|
|
|
set(_Clang_INCLUDE_DIRS ${Clang_INCLUDE_DIR} ${_Clang_CXX_INCLUDE_DIRS})
|
|
|
|
|
|
|
|
add_library(Clang::Clang INTERFACE IMPORTED)
|
2018-04-06 07:26:53 +00:00
|
|
|
set_property(TARGET Clang::Clang PROPERTY
|
2018-03-17 19:03:41 +00:00
|
|
|
INTERFACE_LINK_LIBRARIES ${_Clang_LIBRARIES})
|
2018-04-06 07:26:53 +00:00
|
|
|
set_property(TARGET Clang::Clang PROPERTY
|
|
|
|
INTERFACE_INCLUDE_DIRECTORIES ${_Clang_INCLUDE_DIRS})
|
2018-03-17 19:03:41 +00:00
|
|
|
endif()
|