用它们之间的grep制作多命令配方?

时间:2019-10-25 21:37:39

标签: shell grep gnu-make

我需要为我的对象运行一个多命令配方。但是,如果先前命令的grep导致找到特定的字符串,则不得运行后续命令。我该怎么写?

run_sim:
    $(MAKE) compile; \
    (Here I need grep of results of log of compile command) && $(MAKE) elaborate; \
    (Here I need grep of results of log of elaborate command) && $(MAKE) simulate;

如果grep返回并找到了字符串,那么make一定不要执行下一个命令,而要停止。

2 个答案:

答案 0 :(得分:0)

  

我已经有3个独立的make对象(编译,精心设计,模拟)。现在,我需要将所有3个对象放到一个make对象中,这样当我运行'make run_sim'时,它将同时运行所有3个对象。我怎么做?

就是这样:

.PHONY: run_sim
run_sim:
    $(MAKE) compile
    $(MAKE) elaborate
    $(MAKE) simulate

但是最好创建一个适当的依赖树:

.PHONY: compile
compile: output.out ;

output.out: something.c
     gcc something.c -o output.out

.PHONY: elaborate
elaborate: compile
    do_the_elaboration


.PHONY: simulate
simulate: compile
    do_the_simulation

.PHONY: run_sim
run_sim: compile elaborate simulate ;

查看GNU make introductionGNU make manual并查看有关GNU make: error handling的信息是明智的。有关伪造目标的信息可以在GNU make: phony targetsstackoverflow: What is the purpose of .PHONY target?

中找到

答案 1 :(得分:0)

Verilog?

我在类似情况下所做的事情可能是我自己的“使新手/电气工程师犯错误”,这是

compile.summary: compile.log
    @echo Making $@
    @grep -q PASSES $<
    @echo Compile PASSES > $@

因为如果compile.log中不存在“ PASSES”,grep -q将失败,因此Make会停止运行。

您只需使elaborate依赖于compile.summary

如果要搜索类似'* E'或'Error'的内容,则可以改用grep -qv 'Error',其中'-v'如果在compile.log中找到字符串,则grep将返回错误。 / p>

但是,这还不全是用户友好的。在失败的情况下,您将只看到“制作compile.log”,后跟...什么都没有。

对于我来说,它实际上是在处理永不返回0的程序,因此必须对日志进行grep处理,以确保可接受的失败与致命的失败。

随着另一个答案的到来,以实际文件为目标来管理依赖项要容易得多。 PHONY编译目标可能意味着您在进行详细说明时总是重新编译...,因为它没有文件+时间戳来知道上一次编译与输入的新鲜度。最好让compile.log作为详细说明所依赖的实际文件。

相关问题