forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindLibClang.cmake
79 lines (73 loc) · 3.25 KB
/
FindLibClang.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
if(NOT DEFINED CLANG_ROOT)
set(CLANG_ROOT $ENV{CLANG_ROOT})
endif()
find_path(CLANG_INCLUDE clang-c/Index.h
HINTS
${CLANG_ROOT}/include
${LLVM_INCLUDE_DIRS}
NO_DEFAULT_PATH)
if (NOT EXISTS ${CLANG_INCLUDE})
find_path(CLANG_INCLUDE clang-c/Index.h)
endif ()
find_path(CLANG_COMPILATION_INCLUDE clang-c/CXCompilationDatabase.h
HINTS
${CLANG_ROOT}/include
${LLVM_INCLUDE_DIRS}
NO_DEFAULT_PATH)
if (NOT EXISTS ${CLANG_COMPILATION_INCLUDE})
find_path(CLANG_COMPILATION_INCLUDE clang-c/CXCompilationDatabase.h)
endif ()
if (EXISTS ${CLANG_INCLUDE})
if ("${CLANG_ROOT}" STREQUAL "")
string(REGEX REPLACE "\\/include" "" CLANG_ROOT ${CLANG_INCLUDE})
endif()
endif()
if (EXISTS "${CLANG_INCLUDE}/clang/Basic/Version.inc")
file(READ "${CLANG_INCLUDE}/clang/Basic/Version.inc" CLANG_VERSION_DATA)
string(REGEX REPLACE ";" "\\\\;" CLANG_VERSION_DATA ${CLANG_VERSION_DATA})
string(REGEX REPLACE "\n" ";" CLANG_VERSION_DATA ${CLANG_VERSION_DATA})
foreach (line ${CLANG_VERSION_DATA})
string(REGEX REPLACE "^#define CLANG_VERSION ([0-9]+\\.[0-9]+(\\.[0-9]+)?)$" "\\1" CLANG_VERSION_STRING ${line})
if (DEFINED CLANG_VERSION_STRING)
string(REGEX REPLACE "^([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR ${CLANG_VERSION_STRING})
string(REGEX REPLACE "^[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR ${CLANG_VERSION_STRING})
if (${CLANG_VERSION_STRING} MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+")
string(REGEX REPLACE "^[0-9]+\\.[0-9]+(\\.([0-9]+))?" "\\2" CLANG_VERSION_PATCH ${CLANG_VERSION_STRING})
else ()
set(CLANG_VERSION_PATCH "")
endif ()
if (NOT ${CLANG_VERSION_MAJOR} STREQUAL "" AND NOT ${CLANG_VERSION_MINOR} STREQUAL "")
if (NOT ${CLANG_VERSION_PATCH} STREQUAL "")
set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}.${CLANG_VERSION_PATCH}")
else ()
set(CLANG_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}")
endif ()
break()
endif ()
endif ()
endforeach ()
endif ()
if ("${CLANG_VERSION}" STREQUAL "")
message(FATAL_ERROR "Unable to parse ClangVersion from ${CLANG_INCLUDE}/clang/Basic/Version.inc")
endif ()
message("Using Clang version ${CLANG_VERSION} from ${CLANG_INCLUDE}/clang-c/")
if (EXISTS "${CLANG_INCLUDE}/clang/${CLANG_VERSION}/include/limits.h")
set(CLANG_SYSTEM_INCLUDE "${CLANG_INCLUDE}/clang/${CLANG_VERSION}/include/")
else ()
set(CLANG_SYSTEM_INCLUDE ${CLANG_LIBS})
string(FIND "${CLANG_SYSTEM_INCLUDE}" ";" SEMI)
if (SEMI)
string(SUBSTRING "${CLANG_SYSTEM_INCLUDE}" 0 ${SEMI} CLANG_SYSTEM_INCLUDE)
endif ()
string(REGEX REPLACE "\\/libclang\\.[dylibsoa]+$" "" CLANG_SYSTEM_INCLUDE ${CLANG_SYSTEM_INCLUDE})
if (EXISTS "${CLANG_SYSTEM_INCLUDE}/clang/${CLANG_VERSION}/include/limits.h")
set(CLANG_SYSTEM_INCLUDE "${CLANG_SYSTEM_INCLUDE}/clang/${CLANG_VERSION}/include/")
else ()
if (EXISTS "${CLANG_ROOT}/lib/clang/${CLANG_VERSION}/include/limits.h")
set(CLANG_SYSTEM_INCLUDE "${CLANG_ROOT}/lib/clang/${CLANG_VERSION}/include/")
else ()
set(CLANG_SYSTEM_INCLUDE "")
message(FATAL_ERROR "Couldn't find limits.h in either ${CLANG_INCLUDE}/clang/${CLANG_VERSION}/include/ or ${CLANG_SYSTEM_INCLUDE}/clang/${CLANG_VERSION}/include/")
endif ()
endif ()
endif ()