Changeset View
Changeset View
Standalone View
Standalone View
cmake/modules/FindClang.cmake
1 | # Detect Clang libraries | 1 | # Detect Clang libraries | ||
---|---|---|---|---|---|
2 | # | 2 | # | ||
3 | # Defines the following variables: | 3 | # Defines the following variables: | ||
4 | # CLANG_FOUND - True if Clang was found | 4 | # CLANG_FOUND - True if Clang was found | ||
5 | # CLANG_INCLUDE_DIRS - Where to find Clang includes | 5 | # CLANG_INCLUDE_DIRS - Where to find Clang includes | ||
6 | # CLANG_LIBRARY_DIRS - Where to find Clang libraries | 6 | # CLANG_LIBRARY_DIRS - Where to find Clang libraries | ||
7 | # CLANG_BUILTIN_DIR - Where to find Clang builtin includes | ||||
7 | # | 8 | # | ||
8 | # CLANG_LIBCLANG_LIB - Libclang C library | 9 | # CLANG_CLANG_LIB - Libclang C library | ||
9 | # | 10 | # | ||
10 | # CLANG_CLANGFRONTEND_LIB - Clang Frontend (C++) Library | 11 | # CLANG_CLANGFRONTEND_LIB - Clang Frontend (C++) Library | ||
11 | # CLANG_CLANGDRIVER_LIB - Clang Driver (C++) Library | 12 | # CLANG_CLANGDRIVER_LIB - Clang Driver (C++) Library | ||
12 | # ... | 13 | # ... | ||
13 | # | 14 | # | ||
14 | # CLANG_LIBS - All the Clang C++ libraries | 15 | # CLANG_LIBS - All the Clang C++ libraries | ||
15 | # | 16 | # | ||
16 | # Uses the same include and library paths detected by FindLLVM.cmake | 17 | # Uses the same include and library paths detected by FindLLVM.cmake | ||
17 | # | 18 | # | ||
18 | # See http://clang.llvm.org/docs/InternalsManual.html for full list of libraries | 19 | # See http://clang.llvm.org/docs/InternalsManual.html for full list of libraries | ||
19 | 20 | | |||
20 | #============================================================================= | 21 | #============================================================================= | ||
21 | # Copyright 2014-2015 Kevin Funk <kfunk@kde.org> | 22 | # Copyright 2014-2015 Kevin Funk <kfunk@kde.org> | ||
22 | # | 23 | # | ||
23 | # Distributed under the OSI-approved BSD License (the "License"); | 24 | # Distributed under the OSI-approved BSD License (the "License"); | ||
24 | # see accompanying file Copyright.txt for details. | 25 | # see accompanying file Copyright.txt for details. | ||
25 | # | 26 | # | ||
26 | # This software is distributed WITHOUT ANY WARRANTY; without even the | 27 | # This software is distributed WITHOUT ANY WARRANTY; without even the | ||
27 | # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 28 | # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
28 | # See the License for more information. | 29 | # See the License for more information. | ||
29 | 30 | | |||
30 | #============================================================================= | 31 | #============================================================================= | ||
31 | 32 | | |||
32 | set(KNOWN_VERSIONS 6.0 5.0 4.0 3.9 3.8) | 33 | set(KNOWN_VERSIONS 8 7 6.0 5.0 4.0 3.9 3.8) | ||
33 | 34 | | |||
34 | foreach(version ${KNOWN_VERSIONS}) | 35 | foreach(version ${KNOWN_VERSIONS}) | ||
35 | if (LLVM_DIR OR (DEFINED Clang_FIND_VERSION AND Clang_FIND_VERSION VERSION_GREATER version)) | 36 | if (LLVM_DIR OR (DEFINED Clang_FIND_VERSION AND Clang_FIND_VERSION VERSION_GREATER version)) | ||
36 | break() | 37 | break() | ||
37 | endif () | 38 | endif () | ||
38 | 39 | | |||
39 | if (${Clang_FIND_REQUIRED}) | 40 | if (${Clang_FIND_REQUIRED}) | ||
40 | find_package(LLVM ${version} REQUIRED) | 41 | find_package(LLVM ${version} REQUIRED) | ||
41 | else () | 42 | else () | ||
42 | find_package(LLVM ${version}) | 43 | find_package(LLVM ${version}) | ||
43 | endif () | 44 | endif () | ||
44 | endforeach() | 45 | endforeach() | ||
45 | 46 | | |||
46 | set(CLANG_FOUND FALSE) | 47 | set(CLANG_FOUND FALSE) | ||
47 | 48 | | |||
48 | if (LLVM_FOUND AND LLVM_LIBRARY_DIRS) | 49 | if (LLVM_FOUND AND LLVM_LIBRARY_DIRS) | ||
49 | macro(FIND_AND_ADD_CLANG_LIB _libname_) | 50 | macro(FIND_AND_ADD_CLANG_LIB _libname_) | ||
50 | string(TOUPPER ${_libname_} _prettylibname_) | 51 | string(TOUPPER ${_libname_} _prettylibname_) | ||
51 | find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} HINTS ${LLVM_LIBRARY_DIRS}) | 52 | find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} HINTS ${LLVM_LIBRARY_DIRS} ${ARGN}) | ||
52 | if(CLANG_${_prettylibname_}_LIB) | 53 | if (CLANG_${_prettylibname_}_LIB) | ||
53 | set(CLANG_LIBS ${CLANG_LIBS} ${CLANG_${_prettylibname_}_LIB}) | 54 | set(CLANG_LIBS ${CLANG_LIBS} ${CLANG_${_prettylibname_}_LIB}) | ||
54 | endif() | 55 | endif() | ||
55 | endmacro(FIND_AND_ADD_CLANG_LIB) | 56 | endmacro(FIND_AND_ADD_CLANG_LIB) | ||
56 | 57 | | |||
58 | FIND_AND_ADD_CLANG_LIB(clangFrontend) | ||||
59 | | ||||
57 | # note: On Windows there's 'libclang.dll' instead of 'clang.dll' -> search for 'libclang', too | 60 | # note: On Windows there's 'libclang.dll' instead of 'clang.dll' -> search for 'libclang', too | ||
58 | find_library(CLANG_LIBCLANG_LIB NAMES clang libclang HINTS ${LLVM_LIBRARY_DIRS}) # LibClang: high-level C interface | 61 | FIND_AND_ADD_CLANG_LIB(clang NAMES clang libclang) # LibClang: high-level C interface | ||
59 | 62 | | |||
60 | FIND_AND_ADD_CLANG_LIB(clangFrontend) | | |||
61 | FIND_AND_ADD_CLANG_LIB(clangDriver) | 63 | FIND_AND_ADD_CLANG_LIB(clangDriver) | ||
62 | FIND_AND_ADD_CLANG_LIB(clangCodeGen) | 64 | FIND_AND_ADD_CLANG_LIB(clangCodeGen) | ||
63 | FIND_AND_ADD_CLANG_LIB(clangSema) | 65 | FIND_AND_ADD_CLANG_LIB(clangSema) | ||
64 | FIND_AND_ADD_CLANG_LIB(clangChecker) | 66 | FIND_AND_ADD_CLANG_LIB(clangChecker) | ||
65 | FIND_AND_ADD_CLANG_LIB(clangAnalysis) | 67 | FIND_AND_ADD_CLANG_LIB(clangAnalysis) | ||
66 | FIND_AND_ADD_CLANG_LIB(clangRewriteFrontend) | 68 | FIND_AND_ADD_CLANG_LIB(clangRewriteFrontend) | ||
67 | FIND_AND_ADD_CLANG_LIB(clangRewrite) | 69 | FIND_AND_ADD_CLANG_LIB(clangRewrite) | ||
68 | FIND_AND_ADD_CLANG_LIB(clangAST) | 70 | FIND_AND_ADD_CLANG_LIB(clangAST) | ||
69 | FIND_AND_ADD_CLANG_LIB(clangParse) | 71 | FIND_AND_ADD_CLANG_LIB(clangParse) | ||
70 | FIND_AND_ADD_CLANG_LIB(clangLex) | 72 | FIND_AND_ADD_CLANG_LIB(clangLex) | ||
71 | FIND_AND_ADD_CLANG_LIB(clangBasic) | 73 | FIND_AND_ADD_CLANG_LIB(clangBasic) | ||
72 | FIND_AND_ADD_CLANG_LIB(clangARCMigrate) | 74 | FIND_AND_ADD_CLANG_LIB(clangARCMigrate) | ||
73 | FIND_AND_ADD_CLANG_LIB(clangEdit) | 75 | FIND_AND_ADD_CLANG_LIB(clangEdit) | ||
74 | FIND_AND_ADD_CLANG_LIB(clangFrontendTool) | 76 | FIND_AND_ADD_CLANG_LIB(clangFrontendTool) | ||
75 | FIND_AND_ADD_CLANG_LIB(clangRewrite) | | |||
76 | FIND_AND_ADD_CLANG_LIB(clangSerialization) | 77 | FIND_AND_ADD_CLANG_LIB(clangSerialization) | ||
77 | FIND_AND_ADD_CLANG_LIB(clangTooling) | 78 | FIND_AND_ADD_CLANG_LIB(clangTooling) | ||
78 | FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCheckers) | 79 | FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCheckers) | ||
79 | FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCore) | 80 | FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCore) | ||
80 | FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerFrontend) | 81 | FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerFrontend) | ||
81 | FIND_AND_ADD_CLANG_LIB(clangSema) | | |||
82 | FIND_AND_ADD_CLANG_LIB(clangRewriteCore) | 82 | FIND_AND_ADD_CLANG_LIB(clangRewriteCore) | ||
83 | endif() | 83 | endif() | ||
84 | 84 | | |||
85 | if(CLANG_LIBS OR CLANG_LIBCLANG_LIB) | 85 | if(CLANG_LIBS OR CLANG_CLANG_LIB) | ||
86 | set(CLANG_FOUND TRUE) | 86 | set(CLANG_FOUND TRUE) | ||
87 | else() | 87 | else() | ||
88 | message(STATUS "Could not find any Clang libraries in ${LLVM_LIBRARY_DIRS}") | 88 | message(STATUS "Could not find any Clang libraries in ${LLVM_LIBRARY_DIRS}") | ||
89 | endif() | 89 | endif() | ||
90 | 90 | | |||
91 | if(CLANG_FOUND) | 91 | if(CLANG_FOUND) | ||
92 | set(CLANG_LIBRARY_DIRS ${LLVM_LIBRARY_DIRS}) | 92 | set(CLANG_LIBRARY_DIRS ${LLVM_LIBRARY_DIRS}) | ||
93 | set(CLANG_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS}) | 93 | set(CLANG_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS}) | ||
94 | set(CLANG_VERSION ${LLVM_VERSION}) | ||||
95 | | ||||
96 | # svn version of clang has a svn suffix "8.0.0svn" but installs the header in "8.0.0", without the suffix | ||||
97 | string(REPLACE "svn" "" CLANG_VERSION_CLEAN "${CLANG_VERSION}") | ||||
98 | | ||||
99 | find_path(CLANG_BUILTIN_DIR | ||||
100 | # cpuid.h because it is defined in ClangSupport constructor as valid clang builtin dir indicator | ||||
101 | NAMES "cpuid.h" | ||||
102 | PATHS "${CLANG_LIBRARY_DIRS}" | ||||
103 | "${CLANG_INCLUDE_DIRS}" | ||||
104 | PATH_SUFFIXES "clang/${CLANG_VERSION}/include" | ||||
105 | "../../../clang/${CLANG_VERSION}/include" | ||||
106 | "clang/${CLANG_VERSION_CLEAN}/include" | ||||
107 | "../../../clang/${CLANG_VERSION_CLEAN}/include" | ||||
108 | NO_DEFAULT_PATH | ||||
109 | ) | ||||
110 | | ||||
111 | if (NOT CLANG_BUILTIN_DIR) | ||||
112 | message(FATAL_ERROR "Could not find Clang builtin directory") | ||||
113 | endif() | ||||
114 | get_filename_component(CLANG_BUILTIN_DIR ${CLANG_BUILTIN_DIR} ABSOLUTE) | ||||
94 | 115 | | |||
95 | # check whether llvm-config comes from an install prefix | 116 | # check whether llvm-config comes from an install prefix | ||
96 | execute_process( | 117 | execute_process( | ||
97 | COMMAND ${LLVM_CONFIG_EXECUTABLE} --src-root | 118 | COMMAND ${LLVM_CONFIG_EXECUTABLE} --src-root | ||
98 | OUTPUT_VARIABLE _llvmSourceRoot | 119 | OUTPUT_VARIABLE _llvmSourceRoot | ||
99 | OUTPUT_STRIP_TRAILING_WHITESPACE | 120 | OUTPUT_STRIP_TRAILING_WHITESPACE | ||
100 | ) | 121 | ) | ||
101 | string(FIND "${LLVM_INCLUDE_DIRS}" "${_llvmSourceRoot}" _llvmIsInstalled) | 122 | string(FIND "${LLVM_INCLUDE_DIRS}" "${_llvmSourceRoot}" _llvmIsInstalled) | ||
102 | if (NOT _llvmIsInstalled) | 123 | if (NOT _llvmIsInstalled) | ||
103 | message(STATUS "Detected that llvm-config comes from a build-tree, adding more include directories for Clang") | 124 | message(STATUS "Detected that llvm-config comes from a build-tree, adding more include directories for Clang") | ||
104 | list(APPEND CLANG_INCLUDE_DIRS | 125 | list(APPEND CLANG_INCLUDE_DIRS | ||
105 | "${LLVM_INSTALL_PREFIX}/tools/clang/include" # build dir | 126 | "${LLVM_INSTALL_PREFIX}/tools/clang/include" # build dir | ||
106 | "${_llvmSourceRoot}/tools/clang/include" # source dir | 127 | "${_llvmSourceRoot}/tools/clang/include" # source dir | ||
107 | ) | 128 | ) | ||
108 | endif() | 129 | endif() | ||
109 | 130 | | |||
110 | message(STATUS "Found Clang (LLVM version: ${LLVM_VERSION})") | 131 | message(STATUS "Found Clang (LLVM version: ${CLANG_VERSION})") | ||
111 | message(STATUS " Include dirs: ${CLANG_INCLUDE_DIRS}") | 132 | message(STATUS " Include dirs: ${CLANG_INCLUDE_DIRS}") | ||
112 | message(STATUS " Clang libraries: ${CLANG_LIBS}") | 133 | message(STATUS " Clang libraries: ${CLANG_LIBS}") | ||
113 | message(STATUS " Libclang C library: ${CLANG_LIBCLANG_LIB}") | 134 | message(STATUS " Libclang C library: ${CLANG_CLANG_LIB}") | ||
135 | message(STATUS " Builtin include dir: ${CLANG_BUILTIN_DIR}") | ||||
114 | else() | 136 | else() | ||
115 | if(Clang_FIND_REQUIRED) | 137 | if(Clang_FIND_REQUIRED) | ||
116 | message(FATAL_ERROR "Could NOT find Clang") | 138 | message(FATAL_ERROR "Could NOT find Clang") | ||
117 | endif() | 139 | endif() | ||
118 | endif() | 140 | endif() |