automake: Per-Object Flags

1 
1 27.8 Per-Object Flags Emulation
1 ===============================
1 
1      One of my source files needs to be compiled with different flags.  How
1      do I do?
1 
1    Automake supports per-program and per-library compilation flags (see
DONTPRINTYET 1 ⇒Program and Library Variables and *noteFlag Variables
1DONTPRINTYET 1 ⇒Program and Library Variables and ⇒Flag Variables

 Ordering).  With this you can define compilation flags that apply to
1 all files compiled for a target.  For instance, in
1 
1      bin_PROGRAMS = foo
1      foo_SOURCES = foo.c foo.h bar.c bar.h main.c
1      foo_CFLAGS = -some -flags
1 
1 ‘foo-foo.o’, ‘foo-bar.o’, and ‘foo-main.o’ will all be compiled with
1 ‘-some -flags’.  (If you wonder about the names of these object files,
1 see ⇒Renamed Objects.)  Note that ‘foo_CFLAGS’ gives the flags to
1 use when compiling all the C sources of the _program_ ‘foo’, it has
1 nothing to do with ‘foo.c’ or ‘foo-foo.o’ specifically.
1 
1    What if ‘foo.c’ needs to be compiled into ‘foo.o’ using some specific
1 flags, that none of the other files requires?  Obviously per-program
1 flags are not directly applicable here.  Something like per-object flags
1 are expected, i.e., flags that would be used only when creating
1 ‘foo-foo.o’.  Automake does not support that, however this is easy to
1 simulate using a library that contains only that object, and compiling
1 this library with per-library flags.
1 
1      bin_PROGRAMS = foo
1      foo_SOURCES = bar.c bar.h main.c
1      foo_CFLAGS = -some -flags
1      foo_LDADD = libfoo.a
1      noinst_LIBRARIES = libfoo.a
1      libfoo_a_SOURCES = foo.c foo.h
1      libfoo_a_CFLAGS = -some -other -flags
1 
1    Here ‘foo-bar.o’ and ‘foo-main.o’ will all be compiled with ‘-some
1 -flags’, while ‘libfoo_a-foo.o’ will be compiled using ‘-some -other
1 -flags’.  Eventually, all three objects will be linked to form ‘foo’.
1 
1    This trick can also be achieved using Libtool convenience libraries,
11 for instance ‘noinst_LTLIBRARIES = libfoo.la’ (⇒Libtool Convenience
 Libraries).
1 
1    Another tempting idea to implement per-object flags is to override
1 the compile rules ‘automake’ would output for these files.  Automake
1 will not define a rule for a target you have defined, so you could think
1 about defining the ‘foo-foo.o: foo.c’ rule yourself.  We recommend
1 against this, because this is error prone.  For instance, if you add
1 such a rule to the first example, it will break the day you decide to
1 remove ‘foo_CFLAGS’ (because ‘foo.c’ will then be compiled as ‘foo.o’
1 instead of ‘foo-foo.o’, ⇒Renamed Objects).  Also in order to
1 support dependency tracking, the two ‘.o’/‘.obj’ extensions, and all the
1 other flags variables involved in a compilation, you will end up
1 modifying a copy of the rule previously output by ‘automake’ for this
1 file.  If a new release of Automake generates a different rule, your
1 copy will need to be updated by hand.
1