10

Assuming I have the following Makefile:

.PHONY: mytarget
mytarget:
    echo "Hello World!"

running make mytarget gives the following output:

echo "Hello World!"
Hello World!

what I would like to see is something like the below

[mytarget] echo "Hello World!"
[mytarget] Hello World!

Is this possible?

I've been searching http://www.gnu.org/software/make/manual/make.html but haven't yet found a solution (granted, I haven't yet read the whole manual)

2
  • 2
    The directive is .PHONY: mytarget. Just FYI. Jul 23, 2014 at 2:48
  • Thanks for catching that Etan, corrected now!
    – zsepi
    Jul 23, 2014 at 7:36

3 Answers 3

5
+50

I'm not sure that there is something readily available for what you want in GNUmake (based on your link, I assume that we are speaking about this flavor). You can emulate it somehow, but I'm afraid no solution will be perfect.

use debug flags

make --debug=j mytarget will let make output information about each command it is about to launch, including the target that triggers it. In your case, the result will be:

Live child 0x021d2360 (mytarget) PID 10747 
echo Hello World!
Hello World!
Reaping winning child 0x021d2360 PID 10747 
Removing child 0x021d2360 PID 10747 from chain.

You have the information, but it's not really pretty, and I guess that parallel runs (-j options) won't produce a correct output.

Change SHELL

You can set the SHELL variable to tell make which interpreter is supposed to be used for launching the command. This can be (ab)used to perform an echo before actually launching the command. with the following line in the Makefile

SHELL=/bin/sh -c 'echo -n "[$@]: " && $$@'

we obtain

echo "Hello World!"
[mytarget]: "Hello World!"

This is not perfect, as the echoing of the whole line is done directly by make and thus not impacted by SHELL

change the command itself

If you can modify the commands themselves, it is possible to let make expand them so that the final output is what you want:

echo_target=@echo -n '[$@]: ' && echo $(1) && echo -n '[$@]: ' && $(1)

.PHONY: all bla

mytarget:
    $(call echo_target, echo "Hello World!")

will result in

[mytarget]: echo Hello World!
[mytarget]: Hello World!

but this requires adding the call to each command of your Makefile

Update: stitching it together

As mentioned by Natan Reisner, the SHELL command proposed above is not correct for arguments that contain spaces. As a matter of fact, it also occured to me that you can use SHELL to do the echoing before launching the command: Here is a new proposal that work with arguments containing spaces, and echoes the command with $@ prepended:

 SHELL=/bin/sh -c 'echo -n "[$@] " && echo "$$@" && echo -n "[$@] " && eval "$$@"'

on the following target

mytarget:
    echo 'Hello World!'
    @touch foo\ bar
    @ls -l "foo bar"

we obtain this result

echo 'Hello World!'
[mytarget] echo 'Hello World!'
[mytarget] Hello World!
[mytarget] echo foo\ bar
[mytarget] foo bar
[mytarget] touch foo\ bar
[mytarget] [mytarget] ls -l "foo bar"
[mytarget] -rw-rw-r-- 1 virgile virgile 0 juil. 23 08:44 foo bar

Note that there is still an issue for commands that do not produce any output, such as touch. This could probably be mitigated by redirecting the output of the command in a variable, and echoing only if the resulting length is non-zero.

5
  • 2
    The Change SHELL version has issues when arguments include spaces. It doesn't preserve arguments correctly. Jul 23, 2014 at 3:52
  • 1
    Indeed. I've updated the answer with a better SHELL replacement, thanks.
    – Virgile
    Jul 23, 2014 at 7:18
  • That answer doesn't seem to work with make 4.0 though it does on make 3.81. Jul 23, 2014 at 14:06
  • 1
    I've indeed only 3.81 installed. From what I understand from the Changelog, for make >= 3.82, it might be better to play with .SHELLFLAGS variable rather than SHELL, e.g. something like .SHELLFLAGS=-c 'echo -n "[$$0] " && eval "$$@"' $@ might do the trick.
    – Virgile
    Jul 23, 2014 at 15:24
  • Indeed. That seems to work. I tried to play with .SHELLFLAGS but didn't get it to work myself. I was failing to think about needing to set $0 I think. Though given that I would probably go with $@ in the echo directly and a trailing argument of - or some other placeholder but <shrug>. Jul 23, 2014 at 15:29
4

As a complement to the Change SHELL option from @Virgile (which has issues, see my comment on his answer).

You can get the prefix on the executed shell lines by adjusting PS4.

SHELL=/bin/bash -x
export PS4=[$@]

all: mytarget blah

.PHONY: mytarget blah

mytarget:
        @echo 'Hello World!'

blah:
        @echo '$@: output'

$ make
[mytarget] echo 'Hello World!'
Hello World!
[blah] echo 'blah: output'
blah: output

In theory this means that if you can get the Change SHELL option to work in a safe manner (I couldn't but I feel like I'm missing something here) you should be able to get what you want.

2
  • 1
    Apparently, PS4 is POSIX, so you don't need bash for that, sh is sufficient. On the other hand, note that it gets output on stderr which may render redirections and pipes more difficult.
    – Virgile
    Jul 23, 2014 at 15:31
  • @Virgile Indeed. That bash selection was not one of necessity as much as one of typing habit. And yes, that might very well complicate things. Jul 23, 2014 at 15:36
3

Here is the output using remake (a fork of GNU make) when run on the file Makefile you gave:

$ remake -x 
Reading makefiles...
Updating goal targets....
File 'mytarget' does not exist.
Must remake target 'mytarget'.
Makefile:3: target 'mytarget' does not exist
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
echo "Hello World!"
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Hello World!
Successfully remade target file 'mytarget'.

The number after the colon in Makefile gives the line number inside Makefile of the target. The chevrons separate the command that is invoked from the output it produces. If a target fails, you'll get a target stack trace similar to what you'd get in a programming language.

Although this is already a bit more than you asked, in the unlikely event you need more there is a debugger available if you use -X instead of -x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.