GNU make的回溯信息

6
有没有办法让GNU make在失败时打印导致命令执行的目标的“回溯”?我经常处理严重混淆的makefile,同时解决在新系统上构建软件时的可移植性问题,似乎这对于make来说应该是一件非常简单的事情,可以大大帮助调试,但我找不到任何请求它的方式。我想看到的是类似于以下内容:
gcc: error: ...
make[2]: error: gcc ...
make[2]: error building target bar
make[2]: error building dependency bar for target foo
make[1]: error: make -C subdir
make[1]: error building target subdir
make[1]: error building dependency subdir for target all
...

展示整个依赖路径,以了解失败的命令是如何执行的。有没有办法做到这一点?

就此而言,“回溯”可能不是线性的,因为通常make的依赖图是一个DAG,而不是一棵树。它可以为目标“foo”目标“subdir”制作“bar”,并且可以为目标“subdir”和目标“all”制作目标“foo”,并为“all”制作“subdir”。我认为这并不比堆栈回溯更难生成,只是更难阅读... - Steve Jessop
1
如果您将其视为依赖项尝试,那么它就是一个DAG。但是在GNU Make的遍历中,有严格的依赖关系堆栈。因此,尽管“回溯”通常指调用堆栈,在这里类似的事情是依赖关系堆栈。 - rocky
3个回答

4

使用remake。它是GNU Make的一个修补版本,增加了更好的错误报告、以易懂的方式跟踪执行和调试器。


3

make -pmake -d 提供了有趣的信息,但并不完全符合您的要求。请参阅make的手册页面


这个问题的提问者可能会觉得使用 make -n 命令有所帮助。 - Carl Norum
是的,这些是你的朋友。如果你的 makefile 没有使用任何内置的 make 规则,你可以通过 -Rr 减少输出的冗长程度(你只需要其中一个,但我总是记不住哪个,所以我总是两个都用)。 - bobbogo

1

是的,remake 可以给你一个追踪回溯。这里是使用 remake 的 Makefile 运行示例:

remake --debugger Makefile
GNU Make 4.1+dbg0.91
适用于 x86_64-unknown-linux-gnu 构建
版权所有 (C) 1988-2014 自由软件基金会,Inc.
版权所有 (C) 2015 Rocky Bernstein.
许可证 GPLv3+:GNU GPL 版本3 或更高版本。
这是自由软件:您可以自由更改和重新分发它。
在法律允许的范围内,没有担保。
正在读取 makefile 文件...
正在更新 makefile 文件....
-> (/src/github/remake/Makefile:608)
Makefile:Makefile.in config.status
remake<0> bt
=>#0 /src/github/remake/Makefile 中的 Makefile:608
remake<1> s
-> (/src/github/remake/Makefile:594)
Makefile.in: 
remake<2> bt
=>#0 /src/github/remake/Makefile 中的 Makefile.in:594
  #1 /src/github/remake/Makefile 中的 Makefile:608
remake<3> s
-> (/src/github/remake/Makefile:618)
config.status: configure
remake<4> bt
=>#0 /src/github/remake/Makefile 中的 config.status:618
  #1 /src/github/remake/Makefile 中的 Makefile:608
remake<5> s
-> (/src/github/remake/Makefile:621)
configure: 
remake<6> bt
=<#0 /src/github/remake/Makefile 中的 configure:621
  #1 /src/github/remake/Makefile 中的 config.status:618
  #2 /src/github/remake/Makefile 中的 Makefile:608
remake<7> 
你还可以在特定的目标处设置断点(break),走到那里(continue)并backtrace。如果发生错误,您将得到一个追溯到崩溃时所在位置的回溯信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接