1 AC_DEFUN([AX_DETECT_CLANG], [
2 AC_SUBST(CLANG_CXXFLAGS)
3 AC_SUBST(CLANG_LDFLAGS)
6 AX_SUBMODULE(clang,system,system)
7 llvm_config="llvm-config"
8 AC_CHECK_PROG([llvm_config_found], ["$llvm_config"], [yes])
9 if test "x$with_clang_prefix" != "x"; then
10 llvm_config="$with_clang_prefix/bin/llvm-config"
11 if test -x "$llvm_config"; then
15 if test "$llvm_config_found" != yes; then
16 AC_MSG_ERROR([llvm-config not found])
18 CLANG_CXXFLAGS=`$llvm_config --cxxflags | \
19 $SED -e 's/-Wcovered-switch-default//'`
20 CLANG_LDFLAGS=`$llvm_config --ldflags`
21 CLANG_VERSION=`$llvm_config --version`
22 CLANG_LIB="LLVM-$CLANG_VERSION"
24 SAVE_LDFLAGS="$LDFLAGS"
25 LDFLAGS="$CLANG_LDFLAGS $LDFLAGS"
26 AC_CHECK_LIB([$CLANG_LIB], [main], [have_lib_llvm=yes], [have_lib_llvm=no])
27 LDFLAGS="$SAVE_LDFLAGS"
29 # Use single libLLVM shared library when available.
30 # Otherwise, try and figure out all the required libraries
31 if test "$have_lib_llvm" = yes; then
32 # Construct a -R argument for libtool.
33 # This is apparently required to ensure that libpet.so
34 # keeps track of the location where libLLVM can be found.
35 CLANG_RFLAG=`echo "$CLANG_LDFLAGS" | $SED -e 's/-L/-R/g'`
36 CLANG_LIBS="-l$CLANG_LIB"
38 targets=`$llvm_config --targets-built`
39 components="$targets asmparser bitreader support mc"
40 $llvm_config --components | $GREP option > /dev/null 2> /dev/null
41 if test $? -eq 0; then
42 components="$components option"
44 CLANG_LIBS=`$llvm_config --libs $components`
46 systemlibs=`$llvm_config --system-libs 2> /dev/null | tail -1`
47 if test $? -eq 0; then
48 CLANG_LIBS="$CLANG_LIBS $systemlibs"
50 CLANG_PREFIX=`$llvm_config --prefix`
51 AC_DEFINE_UNQUOTED(CLANG_PREFIX, ["$CLANG_PREFIX"], [Clang installation prefix])
53 SAVE_CPPFLAGS="$CPPFLAGS"
54 CPPFLAGS="$CLANG_CXXFLAGS $CPPFLAGS"
56 AC_CHECK_HEADER([clang/Basic/SourceLocation.h], [],
57 [AC_ERROR([clang header file not found])])
58 AC_EGREP_HEADER([getDefaultTargetTriple], [llvm/Support/Host.h], [],
59 [AC_DEFINE([getDefaultTargetTriple], [getHostTriple],
60 [Define to getHostTriple for older versions of clang])])
61 AC_EGREP_HEADER([getExpansionLineNumber], [clang/Basic/SourceLocation.h], [],
62 [AC_DEFINE([getExpansionLineNumber], [getInstantiationLineNumber],
63 [Define to getInstantiationLineNumber for older versions of clang])])
64 AC_EGREP_HEADER([getExpansionColumnNumber], [clang/Basic/SourceLocation.h], [],
65 [AC_DEFINE([getExpansionColumnNumber], [getInstantiationColumnNumber],
66 [Define to getInstantiationColumnNumber for older versions of clang])])
67 AC_EGREP_HEADER([getExpansionLoc], [clang/Basic/SourceManager.h], [],
68 [AC_DEFINE([getExpansionLoc], [getInstantiationLoc],
69 [Define to getInstantiationLoc for older versions of clang])])
70 AC_EGREP_HEADER([DiagnosticConsumer], [clang/Basic/Diagnostic.h], [],
71 [AC_DEFINE([DiagnosticConsumer], [DiagnosticClient],
72 [Define to DiagnosticClient for older versions of clang])])
73 AC_EGREP_HEADER([DiagnosticsEngine], [clang/Basic/Diagnostic.h],
74 [AC_DEFINE([DiagnosticInfo], [Diagnostic],
75 [Define to Diagnostic for newer versions of clang])],
76 [AC_DEFINE([DiagnosticsEngine], [Diagnostic],
77 [Define to Diagnostic for older versions of clang])])
78 AC_EGREP_HEADER([ArrayRef], [clang/Driver/Driver.h],
79 [AC_DEFINE([USE_ARRAYREF], [],
80 [Define if Driver::BuildCompilation takes ArrayRef])])
81 AC_EGREP_HEADER([CXXIsProduction], [clang/Driver/Driver.h],
82 [AC_DEFINE([HAVE_CXXISPRODUCTION], [],
83 [Define if Driver constructor takes CXXIsProduction argument])])
84 AC_EGREP_HEADER([ IsProduction], [clang/Driver/Driver.h],
85 [AC_DEFINE([HAVE_ISPRODUCTION], [],
86 [Define if Driver constructor takes IsProduction argument])])
87 AC_TRY_COMPILE([#include <clang/Driver/Driver.h>], [
88 using namespace clang;
89 DiagnosticsEngine *Diags;
90 new driver::Driver("", "", "", *Diags);
91 ], [AC_DEFINE([DRIVER_CTOR_TAKES_DEFAULTIMAGENAME], [],
92 [Define if Driver constructor takes default image name])])
93 AC_EGREP_HEADER([void HandleTopLevelDecl\(], [clang/AST/ASTConsumer.h],
94 [AC_DEFINE([HandleTopLevelDeclReturn], [void],
95 [Return type of HandleTopLevelDeclReturn])
96 AC_DEFINE([HandleTopLevelDeclContinue], [],
97 [Return type of HandleTopLevelDeclReturn])],
98 [AC_DEFINE([HandleTopLevelDeclReturn], [bool],
99 [Return type of HandleTopLevelDeclReturn])
100 AC_DEFINE([HandleTopLevelDeclContinue], [true],
101 [Return type of HandleTopLevelDeclReturn])])
102 create_requires_bool=false
103 AC_EGREP_HEADER([isEnclosingLocal,], [clang/AST/Expr.h],
104 [create_requires_bool=true])
105 AC_EGREP_HEADER([RefersToEnclosingVariableOrCapture,], [clang/AST/Expr.h],
106 [create_requires_bool=true])
107 if test "$create_requires_bool" = "true"; then
108 AC_DEFINE([DECLREFEXPR_CREATE_REQUIRES_BOOL], [],
109 [Define if DeclRefExpr::Create takes
110 isEnclosingLocal/RefersToEnclosingVariableOrCapture argument])
112 AC_EGREP_HEADER([HasTemplateKWAndArgsInfo], [clang/AST/Expr.h],
113 [AC_DEFINE([DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION], [],
114 [Define if DeclRefExpr::Create takes SourceLocation argument])])
115 AC_CHECK_HEADER([clang/Basic/DiagnosticOptions.h],
116 [AC_DEFINE([HAVE_BASIC_DIAGNOSTICOPTIONS_H], [],
117 [Define if clang/Basic/DiagnosticOptions.h exists])])
118 AC_CHECK_HEADER([clang/Lex/HeaderSearchOptions.h],
119 [AC_DEFINE([HAVE_LEX_HEADERSEARCHOPTIONS_H], [],
120 [Define if clang/Lex/HeaderSearchOptions.h exists])], [],
121 [#include <clang/Basic/LLVM.h>])
122 AC_CHECK_HEADER([clang/Lex/PreprocessorOptions.h],
123 [AC_DEFINE([HAVE_LEX_PREPROCESSOROPTIONS_H], [],
124 [Define if clang/Lex/PreprocessorOptions.h exists])], [],
125 [#include <clang/Basic/LLVM.h>])
126 AC_TRY_COMPILE([#include <clang/Basic/TargetInfo.h>], [
127 using namespace clang;
128 std::shared_ptr<TargetOptions> TO;
129 DiagnosticsEngine *Diags;
130 TargetInfo::CreateTargetInfo(*Diags, TO);
131 ], [AC_DEFINE([CREATETARGETINFO_TAKES_SHARED_PTR], [],
132 [Define if TargetInfo::CreateTargetInfo takes shared_ptr])])
133 AC_TRY_COMPILE([#include <clang/Basic/TargetInfo.h>], [
134 using namespace clang;
136 DiagnosticsEngine *Diags;
137 TargetInfo::CreateTargetInfo(*Diags, TO);
138 ], [AC_DEFINE([CREATETARGETINFO_TAKES_POINTER], [],
139 [Define if TargetInfo::CreateTargetInfo takes pointer])])
140 AC_EGREP_HEADER([getLangOpts], [clang/Lex/Preprocessor.h], [],
141 [AC_DEFINE([getLangOpts], [getLangOptions],
142 [Define to getLangOptions for older versions of clang])])
143 AC_EGREP_HEADER([findLocationAfterToken], [clang/Lex/Lexer.h],
144 [AC_DEFINE([HAVE_FINDLOCATIONAFTERTOKEN], [],
145 [Define if SourceManager has findLocationAfterToken method])])
146 AC_EGREP_HEADER([translateLineCol], [clang/Basic/SourceManager.h],
147 [AC_DEFINE([HAVE_TRANSLATELINECOL], [],
148 [Define if SourceManager has translateLineCol method])])
149 AC_TRY_COMPILE([#include <clang/Frontend/CompilerInstance.h>], [
150 using namespace clang;
151 DiagnosticConsumer *client;
152 CompilerInstance *Clang;
153 Clang->createDiagnostics(client);
154 ], [], [AC_DEFINE([CREATEDIAGNOSTICS_TAKES_ARG], [],
155 [Define if CompilerInstance::createDiagnostics takes argc and argv])])
156 AC_TRY_COMPILE([#include <clang/Lex/HeaderSearchOptions.h>], [
157 using namespace clang;
158 HeaderSearchOptions HSO;
159 HSO.AddPath("", frontend::Angled, false, false);
160 ], [AC_DEFINE([ADDPATH_TAKES_4_ARGUMENTS], [],
161 [Define if HeaderSearchOptions::AddPath takes 4 arguments])])
162 AC_EGREP_HEADER([getLocWithOffset], [clang/Basic/SourceLocation.h], [],
163 [AC_DEFINE([getLocWithOffset], [getFileLocWithOffset],
164 [Define to getFileLocWithOffset for older versions of clang])])
165 AC_TRY_COMPILE([#include <clang/Frontend/CompilerInstance.h>], [
166 using namespace clang;
167 CompilerInstance *Clang;
168 Clang->createPreprocessor(TU_Complete);
169 ], [AC_DEFINE([CREATEPREPROCESSOR_TAKES_TUKIND], [],
170 [Define if CompilerInstance::createPreprocessor takes TranslationUnitKind])])
171 AC_EGREP_HEADER([DecayedType], [clang/AST/Type.h],
172 [AC_DEFINE([HAVE_DECAYEDTYPE], [], [Define if DecayedType is defined])])
173 AC_EGREP_HEADER([setMainFileID], [clang/Basic/SourceManager.h],
174 [AC_DEFINE([HAVE_SETMAINFILEID], [],
175 [Define if SourceManager has a setMainFileID method])])
176 AC_CHECK_HEADER([llvm/ADT/OwningPtr.h],
177 [AC_DEFINE([HAVE_ADT_OWNINGPTR_H], [],
178 [Define if llvm/ADT/OwningPtr.h exists])])
179 AC_EGREP_HEADER([TypeInfo getTypeInfo], [clang/AST/ASTContext.h],
180 [AC_DEFINE([GETTYPEINFORETURNSTYPEINFO], [],
181 [Define if getTypeInfo returns TypeInfo object])])
182 AC_EGREP_HEADER([TypedefNameDecl], [clang/AST/Type.h], [],
183 [AC_DEFINE([TypedefNameDecl], [TypedefDecl],
184 [Define to TypedefDecl for older versions of clang])
185 AC_DEFINE([getTypedefNameForAnonDecl], [getTypedefForAnonDecl],
186 [Define to getTypedefForAnonDecl for older versions of clang])])
187 AC_EGREP_HEADER([StmtRange], [clang/AST/StmtIterator.h],
188 [AC_DEFINE([HAVE_STMTRANGE], [],
189 [Define if StmtRange class is available])])
190 AC_EGREP_HEADER([initializeBuiltins],
191 [clang/Basic/Builtins.h], [],
192 [AC_DEFINE([initializeBuiltins], [InitializeBuiltins],
193 [Define to InitializeBuiltins for older versions of clang])])
195 CPPFLAGS="$SAVE_CPPFLAGS"
197 SAVE_LDFLAGS="$LDFLAGS"
198 LDFLAGS="$CLANG_LDFLAGS $LDFLAGS"
199 AC_SUBST(LIB_CLANG_EDIT)
200 AC_CHECK_LIB([clangEdit], [main], [LIB_CLANG_EDIT=-lclangEdit], [])
201 LDFLAGS="$SAVE_LDFLAGS"