如何在 autotools 项目中 add/use a --with-package arg?
How to add/use a --with-package arg in an autotools project?
我正在尝试添加强制路径以在我的 autotools 项目中查找 headers。用户将配置如下。
$ configure --with-newproj=/path/to/newproj
然后我希望能够以某种方式使用 src/Makefile 中的给定路径。我就是这样。
$ newproj_CFLAGS = -I${NEWPROJ_HOME}/include @CHECK_CFLAGS@
到目前为止我有以下内容,这似乎在命令行上输出了路径。
AC_ARG_WITH([newproj],
AS_HELP_STRING([--with-newproj],
[The top-level directory to the newproj installation.]),
[with_newproj=yes],
[with_newproj=no])
echo "==== with-newproj := \"$with_newproj\""
但我不确定如何在 CFLAGS 行中使用它或使其成为强制性的。
with_newproj=yes
的说法似乎很奇怪。如果用户实际上提供了路径,您将用字符串 yes
替换它。如果他们指定了 --with-newproj
而没有指定路径,那么 Autoconf 将自动执行的操作就是多余的。
既然你说路径是强制性的,with_newproj=no
论点也似乎很奇怪。如果用户未指定路径,您可以提供 no
作为所需路径。
对于AC_ARG_WITH
来说,这样比较合适:
AC_ARG_WITH([newproj],
[AS_HELP_STRING([--with-newproj=<path>],
[The top-level directory to the newproj installation.])],
[AS_IF([test -d "$with_newproj"],
[AC_MSG_NOTICE([Using newproj from "$with_newproj"])],
[AC_MSG_ERROR([Specify an existing directory for newproj, not "$with_newproj"])])],
[AC_MSG_ERROR([Use --with-newproj=<path> to specify the path to newproj])])
如果用户提供--with
(或--without
)参数那么Autoconf会自动设置shell变量with_newproj
为用户指定的值,或者yes
或 no
,视情况而定。如果他们不这样做,那么 Autoconf 将不会自动设置它,并且由于该参数对您来说是强制性的,所以这是一个错误案例。
但我认为您的主要问题是关于如何将该信息传输到 Makefile。这里的中心概念是“output variable”。这是 configure
中的一个 shell 变量,其值被替换到 AC_OUTPUT
处理的模板中。此外,Automake 自动为其中的每一个提供一个 make
变量(并且 Makefile.am
使用这些而不是直接使用 Autoconf 替换是惯用的)。设置它的主要 Autoconf 宏是 AC_SUBST
:
NEWPROJ_HOME="$with_newproj"
AC_SUBST([NEWPROJ_HOME])
我正在尝试添加强制路径以在我的 autotools 项目中查找 headers。用户将配置如下。
$ configure --with-newproj=/path/to/newproj
然后我希望能够以某种方式使用 src/Makefile 中的给定路径。我就是这样。
$ newproj_CFLAGS = -I${NEWPROJ_HOME}/include @CHECK_CFLAGS@
到目前为止我有以下内容,这似乎在命令行上输出了路径。
AC_ARG_WITH([newproj],
AS_HELP_STRING([--with-newproj],
[The top-level directory to the newproj installation.]),
[with_newproj=yes],
[with_newproj=no])
echo "==== with-newproj := \"$with_newproj\""
但我不确定如何在 CFLAGS 行中使用它或使其成为强制性的。
with_newproj=yes
的说法似乎很奇怪。如果用户实际上提供了路径,您将用字符串 yes
替换它。如果他们指定了 --with-newproj
而没有指定路径,那么 Autoconf 将自动执行的操作就是多余的。
既然你说路径是强制性的,with_newproj=no
论点也似乎很奇怪。如果用户未指定路径,您可以提供 no
作为所需路径。
对于AC_ARG_WITH
来说,这样比较合适:
AC_ARG_WITH([newproj],
[AS_HELP_STRING([--with-newproj=<path>],
[The top-level directory to the newproj installation.])],
[AS_IF([test -d "$with_newproj"],
[AC_MSG_NOTICE([Using newproj from "$with_newproj"])],
[AC_MSG_ERROR([Specify an existing directory for newproj, not "$with_newproj"])])],
[AC_MSG_ERROR([Use --with-newproj=<path> to specify the path to newproj])])
如果用户提供--with
(或--without
)参数那么Autoconf会自动设置shell变量with_newproj
为用户指定的值,或者yes
或 no
,视情况而定。如果他们不这样做,那么 Autoconf 将不会自动设置它,并且由于该参数对您来说是强制性的,所以这是一个错误案例。
但我认为您的主要问题是关于如何将该信息传输到 Makefile。这里的中心概念是“output variable”。这是 configure
中的一个 shell 变量,其值被替换到 AC_OUTPUT
处理的模板中。此外,Automake 自动为其中的每一个提供一个 make
变量(并且 Makefile.am
使用这些而不是直接使用 Autoconf 替换是惯用的)。设置它的主要 Autoconf 宏是 AC_SUBST
:
NEWPROJ_HOME="$with_newproj"
AC_SUBST([NEWPROJ_HOME])