Autoconf check for program and fail if not found

2020-07-02 10:03发布

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.

6条回答
混吃等死
2楼-- · 2020-07-02 10:29

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.])])
查看更多
Lonely孤独者°
3楼-- · 2020-07-02 10:35

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
查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-07-02 10:39

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.])])
查看更多
可以哭但决不认输i
5楼-- · 2020-07-02 10:44

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
查看更多
beautiful°
6楼-- · 2020-07-02 10:46

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
查看更多
女痞
7楼-- · 2020-07-02 10:50

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.])
查看更多
登录 后发表回答