1 AC_DEFUN([AX_DETECT_CLANG], [
2 AC_SUBST(CLANG_CXXFLAGS)
3 AC_SUBST(CLANG_LDFLAGS)
6 AX_SUBMODULE(clang,system,system)
7 if test "x$with_clang_prefix" != "x"; then
8 LLVM_CONFIG="$with_clang_prefix/bin/llvm-config"
10 AC_PATH_PROG([LLVM_CONFIG], ["llvm-config"])
11 if test -z "$LLVM_CONFIG" || test ! -x "$LLVM_CONFIG"; then
12 AC_MSG_ERROR([llvm-config not found])
14 CLANG_CXXFLAGS=`$LLVM_CONFIG --cxxflags | \
15 $SED -e 's/-Wcovered-switch-default//' \
16 -e 's/-gsplit-dwarf//' \
17 -e 's/-Wl,--no-keep-files-mapped//'`
18 CLANG_LDFLAGS=`$LLVM_CONFIG --ldflags`
19 CLANG_VERSION=`$LLVM_CONFIG --version`
20 CLANG_LIB="LLVM-$CLANG_VERSION"
22 SAVE_LDFLAGS="$LDFLAGS"
23 LDFLAGS="$CLANG_LDFLAGS $LDFLAGS"
24 AC_CHECK_LIB([$CLANG_LIB], [main], [have_lib_llvm=yes], [have_lib_llvm=no])
25 LDFLAGS="$SAVE_LDFLAGS"
27 # Use single libLLVM shared library when available.
28 # Otherwise, try and figure out all the required libraries
29 if test "$have_lib_llvm" = yes; then
30 # Construct a -R argument for libtool.
31 # This is apparently required to ensure that libpet.so
32 # keeps track of the location where libLLVM can be found.
33 CLANG_RFLAG=`echo "$CLANG_LDFLAGS" | $SED -e 's/-L/-R/g'`
34 CLANG_LIBS="-l$CLANG_LIB"
36 targets=`$LLVM_CONFIG --targets-built`
37 components="$targets asmparser bitreader support mc"
38 for c in option frontendopenmp; do
39 $LLVM_CONFIG --components | $GREP $c > /dev/null 2> /dev/null
40 if test $? -eq 0; then
41 components="$components $c"
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 -I$srcdir $CPPFLAGS"
56 AC_CHECK_HEADER([clang/Basic/SourceLocation.h], [],
57 [AC_MSG_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([ArrayRef.*CommandLineArgs],
82 [clang/Frontend/CompilerInvocation.h],
83 [AC_DEFINE([CREATE_FROM_ARGS_TAKES_ARRAYREF], [],
84 [Define if CompilerInvocation::CreateFromArgs takes
88 AC_EGREP_HEADER([getReturnType], [clang/AST/Decl.h], [],
89 [AC_DEFINE([getReturnType], [getResultType],
90 [Define to getResultType for older versions of clang])])
91 AC_EGREP_HEADER([CXXIsProduction], [clang/Driver/Driver.h],
92 [AC_DEFINE([HAVE_CXXISPRODUCTION], [],
93 [Define if Driver constructor takes CXXIsProduction argument])])
94 AC_EGREP_HEADER([ IsProduction], [clang/Driver/Driver.h],
95 [AC_DEFINE([HAVE_ISPRODUCTION], [],
96 [Define if Driver constructor takes IsProduction argument])])
97 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <clang/Driver/Driver.h>]], [[
98 using namespace clang;
99 DiagnosticsEngine *Diags;
100 new driver::Driver("", "", "", *Diags);
101 ]])], [AC_DEFINE([DRIVER_CTOR_TAKES_DEFAULTIMAGENAME], [],
102 [Define if Driver constructor takes default image name])])
103 AC_EGREP_HEADER([void HandleTopLevelDecl\(], [clang/AST/ASTConsumer.h],
104 [AC_DEFINE([HandleTopLevelDeclReturn], [void],
105 [Return type of HandleTopLevelDeclReturn])
106 AC_DEFINE([HandleTopLevelDeclContinue], [],
107 [Return type of HandleTopLevelDeclReturn])],
108 [AC_DEFINE([HandleTopLevelDeclReturn], [bool],
109 [Return type of HandleTopLevelDeclReturn])
110 AC_DEFINE([HandleTopLevelDeclContinue], [true],
111 [Return type of HandleTopLevelDeclReturn])])
112 AC_CHECK_HEADER([clang/Basic/DiagnosticOptions.h],
113 [AC_DEFINE([HAVE_BASIC_DIAGNOSTICOPTIONS_H], [],
114 [Define if clang/Basic/DiagnosticOptions.h exists])])
115 AC_CHECK_HEADER([clang/Lex/HeaderSearchOptions.h],
116 [AC_DEFINE([HAVE_LEX_HEADERSEARCHOPTIONS_H], [],
117 [Define if clang/Lex/HeaderSearchOptions.h exists])], [],
118 [#include <clang/Basic/LLVM.h>])
119 AC_CHECK_HEADER([clang/Lex/PreprocessorOptions.h],
120 [AC_DEFINE([HAVE_LEX_PREPROCESSOROPTIONS_H], [],
121 [Define if clang/Lex/PreprocessorOptions.h exists])], [],
122 [#include <clang/Basic/LLVM.h>])
123 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <clang/Basic/TargetInfo.h>]], [[
124 using namespace clang;
125 std::shared_ptr<TargetOptions> TO;
126 DiagnosticsEngine *Diags;
127 TargetInfo::CreateTargetInfo(*Diags, TO);
128 ]])], [AC_DEFINE([CREATETARGETINFO_TAKES_SHARED_PTR], [],
129 [Define if TargetInfo::CreateTargetInfo takes shared_ptr])])
130 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <clang/Basic/TargetInfo.h>]], [[
131 using namespace clang;
133 DiagnosticsEngine *Diags;
134 TargetInfo::CreateTargetInfo(*Diags, TO);
135 ]])], [AC_DEFINE([CREATETARGETINFO_TAKES_POINTER], [],
136 [Define if TargetInfo::CreateTargetInfo takes pointer])])
137 AC_EGREP_HEADER([getLangOpts], [clang/Lex/Preprocessor.h], [],
138 [AC_DEFINE([getLangOpts], [getLangOptions],
139 [Define to getLangOptions for older versions of clang])])
140 AC_EGREP_HEADER([findLocationAfterToken], [clang/Lex/Lexer.h],
141 [AC_DEFINE([HAVE_FINDLOCATIONAFTERTOKEN], [],
142 [Define if SourceManager has findLocationAfterToken method])])
143 AC_EGREP_HEADER([translateLineCol], [clang/Basic/SourceManager.h],
144 [AC_DEFINE([HAVE_TRANSLATELINECOL], [],
145 [Define if SourceManager has translateLineCol method])])
146 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
147 [[#include <clang/Frontend/CompilerInstance.h>]], [[
148 using namespace clang;
149 DiagnosticConsumer *client;
150 CompilerInstance *Clang;
151 Clang->createDiagnostics(client);
152 ]])], [], [AC_DEFINE([CREATEDIAGNOSTICS_TAKES_ARG], [],
153 [Define if CompilerInstance::createDiagnostics takes argc and argv])])
154 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
155 [[#include <clang/Lex/HeaderSearchOptions.h>]], [[
156 using namespace clang;
157 HeaderSearchOptions HSO;
158 HSO.AddPath("", frontend::Angled, false, false);
159 ]])], [AC_DEFINE([ADDPATH_TAKES_4_ARGUMENTS], [],
160 [Define if HeaderSearchOptions::AddPath takes 4 arguments])])
161 AC_EGREP_HEADER([getLocWithOffset], [clang/Basic/SourceLocation.h], [],
162 [AC_DEFINE([getLocWithOffset], [getFileLocWithOffset],
163 [Define to getFileLocWithOffset for older versions of clang])])
164 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
165 [[#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])])
194 AC_EGREP_HEADER([IK_C], [clang/Frontend/FrontendOptions.h], [],
195 [AC_CHECK_HEADER([clang/Basic/LangStandard.h],
196 [IK_C=Language::C], [IK_C=InputKind::C])
197 AC_DEFINE_UNQUOTED([IK_C], [$IK_C],
198 [Define to Language::C or InputKind::C for newer versions of clang])
200 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
201 #include <clang/Basic/TargetOptions.h>
202 #include <clang/Lex/PreprocessorOptions.h>
203 #include <clang/Frontend/CompilerInstance.h>
205 #include "set_lang_defaults_arg4.h"
207 using namespace clang;
208 CompilerInstance *Clang;
210 llvm::Triple T(TO.Triple);
211 PreprocessorOptions PO;
212 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
213 T, setLangDefaultsArg4(PO),
214 LangStandard::lang_unspecified);
215 ]])], [AC_DEFINE([SETLANGDEFAULTS_TAKES_5_ARGUMENTS], [],
216 [Define if CompilerInvocation::setLangDefaults takes 5 arguments])])
217 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
218 #include <clang/Frontend/CompilerInstance.h>
219 #include <clang/Frontend/CompilerInvocation.h>
221 using namespace clang;
222 CompilerInvocation *invocation;
223 CompilerInstance *Clang;
224 Clang->setInvocation(std::make_shared<CompilerInvocation>(*invocation));
225 ]])], [AC_DEFINE([SETINVOCATION_TAKES_SHARED_PTR], [],
226 [Defined if CompilerInstance::setInvocation takes a shared_ptr])])
227 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
228 #include <clang/AST/Decl.h>
230 clang::FunctionDecl *fd;
234 [AC_DEFINE([HAVE_BEGIN_END_LOC], [],
235 [Define if getBeginLoc and getEndLoc should be used])])
236 AC_CHECK_HEADER([llvm/Option/Arg.h],
237 [AC_DEFINE([HAVE_LLVM_OPTION_ARG_H], [],
238 [Define if llvm/Option/Arg.h exists])])
239 AC_EGREP_HEADER([PragmaIntroducer ],
240 [clang/Lex/Pragma.h], [],
241 [AC_DEFINE([PragmaIntroducer], [PragmaIntroducerKind],
242 [Define to PragmaIntroducerKind for older versions of clang])])
243 AC_CHECK_HEADER([clang/Basic/LangStandard.h],
244 [AC_DEFINE([HAVE_CLANG_BASIC_LANGSTANDARD_H], [],
245 [Define if clang/Basic/LangStandard.h exists])])
247 CPPFLAGS="$SAVE_CPPFLAGS"
249 SAVE_LDFLAGS="$LDFLAGS"
250 LDFLAGS="$CLANG_LDFLAGS $LDFLAGS"
252 # Use single libclang-cpp shared library when available.
253 # Otherwise, use a selection of clang libraries that appears to work.
254 AC_CHECK_LIB([clang-cpp], [main], [have_lib_clang=yes], [have_lib_clang=no])
255 if test "$have_lib_clang" = yes; then
256 CLANG_LIBS="-lclang-cpp $CLANG_LIBS"
258 CLANG_LIBS="-lclangBasic -lclangDriver $CLANG_LIBS"
259 CLANG_LIBS="-lclangAnalysis -lclangAST -lclangLex $CLANG_LIBS"
260 LDFLAGS="$CLANG_LDFLAGS $CLANG_LIBS $SAVE_LDFLAGS"
261 AC_CHECK_LIB([clangEdit], [main], [LIB_CLANG_EDIT=-lclangEdit], [])
262 CLANG_LIBS="$LIB_CLANG_EDIT $CLANG_LIBS"
263 CLANG_LIBS="-lclangParse -lclangSema $CLANG_LIBS"
264 CLANG_LIBS="-lclangFrontend -lclangSerialization $CLANG_LIBS"
267 LDFLAGS="$SAVE_LDFLAGS"