Home | History | Annotate | Line # | Download | only in config
      1 # gcc-plugin.m4 -*- Autoconf -*-
      2 # Check whether GCC is able to be built with plugin support.
      3 
      4 dnl Copyright (C) 2014 Free Software Foundation, Inc.
      5 dnl This file is free software, distributed under the terms of the GNU
      6 dnl General Public License.  As a special exception to the GNU General
      7 dnl Public License, this file may be distributed as part of a program
      8 dnl that contains a configuration script generated by Autoconf, under
      9 dnl the same distribution terms as the rest of that program.
     10 
     11 # Check for plugin support.
     12 # Respects --enable-plugin.
     13 # Sets the shell variables enable_plugin and pluginlibs.
     14 AC_DEFUN([GCC_ENABLE_PLUGINS],
     15   [# Check for plugin support
     16    AC_ARG_ENABLE(plugin,
     17    [AS_HELP_STRING([--enable-plugin], [enable plugin support])],
     18    enable_plugin=$enableval,
     19    enable_plugin=yes; default_plugin=yes)
     20 
     21    pluginlibs=
     22    plugin_check=yes
     23 
     24    case "${host}" in
     25      *-*-mingw*)
     26        # Since plugin support under MinGW is not as straightforward as on
     27        # other platforms (e.g., we have to link import library, etc), we
     28        # only enable it if explicitly requested.
     29        if test x"$default_plugin" = x"yes"; then
     30          enable_plugin=no
     31        elif test x"$enable_plugin" = x"yes"; then
     32          # Use make's target variable to derive import library name.
     33          pluginlibs='-Wl,--export-all-symbols -Wl,--out-implib=[$]@.a'
     34 	 plugin_check=no
     35        fi
     36      ;;
     37      *-*-darwin*)
     38        if test x$build = x$host; then
     39 	 export_sym_check="nm${exeext} -g"
     40        elif test x$host = x$target; then
     41 	 export_sym_check="$gcc_cv_nm -g"
     42        else
     43 	 export_sym_check=
     44        fi
     45      ;;
     46      *)
     47        if test x$build = x$host; then
     48 	 export_sym_check="$ac_cv_prog_OBJDUMP -T"
     49        elif test x$host = x$target; then
     50 	 export_sym_check="$gcc_cv_objdump -T"
     51        else
     52 	 export_sym_check="$ac_cv_prog_OBJDUMP -T"
     53        fi
     54      ;;
     55    esac
     56 
     57    if test x"$enable_plugin" = x"yes" -a x"$plugin_check" = x"yes"; then
     58 
     59      AC_MSG_CHECKING([for exported symbols])
     60      if test "x$export_sym_check" != x; then
     61        echo "int main() {return 0;} int foobar() {return 0;}" > conftest.c
     62        ${CC} ${CFLAGS} ${LDFLAGS} conftest.c -o conftest$ac_exeext > /dev/null 2>&1
     63        if $export_sym_check conftest$ac_exeext | grep foobar > /dev/null; then
     64 	 : # No need to use a flag
     65 	 AC_MSG_RESULT([yes])
     66        else
     67 	 AC_MSG_RESULT([yes])
     68 	 AC_MSG_CHECKING([for -rdynamic])
     69 	 ${CC} ${CFLAGS} ${LDFLAGS} -rdynamic conftest.c -o conftest$ac_exeext > /dev/null 2>&1
     70 	 if $export_sym_check conftest$ac_exeext | grep foobar > /dev/null; then
     71 	   plugin_rdynamic=yes
     72 	   pluginlibs="-rdynamic"
     73 	 else
     74 	   plugin_rdynamic=no
     75 	   enable_plugin=no
     76 	 fi
     77 	 AC_MSG_RESULT([$plugin_rdynamic])
     78        fi
     79      else
     80        AC_MSG_RESULT([unable to check])
     81      fi
     82 
     83      # Check -ldl
     84      saved_LIBS="$LIBS"
     85      AC_SEARCH_LIBS([dlopen], [dl])
     86      if test x"$ac_cv_search_dlopen" = x"-ldl"; then
     87        pluginlibs="$pluginlibs -ldl"
     88      fi
     89      LIBS="$saved_LIBS"
     90 
     91      # Check that we can build shared objects with -fPIC -shared
     92      saved_LDFLAGS="$LDFLAGS"
     93      saved_CFLAGS="$CFLAGS"
     94      saved_CXXFLAGS="$CXXFLAGS"
     95      case "${host}" in
     96        *-*-darwin*)
     97 	 CFLAGS=`echo $CFLAGS | sed s/-mdynamic-no-pic//g`
     98 	 CFLAGS="$CFLAGS -fPIC"
     99 	 CXXFLAGS=`echo $CXXFLAGS | sed s/-mdynamic-no-pic//g`
    100 	 CXXFLAGS="$CXXFLAGS -fPIC"
    101 	 LDFLAGS="$LDFLAGS -shared -undefined dynamic_lookup"
    102        ;;
    103        *)
    104 	 CFLAGS="$CFLAGS -fPIC"
    105 	 CXXFLAGS="$CXXFLAGS -fPIC"
    106 	 LDFLAGS="$LDFLAGS -fPIC -shared"
    107        ;;
    108      esac
    109      AC_MSG_CHECKING([for -fPIC -shared])
    110      AC_TRY_LINK(
    111        [extern int X;],[return X == 0;],
    112        [AC_MSG_RESULT([yes]); have_pic_shared=yes],
    113        [AC_MSG_RESULT([no]); have_pic_shared=no])
    114      if test x"$have_pic_shared" != x"yes" -o x"$ac_cv_search_dlopen" = x"no"; then
    115        pluginlibs=
    116        enable_plugin=no
    117      fi
    118      LDFLAGS="$saved_LDFLAGS"
    119      CFLAGS="$saved_CFLAGS"
    120      CXXFLAGS="$saved_CXXFLAGS"
    121 
    122      # If plugin support had been requested but not available, fail.
    123      if test x"$enable_plugin" = x"no" ; then
    124        if test x"$default_plugin" != x"yes"; then
    125 	 AC_MSG_ERROR([
    126    Building GCC with plugin support requires a host that supports
    127    -fPIC, -shared, -ldl and -rdynamic.])
    128        fi
    129      fi
    130    fi
    131 ])
    132 
    133 dnl
    134 dnl
    135 dnl GCC_PLUGIN_OPTION
    136 dnl    (SHELL-CODE_HANDLER)
    137 dnl
    138 AC_DEFUN([GCC_PLUGIN_OPTION],[dnl
    139 AC_MSG_CHECKING([for -plugin option])
    140 
    141 plugin_names="liblto_plugin.so liblto_plugin-0.dll cyglto_plugin-0.dll"
    142 plugin_option=
    143 for plugin in $plugin_names; do
    144   plugin_so=`${CC} ${CFLAGS} --print-prog-name $plugin`
    145   if test x$plugin_so = x$plugin; then
    146     plugin_so=`${CC} ${CFLAGS} --print-file-name $plugin`
    147   fi
    148   if test x$plugin_so != x$plugin; then
    149     plugin_option="--plugin $plugin_so"
    150     break
    151   fi
    152 done
    153 dnl Check if ${AR} $plugin_option rc works.
    154 AC_CHECK_TOOL(AR, ar)
    155 if test "${AR}" = "" ; then
    156   AC_MSG_ERROR([Required archive tool 'ar' not found on PATH.])
    157 fi
    158 touch conftest.c
    159 ${AR} $plugin_option rc conftest.a conftest.c
    160 if test "$?" != 0; then
    161   AC_MSG_WARN([Failed: $AR $plugin_option rc])
    162   plugin_option=
    163 fi
    164 rm -f conftest.*
    165 if test -n "$plugin_option"; then
    166   $1="$plugin_option"
    167   AC_MSG_RESULT($plugin_option)
    168 else
    169   AC_MSG_RESULT([no])
    170 fi
    171 ])
    172