Nathan Farrington
Nathan Farrington

Reputation: 1970

What is a good way to generate text files from a Makefile?

My build process uses lots of 3rd-party utilities. Many of these utilities require the use of ASCII text files for configuration. I would like to generate these short text files from a Makefile. The only way I know how is to use the echo command to write one line of the text file at a time and then redirect the output like this:

foo.script: bar.prj
        touch $@
        echo run >>$@
        echo -input bar.prj >>$@
        echo -output baz.out >>$@

This is kind of ugly. To make matters worse, I'm generating the Makefile from a Python program. So the actual code looks more like this:

TEMPLATE="""{target}: {input}
        touch $@
        echo run >>$@
        echo -input {input} >>$@
        echo -output {output} >>%@"""
TEMPLATE.format(target='foo.script',input='bar.prj',output='baz.out')

This gets to be very difficult to read and maintain. Does anyone know a better way?

ANSWER:

Don't generate text from Make. Call a script written in a language that makes it easier to generate text.

Upvotes: 4

Views: 2224

Answers (1)

daniel kullmann
daniel kullmann

Reputation: 14023

If you use python already in your build process, you could write a small python script that takes a template file, a target file and a number of parameters, and generates the target file from the template file, with the appropriate parameters filled in.

Upvotes: 1

Related Questions