31

I'm having hard time writing a makefile rule that outputs a multiline variable in a file.

Here is the code I have :

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

VARS += $(VAR1)
VARS += $(VAR2)

all:
    echo "$(VARS)" > test

However, the echo fails telling "Unterminated quoted string" for a reason unknown to me. How could i put in the file every lines declared on a separate line ?

1
  • Commenting to link the two questions, this SO answer helped me out the most here and isn't in the answers on this question. Building in devtoolset-11 on EL7, the other solutions don't seem to work even though file is supported. Uses subst to replace newlines with \n, then echoes them back. This is more robust than doing it by hand since it allows nesting of multiline variables. May 13, 2022 at 22:57

3 Answers 3

31

If you export the variable to the shell and reference it as a shell variable, rather than a make variable, you will have better luck:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

define VARS
$(VAR1)
$(VAR2)
endef
export VARS

all:
    echo "$$VARS" > test

Note the following adjustments to your makefile:

  • I used define to create VARS, rather than a series of += assignments, which makes it easier to get a newline between the value of VAR1 and VAR2.
  • I added export VARS to your makefile, to get the variable pushed into the environment for the shell invocation.
  • I used $$VARS rather than $(VARS) to dereference it -- that leaves the expansion to the shell, rather than to make, which will avoid the "Unterminated quoted string" error.
2
  • How could I have missed that? +1
    – Beta
    Sep 3, 2011 at 3:44
  • One small caveat that I regularly encounter (but always forget): this does work in recipes but not within $(shell ...) functions. Somehow that eats all the newlines even on output.
    – stefanct
    Sep 20, 2023 at 12:40
20

GNU make 4.0 adds the ability to write files directly:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

define VARS :=
$(VAR1)
$(VAR2)
endef

all:
        $(file > test,$(VARS))

Note that you still need to use define to define VARS, or the last line of VAR1 and the first line of VAR2 will be glommed onto one line. Also, don't put a space after the comma in the $(file ...) construct, or there will be a leading space in the output!

1
  • Thanks for this - exactly what I needed to know at this time, as I hit the command line argument limitation on Windows, breaking my make build... This helps as there are no calls to the shell to pass parameters. Cheers!
    – sdaau
    Sep 28, 2015 at 9:27
4

It looks as if you're getting "Unterminated quoted string" because Make executes each line of the recipe in a separate shell, and the first line is:

echo "    /dev d 755 - - - - -

Here's the best solution I could come up with (and I admit it's not very good, but you're going against the grain of Make) is to pass VARS to a sub-Make which invokes $(info ...):

Makefile:

define VAR1
    /dev d 755 - - - - -
endef

define VAR2
    /test d 777 - - - - -
    /test2 d 777 - - - - -
endef

define VARS
$(VAR1)
$(VAR2)
endef

export VARS

all:
       $(MAKE) -f Makefile.print > test                                        

Makefile.print:

$(info $(VARS))

.PHONY:all
all:
        @# do nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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