Search code examples
gccconfigureautoconfautomakem4

Is there a way to check for compile flags that doesn't require the autoconf-archive package?


The AX_CHECK_COMPILE_FLAG macro is available in most distributions in the autoconf-archive package, but unfortunately when missing that package the error is impossible to understand:

./configure: 5358: Syntax error: word unexpected (expecting ")")

This is the configure.ac line that causes the error:

AX_CHECK_COMPILE_FLAG(-fcx-fortran-rules, CFLAGS="$CFLAGS -fcx-fortran-rules")

Is there a way to check if a compile flag works without using an AX_ check?

Should I just copy-paste this AX_ check into my configure.ac? That seems like a bit of a hack, but it would work...is there something better?


Solution

  • First, the prudent thing to do when using any macro which does not come with autoconf or automake is to add a line to configure.ac which will check that the generated configure file does not contain the name of the macro (only its expansion which should not contain the macro name):

    m4_pattern_forbid([AX_CHECK_COMPILE_FLAG])dnl
    

    This will make autoreconf fail and produce a halfway decent error message instead of silently generating an invalid configure from configure.ac when the AX_CHECK_COMPILE_FLAG macro definition cannot be found.

    Then you can copy the *.m4 file containing the definition of AX_CHECK_COMPILE_FLAG into your project's source tree (e.g. into a m4/ subdirectory) and then add a line to the top-level Makefile.am which tells aclocal where to look for macro definitions:

    ACLOCAL_AMFLAGS = -I m4
    

    In addition to that, you need to add the m4/whatever-the-name-is.m4 file to EXTRA_DIST in some Makefile.am or Makefile include file so that it becomes possible to run autoreconf on an unpacked dist tarball.

    I personally prefer to use AC_CONFIG_MACRO_DIR([auto-m4]) in configure.ac and ACLOCAL_AMFLAGS = -I auto-m4 -I project-m4 in the top-level Makefile.am to separate *.m4 files of different origin into different directories in my source tree (auto-m4/ for the *.m4 files copied there during autoreconf runs, and project-m4/ for the *.m4 files I have written myself for the project).

    Note that while AC_CONFIG_MACRO_DIRS([...]) exists to help with multiple m4 macro directories and avoids the necessity for the above ACLOCAL_AMFLAG line, AC_CONFIG_MACRO_DIRS only works with autoconf and automake. As soon as your build system also uses libtool/libtoolize or gettext/gettextize/autopoint, they will fail upon encountering AC_CONFIG_MACRO_DIRS. So stay with AC_CONFIG_MACRO_DIR and ACLOCAL_AMFLAGS.