Autoconf check for program and fail if not found

后端 未结 6 1845
小鲜肉
小鲜肉 2021-02-19 03:52

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 ou

相关标签:
6条回答
  • 2021-02-19 04:11

    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.])])
    
    0 讨论(0)
  • 2021-02-19 04:20

    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
    
    0 讨论(0)
  • 2021-02-19 04:23

    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.])
    
    0 讨论(0)
  • 2021-02-19 04: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.])])
    
    0 讨论(0)
  • 2021-02-19 04:27

    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
    0 讨论(0)
  • 2021-02-19 04:32

    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
    
    0 讨论(0)
提交回复
热议问题