我正在创建一个项目并使用GNU Autoconf工具进行配置和制作。我已经设置了所有的库检查和头文件检查,但似乎无法弄清楚如何检查系统上是否存在可执行文件,如果不存在则会失败。
我试过了:
AC_CHECK_PROG(TEST,testprogram,testprogram,AC_MSG_ERROR(Cannot find testprogram.))
当我configure
它运行和输出:
Checking for testprogram... find: `testprogram. 15426 5 ': No such file or directory
但不会失败。
尝试这是我刚从我的一个项目中取出的东西,它在路径中寻找一个叫做quantlib-config
的东西:
# 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
我发现这是最短的方法。
AC_CHECK_PROG(FFMPEG_CHECK,ffmpeg,yes)
AS_IF([test x"$FFMPEG_CHECK" != x"yes"], [AC_MSG_ERROR([Please install ffmpeg before configuring.])])
与上述类似,但具有通过导出条件变量也能够interact with automake的优点
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.])])
使用AC_CHECK_PROG时,这是我遇到的最简洁的版本:
AC_CHECK_PROG(BOGUS,[bogus],[bogus],[no])
test "$BOGUS" == "no" && AC_MSG_ERROR([Required program 'bogus' not found.])
当程序丢失时,将生成此输出:
./configure
...cut...
checking for bogus... no
configure: error: Required program 'bogus' not found.
或者当与内置的autoconf程序检查结合使用时,请使用以下内容:
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.])
在寻找这个问题的时候偶然发现,我应该注意,如果你想让你的程序只是在pathm中看一个运行时测试就足够了:
if ! which programname >/dev/null ; then
AC_MSG_ERROR([Missing programname]
fi
这不是一个简短的方法,它是一种普遍的purporse方法(虽然有几十个程序要检查它可能也是最短的方法)。它来自a project of mine(前缀NA_
代表“Not Autotools”)。
通用宏
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($*))])
])
])
样品用法
NA_REQ_PROGS(
[find], [Unix find utility],
[xargs], [Unix xargs utility],
[customprogram], [Some custom program],
[etcetera], [Et cetera]
)
所以在Makefile.am
你可以做到
$(XARGS)
要么
$(CUSTOMPROGRAM)
等等。
特征
./configure --help
时可见的“有影响的环境变量”中宣传程序,以便可以提供程序的替代路径什么时候应该使用它