1AC_DEFUN([AX_DETECT_CLANG], [
2AC_SUBST(CLANG_CXXFLAGS)
3AC_SUBST(CLANG_LDFLAGS)
4AC_SUBST(CLANG_RFLAG)
5AC_SUBST(CLANG_LIBS)
6AC_PROG_GREP
7AC_PROG_SED
8if test "x$with_clang_prefix" != "x"; then
9	LLVM_CONFIG="$with_clang_prefix/bin/llvm-config"
10fi
11AC_PATH_PROG([LLVM_CONFIG], ["llvm-config"])
12if test -z "$LLVM_CONFIG" || test ! -x "$LLVM_CONFIG"; then
13	AC_MSG_ERROR([llvm-config not found])
14fi
15CLANG_CXXFLAGS=`$LLVM_CONFIG --cxxflags | \
16	$SED -e 's/-Wcovered-switch-default//;s/-gsplit-dwarf//'`
17CLANG_LDFLAGS=`$LLVM_CONFIG --ldflags`
18# Construct a -R argument for libtool.
19# This is needed in case some of the clang libraries are shared libraries.
20CLANG_RFLAG=`echo "$CLANG_LDFLAGS" | $SED -e 's/-L/-R/g'`
21targets=`$LLVM_CONFIG --targets-built`
22components="$targets asmparser bitreader support mc"
23# Link in option and frontendopenmp components when available
24# since they may be used by the clang libraries.
25for c in option frontendopenmp; do
26	$LLVM_CONFIG --components | $GREP $c > /dev/null 2> /dev/null
27	if test $? -eq 0; then
28		components="$components $c"
29	fi
30done
31CLANG_LIBS=`$LLVM_CONFIG --libs $components`
32systemlibs=`$LLVM_CONFIG --system-libs 2> /dev/null | tail -1`
33if test $? -eq 0; then
34	CLANG_LIBS="$CLANG_LIBS $systemlibs"
35fi
36CLANG_PREFIX=`$LLVM_CONFIG --prefix`
37AC_DEFINE_UNQUOTED(CLANG_PREFIX, ["$CLANG_PREFIX"], [Clang installation prefix])
38
39# If $CLANG_PREFIX/bin/clang cannot find the standard include files,
40# then see if setting sysroot to `xcode-select -p`/SDKs/MacOSX.sdk helps.
41# This may be required on some versions of OS X since they lack /usr/include.
42# If so, set CLANG_SYSROOT accordingly.
43SAVE_CC="$CC"
44CC="$CLANG_PREFIX/bin/clang"
45AC_MSG_CHECKING(
46	[whether $CLANG_PREFIX/bin/clang can find standard include files])
47found_header=no
48AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>]], [[]])],
49	[found_header=yes])
50AC_MSG_RESULT([$found_header])
51if test "x$found_header" != "xyes"; then
52	AC_CHECK_PROG(XCODE_SELECT, xcode-select, xcode-select, [])
53	if test -z "$XCODE_SELECT"; then
54		AC_MSG_ERROR([Cannot find xcode-select])
55	fi
56	sysroot=`$XCODE_SELECT -p`/SDKs/MacOSX.sdk
57	SAVE_CPPFLAGS="$CPPFLAGS"
58	CPPFLAGS="$CPPFLAGS -isysroot $sysroot"
59	AC_MSG_CHECKING(
60		[whether standard include files can be found with sysroot set])
61	found_header=no
62	AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>]], [[]])],
63		[found_header=yes])
64	AC_MSG_RESULT([$found_header])
65	CPPFLAGS="$SAVE_CPPFLAGS"
66	if test "x$found_header" != "xyes"; then
67		AC_MSG_ERROR([Cannot find standard include files])
68	else
69		AC_DEFINE_UNQUOTED([CLANG_SYSROOT], ["$sysroot"],
70			[Define to sysroot if needed])
71	fi
72fi
73CC="$SAVE_CC"
74
75AC_LANG_PUSH(C++)
76
77SAVE_CPPFLAGS="$CPPFLAGS"
78CPPFLAGS="$CLANG_CXXFLAGS -I$srcdir $CPPFLAGS"
79AC_CHECK_HEADER([clang/Basic/SourceLocation.h], [],
80	[AC_ERROR([clang header file not found])])
81AC_EGREP_HEADER([getDefaultTargetTriple], [llvm/Support/Host.h], [],
82	[AC_DEFINE([getDefaultTargetTriple], [getHostTriple],
83	[Define to getHostTriple for older versions of clang])])
84AC_EGREP_HEADER([getExpansionLineNumber], [clang/Basic/SourceLocation.h], [],
85	[AC_DEFINE([getExpansionLineNumber], [getInstantiationLineNumber],
86	[Define to getInstantiationLineNumber for older versions of clang])])
87AC_EGREP_HEADER([getImmediateExpansionRange], [clang/Basic/SourceManager.h],
88	[],
89	[AC_DEFINE([getImmediateExpansionRange],
90	[getImmediateInstantiationRange],
91	[Define to getImmediateInstantiationRange for older versions of clang])]
92)
93AC_EGREP_HEADER([DiagnosticsEngine], [clang/Basic/Diagnostic.h], [],
94	[AC_DEFINE([DiagnosticsEngine], [Diagnostic],
95	[Define to Diagnostic for older versions of clang])])
96AC_EGREP_HEADER([ArrayRef], [clang/Driver/Driver.h],
97	[AC_DEFINE([USE_ARRAYREF], [],
98		[Define if Driver::BuildCompilation takes ArrayRef])
99	AC_EGREP_HEADER([ArrayRef.*CommandLineArgs],
100		[clang/Frontend/CompilerInvocation.h],
101		[AC_DEFINE([CREATE_FROM_ARGS_TAKES_ARRAYREF], [],
102			[Define if CompilerInvocation::CreateFromArgs takes
103			 ArrayRef])
104		])
105	])
106AC_EGREP_HEADER([CXXIsProduction], [clang/Driver/Driver.h],
107	[AC_DEFINE([HAVE_CXXISPRODUCTION], [],
108		[Define if Driver constructor takes CXXIsProduction argument])])
109AC_EGREP_HEADER([ IsProduction], [clang/Driver/Driver.h],
110	[AC_DEFINE([HAVE_ISPRODUCTION], [],
111		[Define if Driver constructor takes IsProduction argument])])
112AC_TRY_COMPILE([#include <clang/Driver/Driver.h>], [
113	using namespace clang;
114	DiagnosticsEngine *Diags;
115	new driver::Driver("", "", "", *Diags);
116], [AC_DEFINE([DRIVER_CTOR_TAKES_DEFAULTIMAGENAME], [],
117	      [Define if Driver constructor takes default image name])])
118AC_EGREP_HEADER([void HandleTopLevelDecl\(], [clang/AST/ASTConsumer.h],
119	[AC_DEFINE([HandleTopLevelDeclReturn], [void],
120		   [Return type of HandleTopLevelDeclReturn])
121	 AC_DEFINE([HandleTopLevelDeclContinue], [],
122		   [Return type of HandleTopLevelDeclReturn])],
123	[AC_DEFINE([HandleTopLevelDeclReturn], [bool],
124		   [Return type of HandleTopLevelDeclReturn])
125	 AC_DEFINE([HandleTopLevelDeclContinue], [true],
126		   [Return type of HandleTopLevelDeclReturn])])
127AC_CHECK_HEADER([clang/Basic/DiagnosticOptions.h],
128	[AC_DEFINE([HAVE_BASIC_DIAGNOSTICOPTIONS_H], [],
129		   [Define if clang/Basic/DiagnosticOptions.h exists])])
130AC_CHECK_HEADER([clang/Lex/PreprocessorOptions.h],
131	[AC_DEFINE([HAVE_LEX_PREPROCESSOROPTIONS_H], [],
132		   [Define if clang/Lex/PreprocessorOptions.h exists])], [],
133	[#include <clang/Basic/LLVM.h>])
134AC_TRY_COMPILE([#include <clang/Basic/TargetInfo.h>], [
135	using namespace clang;
136	std::shared_ptr<TargetOptions> TO;
137	DiagnosticsEngine *Diags;
138	TargetInfo::CreateTargetInfo(*Diags, TO);
139], [AC_DEFINE([CREATETARGETINFO_TAKES_SHARED_PTR], [],
140	      [Define if TargetInfo::CreateTargetInfo takes shared_ptr])])
141AC_TRY_COMPILE([#include <clang/Basic/TargetInfo.h>], [
142	using namespace clang;
143	TargetOptions *TO;
144	DiagnosticsEngine *Diags;
145	TargetInfo::CreateTargetInfo(*Diags, TO);
146], [AC_DEFINE([CREATETARGETINFO_TAKES_POINTER], [],
147	      [Define if TargetInfo::CreateTargetInfo takes pointer])])
148AC_TRY_COMPILE([#include <clang/Frontend/CompilerInstance.h>], [
149	using namespace clang;
150	DiagnosticConsumer *client;
151	CompilerInstance *Clang;
152	Clang->createDiagnostics(client);
153], [], [AC_DEFINE([CREATEDIAGNOSTICS_TAKES_ARG], [],
154	[Define if CompilerInstance::createDiagnostics takes argc and argv])])
155AC_TRY_COMPILE([#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])])
161AC_EGREP_HEADER([getNumParams],
162	[clang/AST/CanonicalType.h],
163	[AC_DEFINE([getNumArgs], [getNumParams],
164	    [Define to getNumParams for newer versions of clang])
165	 AC_DEFINE([getArgType], [getParamType],
166	    [Define to getParamType for newer versions of clang])])
167AC_EGREP_HEADER([getReturnType],
168	[clang/AST/CanonicalType.h], [],
169	[AC_DEFINE([getReturnType], [getResultType],
170	    [Define to getResultType for older versions of clang])])
171AC_TRY_COMPILE([#include <clang/Frontend/CompilerInstance.h>], [
172	using namespace clang;
173	CompilerInstance *Clang;
174	Clang->createPreprocessor(TU_Complete);
175], [AC_DEFINE([CREATEPREPROCESSOR_TAKES_TUKIND], [],
176[Define if CompilerInstance::createPreprocessor takes TranslationUnitKind])])
177AC_EGREP_HEADER([setMainFileID], [clang/Basic/SourceManager.h],
178	[AC_DEFINE([HAVE_SETMAINFILEID], [],
179	[Define if SourceManager has a setMainFileID method])])
180AC_CHECK_HEADER([llvm/ADT/OwningPtr.h],
181	[AC_DEFINE([HAVE_ADT_OWNINGPTR_H], [],
182		   [Define if llvm/ADT/OwningPtr.h exists])])
183AC_EGREP_HEADER([initializeBuiltins],
184	[clang/Basic/Builtins.h], [],
185	[AC_DEFINE([initializeBuiltins], [InitializeBuiltins],
186		[Define to InitializeBuiltins for older versions of clang])])
187AC_EGREP_HEADER([IK_C], [clang/Frontend/FrontendOptions.h], [],
188	[AC_CHECK_HEADER([clang/Basic/LangStandard.h],
189		[IK_C=Language::C], [IK_C=InputKind::C])
190	 AC_DEFINE_UNQUOTED([IK_C], [$IK_C],
191	 [Define to Language::C or InputKind::C for newer versions of clang])
192	])
193AC_TRY_COMPILE([
194	#include <clang/Basic/TargetOptions.h>
195	#include <clang/Lex/PreprocessorOptions.h>
196	#include <clang/Frontend/CompilerInstance.h>
197
198	#include "set_lang_defaults_arg4.h"
199], [
200	using namespace clang;
201	CompilerInstance *Clang;
202	TargetOptions TO;
203	llvm::Triple T(TO.Triple);
204	PreprocessorOptions PO;
205	CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
206			T, setLangDefaultsArg4(PO),
207			LangStandard::lang_unspecified);
208], [AC_DEFINE([SETLANGDEFAULTS_TAKES_5_ARGUMENTS], [],
209	[Define if CompilerInvocation::setLangDefaults takes 5 arguments])])
210AC_TRY_COMPILE([
211	#include <clang/Frontend/CompilerInstance.h>
212	#include <clang/Frontend/CompilerInvocation.h>
213], [
214	using namespace clang;
215	CompilerInvocation *invocation;
216	CompilerInstance *Clang;
217	Clang->setInvocation(std::make_shared<CompilerInvocation>(*invocation));
218], [AC_DEFINE([SETINVOCATION_TAKES_SHARED_PTR], [],
219	[Defined if CompilerInstance::setInvocation takes a shared_ptr])])
220AC_CHECK_HEADER([llvm/Option/Arg.h],
221	[AC_DEFINE([HAVE_LLVM_OPTION_ARG_H], [],
222		   [Define if llvm/Option/Arg.h exists])])
223CPPFLAGS="$SAVE_CPPFLAGS"
224
225SAVE_LDFLAGS="$LDFLAGS"
226LDFLAGS="$CLANG_LDFLAGS $LDFLAGS"
227
228# Use single libclang-cpp shared library when available.
229# Otherwise, use a selection of clang libraries that appears to work.
230AC_CHECK_LIB([clang-cpp], [main], [have_lib_clang=yes], [have_lib_clang=no])
231if test "$have_lib_clang" = yes; then
232	CLANG_LIBS="-lclang-cpp $CLANG_LIBS"
233else
234	CLANG_LIBS="-lclangBasic -lclangDriver $CLANG_LIBS"
235	CLANG_LIBS="-lclangAnalysis -lclangAST -lclangLex $CLANG_LIBS"
236	LDFLAGS="$CLANG_LDFLAGS $CLANG_LIBS $SAVE_LDFLAGS"
237	AC_CHECK_LIB([clangEdit], [main], [LIB_CLANG_EDIT=-lclangEdit], [])
238	CLANG_LIBS="$LIB_CLANG_EDIT $CLANG_LIBS"
239	CLANG_LIBS="-lclangParse -lclangSema $CLANG_LIBS"
240	CLANG_LIBS="-lclangFrontend -lclangSerialization $CLANG_LIBS"
241fi
242
243LDFLAGS="$SAVE_LDFLAGS"
244
245AC_LANG_POP
246])
247