15.Site配置

本系列文章均翻译自Autoconf官方文档:Autoconf Manual,github同步项目:question

configure支持几种本地的配置策略。

15.2 使用外部软件

AC_ARG_WITH (package, help-string, [action-if-given], [action-if-not-given])

如果用户运行configure的时候给了选项--with-package 或 --without-package,运行shell命令action-if-given,没有则运行action-if-not-givenpackage应该只由字母、破折号、加号、点组成。

--with-package默认值是yes,可传参然后通过***${withval}***获取。
--without-package等于--with-package=no,不可带参。

选项的参数可以通过shell变量${withval}获取,其值的命名为with_package,其中所有的非字母符号都用_代替。

AC_ARG_WITH(
    [smbios-manufacturer],
    [AS_HELP_STRING([--with-smbios-manufacturer=NAME],
                    [SMBIOS manufacturer name @<:@default=oVirt@:>@])],
    [SMBIOS_MANUFACTURER="${withval}"],
    [SMBIOS_MANUFACTURER="oVirt"])
AC_SUBST([SMBIOS_MANUFACTURER]) # 一定要注册一下,才能给automake使用

configure对应的语句

# Check whether --with-smbios-manufacturer was given.
if test "${with_smbios_manufacturer+set}" = set; then :
  withval=$with_smbios_manufacturer; SMBIOS_MANUFACTURER="${withval}"
else
  SMBIOS_MANUFACTURER="oVirt"
fi

与AC_ARG_ENABLE的区别在于此宏用于指定用户自己的已安装的软件,即可选择自己的程序;而AC_ARG_ENABLE则是类似于功能开关。

15.3 选择包选项

AC_ARG_ENABLE (feature, help-string, [action-if-given], [action-if-not-given])

如果用户给了configure选项--enable-feature--disable-feature(–enable-feature=yes/no),执行shell命令action-if-given;如果没有给出选项,执行action-if-not-givenfeature应该只由字母、破折号、加号、点组成。

选项的参数可以获取,其命名方式为with_feature,其中所有的非字母符号都用_代替。

help-string类似于AC_ARG_WITH;你应该使用AS_HELP_STRING格式化help-string

AC_ARG_ENABLE(
    [hooks],
    [AS_HELP_STRING(
        [--enable-hooks],
        [build hooks packages @<:@default=no@:>@]
    )],
    ,
    [enable_hooks="no"]
)

效果:
~$ ./configure --help
--enable-hooks          build hooks packages [default=no]

configure对应的语句

# Check whether --enable-hooks was given.
if test "${enable_hooks+set}" = set; then :
  enableval=$enable_hooks;
else
  enable_hooks="no"

fi

15.4 让帮助文档漂亮

AS_HELP_STRING (left-hand-side, right-hand-side [indent-column = ‘26’], [wrap-column = ‘79’])

格式化帮助文档,使其看起来跟官方的一样。

AC_ARG_WITH([foo],
    [AS_HELP_STRING([--with-foo],
       [use foo (default is no)])],
    [use_foo=$withval],
    [use_foo=no])

Then the last few lines of ‘configure --help’ appear like this:

  --enable and --with options recognized:
  --with-foo              use foo (default is no)

通常indent-columnwrap-column不必指定。

猜你喜欢

转载自blog.csdn.net/SHRINKSHR/article/details/85837395