Automake:如何可移植地抛出错误并终止目标执行
解决
make check在缺少测试框架时提前终止并输出诊断信息的问题 需求:根据configure脚本的检测结果,当缺少测试框架(如check)时,执行make check要直接输出诊断信息并终止目标,避免用户误解测试已完成。
尝试过的方案及问题
方案1:在Makefile.am中使用
$(error)指令
触发Automake警告,因为$(error)是GNU Make的扩展,不符合POSIX标准,导致构建失败:automake-1.16: warnings are treated as errors Makefile.am:9: warning: error tests are disabled due to missing check: non-POSIX variable name Makefile.am:9: (probably a GNU make extension) make: *** [Makefile:347: ../Makefile.in] Error 1方案2:改用
@exit 1
执行make check时会先运行测试套件,之后才输出错误提示,用户容易误以为测试已经成功执行。
最终解决方案
通过在tests/Makefile.am中添加专属脚本,结合configure生成可执行脚本和AM_TESTS_FD_REDIRECT处理输出重定向,实现提前提示并终止的效果:
1. tests/Makefile.am 配置
根据MISSING_CHECK条件切换测试目标:当缺少测试框架时,指定错误提示脚本为测试脚本;正常情况则编译测试程序。AM_TESTS_FD_REDIRECT用于确保脚本的错误输出能正确显示。
# if check is missing, run script to inform the user of this if MISSING_CHECK check_SCRIPTS = no_test.sh # allow the script to echo to stderr AM_TESTS_FD_REDIRECT = 9>&2 endif if !MISSING_CHECK check_PROGRAMS = check_PROGRAMS += test_integer endif TESTS = $(check_SCRIPTS) $(check_PROGRAMS)
2. configure.ac 配置
让configure生成可执行的no_test.sh脚本:
AC_CONFIG_FILES([tests/no_test.sh], [chmod +x tests/no_test.sh])
3. tests/no_test.sh.in 脚本模板
输出诊断信息并以非零状态退出,确保make check直接终止:
#!/bin/bash # Inform the user that tests are disabled and how to change that. echo "No tests created due to missing check or user preference." >&2; echo "No tests created due to missing check or user preference." >&9; echo "To enable tests, run configure with --enable-tests=yes" >&2; echo "To enable tests, run configure with --enable-tests=yes" >&9; exit 1
内容的提问来源于stack exchange,提问作者caulder
相关产品推荐
相关产品推荐

