Heredoc in a Makefile?

nalply picture nalply · May 3, 2011 · Viewed 19.1k times · Source

Is this possible at all and how?

Update: I need this because I create a file both from dynamic and static data.

Use case: I have a test directory. Each C file produces a test executable. With

SRCS = $(wildcard [a-z]*.c)

I can add new tests as needed and make will find the new tests, compile, run and valgrind them. I also use git. I would like .gitignoreto include the executables.

So there. How to create .gitignore and include static data, i.e. the files I want to be ignored (*.o and depend) and also the executables dynamically?

Answer

Ash Berlin-Taylor picture Ash Berlin-Taylor · Sep 11, 2011

Another GNU Make solution.

You can do it using the define and export commands as follows:

define GITIGNOREDS
*.o
depend
endef

SRCS = $(wildcard [a-z]*.c)
EXES = $(SRCS:.c=)


export GITIGNOREDS
.gitignore: $(SRCS)
    echo $(EXES) | sed 's/ /\n/g' > $@
    echo "$$GITIGNOREDS" >> $@

You have to be careful of make expansions (i.e. $(x)) inside the define block though.