Autoconf check for program and fail if not found
Asked Answered
L

6

16

I'm creating a project and using GNU Autoconf tools to do the configuring and making. I've set up all my library checking and header file checking but can't seem to figure out how to check if an executable exists on the system and fail if it doesn't exist.

I've tried:

AC_CHECK_PROG(TEST,testprogram,testprogram,AC_MSG_ERROR(Cannot find testprogram.))

When I configure it runs and outputs:

Checking for testprogram... find: `testprogram. 15426 5 ': No such file or directory

but does not fail.

Lauralee answered 20/9, 2011 at 19:52 Comment(0)
C
12

Try this which is what I just lifted from a project of mine, it looks for something called quantlib-config in the path:

# borrowed from a check for gnome in GNU gretl: def. a check for quantlib-config
AC_DEFUN(AC_PROG_QUANTLIB, [AC_CHECK_PROG(QUANTLIB,quantlib-config,yes)])
AC_PROG_QUANTLIB
if test x"${QUANTLIB}" == x"yes" ; then
    # use quantlib-config for QL settings
    [.... more stuff omitted here ...]
else
    AC_MSG_ERROR([Please install QuantLib before trying to build RQuantLib.])
fi
Cosmorama answered 20/9, 2011 at 19:55 Comment(2)
Thanks for help, I used it create a shorter version which suits my needs.Lauralee
Yes, sure that looks good if you just need to bail out if the binary is not found.Cosmorama
L
26

I found this to be the shortest approach.

AC_CHECK_PROG(FFMPEG_CHECK,ffmpeg,yes)
AS_IF([test x"$FFMPEG_CHECK" != x"yes"], [AC_MSG_ERROR([Please install ffmpeg before configuring.])])
Lauralee answered 20/9, 2011 at 20:50 Comment(2)
I’m not sure if it makes any difference in practice, but note that M4sh documents the AS_IF construct as a portable version. This is how the conditional is expressed with AS_IF: AS_IF([test x"$PATH_XMLTO" != x"yes"], [AC_MSG_ERROR([Please install ffmpeg before installing.])])Cas
Even more portably: AS_VAR_IF([FFMPEG_CHECK], [yes], [], [AC_MSG_ERROR(...)])Westonwestover
C
12

Try this which is what I just lifted from a project of mine, it looks for something called quantlib-config in the path:

# borrowed from a check for gnome in GNU gretl: def. a check for quantlib-config
AC_DEFUN(AC_PROG_QUANTLIB, [AC_CHECK_PROG(QUANTLIB,quantlib-config,yes)])
AC_PROG_QUANTLIB
if test x"${QUANTLIB}" == x"yes" ; then
    # use quantlib-config for QL settings
    [.... more stuff omitted here ...]
else
    AC_MSG_ERROR([Please install QuantLib before trying to build RQuantLib.])
fi
Cosmorama answered 20/9, 2011 at 19:55 Comment(2)
Thanks for help, I used it create a shorter version which suits my needs.Lauralee
Yes, sure that looks good if you just need to bail out if the binary is not found.Cosmorama
V
4

Similar to the above, but has the advantage of also being able to interact with automake by exporting the condition variable

AC_CHECK_PROG([ffmpeg],[ffmpeg],[yes],[no])
AM_CONDITIONAL([FOUND_FFMPEG], [test "x$ffmpeg" = xyes])
AM_COND_IF([FOUND_FFMPEG],,[AC_MSG_ERROR([required program 'ffmpeg' not found.])])
Valais answered 7/5, 2013 at 20:43 Comment(0)
L
2

Stumbled here while looking for this issue, I should note that if you want to have your program just looked in pathm a runtime test is enough:

if ! which programname >/dev/null ; then
   AC_MSG_ERROR([Missing programname]
fi
Lingo answered 4/4, 2014 at 7:37 Comment(0)
W
2

When using AC_CHECK_PROG, this is the most concise version that I've run across is:

AC_CHECK_PROG(BOGUS,[bogus],[bogus],[no])
test "$BOGUS" == "no" && AC_MSG_ERROR([Required program 'bogus' not found.])

When the program is missing, this output will be generated:

./configure
...cut...
checking for bogus... no
configure: error: Required program 'bogus' not found.

Or when coupled with the built-in autoconf program checks, use this instead:

AC_PROG_YACC
AC_PROG_LEX

test "$YACC" == ":" && AC_MSG_ERROR([Required program 'bison' not found.])
test "$LEX" == ":" && AC_MSG_ERROR([Required program 'flex' not found.])
Wearable answered 3/4, 2016 at 19:0 Comment(0)
W
1

This is not exactly a short approach, it's rather a general purporse approach (although when there are dozens of programs to check it might be also the shortest approach). It's taken from a project of mine (the prefix NA_ stands for “Not Autotools”).

A general purpose macro

dnl  ***************************************************************************
dnl  NA_REQ_PROGS(prog1, [descr1][, prog2, [descr2][, etc., [...]]])
dnl
dnl  Checks whether one or more programs have been provided by the user or can
dnl  be retrieved automatically. For each program `progx` an uppercase variable
dnl  named `PROGX` containing the path where `progx` is located will be created.
dnl  If a program is not reachable and the user has not provided any path for it
dnl  an error will be generated. The program names given to this function will
dnl  be advertised among the `influential environment variables` visible when
dnl  launching `./configure --help`.
dnl  ***************************************************************************
AC_DEFUN([NA_REQ_PROGS], [
    m4_if([$#], [0], [], [
        AC_ARG_VAR(m4_translit([$1], [a-z], [A-Z]), [$2])
        AS_IF([test "x@S|@{]m4_translit([$1], [a-z], [A-Z])[}" = x], [
            AC_PATH_PROG(m4_translit([$1], [a-z], [A-Z]), [$1])
            AS_IF([test "x@S|@{]m4_translit([$1], [a-z], [A-Z])[}" = x], [
                AC_MSG_ERROR([$1 utility not found])
            ])
        ])
        m4_if(m4_eval([$# + 1 >> 1]), [1], [], [NA_REQ_PROGS(m4_shift2($*))])
    ])
])

Sample usage

NA_REQ_PROGS(
    [find],             [Unix find utility],
    [xargs],            [Unix xargs utility],
    [customprogram],    [Some custom program],
    [etcetera],         [Et cetera]
)

So that within Makefile.am you can do

$(XARGS)

or

$(CUSTOMPROGRAM)

and so on.

Features

  • It advertises the programs among the “influential environment variables” visible when the final user launches ./configure --help, so that an alternative path to the program can be provided
  • A bash variable named with the same name of the program, but upper case, containing the path where the program is located, is created
  • En error is thrown if any of the programs given have not been found and the user has not provided any alternative path for them
  • The macro can take infinite (couples of) arguments

When you should use it

  1. When the programs to be tested are vital for compiling your project, so that the user must be able to provide an alternative path for them and an error must be thrown if at least one program is not available at all
  2. When condition #1 applies to more than one single program, in which case there is no need to write a general purpose macro and you should just use your own customized code
Wira answered 26/3, 2019 at 1:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.