Search code examples
autotoolsautomake

Using automake to copy a file as part of the default target


I'm working on a project that's built with GNU Autotools. As part of the build that's done by default when the user runs ./configure && make, I want it to copy the file config/foo.conf.orig to config/foo.conf, generating an example configuration that the user can edit.

What do I put in the Makefile.am file to make this happen?

I've tried:

EXTRA_PROGRAMS = localconf
localconf_SOURCES = conf/foo.conf.orig
.PHONY: localconf
localconf:
    echo Creating local configuration...
    cp conf/foo.conf/orig conf/foo.conf

But that has no effect.

Doing ./configure && make localconf works. But I don't want the user to have to know that they need to call a special target; I want it to happen as part of the default ./configure && make.


Solution

  • Naming a program in EXTRA_PROGRAMS does not cause it to be included in the build. This variable can be viewed as a declaration of programs that might be included in the build, but are not named explicitly in any other *_PROGRAMS variable. Actually causing them to be built generally involves configure substitutions. This helps satisfy Automake's need for static knowledge of all the targets.

    If the file to be built is one that is going to be installed with the software (possibly after user modification) then the thing to do is name it as a normal target, and provide a build rule for it. Perhaps something like this:

    sysconf_DATA = config/foo.conf
    EXTRA_SOURCES = config/foo.conf.orig
    CLEANFILES = config/foo.conf
    
    config/foo.conf: config/foo.conf.orig
        cp -p $< $@
    

    Listing the .orig file among the EXTRA_SOURCES ensures that it gets included when you make dist, and I think you need to explicitly name the built file in CLEANFILES if you want it to be affected by make clean.

    If for some reason that does not meet your needs, then Automake has some fairly general provisions for attaching your own build rules. Using those, you could cause your file to be built this way:

    EXTRA_SOURCES = config/foo.conf.orig
    
    all-local: config/foo.conf
    
    config/foo.conf: config/foo.conf.orig
        cp -p $< $@
    

    You might also want to use some or all of install-data-local, uninstall-local, and either clean-local or distclean-local.


    As an aside, note

    • Definition of a dependency for the built config file, ensuring that the file gets recopied if the model file changes.
    • Use of $< in the build rule to refer to the model config file. This helps support out-of-source builds.