Makefiles: 强制Shell在执行每个命令时打印当前工作目录。

huangapple go评论64阅读模式
英文:

Makefiles: force the shell to print the cwd for each executed command

问题

相关:https://stackoverflow.com/questions/11004695/making-make-print-commands-before-executing-when-not-using-cmake

我有任务要重新收集用于编译我们项目的全部优化标志的完整列表。想法是编译整个项目并解析编译日志以收集使用的所有标志。

我们的项目使用make编译,但我们的900个make文件并不都遵循相同的模式,特别是静默模式让我头疼。有使用make -s$(MAKE) -sMAKEFLAGS += -s,在行首使用@,依赖于环境变量的条件结构,一行中有多个编译命令,用;分隔等情况。

使用make SHELL="/bin/bash -x" | grep -E "Entering|Leaving" 2> log.txt"很好,因为我可以获取已展开的所有变量的所有已使用命令的完整列表,并且每个单独的命令在不同的行上,这对于解析非常有用。但问题是make的消息EnteringLeaving,因为这允许我跟踪每个“子文件夹”在每个时刻的位置,以便我可以按模块收集执行的命令。

但是静默模式又出现了。如果在静默模式下执行子make,那么Entering|Leaving行根本不会被打印出来。

如果在-x的情况下,有一种方法可以强制bash在执行每个命令时打印pwd bash所在的位置吗?因为到目前为止,我唯一看到的解决方案是编辑所有文件以删除静默模式的所有用法,这并不容易,因为我甚至可能编辑根本不是make文件的文件(并非所有make文件都必然命名为makefile)。

还有其他解决方案吗?

注意: --dry-run不是一个选项。它也有自己的问题。使用SHELL=/bin/bash -x目前是最佳选择。

英文:

Related: https://stackoverflow.com/questions/11004695/making-make-print-commands-before-executing-when-not-using-cmake

I have the task to recollect the full list of optimizations flags that are being used to compile our project. The idea is to compile the whole project and parse the compilation log to collect all the flags that are being used.

Our project is compiled using make, but our 900 makefiles doesn't follow the same patterns everywhere and, in particular, the silent mode is giving me headaches. There are usages of make -s, of $(MAKE) -s, of MAKEFLAGS += -s, of @ at the beginning of lines, usages of conditional constructs that depends on environmental variables, several compilation commands in one line separated by ;, and so on.

The usage of make SHELL="/bin/bash -x" | grep -E "Entering|Leaving" 2> log.txt" is great, because I get the whole list of used commands with all variables already expanded and each individual command in a different line, which is awesome for parsing purposes, but the problem is with the make's messages of Entering and Leaving, because that is what allows me to track which "subfolder" I'm in at each moment, so I can gather the executed commands on a per-module basis.

But the silent mode strikes again. If a submake is executed in silent mode, the Entering|Leaving lines are not printed at all.

If there a way, together with -x, to force bash to print the pwd bash is in at the moment of executing each command? Because the only solution I see so far is to edit all the files to remove all usages of the silent mode, which is not easy because I could even edit files that are not even makefiles in the first place (not all makefiles are necesarily named makefile).

Any other solution?

NOTE: --dry-run is not an option. It has its own problems as well. Using SHELL=/bin/bash -x is the best option so far.

答案1

得分: 2

定义一个名为`/tmp/bash`的文本文件:

pwd
/bin/bash "$@"


然后运行 `chmod +x /tmp/bash`

定义另一个名为`/tmp/Makefile`的文本文件:

all:
date
sleep 1


现在在`/tmp`目录中运行 `SHELL=/tmp/bash make`,您将得到:

$ SHELL=/tmp/bash make
date
/tmp
Sun 11 Jun 12:56:17 2023
sleep 1
/tmp


*更新*

实际上,我们不需要`/tmp/bash`,只需运行 `SHELL="pwd; exec /bin/bash"` make
英文:

Define a text file /tmp/bash as :

pwd
/bin/bash "$@"

then run chmod +x /tmp/bash

Define another text file /tmp/Makefile as :

all:
        date
        sleep 1

Now run SHELL=/tmp/bash make in /tmp, you'll get :

$ SHELL=/tmp/bash make
date
/tmp
Sun 11 Jun 12:56:17 2023
sleep 1
/tmp

Update

Actually, we don't need /tmp/bash, just run SHELL="pwd; exec /bin/bash" make

huangapple
  • 本文由 发表于 2023年6月12日 04:26:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定