你能将每个命令包装在 GNU 的 make 中吗?

发布于 2024-11-07 11:44:47 字数 373 浏览 0 评论 0 原文

我想在 make 文件中的每个 shell 命令上注入透明的换行命令。类似于 time shell 命令。 (但是,不是 time 命令。这是一个完全不同的命令。)

有没有办法指定某种包装器或 装饰器 用于 GNU Make 会发行吗?

I want to inject a transparent wrap command on each shell command in a make file. Something like the time shell command. (However, not the time command. This is a completely different command.)

Is there a way to specify some sort of wrapper or decorator for each shell command that GNU Make will issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

背叛残局 2024-11-14 11:44:47

有点儿。您可以告诉 make 使用不同的 shell。

SHELL = myshell

其中 myshell 是一个包装器,例如

#!/bin/sh
time /bin/sh "$0" "$@"

但是,通常的方法是将变量添加到所有命令调用的前缀。虽然我看不到 SHELL 方法有任何阻碍,但前缀方法的优点是更灵活(您可以为不同的命令指定不同的前缀,并在命令行上覆盖前缀值),并且可以明显更快。

# Set Q=@ to not display command names
TIME = time
foo:
    $(Q)$(TIME) foo_compiler

这是一个完整的、有效的 shell 包装器示例:

#!/bin/bash

RESULTZ=/home/rbroger1/repos/knl/results
if [  "$1" == "-c" ] ; then
    shift
fi

strace -f -o `mktemp $RESULTZ/result_XXXXXXX` -e trace=open,stat64,execve,exit_group,chdir /bin/sh -c "$@"  | awk '{if (match("Process PID=\d+ runs in (64|32) bit",$0) == 0) {print $0}}'

# EOF

Kind of. You can tell make to use a different shell.

SHELL = myshell

where myshell is a wrapper like

#!/bin/sh
time /bin/sh "$0" "$@"

However, the usual way to do that is to prefix a variable to all command calls. While I can't see any show-stopper for the SHELL approach, the prefix approach has the advantage that it's more flexible (you can specify different prefixes for different commands, and override prefix values on the command line), and could be visibly faster.

# Set Q=@ to not display command names
TIME = time
foo:
    $(Q)$(TIME) foo_compiler

And here's a complete, working example of a shell wrapper:

#!/bin/bash

RESULTZ=/home/rbroger1/repos/knl/results
if [  "$1" == "-c" ] ; then
    shift
fi

strace -f -o `mktemp $RESULTZ/result_XXXXXXX` -e trace=open,stat64,execve,exit_group,chdir /bin/sh -c "$@"  | awk '{if (match("Process PID=\d+ runs in (64|32) bit",$0) == 0) {print $0}}'

# EOF
花开雨落又逢春i 2024-11-14 11:44:47

我做了一些事情,比如修改 Makefile 中的 PATH 环境变量,以便执行一个目录,其中我的脚本链接到我想要包装的所有名称的 bin,而不是实际的 bin。然后,脚本将查看它是如何被调用的,并使用包装的命令执行实际的 bin。

IE。 exec time "$0" "$@"

这些天我通常只更新 Makefile 本身中的目标。在我看来,将所有修改保留在一个文件中通常比管理链接目录更好。

我遵循Gilles的回答。这是比我更好的答案。

I have done things like modify the PATH env variable in the Makefile so a directory with my script linked to all name the bins I wanted wrapped was executed rather than the actual bin. The script would then look at how it was called and exec the actual bin with the wrapped command.

ie. exec time "$0" "$@"

These days I usually just update the targets in the Makefile itself. Keeping all your modifications to one file is usually better IMO than managing a directory of links.

I defer to Gilles' answer. It's a better answer than mine.

把昨日还给我 2024-11-14 11:44:47

GNU make(1) 用于运行命令的程序由 SHELL make 变量指定。它将运行每个命令,因为

$SHELL -c <command>

您无法让 make 不放入 -c ,因为这是大多数 shell 所必需的。 -c 作为第一个参数 ($1) 传递, 作为单个参数字符串作为第二个参数传递 (<代码>$2)。

您可以编写自己的 shell 包装器,在前面添加所需的命令,同时考虑到 -c

#!/bin/sh
eval time "$2"

这将导致 time 在每个命令前面运行。您需要 eval,因为 $2 通常不是单个命令,并且可能包含需要扩展或处理的各种 shell 元字符。

The program that GNU make(1) uses to run commands is specified by the SHELL make variable. It will run each command as

$SHELL -c <command>

You cannot get make to not put the -c in, since that is required for most shells. -c is passed as the first argument ($1) and <command> is passed as a single argument string as the second argument ($2).

You can write your own shell wrapper that prepends the command that you want, taking into account the -c:

#!/bin/sh
eval time "$2"

That will cause time to be run in front of each command. You need eval since $2 will often not be a single command and can contain all sorts of shell metacharacters that need to be expanded or processed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文