Gnu Makefile,编译应该失败

发布于 2024-10-22 06:59:59 字数 1325 浏览 8 评论 0原文

我有一组示例,我想确保它们无法编译。使用 *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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2024-10-29 06:59:59
  1. 对我有用。您在 echo 中得到什么?
  2. 您需要否定 if then 中的条件。现在,当文件未编译时它就会退出,并且 AFAIU 你需要相反的东西。
  3. 为什么这不是一个好主意?但是您可以编写一个脚本来调用 $(CXX) 并返回正确的错误代码(如果未编译则返回 0)。然后你就可以用这个脚本得到正常的目标了。我不太了解 GNU make 的细节,也许内置的东西是可能的。

高级&可选:
1. 首先让我们开始工作:)
2.我个人不使用python,因此这里没有必要=:P

  1. Works for me. What do you get in echo?
  2. You need to negate the condition in if then. Right now it quits when the file doesn't compile and AFAIU you need the opposite.
  3. Why it's not a good idea? But you can write a script that will call $(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文