extract-interface: link in clangDriver before clangBasic
[isl.git] / m4 / ax_detect_clang.m4
blob50a8047c3489b7c3e0be88be77b16cf812bf2367
1 AC_DEFUN([AX_DETECT_CLANG], [
2 AC_SUBST(CLANG_CXXFLAGS)
3 AC_SUBST(CLANG_LDFLAGS)
4 AC_SUBST(CLANG_RFLAG)
5 AC_SUBST(CLANG_LIBS)
6 AC_PROG_GREP
7 AC_PROG_SED
8 if test "x$with_clang_prefix" != "x"; then
9         LLVM_CONFIG="$with_clang_prefix/bin/llvm-config"
11 AC_PATH_PROG([LLVM_CONFIG], ["llvm-config"])
12 if test -z "$LLVM_CONFIG" || test ! -x "$LLVM_CONFIG"; then
13         AC_MSG_ERROR([llvm-config not found])
15 CLANG_CXXFLAGS=`$LLVM_CONFIG --cxxflags | \
16         $SED -e 's/-Wcovered-switch-default//;s/-gsplit-dwarf//'`
17 CLANG_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.
20 CLANG_RFLAG=`echo "$CLANG_LDFLAGS" | $SED -e 's/-L/-R/g'`
21 targets=`$LLVM_CONFIG --targets-built`
22 components="$targets asmparser bitreader support mc"
23 # Link in option and frontendopenmp components when available
24 # since they may be used by the clang libraries.
25 for 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
30 done
31 CLANG_LIBS=`$LLVM_CONFIG --libs $components`
32 systemlibs=`$LLVM_CONFIG --system-libs 2> /dev/null | tail -1`
33 if test $? -eq 0; then
34         CLANG_LIBS="$CLANG_LIBS $systemlibs"
36 CLANG_PREFIX=`$LLVM_CONFIG --prefix`
37 AC_DEFINE_UNQUOTED(CLANG_PREFIX, ["$CLANG_PREFIX"], [Clang installation prefix])
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.
43 SAVE_CC="$CC"
44 CC="$CLANG_PREFIX/bin/clang"
45 AC_MSG_CHECKING(
46         [whether $CLANG_PREFIX/bin/clang can find standard include files])
47 found_header=no
48 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>]], [[]])],
49         [found_header=yes])
50 AC_MSG_RESULT([$found_header])
51 if 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
73 CC="$SAVE_CC"
75 AC_LANG_PUSH(C++)
77 SAVE_CPPFLAGS="$CPPFLAGS"
78 SAVE_LDFLAGS="$LDFLAGS"
79 SAVE_LIBS="$LIBS"
81 CPPFLAGS="$CLANG_CXXFLAGS -I$srcdir $CPPFLAGS"
82 AC_CHECK_HEADER([clang/Basic/SourceLocation.h], [],
83         [AC_MSG_ERROR([clang header file not found])])
84 AC_CHECK_HEADER([llvm/TargetParser/Host.h],
85         [AC_DEFINE([HAVE_TARGETPARSER_HOST_H], [],
86                    [Define if llvm/TargetParser/Host.h exists])],
87         [AC_EGREP_HEADER([getDefaultTargetTriple], [llvm/Support/Host.h], [],
88                 [AC_DEFINE([getDefaultTargetTriple], [getHostTriple],
89                 [Define to getHostTriple for older versions of clang])])
90         ])
91 AC_EGREP_HEADER([getExpansionLineNumber], [clang/Basic/SourceLocation.h], [],
92         [AC_DEFINE([getExpansionLineNumber], [getInstantiationLineNumber],
93         [Define to getInstantiationLineNumber for older versions of clang])])
94 AC_EGREP_HEADER([getImmediateExpansionRange], [clang/Basic/SourceManager.h],
95         [],
96         [AC_DEFINE([getImmediateExpansionRange],
97         [getImmediateInstantiationRange],
98         [Define to getImmediateInstantiationRange for older versions of clang])]
100 AC_EGREP_HEADER([DiagnosticsEngine], [clang/Basic/Diagnostic.h], [],
101         [AC_DEFINE([DiagnosticsEngine], [Diagnostic],
102         [Define to Diagnostic for older versions of clang])])
103 AC_EGREP_HEADER([ArrayRef], [clang/Driver/Driver.h],
104         [AC_DEFINE([USE_ARRAYREF], [],
105                 [Define if Driver::BuildCompilation takes ArrayRef])
106         AC_EGREP_HEADER([ArrayRef.*CommandLineArgs],
107                 [clang/Frontend/CompilerInvocation.h],
108                 [AC_DEFINE([CREATE_FROM_ARGS_TAKES_ARRAYREF], [],
109                         [Define if CompilerInvocation::CreateFromArgs takes
110                          ArrayRef])
111                 ])
112         ])
113 AC_EGREP_HEADER([CXXIsProduction], [clang/Driver/Driver.h],
114         [AC_DEFINE([HAVE_CXXISPRODUCTION], [],
115                 [Define if Driver constructor takes CXXIsProduction argument])])
116 AC_EGREP_HEADER([ IsProduction], [clang/Driver/Driver.h],
117         [AC_DEFINE([HAVE_ISPRODUCTION], [],
118                 [Define if Driver constructor takes IsProduction argument])])
119 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <clang/Driver/Driver.h>]], [[
120         using namespace clang;
121         DiagnosticsEngine *Diags;
122         new driver::Driver("", "", "", *Diags);
123 ]])], [AC_DEFINE([DRIVER_CTOR_TAKES_DEFAULTIMAGENAME], [],
124               [Define if Driver constructor takes default image name])])
125 AC_EGREP_HEADER([void HandleTopLevelDecl\(], [clang/AST/ASTConsumer.h],
126         [AC_DEFINE([HandleTopLevelDeclReturn], [void],
127                    [Return type of HandleTopLevelDeclReturn])
128          AC_DEFINE([HandleTopLevelDeclContinue], [],
129                    [Return type of HandleTopLevelDeclReturn])],
130         [AC_DEFINE([HandleTopLevelDeclReturn], [bool],
131                    [Return type of HandleTopLevelDeclReturn])
132          AC_DEFINE([HandleTopLevelDeclContinue], [true],
133                    [Return type of HandleTopLevelDeclReturn])])
134 AC_CHECK_HEADER([clang/Basic/DiagnosticOptions.h],
135         [AC_DEFINE([HAVE_BASIC_DIAGNOSTICOPTIONS_H], [],
136                    [Define if clang/Basic/DiagnosticOptions.h exists])])
137 AC_CHECK_HEADER([clang/Lex/PreprocessorOptions.h],
138         [AC_DEFINE([HAVE_LEX_PREPROCESSOROPTIONS_H], [],
139                    [Define if clang/Lex/PreprocessorOptions.h exists])], [],
140         [#include <clang/Basic/LLVM.h>])
141 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <clang/Basic/TargetInfo.h>]], [[
142         using namespace clang;
143         std::shared_ptr<TargetOptions> TO;
144         DiagnosticsEngine *Diags;
145         TargetInfo::CreateTargetInfo(*Diags, TO);
146 ]])], [AC_DEFINE([CREATETARGETINFO_TAKES_SHARED_PTR], [],
147               [Define if TargetInfo::CreateTargetInfo takes shared_ptr])])
148 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <clang/Basic/TargetInfo.h>]], [[
149         using namespace clang;
150         TargetOptions *TO;
151         DiagnosticsEngine *Diags;
152         TargetInfo::CreateTargetInfo(*Diags, TO);
153 ]])], [AC_DEFINE([CREATETARGETINFO_TAKES_POINTER], [],
154               [Define if TargetInfo::CreateTargetInfo takes pointer])])
155 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
156                 [[#include <clang/Frontend/CompilerInstance.h>]], [[
157         using namespace clang;
158         DiagnosticConsumer *client;
159         CompilerInstance *Clang;
160         Clang->createDiagnostics(client);
161 ]])], [], [AC_DEFINE([CREATEDIAGNOSTICS_TAKES_ARG], [],
162         [Define if CompilerInstance::createDiagnostics takes argc and argv])])
163 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
164                 [[#include <clang/Lex/HeaderSearchOptions.h>]], [[
165         using namespace clang;
166         HeaderSearchOptions HSO;
167         HSO.AddPath("", frontend::Angled, false, false);
168 ]])], [AC_DEFINE([ADDPATH_TAKES_4_ARGUMENTS], [],
169         [Define if HeaderSearchOptions::AddPath takes 4 arguments])])
170 AC_EGREP_HEADER([getNumParams],
171         [clang/AST/CanonicalType.h],
172         [AC_DEFINE([getNumArgs], [getNumParams],
173             [Define to getNumParams for newer versions of clang])
174          AC_DEFINE([getArgType], [getParamType],
175             [Define to getParamType for newer versions of clang])])
176 AC_EGREP_HEADER([getReturnType],
177         [clang/AST/CanonicalType.h], [],
178         [AC_DEFINE([getReturnType], [getResultType],
179             [Define to getResultType for older versions of clang])])
180 AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
181                 [[#include <clang/Frontend/CompilerInstance.h>]], [[
182         using namespace clang;
183         CompilerInstance *Clang;
184         Clang->createPreprocessor(TU_Complete);
185 ]])], [AC_DEFINE([CREATEPREPROCESSOR_TAKES_TUKIND], [],
186 [Define if CompilerInstance::createPreprocessor takes TranslationUnitKind])])
187 AC_EGREP_HEADER([setMainFileID], [clang/Basic/SourceManager.h],
188         [AC_DEFINE([HAVE_SETMAINFILEID], [],
189         [Define if SourceManager has a setMainFileID method])])
190 AC_CHECK_HEADER([llvm/ADT/OwningPtr.h],
191         [AC_DEFINE([HAVE_ADT_OWNINGPTR_H], [],
192                    [Define if llvm/ADT/OwningPtr.h exists])])
193 AC_EGREP_HEADER([initializeBuiltins],
194         [clang/Basic/Builtins.h], [],
195         [AC_DEFINE([initializeBuiltins], [InitializeBuiltins],
196                 [Define to InitializeBuiltins for older versions of clang])])
197 AC_EGREP_HEADER([IK_C], [clang/Frontend/FrontendOptions.h], [],
198         [AC_CHECK_HEADER([clang/Basic/LangStandard.h],
199                 [IK_C=Language::C], [IK_C=InputKind::C])
200          AC_DEFINE_UNQUOTED([IK_C], [$IK_C],
201          [Define to Language::C or InputKind::C for newer versions of clang])
202         ])
203 # llvmorg-15-init-7544-g93471e65df48
204 AC_EGREP_HEADER([setLangDefaults], [clang/Basic/LangOptions.h],
205         [SETLANGDEFAULTS=LangOptions],
206         [SETLANGDEFAULTS=CompilerInvocation])
207 AC_DEFINE_UNQUOTED([SETLANGDEFAULTS], [$SETLANGDEFAULTS],
208         [Define to class with setLangDefaults method])
209 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
210         #include <clang/Basic/TargetOptions.h>
211         #include <clang/Lex/PreprocessorOptions.h>
212         #include <clang/Frontend/CompilerInstance.h>
214         #include "set_lang_defaults_arg4.h"
215 ]], [[
216         using namespace clang;
217         CompilerInstance *Clang;
218         TargetOptions TO;
219         llvm::Triple T(TO.Triple);
220         PreprocessorOptions PO;
221         SETLANGDEFAULTS::setLangDefaults(Clang->getLangOpts(), IK_C,
222                         T, setLangDefaultsArg4(PO),
223                         LangStandard::lang_unspecified);
224 ]])], [AC_DEFINE([SETLANGDEFAULTS_TAKES_5_ARGUMENTS], [],
225         [Define if CompilerInvocation::setLangDefaults takes 5 arguments])])
226 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
227         #include <clang/Frontend/CompilerInstance.h>
228         #include <clang/Frontend/CompilerInvocation.h>
229 ]], [[
230         using namespace clang;
231         CompilerInvocation *invocation;
232         CompilerInstance *Clang;
233         Clang->setInvocation(std::make_shared<CompilerInvocation>(*invocation));
234 ]])], [AC_DEFINE([SETINVOCATION_TAKES_SHARED_PTR], [],
235         [Defined if CompilerInstance::setInvocation takes a shared_ptr])])
236 AC_CHECK_HEADER([llvm/Option/Arg.h],
237         [AC_DEFINE([HAVE_LLVM_OPTION_ARG_H], [],
238                    [Define if llvm/Option/Arg.h exists])])
240 LDFLAGS="$CLANG_LDFLAGS $LDFLAGS"
242 # A test program for checking whether linking against libclang-cpp works.
243 m4_define([_AX_DETECT_CLANG_PROGRAM], [AC_LANG_PROGRAM(
244         [[#include <clang/Frontend/CompilerInstance.h>]],
245         [[
246                 new clang::CompilerInstance();
247         ]])])
249 # Use single libclang-cpp shared library when available.
250 # Otherwise, use a selection of clang libraries that appears to work.
251 AC_CHECK_LIB([clang-cpp], [main], [have_lib_clang=yes], [have_lib_clang=no])
252 if test "$have_lib_clang" = yes; then
253         # The LLVM libraries may be linked into libclang-cpp already.
254         # Linking against them again can cause errors about options
255         # being registered more than once.
256         # Check whether linking against libclang-cpp requires
257         # linking against the LLVM libraries as well.
258         # Fail if linking fails with or without the LLVM libraries.
259         AC_MSG_CHECKING([whether libclang-cpp needs LLVM libraries])
260         LIBS="-lclang-cpp $SAVE_LIBS"
261         AC_LINK_IFELSE([_AX_DETECT_CLANG_PROGRAM], [clangcpp_needs_llvm=no], [
262                 LIBS="-lclang-cpp $CLANG_LIBS $SAVE_LIBS"
263                 AC_LINK_IFELSE([_AX_DETECT_CLANG_PROGRAM],
264                         [clangcpp_needs_llvm=yes],
265                         [clangcpp_needs_llvm=unknown])
266         ])
267         AC_MSG_RESULT([$clangcpp_needs_llvm])
268         AS_IF([test "$clangcpp_needs_llvm" = "no"],
269                         [CLANG_LIBS="-lclang-cpp"],
270               [test "$clangcpp_needs_llvm" = "yes"],
271                         [CLANG_LIBS="-lclang-cpp $CLANG_LIBS"],
272               [AC_MSG_FAILURE([unable to link against libclang-cpp])])
273 else
274         CLANG_LIBS="-lclangDriver -lclangBasic $CLANG_LIBS"
275         CLANG_LIBS="-lclangAnalysis -lclangAST -lclangLex $CLANG_LIBS"
276         LDFLAGS="$CLANG_LDFLAGS $CLANG_LIBS $SAVE_LDFLAGS"
277         AC_CHECK_LIB([clangEdit], [main], [LIB_CLANG_EDIT=-lclangEdit], [])
278         CLANG_LIBS="$LIB_CLANG_EDIT $CLANG_LIBS"
279         CLANG_LIBS="-lclangParse -lclangSema $CLANG_LIBS"
280         CLANG_LIBS="-lclangFrontend -lclangSerialization $CLANG_LIBS"
283 CPPFLAGS="$SAVE_CPPFLAGS"
284 LDFLAGS="$SAVE_LDFLAGS"
285 LIBS="$SAVE_LIBS"
287 AC_LANG_POP