Search code examples
bashmakefilegnu-make

Can you wrap each command in GNU's make?


I want to inject a transparent wrap command on each shell command in a make file. Something like the time shell command. (However, not the time command. This is a completely different command.)

Is there a way to specify some sort of wrapper or decorator for each shell command that GNU Make will issue?


Solution

  • Kind of. You can tell make to use a different shell.

    SHELL = myshell
    

    where myshell is a wrapper like

    #!/bin/sh
    time /bin/sh "$0" "$@"
    

    However, the usual way to do that is to prefix a variable to all command calls. While I can't see any show-stopper for the SHELL approach, the prefix approach has the advantage that it's more flexible (you can specify different prefixes for different commands, and override prefix values on the command line), and could be visibly faster.

    # Set Q=@ to not display command names
    TIME = time
    foo:
        $(Q)$(TIME) foo_compiler
    

    And here's a complete, working example of a shell wrapper:

    #!/bin/bash
    
    RESULTZ=/home/rbroger1/repos/knl/results
    if [  "$1" == "-c" ] ; then
        shift
    fi
    
    strace -f -o `mktemp $RESULTZ/result_XXXXXXX` -e trace=open,stat64,execve,exit_group,chdir /bin/sh -c "$@"  | awk '{if (match("Process PID=\d+ runs in (64|32) bit",$0) == 0) {print $0}}'
    
    # EOF