Home > Back-end >  How to pass a Makefile target to another
How to pass a Makefile target to another

Time:04-17

so I have a large Makefile that runs all of my tests for my particular project. Each target is a different group of tests. The script will run the target, store its output into a temporary file. Currently the target looks like this:

count:
# USE: make count test=<name of test to run>
# Save output to target 
        $(MAKE) $(test) > last_output.txt
        cat last_output.txt
# Print Passed
        @cat last_output.txt | { grep -E -w "SUCCESS|RELAX-PASS" || true; }
# Print Failed
        @cat last_output.txt | { grep -E -w "FAILED" || true; }
# Failed Count
        @echo "\e[1;31mFAILED:\e[1;37m"
        @cat last_output.txt | { grep -c "FAILED" || true; }
# Passed Count
        @echo "\e[1;32mPASSED:\e[1;37m"
        @cat last_output.txt | grep -E -c "SUCCESS|RELAX-PASS"
# Count all
        @echo "TOTAL: "
        @cat last_output.txt | { grep -E -c "FAILED|SUCCESS|RELAX-PASS" || true; }
                                                       

And the instruction to execute it looks like:

make count test=add

What I was wondering was if I could not specify test= when I'm running the command so that it would look like this:

make count add

and then the add target will execute which looks like:

add:
         clear && run.pl add_0.asm
         clear && run.pl add_1.asm
         clear && run.pl add_2.asm
         clear && run.pl add_3.asm
         ect.
        

CodePudding user response:

Every command line argument to make is either an option, a variable assignment, or a target. There's no possible way to treat an argument as anything else. So when you run make check add, the add will be a target that make will attempt to build and there's no way to have it be considered any other way.

As @Beta suggests if you are willing to embed to the test name into the target, like make count_add, then you can do this:

SHOW = \
    cat last_output.txt; \
    grep -E -w "SUCCESS|RELAX-PASS" < last_output.txt; \
    grep -E -w "FAILED" < last_output.txt; \
    echo "\e[1;31mFAILED:\e[1;37m"; \
    grep -c "FAILED" < last_output.txt; \
    echo "\e[1;32mPASSED:\e[1;37m"; \
    grep -E -c "SUCCESS|RELAX-PASS" < last_output.txt; \
    echo "TOTAL: "; \
    grep -E -c "FAILED|SUCCESS|RELAX-PASS" < last_output.txt;
    true

count:
        $(MAKE) $(test) > last_output.txt
        @$(SHOW)

count_%:
        $(MAKE) $* > last_output.txt
        @$(SHOW)

If you don't want to do that, the only possible solution is to use ifeq combined with $(MAKECMDGOALS) to break your makefile up into two sections: one that has a .DEFAULT target that does nothing (to ignore the extra targets like add etc.) and a second that runs those targets.

  • Related