抑制 make 规则错误输出

发布于 2024-09-14 13:37:04 字数 255 浏览 11 评论 0原文

我有一个创建目录的规则

bin:
    -mkdir $@

但是在第一次生成目录后,我收到以下输出:

mkdir bin
mkdir: cannot create directory `bin': File exists
make: [bin] Error 1 (ignored)

有什么方法我只能在目录不存在时运行规则,或者在目录已经存在时抑制输出存在吗?

I have an rule that creates a directory

bin:
    -mkdir $@

However after the first time the directory has been generated, I receive this output:

mkdir bin
mkdir: cannot create directory `bin': File exists
make: [bin] Error 1 (ignored)

Is there some way I can only run the rule if the directory doesn't exist, or suppress the output when the directory already exists?

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

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

发布评论

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

评论(4

南风起 2024-09-21 13:37:04

抑制 make: error ... (ignored) 输出的另一种方法是附加 || true 表示可能失败的命令。使用 grep 检查 LaTeX 日志文件中的错误的示例:

undefined:
  @grep -i undefined *.log || true

没有 || true,当 grep 找不到任何匹配项时发出抱怨。

这适用于所有命令,而不仅仅是 mkdir;这就是我添加这个答案的原因。

Another way to suppress the make: error ... (ignored) output is to append || true to a command that could fail. Example with grep that checks for errors in a LaTeX log file:

undefined:
  @grep -i undefined *.log || true

Without the || true, make complains when grep fails to find any matches.

This works for all commands, not just mkdir; that's why I added this answer.

缱绻入梦 2024-09-21 13:37:04

命令行上的前导“-”已忽略该错误。如果您确实想丢失来自 mkdir 的错误消息,请使用 I/O 重定向:

bin:
    -mkdir bin 2> /dev/null

不过,您仍然会收到来自 make 的“忽略”警告,因此可能是最好使用 mkdir 选项,当目标已存在时不会导致失败,即 -p 选项:

MKDIR_P = mkdir -p

bin:
    ${MKDIR_P} $@

-p 选项实际上创建了给定路径上缺少的所有目录,因此它可以在一次调用中生成多个目录,但副作用是它不会为已经存在的目录生成错误。这确实假设了 mkdir 的 POSIX-ish 实现;较旧的机器可能不支持它(尽管它已经成为标准很长时间了)。

The error is ignored already by the leading '-' on the command line. If you really want to lose the error messages from mkdir, use I/O redirection:

bin:
    -mkdir bin 2> /dev/null

You will still get the 'ignored' warning from make, though, so it might be better to use the option to mkdir that doesn't cause it to fail when the target already exists, which is the -p option:

MKDIR_P = mkdir -p

bin:
    ${MKDIR_P} $@

The -p option actually creates all the directories that are missing on the given paths, so it can generate a a number of directories in one invocation, but a side-effect is that it does not generate an error for already existing directories. This does assume a POSIX-ish implementation of mkdir; older machines may not support it (though it has been standard for a long time now).

神妖 2024-09-21 13:37:04

处理目录创建的传统方法是使用所依赖的标记文件并创建目录作为副作用。在进行 distclean 或任何“真正干净”的目标时删除标记文件:

bin/.dirstamp:
    mkdir -p $(DIRS)
    touch $@

bin/foo: bin/.dirstamp
    $(MKFOO) -o $@

distclean:
    rm -rf bin

原因如下:每当创建/删除 bin 中的文件时,包含目录的 mtime 已更新。如果目标依赖于 bin,那么下次运行 make 时,它将重新创建不需要的文件。

The traditional way to handle directory creation is to use a stamp file that is depended on and creates the dir as a side effect. Remove the stamp file when making distclean or whatever your "really clean" target is:

bin/.dirstamp:
    mkdir -p $(DIRS)
    touch $@

bin/foo: bin/.dirstamp
    $(MKFOO) -o $@

distclean:
    rm -rf bin

The reason for this is as follows: whenever a file in bin is created/removed, the mtime of the containing directory is updated. If a target depends on bin, then the next time make runs, it will then recreate files that it doesn't need to.

烟柳画桥 2024-09-21 13:37:04

好吧,我最终得到了这个构造,也许有人会发现它有用或者可以评论它:

BINDIR = .
TMPDIR = tmp
OUTDIRS = $(BINDIR) $(TMPDIR)
$(OUTDIRS):
    @test -d $@ || mkdir $@

Well I ended up with this construct, maybe someone will find it useful or can comment on it:

BINDIR = .
TMPDIR = tmp
OUTDIRS = $(BINDIR) $(TMPDIR)
$(OUTDIRS):
    @test -d $@ || mkdir $@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文