lib-ffi.m4 revision 2369:66b770c85156
1#
2# Copyright (c) 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 libffi (Foreign Function Interface)
28################################################################################
29AC_DEFUN_ONCE([LIB_SETUP_LIBFFI],
30[
31  AC_ARG_WITH(libffi, [AS_HELP_STRING([--with-libffi],
32      [specify prefix directory for the libffi package
33      (expecting the libraries under PATH/lib and the headers under PATH/include)])])
34  AC_ARG_WITH(libffi-include, [AS_HELP_STRING([--with-libffi-include],
35      [specify directory for the libffi include files])])
36  AC_ARG_WITH(libffi-lib, [AS_HELP_STRING([--with-libffi-lib],
37      [specify directory for the libffi library])])
38  AC_ARG_ENABLE(libffi-bundling, [AS_HELP_STRING([--enable-libffi-bundling],
39      [enable bundling of libffi.so to make the built JDK runnable on more systems])])
40
41  if test "x$NEEDS_LIB_FFI" = xfalse; then
42    if (test "x${with_libffi}" != x && test "x${with_libffi}" != xno) || \
43        (test "x${with_libffi_include}" != x && test "x${with_libffi_include}" != xno) || \
44        (test "x${with_libffi_lib}" != x && test "x${with_libffi_lib}" != xno); then
45      AC_MSG_WARN([[libffi not used, so --with-libffi[-*] is ignored]])
46    fi
47    LIBFFI_CFLAGS=
48    LIBFFI_LIBS=
49  else
50    LIBFFI_FOUND=no
51
52    if test "x${with_libffi}" = xno || test "x${with_libffi_include}" = xno || test "x${with_libffi_lib}" = xno; then
53      AC_MSG_ERROR([It is not possible to disable the use of libffi. Remove the --without-libffi option.])
54    fi
55
56    if test "x${with_libffi}" != x; then
57      LIBFFI_LIB_PATH="${with_libffi}/lib"
58      LIBFFI_LIBS="-L${with_libffi}/lib -lffi"
59      LIBFFI_CFLAGS="-I${with_libffi}/include"
60      LIBFFI_FOUND=yes
61    fi
62    if test "x${with_libffi_include}" != x; then
63      LIBFFI_CFLAGS="-I${with_libffi_include}"
64      LIBFFI_FOUND=yes
65    fi
66    if test "x${with_libffi_lib}" != x; then
67      LIBFFI_LIB_PATH="${with_libffi_lib}"
68      LIBFFI_LIBS="-L${with_libffi_lib} -lffi"
69      LIBFFI_FOUND=yes
70    fi
71    # Do not try pkg-config if we have a sysroot set.
72    if test "x$SYSROOT" = x; then
73      if test "x$LIBFFI_FOUND" = xno; then
74        # Figure out LIBFFI_CFLAGS and LIBFFI_LIBS
75        PKG_CHECK_MODULES([LIBFFI], [libffi], [LIBFFI_FOUND=yes], [LIBFFI_FOUND=no])
76      fi
77    fi
78    if test "x$LIBFFI_FOUND" = xno; then
79      AC_CHECK_HEADERS([ffi.h],
80          [
81            LIBFFI_FOUND=yes
82            LIBFFI_CFLAGS=
83            LIBFFI_LIBS=-lffi
84          ],
85          [LIBFFI_FOUND=no]
86      )
87    fi
88    if test "x$LIBFFI_FOUND" = xno; then
89      HELP_MSG_MISSING_DEPENDENCY([ffi])
90      AC_MSG_ERROR([Could not find libffi! $HELP_MSG])
91    fi
92
93    AC_MSG_CHECKING([if libffi works])
94    AC_LANG_PUSH(C)
95    OLD_CFLAGS="$CFLAGS"
96    CFLAGS="$CFLAGS $LIBFFI_CFLAGS"
97    OLD_LIBS="$LIBS"
98    LIBS="$LIBS $LIBFFI_LIBS"
99    AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <ffi.h>],
100        [
101          ffi_call(NULL, NULL, NULL, NULL);
102          return 0;
103        ])],
104        [LIBFFI_WORKS=yes],
105        [LIBFFI_WORKS=no]
106    )
107    CFLAGS="$OLD_CFLAGS"
108    LIBS="$OLD_LIBS"
109    AC_LANG_POP(C)
110    AC_MSG_RESULT([$LIBFFI_WORKS])
111
112    if test "x$LIBFFI_WORKS" = xno; then
113      HELP_MSG_MISSING_DEPENDENCY([ffi])
114      AC_MSG_ERROR([Found libffi but could not link and compile with it. $HELP_MSG])
115    fi
116
117    AC_MSG_CHECKING([if libffi should be bundled])
118    if test "x$enable_libffi_bundling" = "x"; then
119      AC_MSG_RESULT([no])
120      ENABLE_LIBFFI_BUNDLING=false
121    elif  test "x$enable_libffi_bundling" = "xno"; then
122      AC_MSG_RESULT([no, forced])
123      ENABLE_LIBFFI_BUNDLING=false
124    elif  test "x$enable_libffi_bundling" = "xyes"; then
125      AC_MSG_RESULT([yes, forced])
126      ENABLE_LIBFFI_BUNDLING=true
127    else
128      AC_MSG_ERROR([Invalid value for --enable-libffi-bundling])
129    fi
130
131    # Find the libffi.so.X to bundle
132    if test "x${ENABLE_LIBFFI_BUNDLING}" = "xtrue"; then
133      AC_MSG_CHECKING([for libffi lib file location])
134      if test "x${LIBFFI_LIB_PATH}" != x; then
135        if test -e ${LIBFFI_LIB_PATH}/libffi.so.?; then
136          LIBFFI_LIB_FILE="${LIBFFI_LIB_PATH}/libffi.so.?"
137        else
138          AC_MSG_ERROR([Could not locate libffi.so.? for bundling in ${LIBFFI_LIB_PATH}])
139        fi
140      else
141        # If we don't have an explicit path, look in a few obvious places
142        if test "x${OPENJDK_TARGET_CPU}" = "xx86"; then
143          if test -e ${SYSROOT}/usr/lib/libffi.so.? ; then
144            LIBFFI_LIB_FILE="${SYSROOT}/usr/lib/libffi.so.?"
145          elif test -e ${SYSROOT}/usr/lib/i386-linux-gnu/libffi.so.? ; then
146            LIBFFI_LIB_FILE="${SYSROOT}/usr/lib/i386-linux-gnu/libffi.so.?"
147          else
148            AC_MSG_ERROR([Could not locate libffi.so.? for bundling])
149          fi
150        elif test "x${OPENJDK_TARGET_CPU}" = "xx86_64"; then
151          if test -e ${SYSROOT}/usr/lib64/libffi.so.? ; then
152            LIBFFI_LIB_FILE="${SYSROOT}/usr/lib64/libffi.so.?"
153          elif test -e ${SYSROOT}/usr/lib/x86_64-linux-gnu/libffi.so.? ; then
154            LIBFFI_LIB_FILE="${SYSROOT}/usr/lib/x86_64-linux-gnu/libffi.so.?"
155          else
156            AC_MSG_ERROR([Could not locate libffi.so.? for bundling])
157          fi
158        else
159          # Fallback on the default /usr/lib dir
160          if test -e ${SYSROOT}/usr/lib/libffi.so.? ; then
161            LIBFFI_LIB_FILE="${SYSROOT}/usr/lib/libffi.so.?"
162          else
163            AC_MSG_ERROR([Could not locate libffi.so.? for bundling])
164          fi
165        fi
166      fi
167      # Make sure the wildcard is evaluated
168      LIBFFI_LIB_FILE="$(ls ${LIBFFI_LIB_FILE})"
169      AC_MSG_RESULT([${LIBFFI_LIB_FILE}])
170    fi
171  fi
172
173  AC_SUBST(LIBFFI_CFLAGS)
174  AC_SUBST(LIBFFI_LIBS)
175  AC_SUBST(ENABLE_LIBFFI_BUNDLING)
176  AC_SUBST(LIBFFI_LIB_FILE)
177])
178