1

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

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
Ernelli
  • 3,960
  • 3
  • 28
  • 34

1 Answers1

2

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..." > $@
Moldova
  • 1,641
  • 10
  • 13
  • You probably want `.INTERMEDIATE: $(SETTINGS)` in the `tmp/` block to have make delete the temporary file when done. – Etan Reisner Apr 27 '15 at 13:19