0

I'm using Makefile such that the default step is to print out a help document for the other options. It would be great if I could add an EOF section instead of multiple echos. Is it possible to use EOF in a Makefile like you can do it a bash script?

1

If you're using GNU make, you can use the define syntax for multi-line variables:

define message =
You can also include make macros like
    $$@ = $@
in a multi-line variable
endef

help:; @ $(info $(message)) :

By playing with the value function and the export directive, you can also include a whole script in your Makefile, which will allow you to use a multi-line command without turning on .ONESHELL globally:

define _script
echo SHELL is $SHELL, PID is $$
cat <<'EOF'
literally $SHELL and $$
EOF
endef
export script = $(value _script)

run:; @ eval "$$script"

will give

SHELL is /bin/sh, PID is 12434
literally $SHELL and $$
  • This is exactly what I needed! I did some research on .ONESHELL and it looks like its really good for streamlining multi-line recipes but this solution is really what I need in this instance. Thanks for your help! – triplethreat78 May 1 at 4:04
1

The simple answer is "not by default". But you can configure GNU make to allow it using .ONESHELL:

Normally each line from a recipe is executed in its own shell. To make matters worse, there is a way to make multi-line commands in make, using a \ at the end of the line to continue it. Unfortunately, this \ is also passed to the shell which is less of a problem for other commands, but for heredoc it really messes up the syntax.

However if you enable .ONESHELL:, this logic changes and commands are executed in a single shell allowing it to work:

.ONESHELL:

all:
    cat << EOF
    aaa
    bbb
    EOF

Be aware that this will have other implications to the way Make functions. Most notably Make may not notice if one build line fails if it is followed by others. Normally the result of each line would be checked but with .ONESHELL: make can only see the result of all lines.

  • @Phillip This is great information, Thank you! Also, I believe you misspelled recipe unless this is a clever wording concept for make? I tried to edit your post but I have to add 6 characters or something. – triplethreat78 May 1 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.