FindClang.cmake 4.29 KB
Newer Older
Kevin Funk's avatar
Kevin Funk committed
1
2
3
# Detect Clang libraries
#
# Defines the following variables:
4
#  CLANG_FOUND                 - True if Clang was found
5
6
#  CLANG_INCLUDE_DIRS          - Where to find Clang includes
#  CLANG_LIBRARY_DIRS          - Where to find Clang libraries
7
#
8
#  CLANG_CLANG_LIB             - Libclang C library
9
#
10
11
#  CLANG_CLANGFRONTEND_LIB     - Clang Frontend (C++) Library
#  CLANG_CLANGDRIVER_LIB       - Clang Driver (C++) Library
Kevin Funk's avatar
Kevin Funk committed
12
13
#  ...
#
14
15
#  CLANG_LIBS                  - All the Clang C++ libraries
#
Kevin Funk's avatar
Kevin Funk committed
16
17
18
19
# Uses the same include and library paths detected by FindLLVM.cmake
#
# See http://clang.llvm.org/docs/InternalsManual.html for full list of libraries

Kevin Funk's avatar
Kevin Funk committed
20
#=============================================================================
21
# Copyright 2014-2015 Kevin Funk <kfunk@kde.org>
Kevin Funk's avatar
Kevin Funk committed
22
23
24
25
26
27
28
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
29

Kevin Funk's avatar
Kevin Funk committed
30
31
#=============================================================================

32
set(KNOWN_VERSIONS 6.0 5.0 4.0 3.9 3.8)
33
34
35
36
37
38

foreach(version ${KNOWN_VERSIONS})
    if (LLVM_DIR OR (DEFINED Clang_FIND_VERSION AND Clang_FIND_VERSION VERSION_GREATER version))
        break()
    endif ()

Kevin Funk's avatar
Kevin Funk committed
39
    if (${Clang_FIND_REQUIRED})
40
        find_package(LLVM ${version} REQUIRED)
Kevin Funk's avatar
Kevin Funk committed
41
    else ()
42
        find_package(LLVM ${version})
Kevin Funk's avatar
Kevin Funk committed
43
    endif ()
44
endforeach()
Kevin Funk's avatar
Kevin Funk committed
45

Kevin Funk's avatar
Kevin Funk committed
46
47
set(CLANG_FOUND FALSE)

48
if (LLVM_FOUND AND LLVM_LIBRARY_DIRS)
49
50
  macro(FIND_AND_ADD_CLANG_LIB _libname_)
    string(TOUPPER ${_libname_} _prettylibname_)
51
52
53
54
55
    find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} HINTS ${LLVM_LIBRARY_DIRS} ${ARGN})
    if (CLANG_${_prettylibname_}_LIB)
      add_library(${_libname_} UNKNOWN IMPORTED GLOBAL)
      set_property(TARGET ${_libname_} PROPERTY IMPORTED_LOCATION ${CLANG_${_prettylibname_}_LIB})

56
57
58
      set(CLANG_LIBS ${CLANG_LIBS} ${CLANG_${_prettylibname_}_LIB})
    endif()
  endmacro(FIND_AND_ADD_CLANG_LIB)
59

60
  # note: On Windows there's 'libclang.dll' instead of 'clang.dll' -> search for 'libclang', too
61
  FIND_AND_ADD_CLANG_LIB(clangFrontend)
62
63
64
65

  FIND_AND_ADD_CLANG_LIB(clang NAMES clang libclang HINTS ${LLVM_LIBRARY_DIRS}) # LibClang: high-level C interface


66
67
68
69
70
  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)
71
  FIND_AND_ADD_CLANG_LIB(clangRewriteFrontend)
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  FIND_AND_ADD_CLANG_LIB(clangRewrite)
  FIND_AND_ADD_CLANG_LIB(clangAST)
  FIND_AND_ADD_CLANG_LIB(clangParse)
  FIND_AND_ADD_CLANG_LIB(clangLex)
  FIND_AND_ADD_CLANG_LIB(clangBasic)
  FIND_AND_ADD_CLANG_LIB(clangARCMigrate)
  FIND_AND_ADD_CLANG_LIB(clangEdit)
  FIND_AND_ADD_CLANG_LIB(clangFrontendTool)
  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(clangRewriteCore)
endif()
87

88
if(CLANG_LIBS OR CLANG_CLANG_LIB)
89
  set(CLANG_FOUND TRUE)
Kevin Funk's avatar
Kevin Funk committed
90
else()
91
  message(STATUS "Could not find any Clang libraries in ${LLVM_LIBRARY_DIRS}")
Kevin Funk's avatar
Kevin Funk committed
92
endif()
93
94

if(CLANG_FOUND)
95
96
  set(CLANG_LIBRARY_DIRS ${LLVM_LIBRARY_DIRS})
  set(CLANG_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS})
97
98
99
100
101
102
103

  # check whether llvm-config comes from an install prefix
  execute_process(
    COMMAND ${LLVM_CONFIG_EXECUTABLE} --src-root
    OUTPUT_VARIABLE _llvmSourceRoot
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
104
  string(FIND "${LLVM_INCLUDE_DIRS}" "${_llvmSourceRoot}" _llvmIsInstalled)
105
  if (NOT _llvmIsInstalled)
106
107
108
109
110
    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
    )
111
112
  endif()

Kevin Funk's avatar
Kevin Funk committed
113
  message(STATUS "Found Clang (LLVM version: ${LLVM_VERSION})")
114
  message(STATUS "  Include dirs:       ${CLANG_INCLUDE_DIRS}")
115
116
  message(STATUS "  Clang libraries:    ${CLANG_LIBS}")
  message(STATUS "  Libclang C library: ${CLANG_LIBCLANG_LIB}")
Kevin Funk's avatar
Kevin Funk committed
117
else()
118
  if(Clang_FIND_REQUIRED)
119
    message(FATAL_ERROR "Could NOT find Clang")
120
  endif()
Kevin Funk's avatar
Kevin Funk committed
121
endif()