FindClang.cmake 3.91 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
9
#  CLANG_LIBCLANG_LIB          - Libclang C library
#
Kevin Funk's avatar
Kevin Funk committed
10
#  CLANG_CLANGFRONTEND_LIB     - Clang Frontend Library
Kevin Funk's avatar
Kevin Funk committed
11
12
13
14
15
16
17
#  CLANG_CLANGDRIVER_LIB       - Clang Driver Library
#  ...
#
# 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
18
#=============================================================================
19
# Copyright 2014-2015 Kevin Funk <kfunk@kde.org>
Kevin Funk's avatar
Kevin Funk committed
20
21
22
23
24
25
26
#
# 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.
27

Kevin Funk's avatar
Kevin Funk committed
28
29
#=============================================================================

Milian Wolff's avatar
Milian Wolff committed
30
31
32
33
34
if (${Clang_FIND_REQUIRED})
    find_package(LLVM ${Clang_FIND_VERSION} REQUIRED)
else ()
    find_package(LLVM ${Clang_FIND_VERSION})
endif ()
Kevin Funk's avatar
Kevin Funk committed
35

Kevin Funk's avatar
Kevin Funk committed
36
37
set(CLANG_FOUND FALSE)

38
if (LLVM_FOUND AND LLVM_LIBRARY_DIRS)
39
40
  macro(FIND_AND_ADD_CLANG_LIB _libname_)
    string(TOUPPER ${_libname_} _prettylibname_)
41
    find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} HINTS ${LLVM_LIBRARY_DIRS})
42
43
44
45
    if(CLANG_${_prettylibname_}_LIB)
      set(CLANG_LIBS ${CLANG_LIBS} ${CLANG_${_prettylibname_}_LIB})
    endif()
  endmacro(FIND_AND_ADD_CLANG_LIB)
46

47
  # note: On Windows there's 'libclang.dll' instead of 'clang.dll' -> search for 'libclang', too
48
  find_library(CLANG_LIBCLANG_LIB NAMES clang libclang HINTS ${LLVM_LIBRARY_DIRS}) # LibClang: high-level C interface
49

50
51
52
53
54
55
  FIND_AND_ADD_CLANG_LIB(clangFrontend)
  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)
56
  FIND_AND_ADD_CLANG_LIB(clangRewriteFrontend)
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  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(clangRewrite)
  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(clangSema)
  FIND_AND_ADD_CLANG_LIB(clangRewriteCore)
endif()
74
75
76

if(CLANG_LIBS)
  set(CLANG_FOUND TRUE)
Kevin Funk's avatar
Kevin Funk committed
77
else()
78
  message(STATUS "Could not find any Clang libraries in ${LLVM_LIBRARY_DIRS}")
Kevin Funk's avatar
Kevin Funk committed
79
endif()
80
81

if(CLANG_FOUND)
82
83
  set(CLANG_LIBRARY_DIRS ${LLVM_LIBRARY_DIRS})
  set(CLANG_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS})
84
85
86
87
88
89
90

  # check whether llvm-config comes from an install prefix
  execute_process(
    COMMAND ${LLVM_CONFIG_EXECUTABLE} --src-root
    OUTPUT_VARIABLE _llvmSourceRoot
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
91
  string(FIND "${LLVM_INCLUDE_DIRS}" "${_llvmSourceRoot}" _llvmIsInstalled)
92
  if (NOT _llvmIsInstalled)
93
94
95
96
97
    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
    )
98
99
  endif()

Kevin Funk's avatar
Kevin Funk committed
100
  message(STATUS "Found Clang (LLVM version: ${LLVM_VERSION})")
101
  message(STATUS "  Include dirs:       ${CLANG_INCLUDE_DIRS}")
102
103
  message(STATUS "  Clang libraries:    ${CLANG_LIBS}")
  message(STATUS "  Libclang C library: ${CLANG_LIBCLANG_LIB}")
Kevin Funk's avatar
Kevin Funk committed
104
else()
105
  if(Clang_FIND_REQUIRED)
106
    message(FATAL_ERROR "Could NOT find Clang")
107
  endif()
Kevin Funk's avatar
Kevin Funk committed
108
endif()