Search code examples
makefilebuild-processgnu-makedefault-value

GNU Make, generate file with default settings/content when absent


I would like make to either copy a file from the source tree into the target/build directory if it exits or generate an empty/default file if not.

It would be easy to do the following:

target/settings.json: src/settings.json
        cp $? $@

src/settings.json:
        echo "default..." > $@

But that taints the source repository with a file that could inadvertently be checked into RCS.

Is there a simple make rule that can copy the file if it exits, or just generate the target with a command/copy from some other source?

A GNU-Make specific solution is fine


Solution

  • You can check if the file exists using $(wildcard), so maybe something like this:

    ifeq ($(wildcard src/settings.json),)
        SETTINGS = tmp/settings.json
    else
        SETTINGS = src/settings.json
    endif
    
    target/settings.json: $(SETTINGS)
        cp $? $@
    
    tmp/settings.json:
        echo "default..." > $@