Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automake, generated source files and VPATH builds

I'm doing VPATH builds with automake. I'm now also using generated source, with SWIG. I've got rules in Makefile.am like:

dist_noinst_DATA = whatever.swig

whatever.cpp: whatever.swig
    swig -c++ -php $^

Then the file gets used later:

myprogram_SOURCES = ... whatever.cpp

It works fine when $builddir == $srcdir. But when doing VPATH builds (e.g. mkdir build; cd build; ../configure; make), I get error messages about missing whatever.cpp.

Should generated source files go to $builddir or $srcdir? (I reckon probably $builddir.)

How should dependencies and rules be specified to put generated files in the right place?

like image 567
Craig McQueen Avatar asked Aug 20 '13 05:08

Craig McQueen


1 Answers

Simple answer

You should assume that $srcdir is a read-only, so you must not write anything there. So, your generated source-code will end up in $(builddir).

By default, autotool-generated Makefiles will only look for source-files in $srcdir, so you have to tell it to check $builddir as well. Adding the following to your Makefile.am should help:

VPATH = $(srcdir) $(builddir)

After that you might end up with a no rule to make target ... error, which you should be able to fix by updating your source-generating rule as in:

$(builddir)/whatever.cpp: whatever.swig
        # ...

A better solution

You might notice that in your current setup, the release tarball (as created by make dist) will contain the whatever.cpp file as part of your sources, since you added this file to the myprogram_SOURCES. If you don't want this (e.g. because it might mean that the build-process will really take the pregenerated file rather than generating it again), you might want to use something like the following. It uses a wrapper source-file (whatever_includer.cpp) that simply includes the generated file, and it uses -I$(builddir) to then find the generated file.

Makefile.am:

dist_noinst_DATA = whatever.swig

whatever.cpp: whatever.swig
    swig -c++ -php $^

whatever_includer.cpp: whatever.cpp

myprogram_SOURCES = ... whatever_includer.cpp
myprogram_CPPFLAGS = ... -I$(builddir)

clean-local::
    rm -f $(builddir)/whatever.cpp

whatever_includer.cpp:

#include "whatever.cpp"
like image 56
umläute Avatar answered Sep 27 '22 23:09

umläute