jmlopez
jmlopez

Reputation: 4953

How to write inline files in a Makefile

I've seen some shell scripts in which they pass a file by writing the contents of the file in the same shell script. For instance:

if [[ $uponly -eq 1 ]]; then
    mysql $tabular -h database1 -u usertrack -pabulafia usertrack << END                                                                                           

    select host from host_status where host like 'ld%' and status = 'up';                                                                                          

END
     exit 0
fi

I've been able to do something similar in which I do:

python << END
print 'hello world'
END

If the name of the script is say myscript.sh then I can run it by executing sh myscript.sh. and I obtain my desired output.

Question is, can we do something similar in a Makefile? I've been looking around and they all say that I have do something like:

target:
        python @<<
print 'hello world'
<<

But that doesn't work.

Here are the links where I've been looking:

http://www.opussoftware.com/tutorial/TutMakefile.htm#Response%20Files

http://www.scribd.com/doc/2369245/Makefile-Memo

Upvotes: 1

Views: 2316

Answers (2)

Norman Gray
Norman Gray

Reputation: 12514

The reason your @<< thing didn't work is that it appears to be a feature of a non-standard make variant. Similarly, the define command that mVChr mentions is specific (as far as I'm aware) to GNU Make. While GNU Make is very widely distributed, this trick won't work in a BSD make, nor in a POSIX-only make.

I feel it's good, as a general principle, to keep makefiles as portable as possible; and if you're writing a Makefile in the context of an autoconf-ed system, it's more important still.

A fully portable technique for doing what you're looking for is:

target:
    { echo "print 'First line'"; echo "print 'second'"; } | python

or equivalently, if you want to lay things out a bit more tidily:

target:
    { echo "print 'First line'"; \
      echo "print 'second'"; \
    } | python

Upvotes: 4

mVChr
mVChr

Reputation: 50185

You can do something like this:

define TMP_PYTHON_PROG
print 'hello'
print 'world'
endef

export TMP_PYTHON_PROG
target:
    @python -c "$$TMP_PYTHON_PROG"

First, you're defining a multi line variable with define and endef. Then you need to export it to the shell otherwise it will treat each new line as a new command. Then you reinsert the shell variable using $$.

Upvotes: 4

Related Questions