automake: Conditional Sources
1
1 8.1.3 Conditional compilation of sources
1 ----------------------------------------
1
1 You can’t put a configure substitution (e.g., ‘@FOO@’ or ‘$(FOO)’ where
1 ‘FOO’ is defined via ‘AC_SUBST’) into a ‘_SOURCES’ variable. The reason
1 for this is a bit hard to explain, but suffice to say that it simply
1 won’t work. Automake will give an error if you try to do this.
1
1 Fortunately there are two other ways to achieve the same result. One
1 is to use configure substitutions in ‘_LDADD’ variables, the other is to
1 use an Automake conditional.
1
1 Conditional Compilation using ‘_LDADD’ Substitutions
1 ....................................................
1
1 Automake must know all the source files that could possibly go into a
1 program, even if not all the files are built in every circumstance. Any
1 files that are only conditionally built should be listed in the
1 appropriate ‘EXTRA_’ variable. For instance, if ‘hello-linux.c’ or
1 ‘hello-generic.c’ were conditionally included in ‘hello’, the
1 ‘Makefile.am’ would contain:
1
1 bin_PROGRAMS = hello
1 hello_SOURCES = hello-common.c
1 EXTRA_hello_SOURCES = hello-linux.c hello-generic.c
1 hello_LDADD = $(HELLO_SYSTEM)
1 hello_DEPENDENCIES = $(HELLO_SYSTEM)
1
1 You can then setup the ‘$(HELLO_SYSTEM)’ substitution from
1 ‘configure.ac’:
1
1 ...
1 case $host in
1 *linux*) HELLO_SYSTEM='hello-linux.$(OBJEXT)' ;;
1 *) HELLO_SYSTEM='hello-generic.$(OBJEXT)' ;;
1 esac
1 AC_SUBST([HELLO_SYSTEM])
1 ...
1
1 In this case, the variable ‘HELLO_SYSTEM’ should be replaced by
1 either ‘hello-linux.o’ or ‘hello-generic.o’, and added to both
1 ‘hello_DEPENDENCIES’ and ‘hello_LDADD’ in order to be built and linked
1 in.
1
1 Conditional Compilation using Automake Conditionals
1 ...................................................
1
1 An often simpler way to compile source files conditionally is to use
1 Automake conditionals. For instance, you could use this ‘Makefile.am’
1 construct to build the same ‘hello’ example:
1
1 bin_PROGRAMS = hello
1 if LINUX
1 hello_SOURCES = hello-linux.c hello-common.c
1 else
1 hello_SOURCES = hello-generic.c hello-common.c
1 endif
1
1 In this case, ‘configure.ac’ should setup the ‘LINUX’ conditional
1 using ‘AM_CONDITIONAL’ (⇒Conditionals).
1
1 When using conditionals like this you don’t need to use the ‘EXTRA_’
1 variable, because Automake will examine the contents of each variable to
1 construct the complete list of source files.
1
1 If your program uses a lot of files, you will probably prefer a
1 conditional ‘+=’.
1
1 bin_PROGRAMS = hello
1 hello_SOURCES = hello-common.c
1 if LINUX
1 hello_SOURCES += hello-linux.c
1 else
1 hello_SOURCES += hello-generic.c
1 endif
1