4

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.

Nathan Farrington
  • 1,890
  • 1
  • 17
  • 27
  • 2
    Why don't you just write a small python (or whatever you're more comfortable with) script to generate those text files, and call that from your Makefile? – Mat Dec 30 '11 at 08:57
  • 3
    I think the first answer to this SO question will let you do what you want: http://stackoverflow.com/questions/649246/is-it-possible-to-create-a-multi-line-string-variable-in-a-makefile – Adrian Ratnapala Dec 30 '11 at 09:09
  • These are both great comments. I don't like having two (or more) Python files, the first to generate the Makefile and the second to be called by the Makefile. Perhaps the first could be called with arguments to make it act like two different scripts? I will try applying the answer from http://stackoverflow.com/questions/649246/is-it-possible-to-create-a-multi-line-string-variable-in-a-makefile – Nathan Farrington Dec 30 '11 at 18:59
  • Make isn't very elegant at building text files; you want (exactly) one makefile to build all the text files. Python doesn't look very elegant at building makefiles; you want (exactly) one Python script to build the makefile. If you post your makefie, maybe we can simplify it. Other than that, I don't see how we can help. – Beta Jan 02 '12 at 05:12
  • The conclusion might be that there is no good way to create text files using `make`, and by extension, the shell. There are only less bad ways. Adrian Ratnapala's comment should be an answer so I can mark it as the solution since it is the closest to what I was looking for. However I probably wouldn't use that solution and would probably just create a script to generate the text file that is called by `make`. – Nathan Farrington Jan 02 '12 at 11:04

1 Answers1

1

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.

daniel kullmann
  • 13,653
  • 8
  • 51
  • 67
  • I wanted to have one Python script to generate one Makefile, and then have the Makefile generate everything else that is needed. I didn't want to have the Python script generate lots of files because then it would also have to create lots of subdirectories for those lots of files and it would make it harder for Make to know which targets need to be created, and harder to run `make clean`. If the only answer is to use Python then I can do that, but I was looking for something entirely contained in a single Makefile. – Nathan Farrington Dec 30 '11 at 18:57
  • I'm going to generalize your answer to say that the best way to generate text from a Makefile is to call a script written in a language such as Python, Ruby, or Perl. Don't bother trying to generate text from Make; it wasn't designed for that. – Nathan Farrington Jan 17 '12 at 04:05