Insert a new-line in a Makefile $(foreach ) loop
As already mentioned in Jonathan Wakely's answer, the straightforward answer is
define newline
endef
Interestingly enough, for all chars except newline there is an easier way to get non-printable characters into variables with some help from the shell, e.g.:
tab := $(shell printf '\011')
It won't work here, though, because the builtin shell function replaces all newlines by spaces.
The version above is still a bit fragile though, in particular when combining with automake which will silently remove the second consecutive newline from the input file, turning newline
into an empty variable. To force it to keep the newline, we can extend the snippet:
blank :=
define newline
$(blank)
endef
Finally, to actually get a separate shell command for each dependency, you need to run the generated string through eval
:
define generate-rule =
my.list : $(1)
rm -f $$@
$(foreach F,$$^,$(newline)$(tab)echo "${F}" >> $@)
endef
$(eval $(call generate-rule,$(deps)))
You can define a variable that expands to a newline like so:
define NEWLINE
endef
Now you can use $(NEWLINE)
to expand to a newline.
This won't change anything though, the foreach
will still generate a single command with embedded newlines between the statements, rather than a single command with ;
between the statements.