I came up with this cunning use of bash to remove the back-slash before the newline, allowing true multi-line shell fragments as make recipes.

SHELL:=bash -c 'exec $(SHELL) "$${@//\\$$'\''\012'\''/$$'\''\012'\''}"' --

test:
        if test 1 = 1 \
        then echo yes \
        else echo no \
        fi


Or for those who prefer:

NEW_SHELL:=bash -c 'exec $(SHELL) "$${@//\\$$'\''\012'\''/$$'\''\012'\''}"' --

test: SHELL=$(NEW_SHELL)
        if test 1 = 1 \
        then echo yes \
        else echo no \
        fi

with associated warnings about SHELL being set for dependencies (if any).

Why? Because my literate-programming tools which automatically quote characters based on file type make it all too easy to include a shell-script fragment in a makefile (with an $ automatically quoted to $$) and I wanted a way to allow multi-line shell script - which might only be multi-line because it includes a multi-line sed fragment (so the old "; \" line continuation trick for makefiles won't work).

This change makes it hard to have a single shell line represented as 2 makefile lines (i.e. wrap a single statement in the makefile) but this was difficult to do right anyway as the backslash and newline were both passed through, and it depended on knowledge of the shell - and generally to make sure you broke the line inside a shell double-quote. Compared to that secret knowledge, I think this little hack is much more straightforward.

Sam

--
[FSF Associate Member #2325] <http://www.fsf.org/register_form?referrer=2325>

<http://www.openrightsgroup.org/>
_______________________________________________
Help-make mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/help-make

Reply via email to