如何将 Makefile 目标传递给另一个目标

How to pass a Makefile target to another

所以我有一个很大的 Makefile,运行 是我针对我的特定项目的所有测试。每个目标都是一组不同的测试。该脚本将 运行 目标,将其输出存储到一个临时文件中。 目前目标看起来像这样:

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; }
                                                       

执行它的指令如下:

make count test=add

我想知道的是,当我 运行 执行命令时是否无法指定 test= 以便它看起来像这样:

make count add

然后添加目标将执行,如下所示:

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.
        

make 的每个命令行参数要么是一个选项、一个变量赋值,要么是一个目标。没有办法把一个论点当作其他任何东西来对待。因此,当您 运行 make check add 时,add 将成为 make 尝试构建的目标,并且无法以任何其他方式考虑它。

正如@Beta 建议的那样,如果您愿意将测试名称嵌入到目标中,例如 make count_add,那么您可以这样做:

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)

如果您不想这样做,唯一可能的解决方案是使用 ifeq 结合 $(MAKECMDGOALS) 将您的 makefile 分成两部分:一个有 .DEFAULT 什么都不做的目标(忽略像 add 等额外的目标)和第二个 运行 那些目标。