Subject: bug#9240: Implementing a plugin-like module

From: Stefano Lattarini <[email-masked]>
Date: Thu, 4 Aug 2011 15:52:12 +0200

Hello Sam.

On Thursday 04 August 2011, Sam Varshavchik wrote:
> I'm looking for a way to implement an optional plugin-like facility in  
> automake.
>
> Basically, take a stock distro installation of automake, then augment it  
> somehow so that automake would add my stuff into each Makefile.in file that  
> it generates from Makefile.am.
>
A quick workaround to obtain this behaviour is to write your custom/rules
defintion in a `.am' fragment in your source tree, and then include it
from all the Makefile.am files that need it:

  $ cat my-rules.am
  my_variable = ...
  .ctpl.c:
	# rules to create a C file from a template `.ctpl'.

  $ cat lib/Makefile.am
  include  $(top_srcdir)/my-rules.am
  ... [specific rules]

  $ cat src/Makefile.am
  include  $(top_srcdir)/my-rules.am
  ... [other specific rules]

Automake will take care of processing the contents of my-rules.am, and will
copy the result in all the Makefile.in files derived from the Makefile.am
files that include it.

This way, if you have to change your custom rules or definitions, you
will only have to do so in one place (i.e., in my-rules.am).

All of this is documented in the Automake manual (in a very concise way
though, so I can understand why you've missed it):
 <http://www.gnu.org/software/automake/manual/html_node/Include.html>

Maybe a polished version of my example above will make a nice
addition to the "Extending Automake Rules" section in the automake
manual ...  I'm adding bug-automake on CC: so that we won't forget
about the issue.

> My situation is that I'm installing a toolkit; a library, a part of which is  
> a code generator. Let's say that my code generator produces filename.H from  
> filename.tmpl.
> 
> I can always write out the rules explicitly, something like:
> 
> BUILT_SOURCES=filename.H
> 
> filename.H: filename.tmpl
>      # build rule that runs the code generator goes here
> 
> bin_PROGRAMS=app
> 
> app_SOURCES=appmain.C
> 
> # and appmain.C #includes filename.H
> 
> 
> Something like that. That's the basic scenario. What I would like to  
> accomplish is to have the actual build rules defined by the installed  
> toolkit. The build rules are somewhat non-trivial, and with many code- 
> generated .H files, it gets tedious. I'm fine with limiting my portability  
> to gmake, so I can put something like
> 
> $(call GENERATE_TMPL_RULE,filename)
> 
> and my GENERATE_TMPL_RULE macro eval()s the appropriate make rules that  
> produce filename.H from filename.tmpl; then I just add additional calls to  
> this macro to generate rules for each code-generated .H file. Works fine.
>
Well, if the flags that need to be passes to the code generator are the
same for all your generated headers, you could use a suffix rule instead,
which is portable to non-GNU make:

  .tmpl.H:
	# build rule that runs the code generator goes here

> The definition of the GENERATE_TMPL_RULE is owned by the toolkit, and I'm  
> looking for a way to avoid having each app, that builds against the toolkit,  
> from explicitly adding the macro definition in its Makefile.am. I'd like to  
> have automake put the canned GENERATE_TMP_RULE into the Makefile.in that it  
> generates.
>
My proposed solution above should work quite nicely in this situation: you
just copy the GENERATE_TMP_RULE defintion in a, say, `my-toolkit.am' file,
and include it in all the Makefile.am files that require its rules.  If you
need to use this file in several projects, and don't want to keep multiple
copies around, you can save the "master copy" in a known location (say
under `~/my-toolkit.am'), and then symlink it into the source trees all the
required projects.

> In my distro, I see that automake has a collection of files in  
> /usr/share/automake-[VERSION]/am/*.am, which look like various snippets that  
> the main automake script pulls together into the Makefile.in script that it  
> produces. But it's not clear to me if it's possible for me to make the  
> necessary arrangements for the automake script to insert my own *.am script,  
> that my toolkit could install into that directory,
>
There's no need to install an `.am' file there in order to use it with the
`include' directive.

> into a Makefile.in  
> produced by automake. That's basically what I'm looking to do, in a  
> nutshell; I'm unable to locate any info on this topic in the automake  
> manual; any tips would be appreciated.
> 

HTH,
  Stefano