Commit 49391d8a authored by Kevin Funk's avatar Kevin Funk
Browse files

CMake: Modernize Clang/LLVM find scripts

Changes:
- LLVM_INCLUDE_DIR -> LLVM_INCLUDE_DIRS
- LLVM_LIBRARY_DIR -> LLVM_LIBRARY_DIRS
- CLANG_INCLUDE_DIR -> CLANG_INCLUDE_DIRS
- CLANG_LIBRARY_DIR -> CLANG_LIBRARY_DIRS
- Introduce LLVM_INSTALL_PREFIX
- Fix compilation with self-built non-installed LLVM/Clang
parent 1e5808b8
......@@ -2,8 +2,8 @@
#
# Defines the following variables:
# CLANG_FOUND - True if Clang was found
# CLANG_INCLUDE_DIR - Where to find Clang includes
# CLANG_LIBRARY_DIR - Where to find Clang libraries
# CLANG_INCLUDE_DIRS - Where to find Clang includes
# CLANG_LIBRARY_DIRS - Where to find Clang libraries
#
# CLANG_LIBCLANG_LIB - Libclang C library
#
......@@ -35,17 +35,17 @@ endif ()
set(CLANG_FOUND FALSE)
if (LLVM_FOUND AND LLVM_LIBRARY_DIR)
if (LLVM_FOUND AND LLVM_LIBRARY_DIRS)
macro(FIND_AND_ADD_CLANG_LIB _libname_)
string(TOUPPER ${_libname_} _prettylibname_)
find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} HINTS ${LLVM_LIBRARY_DIR})
find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} HINTS ${LLVM_LIBRARY_DIRS})
if(CLANG_${_prettylibname_}_LIB)
set(CLANG_LIBS ${CLANG_LIBS} ${CLANG_${_prettylibname_}_LIB})
endif()
endmacro(FIND_AND_ADD_CLANG_LIB)
# 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_DIR}) # LibClang: high-level C interface
find_library(CLANG_LIBCLANG_LIB NAMES clang libclang HINTS ${LLVM_LIBRARY_DIRS}) # LibClang: high-level C interface
FIND_AND_ADD_CLANG_LIB(clangFrontend)
FIND_AND_ADD_CLANG_LIB(clangDriver)
......@@ -75,12 +75,12 @@ endif()
if(CLANG_LIBS)
set(CLANG_FOUND TRUE)
else()
message(STATUS "Could not find any Clang libraries in ${LLVM_LIBRARY_DIR}")
message(STATUS "Could not find any Clang libraries in ${LLVM_LIBRARY_DIRS}")
endif()
if(CLANG_FOUND)
set(CLANG_LIBRARY_DIR ${LLVM_LIBRARY_DIR})
set(CLANG_INCLUDE_DIR ${LLVM_INCLUDE_DIR})
set(CLANG_LIBRARY_DIRS ${LLVM_LIBRARY_DIRS})
set(CLANG_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS})
# check whether llvm-config comes from an install prefix
execute_process(
......@@ -88,14 +88,17 @@ if(CLANG_FOUND)
OUTPUT_VARIABLE _llvmSourceRoot
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(FIND "${LLVM_INCLUDE_DIR}" "${_llvmSourceRoot}" _llvmIsInstalled)
string(FIND "${LLVM_INCLUDE_DIRS}" "${_llvmSourceRoot}" _llvmIsInstalled)
if (NOT _llvmIsInstalled)
message(STATUS "Detected that llvm-config comes from a build-tree, adding includes from source dir")
list(APPEND CLANG_INCLUDE_DIR "${_llvmSourceRoot}/tools/clang/include")
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
"${_llvmSourceRoot}/tools/clang/include" # source dir
)
endif()
message(STATUS "Found Clang (LLVM version: ${LLVM_VERSION})")
message(STATUS " Include dirs: ${CLANG_INCLUDE_DIR}")
message(STATUS " Include dirs: ${CLANG_INCLUDE_DIRS}")
message(STATUS " Clang libraries: ${CLANG_LIBS}")
message(STATUS " Libclang C library: ${CLANG_LIBCLANG_LIB}")
else()
......
# Find the native LLVM includes and libraries
#
# Defines the following variables
# LLVM_INCLUDE_DIR - where to find llvm include files
# LLVM_LIBRARY_DIR - where to find llvm libs
# LLVM_CFLAGS - llvm compiler flags
# LLVM_LFLAGS - llvm linker flags
# LLVM_MODULE_LIBS - list of llvm libs for working with modules.
# LLVM_LIBS - list of all llvm libs.
# LLVM_FOUND - True if llvm found.
# LLVM_VERSION - Version string ("llvm-config --version")
# LLVM_INCLUDE_DIRS - where to find llvm include files
# LLVM_LIBRARY_DIRS - where to find llvm libs
# LLVM_CFLAGS - llvm compiler flags
# LLVM_LFLAGS - llvm linker flags
# LLVM_MODULE_LIBS - list of llvm libs for working with modules.
# LLVM_INSTALL_PREFIX - LLVM installation prefix
# LLVM_FOUND - True if llvm found.
# LLVM_VERSION - Version string ("llvm-config --version")
#
# This module reads hints about search locations from variables
# LLVM_ROOT - Preferred LLVM installation prefix (containing bin/, lib/, ...)
# LLVM_ROOT - Preferred LLVM installation prefix (containing bin/, lib/, ...)
#
# Note: One may specify these as environment variables if they are not specified as
# CMake variables or cache entries.
......@@ -66,7 +66,6 @@ if (LLVM_CONFIG_EXECUTABLE)
elseif (LLVM_FIND_VERSION VERSION_GREATER LLVM_VERSION)
set(_LLVM_ERROR_MESSAGE "LLVM version too old: ${LLVM_VERSION}")
else()
message(STATUS "Found LLVM (version: ${LLVM_VERSION}): (using ${LLVM_CONFIG_EXECUTABLE})")
set(LLVM_FOUND TRUE)
endif()
else()
......@@ -76,13 +75,13 @@ endif()
if (LLVM_FOUND)
execute_process(
COMMAND ${LLVM_CONFIG_EXECUTABLE} --includedir
OUTPUT_VARIABLE LLVM_INCLUDE_DIR
OUTPUT_VARIABLE LLVM_INCLUDE_DIRS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND ${LLVM_CONFIG_EXECUTABLE} --libdir
OUTPUT_VARIABLE LLVM_LIBRARY_DIR
OUTPUT_VARIABLE LLVM_LIBRARY_DIRS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
......@@ -109,6 +108,23 @@ if (LLVM_FOUND)
OUTPUT_VARIABLE LLVM_LIBS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND ${LLVM_CONFIG_EXECUTABLE} --prefix
OUTPUT_VARIABLE LLVM_INSTALL_PREFIX
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# potentially add include dir from binary dir for non-installed LLVM
execute_process(
COMMAND ${LLVM_CONFIG_EXECUTABLE} --src-root
OUTPUT_VARIABLE _llvmSourceRoot
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(FIND "${LLVM_INCLUDE_DIRS}" "${_llvmSourceRoot}" _llvmIsInstalled)
if (NOT _llvmIsInstalled)
list(APPEND LLVM_INCLUDE_DIRS "${LLVM_INSTALL_PREFIX}/include")
endif()
endif()
if (LLVM_FIND_REQUIRED AND NOT LLVM_FOUND)
......@@ -116,3 +132,9 @@ if (LLVM_FIND_REQUIRED AND NOT LLVM_FOUND)
elseif(_LLVM_ERROR_MESSAGE)
message(STATUS "Could not find LLVM: ${_LLVM_ERROR_MESSAGE}")
endif()
if (LLVM_FOUND)
message(STATUS "Found LLVM (version: ${LLVM_VERSION}): (using ${LLVM_CONFIG_EXECUTABLE})")
message(STATUS " Include dirs: ${LLVM_INCLUDE_DIRS}")
message(STATUS " LLVM libraries: ${LLVM_LIBS}")
endif()
find_package(Clang 3.5 REQUIRED)
add_definitions(${LLVM_CFLAGS})
include_directories(${CLANG_INCLUDE_DIR})
include_directories(${CLANG_INCLUDE_DIRS})
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/version.h.cmake"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment