Makefile:从输出中沉默/隐藏规则的部分配方

问题描述 投票:0回答:1
CB_RED := $$(echo "\e[1;41m")
CB_LIGHT_CYAN := $$(echo "\e[1;106m")
C_END := $$(echo "\e[0m")

PRINT_BUILDING = echo $(CB_LIGHT_CYAN)Building $@$(C_END)
PRINT_BUILDING_FAILED = echo $(CB_RED)Building of $@ failed!$(C_END)

foo.txt:
    @$(PRINT_BUILDING)
    touch foo.txt || ($(PRINT_BUILDING_FAILED) && exit 1)

输出:

如何在不隐藏

|| (echo $(echo "\e[1;41m")Building of foo.txt failed!$(echo "\e[0m") && exit 1)
的情况下隐藏
touch foo.txt

makefile gnu-make
1个回答
0
投票

你不能。你可以做这样的事情:

foo.txt:
        @$(PRINT_BUILDING)
        @echo 'touch foo.txt'
        @touch foo.txt || ($(PRINT_BUILDING_FAILED) && exit 1)

您可以将其捆绑到这样的函数中:

CHECKCMD = @echo '$1'; $1 || ($(PRINT_BUILDING_FAILED) && exit 1)

foo.txt:
        @$(PRINT_BUILDING)
        $(call CHECKCMD, touch foo.txt)

需要注意的一点是,如果命令中包含逗号,则不起作用;你必须做这样的事情:

foo.txt:
        @$(PRINT_BUILDING)
        $(call CHECKCMD, (touch foo.txt,bar.txt))

(我没有测试过,但我认为它会起作用)

© www.soinside.com 2019 - 2024. All rights reserved.