top | item 3518348

(no title)

reedhedges | 14 years ago

Oh god, this way lies madness. :) It really does. Once you get beyond a fairly low level of complexity, there will be maintainence problems: It will take you too long to get it right, it will be impossible to figure out when something goes wrong, and you'll drive the next poor developer crazy if he doesn't have a complete knowlege of make (which is most of us). Yes, sometimes it may be neccesary to do this if you want to reuse some rules and functions but modify them under different conditions, AND you are restricted to doing it all in the Makefile (rather than preprocessing, calling out to external scripts, etc.)

discuss

order

mturmon|14 years ago

Using "define" to set up templates for rules is pretty straightforward and very powerful. It allows the set of rules to be augmented at runtime, which can enable new uses of make.

I used this mechanism to automate a pipeline for scientific data. The template (the "define...endef" block) held several rules needed to re-make results for one granule of data. When you ran make, it looked for all source granules, and a foreach() mapping the template across the source granules, just as in the OP, set up rules for each one.

Then you could dump a new source granule in a directory, run make -j 8, and get parallel "builds" of the results for free.

As long as it's documented, it can save a lot of repetition.

Not coincidentally, jgc has a nice article on make debugging ;-)

http://drdobbs.com/article/print?articleId=197003338&sit...

reedhedges|14 years ago

I'm not saying you can't do it. Make is really powerful and fun once you take a few minutes to think about how it is supposed to work (rules that generate file outputs), but a lot of programmers don't (who treat rules as procedures resulting in a file thats 90% phony rules). You have to be very careful with define to generate rules. Just be aware there are syntax pitfalls, compatibility pitfalls (between relatively recent versions of make), and just confusion if you aren't clear on what you're doing.

ArbitraryLimits|14 years ago

I couldn't agree more. Once upon a time I spent about a week refactoring a really ugly build system (produced a combination of .NET assemblies, C++ libraries, Java applets, and a hacked version of Apache with bash embedded - don't ask) and got everything just right with this approach, then within a week it was all broken again. I removed the rule-generation from the Makefiles and everything was fine after that. Since then my policy has always been that copy-pasta is OK in the build system, and not to worry about it.

(I'm not saying generating the rules from code in a real build system wouldn't be a good idea, jut that it isn't with make.)