1 #
  2 # Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
  3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4 #
  5 # This code is free software; you can redistribute it and/or modify it
  6 # under the terms of the GNU General Public License version 2 only, as
  7 # published by the Free Software Foundation.  Oracle designates this
  8 # particular file as subject to the "Classpath" exception as provided
  9 # by Oracle in the LICENSE file that accompanied this code.
 10 #
 11 # This code is distributed in the hope that it will be useful, but WITHOUT
 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14 # version 2 for more details (a copy is included in the LICENSE file that
 15 # accompanied this code).
 16 #
 17 # You should have received a copy of the GNU General Public License version
 18 # 2 along with this work; if not, write to the Free Software Foundation,
 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20 #
 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22 # or visit www.oracle.com if you need additional information or have any
 23 # questions.
 24 #
 25 
 26 ################################################################################
 27 # Setup libclang from llvm project
 28 ################################################################################
 29 AC_DEFUN_ONCE([LIB_SETUP_LIBCLANG],
 30 [
 31   AC_ARG_WITH([libclang], [AS_HELP_STRING([--with-libclang=<path to llvm>],
 32       [Specify path of llvm installation containing libclang. Pre-built llvm
 33       binary can be downloaded from http://llvm.org/releases/download.html])])
 34   AC_ARG_WITH([libclang-lib], [AS_HELP_STRING([--with-libclang-lib=<path>],
 35       [Specify where to find libclang binary, so/dylib/lib ])])
 36   AC_ARG_WITH([libclang-include], [AS_HELP_STRING([--with-libclang-include=<path>],
 37       [Specify where to find libclang header files, clang-c/Index.h ])])
 38   AC_ARG_WITH([libclang-include-aux], [AS_HELP_STRING([--with-libclang-include-aux=<path>],
 39       [Specify where to find libclang auxiliary header files, lib/clang/<clang-version>/include/stddef.h ])])
 40   AC_ARG_WITH([libclang-bin], [AS_HELP_STRING([--with-libclang-bin=<path>],
 41       [Specify where to find clang binary, libclang.dll ])])
 42   AC_ARG_WITH([libclang-version], [AS_HELP_STRING([--with-libclang-version=<version>],
 43       [Specify which libclang version to use ])])
 44 
 45   if test "x$with_libclang" = "xno"; then
 46     AC_MSG_CHECKING([if libclang should be enabled])
 47     AC_MSG_RESULT([no, forced])
 48     ENABLE_LIBCLANG="false"
 49   else
 50     if test "x$with_libclang" != "x"; then
 51       AC_MSG_CHECKING([if libclang should be enabled])
 52       AC_MSG_RESULT([yes, forced])
 53       ENABLE_LIBCLANG_FORCED="true"
 54     else
 55       ENABLE_LIBCLANG_FORCED="false"
 56     fi
 57     ENABLE_LIBCLANG="true"
 58 
 59     AC_MSG_CHECKING([libclang version to be used])
 60     if test "x$with_libclang_version" != "x"; then
 61       LIBCLANG_VERSION="$with_libclang_version"
 62       AC_MSG_RESULT([$LIBCLANG_VERSION (manually specified)])
 63     else
 64       LIBCLANG_VERSION="9"
 65       AC_MSG_RESULT([$LIBCLANG_VERSION (default)])
 66     fi
 67 
 68     if test "x$with_libclang" != "x" -a "x$with_libclang" != "xyes"; then
 69       CLANG_LIB_PATH="$with_libclang/lib"
 70       CLANG_BIN_PATH="$with_libclang/bin"
 71       CLANG_INCLUDE_PATH="$with_libclang/include"
 72 
 73       AC_MSG_CHECKING([libclang auxiliary include path])
 74       if test "x$with_libclang_include_aux" != "x"; then
 75         CLANG_INCLUDE_AUX_PATH="$with_libclang_include_aux"
 76         AC_MSG_RESULT([$CLANG_INCLUDE_AUX_PATH])
 77         if test "x$with_libclang_version" != "x"; then
 78           AC_MSG_WARN([--with-libclang-include-aux was specified. Manually specified value of --with-libclang-version was ignored])
 79         fi        
 80       else 
 81         # There may be more than one version of clang matching the specifed version.
 82         # Pick the last one if there are more than one versions.
 83         VER=`$LS $with_libclang/lib/clang/ | $GREP "^$LIBCLANG_VERSION" | $TAIL -n1`
 84         if test "x$VER" = "x"; then
 85           AC_MSG_ERROR([Can not find libclang version matching the specified version: '$LIBCLANG_VERSION' in
 86             $($FIND $with_libclang/lib/clang/ -mindepth 1 -maxdepth 1)])
 87         fi        
 88         CLANG_INCLUDE_AUX_PATH="$with_libclang/lib/clang/$VER/include"
 89         AC_MSG_RESULT([$CLANG_INCLUDE_AUX_PATH])
 90       fi      
 91     fi
 92 
 93     if test "x$with_libclang_lib" != "x"; then
 94       CLANG_LIB_PATH="$with_libclang_lib"
 95     fi
 96     if test "x$with_libclang_include" != "x"; then
 97       CLANG_INCLUDE_PATH="$with_libclang_include"
 98     fi
 99     if test "x$with_libclang_bin" != "x"; then
100       CLANG_BIN_PATH="$with_libclang_bin"
101     fi
102 
103     dnl Only for Windows platform now, as we don't need bin yet for other platform
104     if test "x$OPENJDK_TARGET_OS" = xwindows; then
105         UTIL_FIXUP_PATH(CLANG_BIN_PATH)
106     else
107         CLANG_BIN_PATH=""
108     fi
109 
110     UTIL_FIXUP_PATH(CLANG_INCLUDE_PATH)
111     UTIL_FIXUP_PATH(CLANG_LIB_PATH)
112     UTIL_FIXUP_PATH(CLANG_INCLUDE_AUX_PATH)
113 
114     if test "x$CLANG_INCLUDE_PATH" != "x"; then
115         LIBCLANG_CPPFLAGS="-I$CLANG_INCLUDE_PATH"
116     else
117         LIBCLANG_CPPFLAGS=""
118     fi
119 
120     if test "x$CLANG_LIB_PATH" != "x"; then
121       if test "x$TOOLCHAIN_TYPE" = "xmicrosoft"; then
122         LIBCLANG_LDFLAGS="/LIBPATH:$CLANG_LIB_PATH"
123         LIBCLANG_LIBS="$CLANG_LIB_PATH/libclang.lib"
124       else
125         LIBCLANG_LDFLAGS="-L$CLANG_LIB_PATH"
126         LIBCLANG_LIBS="-lclang"
127       fi
128     else
129         LIBCLANG_LDFLAGS=""
130     fi
131 
132     OLD_CPPFLAGS=$CPPFLAGS
133     OLD_LDFLAGS=$LDFLAGS
134     OLD_LIBS=$LIBS
135 
136     CPPFLAGS="$LIBCLANG_CPPFLAGS"
137     LDFLAGS="$LIBCLANG_LDFLAGS"
138     LIBS=""
139 
140     OLD_CXX=$CXX
141     OLD_CXXCPP=$CXXCPP
142     CXX="$FIXPATH $CXX"
143     CXXCPP="$FIXPATH $CXXCPP"
144 
145     AC_CHECK_HEADER("clang-c/Index.h", [], [ENABLE_LIBCLANG="false"])
146     if test "x$ENABLE_LIBCLANG" = "xtrue"; then
147       if test "x$TOOLCHAIN_TYPE" = "xmicrosoft" || test "x$COMPILE_TYPE" = "xcross"; then
148         # Just trust the lib is there
149         LIBS=$LIBCLANG_LIBS
150       else
151         AC_CHECK_LIB(clang, clang_getClangVersion, [], [ENABLE_LIBCLANG="false"])
152       fi
153     fi
154 
155     CXX=$OLD_CXX
156     CXXCPP=$OLD_CXXCPP
157 
158     if test "x$ENABLE_LIBCLANG" = "xfalse"; then
159       if test "x$ENABLE_LIBCLANG_FORCED" = "xtrue"; then
160         AC_MSG_ERROR([Cannot locate libclang or headers at the specified locations:
161             $CLANG_LIB_PATH
162             $CLANG_INCLUDE_PATH])
163       else
164         AC_MSG_CHECKING([if libclang should be enabled])
165         AC_MSG_RESULT([no, not found])
166         AC_MSG_ERROR([Cannot locate libclang! You can download pre-built llvm
167             binary from http://llvm.org/releases/download.html, then specify the
168             location using --with-libclang])
169       fi
170     fi
171 
172     LIBCLANG_LIBS="$LIBS"
173 
174     LIBS="$OLD_LIBS"
175     LDFLAGS="$OLD_LDFLAGS"
176     CPPFLAGS="$OLD_CPPFLAGS"
177   fi
178 
179   if test "x$ENABLE_LIBCLANG" = "xfalse"; then
180     CLANG_INCLUDE_PATH=""
181     CLANG_INCLUDE_AUX_PATH=""
182     CLANG_LIB_PATH=""
183     LIBCLANG_CPPFLAGS=""
184     LIBCLANG_LDFLAGS=""
185     LIBCLANG_LIBS=""
186   else
187     if test "x$OPENJDK_TARGET_OS" = xwindows; then
188       CLANG_LIBNAME=[$CLANG_BIN_PATH]["/libclang"][$SHARED_LIBRARY_SUFFIX]
189     else
190       CLANG_LIBNAME=[$CLANG_LIB_PATH/$LIBRARY_PREFIX]["clang"][$SHARED_LIBRARY_SUFFIX]
191     fi
192     UTIL_REMOVE_SYMBOLIC_LINKS(CLANG_LIBNAME)
193   fi
194 
195   AC_SUBST(ENABLE_LIBCLANG)
196   AC_SUBST(CLANG_INCLUDE_PATH)
197   AC_SUBST(CLANG_INCLUDE_AUX_PATH)
198   AC_SUBST(CLANG_LIB_PATH)
199   AC_SUBST(CLANG_LIBNAME)
200   AC_SUBST(LIBCLANG_CPPFLAGS)
201   AC_SUBST(LIBCLANG_LDFLAGS)
202   AC_SUBST(LIBCLANG_LIBS)
203 ])