当前位置: 动力学知识库 > 问答 > 编程问答 >

command line - Delayed Windows cmd echo with GNU Make environment

问题描述:

I have few simple targets which create some files for me.

Example:

$(MAKE_INA):

@echo Building ASM compilation flags file $(notdir $(MAKE_INA))

@$(foreach i, $(sort $(ASMFLAGS) $(PFLAGS) $(ALL_INC_DIR) $(cppGetPreProcessorDefines)), $(shell echo $i >> [email protected] ))

The target works fine, the file is being created and echo text displayed, but in that order (first the file is build then the echo is shown on cmd.exe console).

I guess that is related somehow with output buffering, but I was not able to find the way to flush the echos immediately.

Any hint? Is it even possible?

I am using Gnu Make 4.0

网友答案:

You are mixing up contexts here.

The first @echo line is a recipe line and is run by the shell when the target runs.

The second $(foreach) line is within the rule but is a make context line and is evaluated by make before running the recipe lines. Within that line $(shell) is also a make command and is run during the make expansion of the recipe instead of being run by the shell at recipe execution time.

To do what you want you can just use:

$(MAKE_INA):
    @echo Building ASM compilation flags file $(notdir $(MAKE_INA))
    @printf "%s\\n" $(sort $(ASMFLAGS) $(PFLAGS) $(ALL_INC_DIR) $(cppGetPreProcessorDefines)) >> [email protected]

Which does the echoing at recipe execution time (so has the right order) and uses a single call to the printf built-in to output to the file instead of running N calls to echo.

Edit: For Windows cmd.exe compat you need to use echo $i >> [email protected] & as the $(foreach) body so that cmd.exe runs multiple commands correctly.

If you did want to keep the N echo calls then you could use:

$(MAKE_INA):
    @echo Building ASM compilation flags file $(notdir $(MAKE_INA))
    @$(foreach i, $(sort $(ASMFLAGS) $(PFLAGS) $(ALL_INC_DIR) $(cppGetPreProcessorDefines)), echo $i >> [email protected]; ))

Which has the $(foreach) output echo XXX >> [email protected]; ....; echo ZZZ >> [email protected]; as the recipe line to then execute during recipe execution.

分享给朋友:
您可能感兴趣的文章:
随机阅读: