Gnu Makefile, compile should fail
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 se开发者_StackOverflowveral 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 $@ $< ; then exit 1 ; else echo OK ; fi
$(CXX) $(CXXFLAGS) -DEXCLUDE_FAILCODE -o $@ $<
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?
- Works for me. What do you get in
echo
? - 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. - 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
精彩评论