makefilegnu-makebsdmake

How to use shell-assignment operator with gmake?


I'm trying to keep my Makefile compatible between BSD and GNU make. It is not particularly complicated, but there is one spot, where I'd very much like to assign a value based on the output of a command.

GNU make manual states:

The shell assignment operator ‘!=’ can be used to execute a shell script and set a variable to its output. This operator first evaluates the right-hand side, then passes that result to the shell for execution. If the result of the execution ends in a newline, that one newline is removed; all other newlines are replaced by spaces. The resulting string is then placed into the named recursively-expanded variable.

This matches BSD make's behavior, so I tried to use it. Unfortunately, it does not work. When I try a simple Makefile like this:

HELLO!= echo Hello!

all:
        # HELLO is "${HELLO}"

I get simply:

# HELLO is ""

There are no errors, but the variable remains empty... This is on RHEL7, where gmake is of version 3.82...

Ironically, the construct works as expected with gmake-4.4.1 on my FreeBSD desktop -- where I don't need it, because FreeBSD's own make suits me just fine. Is there some trick for the older gmake, which RedHat installs, that would still be compatible with BSD make?


Solution

  • Implemented a work-around by creating two small files specific to each make-flavor. They perform flavor-specific things, and then include the common Makefile:

    HELLO!= echo Hello!
    
    .include "Makefile"
    
    HELLO=  $(shell echo Hello!)
    
    include Makefile
    

    Now each make starts with its own makefile-wrapper, and then proceeds to the common file with the rules, etc.