1

When I execute Make, I'd like to know what shell commands / recipes are executed (and perhaps which line in which Makefile invoked these). Is there a way of doing this (print onto the stdout or write to a file) without modifying the Makefiles?

forgodsakehold
  • 870
  • 10
  • 26
  • Related: https://stackoverflow.com/questions/54753/tool-for-debugging-makefiles – Ray May 22 '19 at 05:45

1 Answers1

1

The commands will be echoed to stdout by default, unless you disabled that by prefixing them with @.

A few options you can pass to make:

  • -n: echos commands that would be run, but without running them. This will also echo commands that are prefixed with @.
  • -d: print debug output. This will output the process that make goes through in attempting to find rules that match the targets that it's trying to make. There will be a lot of useless information in here, as it will try many default build rules (like how to build .o files from .c). There are a few ways to cut down on the noise:
    • -r: disable implicit rules. If you don't depend on any of the default rules , this can be used in conjunction with -d to just print the parts you (mostly) care about
    • --debug=b: basic debug mode. prints what it's trying to make, but not any information about implicit rules.

None of these print the line numbers of the commands, but make -n --debug=b will print both the targets being built and the commands being run, so it's almost as good. Example below.

$ cat makefile:

c: a b
    cat $^ > $@

a:
    echo 'foo' > $@

b: a
    cat $^ > $@
    echo 'bar' >> $@

$ make -n --debug=b:

Reading makefiles...
Updating goal targets....
   File 'a' does not exist.
  Must remake target 'a'.
echo 'foo' > a
  Successfully remade target file 'a'.
   File 'b' does not exist.
  Must remake target 'b'.
cat a > b
echo 'bar' >> b
  Successfully remade target file 'b'.
 Prerequisite 'a' is newer than target 'c'.
 Prerequisite 'b' is newer than target 'c'.
Must remake target 'c'.
cat a b > c
Successfully remade target file 'c'.
Ray
  • 1,706
  • 22
  • 30