抑制 make 规则错误输出
我有一个创建目录的规则
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
抑制
make: error ... (ignored)
输出的另一种方法是附加|| true
表示可能失败的命令。使用 grep 检查 LaTeX 日志文件中的错误的示例:没有
|| 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: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.
命令行上的前导“
-
”已忽略该错误。如果您确实想丢失来自mkdir
的错误消息,请使用 I/O 重定向:不过,您仍然会收到来自
make
的“忽略”警告,因此可能是最好使用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 frommkdir
, use I/O redirection:You will still get the 'ignored' warning from
make
, though, so it might be better to use the option tomkdir
that doesn't cause it to fail when the target already exists, which is the-p
option: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 ofmkdir
; older machines may not support it (though it has been standard for a long time now).处理目录创建的传统方法是使用所依赖的标记文件并创建目录作为副作用。在进行
distclean
或任何“真正干净”的目标时删除标记文件:原因如下:每当创建/删除
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: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 onbin
, then the next timemake
runs, it will then recreate files that it doesn't need to.好吧,我最终得到了这个构造,也许有人会发现它有用或者可以评论它:
Well I ended up with this construct, maybe someone will find it useful or can comment on it: