Search code examples
autotoolsautoconfautomakelibtool

Build tests depending on program sources


I want to build tests for a foo program. My root Makefile.am looks like:

SUBDIRS = src tests

Makefile.am from src contains:

bin_PROGRAMS = foo
foo_CXXFLAGS = # a lot of $(XXX_CFLAGS)
foo_LDADD = # a lot of $(XXX_LIBS)
foo_SOURCES = # a lot of source files

Makefile.am from tests contains:

check_PROGRAMS = footesta footestb
TESTS = footesta footestb
footesta_SOURCES = footesta.cpp
footestb_SOURCES = footestb.cpp

These tests depend on foo symbols and I consider to create a convenience library by using some foo_LIBADD = libfoo.la on top and place noinst_LTLIBRARIES = libfoo.la in Makefile.am's src. Am I on the right way to solve this?


Solution

  • You are on the right way, but I would suggest you a couple of different options too.

    First of all, I'd suggest you to look into using non-recursive automake so that instead of three Makefile.am you only have one to keep up to date, and reduces the nasty factor of autotools.

    In particular, using the convenience library method across separate Makefile.am makes dependency tracking terribly complicated, which means that you may or may not re-run the tests properly if you're changing, say, a header file. The dependency tracking is done properly when using a single, non-recursive, Makefile.am.

    The other option, again using a non-recursive build system, is to declare variables with group of sources, so that you would have

    SELF_CONTAINED_SRCS = foo.h foo1.c foo2.c foo3.c
    foo_SOURCES = $(SELF_CONTAINED_SRCS) main.c other.c
    
    footest_SOURCES = footesta.c $(SELF_CONTAINED_SRCS)
    

    this avoids the intra-step of running the archiver to generate the .a file and then for the linker to unpack it, while still sharing the compiled sources (as long as you don't use per-target CFLAGS).

    If you plan on using per-target CFLAGS (say you want to have test-specific code in foo3.c), then you will not be able to share the compiled object files between the two targets, but that is also possible when using the variable-expansion, and not possible with the convenience library, which is why that's actually my preferred option.