Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output multiline variable to a file with GNU Make

Tags:

makefile

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 ?

like image 336
mripard Avatar asked Sep 02 '11 09:09

mripard


2 Answers

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.
like image 51
Eric Melski Avatar answered Sep 22 '22 14:09

Eric Melski


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!

like image 43
zwol Avatar answered Sep 21 '22 14:09

zwol