Gnu Makefile,编译应该失败
我有一组示例,我想确保它们无法编译。使用 *GNU Makefile 实现这一点的最佳方法是什么?
test_nocompile: $(NOCOMPILE_CPP)
for cpp in $(NOCOMPILE_CPP) ; do \
echo === $$cpp === ; \
if $(CXX) $(CXXFLAGS) -c -o fail.o $$cpp ; then echo ok ; else exit 1; fi ; \
done
正如你所看到的,我在这里遇到了几个困难:
- 访问shell-for变量
cpp
:$cpp
,\$cpp
和$$cpp
两者都不起作用。 - 即使使用我的
if
,make 也会在第一个 g++ 编译失败后停止。但这正是我想要的。我希望 g++ 编译失败被认为是正确的行为。 - Makefile 中的 Shell-for 循环并不是最好的主意。有更好的办法吗?我想不出一个,因为既然我预计编译会失败,我就没有正确的目标,对吗?
高级和可选:
- 如果我能够设法让上面的失败检查正常工作,我可以尝试第二次编译,这次使用额外的
-DEXCLUDE_FAIL
,这需要从我的示例中找出有问题的行,然后代码应该可以编译。有什么想法吗? - 或者应该为此编写一个Python脚本......? ;-)
编辑:
我认为我的“高级和可选”现在给了我一个非常好的主意。我可以再次使用 make 依赖性检查。这里只是一个粗略的草图:
NOCOMPILE_CPP := $(wildcard nocompile/ *.cpp)
NOCOMPILE_XFAILS := $(addsuffix .xfail,$(basename $(NOCOMPILE_CPP)))
%.xfail: %.cpp
if $(CXX) $(CXXFLAGS) -o $@ $< ; then exit 1 ; else echo OK ; fi
$(CXX) $(CXXFLAGS) -DEXCLUDE_FAILCODE -o $@ $<
test_nocompile: $(NOCOMPILE_XFAILS)
这优雅吗?然后我只需要弄清楚 -DEXCLUDE_FAILCODE
如何使失败的测试工作......不简单,但可行。我认为这可以做到,对吗?
I have a collection of examples that I want to make sure they fail to compile. What is the best way to to that with a *GNU Makefile?
test_nocompile: $(NOCOMPILE_CPP)
for cpp in $(NOCOMPILE_CPP) ; do \
echo === $cpp === ; \
if $(CXX) $(CXXFLAGS) -c -o fail.o $cpp ; then echo ok ; else exit 1; fi ; \
done
As you can see, I have several difficulties here:
- Accessing the shell-for variable
cpp
:$cpp
,\$cpp
and$$cpp
both do not work. - Even with my
if
the make stops after the first g++-compile fails. But thats exactly what I want. I want failing to g++-compile to be considered the correct behaviour. - Shell-for-loops in Makefiles are not the best idea. Is there a better way? I can not think of one, because since I expect the compiles to fail, I do not have a correct target, right?
Advanced and optional:
- If I could manage to have the fail-check above working, I could try a second pass of compilation, this time with an additional
-DEXCLUDE_FAIL
, which takes out the offending line from my examples, and then the code should compile. Any idea? - or should write a Python script for that...? ;-)
Edit:
I think my "advanced and optional" gave me a very good idea right now. I could use makes dependency checking again. Just a rough sketch here:
NOCOMPILE_CPP := $(wildcard nocompile/ *.cpp)
NOCOMPILE_XFAILS := $(addsuffix .xfail,$(basename $(NOCOMPILE_CPP)))
%.xfail: %.cpp
if $(CXX) $(CXXFLAGS) -o $@ lt; ; then exit 1 ; else echo OK ; fi
$(CXX) $(CXXFLAGS) -DEXCLUDE_FAILCODE -o $@ lt;
test_nocompile: $(NOCOMPILE_XFAILS)
Is this elegant? Then I only have to work out how -DEXCLUDE_FAILCODE
can make the failing tests work.... Non-trivial, but doable. I think that could do it, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
echo
中得到什么?if
then 中的条件。现在,当文件未编译时它就会退出,并且 AFAIU 你需要相反的东西。$(CXX)
并返回正确的错误代码(如果未编译则返回 0)。然后你就可以用这个脚本得到正常的目标了。我不太了解 GNU make 的细节,也许内置的东西是可能的。高级&可选:
1. 首先让我们开始工作:)
2.我个人不使用python,因此这里没有必要=:P
echo
?if
then. Right now it quits when the file doesn't compile and AFAIU you need the opposite.$(CXX)
and return a proper error code (0 if it doesn't compile). Then you may have normal targets with this script. I'm not very good with specifics of GNU make, probably it's possible with the builtin stuff.Advanced & optional:
1. Let's first make the thing work:)
2. Personally i don't use python, therefore don't see a need here =:P