Reid
Reid

Reputation: 2078

Can GNU Make add a prefix to recipe echo lines?

I am using GNU Make and would like it to add a prefix to each recipe command as it's echoed. For example, given this makefile:

foo:
    echo bar
    echo baz

Actual behavior:

$ make -B foo
echo bar
bar
echo baz
baz

Desired behavior:

$ make -B foo
+ echo bar
bar
+ echo baz
baz

(In this case, + is the prefix.)

It would be nice if I could configure what the prefix is, but that's not essential.

Upvotes: 2

Views: 1144

Answers (2)

bobbogo
bobbogo

Reputation: 15483

The easiest way is to change the SHELL make variable. In this case, running the shell with the -x parameter should do the trick. Something like

$ make SHELL:='bash -x'

Or even

$ make .SHELLFLAGS:='-x -c'

in this case. For more sophisticated output a quick shell script fits the bill:

$ cat script
#!/bin/bash
echo "EXECUTING [$*]"
$*

followed by

$ make SHELL:=./script .SHELLFLAGS:=

Upvotes: 2

Beta
Beta

Reputation: 99124

This isn't something Make does very naturally. I don't think you can do better than a horrible kludge:

define trick
    @echo + $(1)
    @$(1)
endef

foo:
    $(call trick, echo bar)
    $(call trick, echo baz)

Upvotes: 3

Related Questions