I'm developing a shared library which ships with test suites, all written in C.
I have a separate makefile which is included by the main Makefile.am, specifically for the tests, which looks similar to this:
TESTS = %reldir%/foo\
%reldir%/bar\
# etc.
check_PROGRAMS = $(TESTS)
LDADD = $(top_builddir)/src/libmylib.la
Each test listed in TESTS
is a .c program that links against the LDADD library, but this linkage is only required for the test programs. Similarly, there are CFLAGS I want to pass for the test programs, but not the library itself, such as -O0 -g --coverage
and the like.
Since my TESTS
/check_PROGRAMS
lists are getting longer the more tests I add, I'd prefer to avoid using per-program tests_foo_CFLAGS = ...
or tests_foo_LDADD = ...
assignments, as they're all going to be identical - link against the library, add debug symbols, build for gcov
, and so on.
Is it possible to just declare these variables for all targets in TESTS
?
CodePudding user response:
Is it possible to just declare these variables for all targets in TESTS?
Not as such. Automake provides for flag-bearing variables expressing default flags and for variables expressing target-specific flags, but there is no built in way to specify flags that apply to a designated subset of targets. Nevertheless, there are ways to exercise the available features to get at least close to what you want.
One way is to set up for recursive building via SUBDIRS
and multiple Makefile.am
files. With such a setup, you should be able to have separate default flags of each category for each Makefile.am
.
But from the wording and the details of the code snippet in the question, I take it that you are using Automake
's include
directive to build a single makefile, and within that makefile you want to express flags that apply to all the test targets, but not any other targets. Here are some ways to approach that:
define all the wanted flags in an ordinary
make
variable, and interpolate them into test-specific variables. Example:tests_cflags = -O0 -g --coverage # ... tests_foo_CFLAGS = $(tests_cflags) tests_bar_CFLAGS = $(tests_cflags) tests_baz_CFLAGS = $(tests_cflags)
Although that does not overcome the need for target-specific variables for each test target, it does allow you to manage the actual flags all in one place instead of separately for each test.
Flip the script. Supposing that you have a much higher number of tests than you have other compiled targets, set up the default flags to serve the tests, and, where necessary, override them with target-specific flags for the other targets. Most of the Automake defaults are conveyed via variables with the
AM_
prefix (LDADD
is an exception), so that might look something like this:LDADD = $(top_builddir)/src/libmylib.la AM_CFLAGS = -O0 -g --coverage # ... # no tests_foo_CFLAGS, etc. # ... libmylib_la_CFLAGS = ...
Note that the value of the target-specific variables (
libmylib_la_CFLAGS
in this case) can be empty if you just want to suppress the default flags instead of replacing them with different ones.
Either way, you probably want to be familiar with Automake's flag-variable ordering rules.