1dnl This Source Code Form is subject to the terms of the Mozilla Public
2dnl License, v. 2.0. If a copy of the MPL was not distributed with this
3dnl file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5AC_DEFUN([MOZ_CONFIG_CLANG_PLUGIN], [
6
7MOZ_ARG_ENABLE_BOOL(clang-plugin,
8[  --enable-clang-plugin   Enable building with the mozilla clang plugin ],
9   ENABLE_CLANG_PLUGIN=1,
10   ENABLE_CLANG_PLUGIN= )
11if test -n "$ENABLE_CLANG_PLUGIN"; then
12    if test -z "${CLANG_CC}${CLANG_CL}"; then
13        AC_MSG_ERROR([Can't use clang plugin without clang.])
14    fi
15
16    AC_MSG_CHECKING([for llvm-config])
17    if test -z "$LLVMCONFIG"; then
18      if test -n "$CLANG_CL"; then
19          CXX_COMPILER="$(dirname "$CXX")/clang"
20      else
21          CXX_COMPILER="${CXX}"
22      fi
23      LLVMCONFIG=`$CXX_COMPILER -print-prog-name=llvm-config`
24    fi
25
26    if test -z "$LLVMCONFIG"; then
27      LLVMCONFIG=`which llvm-config`
28    fi
29
30    if test ! -x "$LLVMCONFIG"; then
31      AC_MSG_RESULT([not found])
32      AC_MSG_ERROR([Cannot find an llvm-config binary for building a clang plugin])
33    fi
34
35    AC_MSG_RESULT([$LLVMCONFIG])
36
37    if test -z "$LLVMCONFIG"; then
38        AC_MSG_ERROR([Cannot find an llvm-config binary for building a clang plugin])
39    fi
40    dnl For some reason the llvm-config downloaded from clang.llvm.org for clang3_8
41    dnl produces a -isysroot flag for a sysroot which might not ship when passed
42    dnl --cxxflags. We use sed to remove this argument so that builds work on OSX
43    LLVM_CXXFLAGS=`$LLVMCONFIG --cxxflags | sed -e 's/-isysroot [[^ ]]*//'`
44
45    dnl We are loaded into clang, so we don't need to link to very many things,
46    dnl we just need to link to clangASTMatchers because it is not used by clang
47    LLVM_LDFLAGS=`$LLVMCONFIG --ldflags | tr '\n' ' '`
48
49    if test "${HOST_OS_ARCH}" = "Darwin"; then
50        dnl We need to make sure that we use the symbols coming from the clang
51        dnl binary. In order to do this, we need to pass -flat_namespace and
52        dnl -undefined suppress to the linker. This makes sure that we link the
53        dnl symbols into the flat namespace provided by clang, and thus get
54        dnl access to all of the symbols which are undefined in our dylib as we
55        dnl are building it right now, and also that we don't fail the build
56        dnl due to undefined symbols (which will be provided by clang).
57        CLANG_LDFLAGS="-Wl,-flat_namespace -Wl,-undefined,suppress -lclangASTMatchers"
58    elif test "${HOST_OS_ARCH}" = "WINNT"; then
59        CLANG_LDFLAGS="clangASTMatchers.lib"
60    else
61        CLANG_LDFLAGS="-lclangASTMatchers"
62    fi
63
64    if test -n "$CLANG_CL"; then
65        dnl The llvm-config coming with clang-cl may give us arguments in the
66        dnl /ARG form, which in msys will be interpreted as a path name.  So we
67        dnl need to split the args and convert the leading slashes that we find
68        dnl into a dash.
69        LLVM_REPLACE_CXXFLAGS=''
70        for arg in $LLVM_CXXFLAGS; do
71            dnl The following expression replaces a leading slash with a dash.
72            dnl Also replace any backslashes with forward slash.
73            arg=`echo "$arg"|sed -e 's/^\//-/' -e 's/\\\\/\//g'`
74            LLVM_REPLACE_CXXFLAGS="$LLVM_REPLACE_CXXFLAGS $arg"
75        done
76        LLVM_CXXFLAGS="$LLVM_REPLACE_CXXFLAGS"
77
78        LLVM_REPLACE_LDFLAGS=''
79        for arg in $LLVM_LDFLAGS; do
80            dnl The following expression replaces a leading slash with a dash.
81            dnl Also replace any backslashes with forward slash.
82            arg=`echo "$arg"|sed -e 's/^\//-/' -e 's/\\\\/\//g'`
83            LLVM_REPLACE_LDFLAGS="$LLVM_REPLACE_LDFLAGS $arg"
84        done
85        LLVM_LDFLAGS="$LLVM_REPLACE_LDFLAGS"
86
87        CLANG_REPLACE_LDFLAGS=''
88        for arg in $CLANG_LDFLAGS; do
89            dnl The following expression replaces a leading slash with a dash.
90            dnl Also replace any backslashes with forward slash.
91            arg=`echo "$arg"|sed -e 's/^\//-/' -e 's/\\\\/\//g'`
92            CLANG_REPLACE_LDFLAGS="$CLANG_REPLACE_LDFLAGS $arg"
93        done
94        CLANG_LDFLAGS="$CLANG_REPLACE_LDFLAGS"
95    fi
96
97    dnl Check for the new ASTMatcher API names.  Since this happened in the
98    dnl middle of the 3.8 cycle, our CLANG_VERSION_FULL is impossible to use
99    dnl correctly, so we have to detect this at configure time.
100    AC_CACHE_CHECK(for new ASTMatcher API,
101                   ac_cv_have_new_ASTMatcher_api,
102        [
103            AC_LANG_SAVE
104            AC_LANG_CPLUSPLUS
105            _SAVE_CXXFLAGS="$CXXFLAGS"
106            _SAVE_CXX="$CXX"
107            _SAVE_MACOSX_DEPLOYMENT_TARGET="$MACOSX_DEPLOYMENT_TARGET"
108            unset MACOSX_DEPLOYMENT_TARGET
109            CXXFLAGS="${LLVM_CXXFLAGS}"
110            CXX="${HOST_CXX}"
111            AC_TRY_COMPILE([#include "clang/ASTMatchers/ASTMatchers.h"],
112                           [clang::ast_matchers::cxxConstructExpr();],
113                           ac_cv_have_new_ASTMatcher_names="yes",
114                           ac_cv_have_new_ASTMatcher_names="no")
115            CXX="$_SAVE_CXX"
116            CXXFLAGS="$_SAVE_CXXFLAGS"
117            export MACOSX_DEPLOYMENT_TARGET="$_SAVE_MACOSX_DEPLOYMENT_TARGET"
118            AC_LANG_RESTORE
119        ])
120    if test "$ac_cv_have_new_ASTMatcher_names" = "yes"; then
121      LLVM_CXXFLAGS="$LLVM_CXXFLAGS -DHAVE_NEW_ASTMATCHER_NAMES"
122    fi
123
124    dnl Check if we can compile has(ignoringParenImpCasts()) because
125    dnl before 3.9 that ignoringParenImpCasts was done internally by "has".
126    dnl See https://www.mail-archive.com/cfe-commits@lists.llvm.org/msg25234.html
127    AC_CACHE_CHECK(for has with ignoringParenImpCasts,
128                   ac_cv_has_accepts_ignoringParenImpCasts,
129        [
130            AC_LANG_SAVE
131            AC_LANG_CPLUSPLUS
132            _SAVE_CXXFLAGS="$CXXFLAGS"
133            _SAVE_CXX="$CXX"
134            _SAVE_MACOSX_DEPLOYMENT_TARGET="$MACOSX_DEPLOYMENT_TARGET"
135            unset MACOSX_DEPLOYMENT_TARGET
136            CXXFLAGS="${LLVM_CXXFLAGS}"
137            CXX="${HOST_CXX}"
138            AC_TRY_COMPILE([#include "clang/ASTMatchers/ASTMatchers.h"],
139                           [using namespace clang::ast_matchers;
140                            expr(has(ignoringParenImpCasts(declRefExpr())));
141                           ],
142                           ac_cv_has_accepts_ignoringParenImpCasts="yes",
143                           ac_cv_has_accepts_ignoringParenImpCasts="no")
144            CXX="$_SAVE_CXX"
145            CXXFLAGS="$_SAVE_CXXFLAGS"
146            export MACOSX_DEPLOYMENT_TARGET="$_SAVE_MACOSX_DEPLOYMENT_TARGET"
147            AC_LANG_RESTORE
148        ])
149    if test "$ac_cv_has_accepts_ignoringParenImpCasts" = "yes"; then
150      LLVM_CXXFLAGS="$LLVM_CXXFLAGS -DHAS_ACCEPTS_IGNORINGPARENIMPCASTS"
151    fi
152
153    AC_DEFINE(MOZ_CLANG_PLUGIN)
154fi
155
156AC_SUBST(LLVM_CXXFLAGS)
157AC_SUBST(LLVM_LDFLAGS)
158AC_SUBST(CLANG_LDFLAGS)
159
160AC_SUBST(ENABLE_CLANG_PLUGIN)
161
162])
163