代码之家  ›  专栏  ›  技术社区  ›  Angel DC

makefile目标中的foreach和消音器

  •  0
  • Angel DC  · 技术社区  · 8 年前

    我在特定目标的生成文件中使用foreach。

    my_Target:
        $(foreach x,$(MY_TARGET_DIRS), @echo "  BUILD   $(x)";$(Q)$(MAKE) --directory=$(x) LIB_DIR=$(MY_LIB_DIR) $(MY_TARGET_RULES); )
    

    这是为了在打印字符串以告知正在进行的构建之后,调用包含MY\u TARGET\u DIRS的每个指定目录的makefile。

    Q变量定义如下:

    # Silent per default, 'make V=1' shows all compiler calls. 
    ifneq ($(V),1)
        Q := @
    endif
    

    因此,如果定义了V=1,则make命令是详细的。

    对于V=1,输出日志为:

    make my_Target V=1 
    BUILD   /dir1
    make[1]: Entering directory `/dir1'
    make[1]: Nothing to be done for `elf'.
    make[1]: Nothing to be done for `bin'.
    make[1]: Leaving directory `/dir1'
    /bin/sh: @echo: command not found
    make[1]: Entering directory `/dir2'
    make[1]: Nothing to be done for `elf'.
    make[1]: Nothing to be done for `bin'.
    make[1]: Leaving directory `/dir2'
    

    如果我不定义V=1,则输出日志为:

    make my_Target 
    BUILD   /dir1
    /bin/sh: @make: command not found
    /bin/sh: @echo: command not found
    /bin/sh: @make: command not found
    make: *** [my_Target] Error 127
    

    我如何才能正确地获得我想要的行为?

    再见

    1 回复  |  直到 8 年前
        1
  •  1
  •   HardcoreHenry    8 年前

    将 @ ( $(Q) )在配方开始时:

    my_Target:
        $(Q)$(foreach x,$(MY_TARGET_DIRS), echo "  BUILD   $(x)";$(MAKE) --directory=$(x) LIB_DIR=$(MY_LIB_DIR) $(MY_TARGET_RULES); )
    

    请注意 foreach 将输出一行配方,然后 @ 将适用于所有内容。另一种选择是,如果您想更有趣,可以为每个目标目录创建一个规则:

    MY_TARGET_DIRS_TARGS=$(MY_TARGET_DIRS:%=%/.phony)
    
    my_Target: $(MY_TARGET_DIRS_TARG)
    
    $(MY_TARGET_DIRS_TARGS):
         @echo "  BUILD   $(x)"
         $(Q)(MAKE) --directory=$(x) LIB_DIR=$(MY_LIB_DIR) $(MY_TARGET_RULES);
    
    .PHONY: $(MY_TARGET_DIRS_TARGS)