Search code examples
linuxautotoolsautoconfm4

How to specify different feedback for different platforms if AC_CHECK_HEADER fails in autoconf/configure.ac?


I have a check for a header file in configure.ac in the source root

AC_CHECK_HEADER(log4c.h,
    [],
    [AC_MSG_ERROR([Couldn't find or include log4c.h])])

and I'd like to give different feedback on different platform to reflect different most straight forward ways of providing the header:

  • on Debian it should error with the message Couldn't find or include log4c.h. Install log4c using 'sudo apt-get install liblog4c-dev'
  • on OpenSUSE it should error with ... Install log4c using 'sudo yum install log4c-devel' (didn't research the package name, but you catch my drift)
  • on other systems (where I'm too lazy to research the package name) it should error with ... Install log4c by fetching ftp://.../log4c.tar.gz and installing with './configure && make && make install' in the source root

I

  • checked the AM_CONDITIONAL macro, but I don't get how to use it in configure.ac rather than in Makefile.am (as described in autoconf/automake: conditional compilation based on presence of library?)
  • found the tip to run esyscmd in stackoverflow.com/questions/4627900/m4-executing-a-shell-command, but adding esyscmd (/bin/echo abc) to configure.ac doesn't print anything when I run autoreconf --install --verbose --force.

Both answers describing the usage of conditional macros without the shell commands for the mentioned OS and links to predefined macros (like AC_CHECK_HEADER_DEBIAN, AC_CHECK_HEADER_SUSE, etc.) are appreciated.

The following configure.ac doesn't work:

AC_INIT([cndrvcups-common], [2.90], [krichter722@aol.de])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([foreign -Wall subdir-objects]) 
AC_PROG_CC
AM_PROG_AR
AM_PROG_CC_C_O

AC_MSG_NOTICE([Hello, world.])
AC_INCLUDES_DEFAULT
AC_CHECK_HEADER(check.h,
    [],
    [
        AS_IF (test "$(lsb_release -cs)" = "vivid", [echo aaaaaa], [echo bbbbbb])
    ])

LT_INIT # needs to be after AM_PROGS_AR
AC_CONFIG_HEADERS([config.h])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

because ./configure fails with

checking check.h usability... no
checking check.h presence... no
checking for check.h... no
./configure: line 4433: syntax error near unexpected token `;'
./configure: line 4433: `        if ; then :'

There's also ./configure: line 4427: #include: command not found which happens no matter whether AC_CHECK_HEADER is specified.


Solution

  • Your configure.ac is almost ok. The only problem is space between AS_IF and the parenthesis. No whitespace is allowed between a macro name and the opening parenthesis in m4 scripts. This is correct syntax:

    AC_CHECK_HEADER(check.h,
        [],
        [
            AS_IF(test "$(lsb_release -cs)" = "vivid", [echo aaaaaa], [echo bbbbbb])
        ])
    

    If you are looking for a way to detect different distros look for example at configure.ac of cgmanager.

    Update

    I noticed one more problem in your configure.ac.

    AC_INCLUDES_DEFAULT macro expands to a set of default includes and can't be used here. It is not needed also. It will be used by default in your AC_CHECK_HEADER macro, as you omit last parameter.

    This is the cause of line 4427: #include: command not found error you mention.

    Update to your comment

    First of all, running a system command itself, like lsb_release is not portable. You should first check, for example with AC_CHECK_PROG, for its presence.

    Regarding the syntax I would first get the output of the command using backticks: result=`lsb_release -cs` and later test resulting output: test "x$result" = "xvivid". x is needed to avoid problems with empty value in some shells.

    At last, I have doubts whether configure script is a proper place for all this distro specific messages. You may consider placing it in the README file.