automake Environment Variable Condition

2020-07-23 03:53发布

问题:

I have a file Makefile.am I am using to generate a Makefile. In the generated Makefile I want to have something like:

ifndef SOURCECODEPATH
   SOURCECODEPATH := /home/root/source_code_path
endif

It seems so simple, does anyone know how I can do it?

回答1:

Use the AM_CONDITIONAL macro in configure.ac.

The script sets a variable you can test, e.g., a variable that is set to non-empty if the condition is enabled: AM_CONDITIONAL([ENABLE_SOURCECODEPATH], [test "x$ac_srcpath" != "x"])

Then in Makefile.am:

if !ENABLE_SOURCECODEPATH
SOURCECODEPATH = ...
endif

However, since you are explicitly defining the variable if it's not defined, you should probably define it in configure.ac regardless, using AC_SUBST(SRCPATH, $ac_srcpath) :

SOURCECODEPATH = @SRCPATH@ # or $(SRCPATH)


回答2:

you could simply use an auxiliary makefile that get's included by Makefile.am (and it's expansion).

Makefile.am:

#...
include Makefile.env
#...

and Makefile.env:

ifndef SOURCECODEPATH
   SOURCECODEPATH := /home/root/source_code_path
endif

automake will not touch (or try to parse) the included Makefile.env



回答3:

The following solution should work in at least GNU Make and BSD Make. The autotools solution by Brett Hale should work everywhere, but it's considerably more complex.

SOURCECODEPATH?=/home/root/source_code_path


回答4:

You really should not be using automake to generate non-portable makefiles, but if you really want to do this to generate a Makefile for use with GNU make then you can simply add a space before the endif:

ifndef SOURCECODEPATH
   SOURCECODEPATH := /home/root/source_code_path
 endif

If the e is not in the first column, Automake will not try to parse endif as the end of an automake conditional, but will copy the text verbatim to the Makefile. GNU-make will recognize the conditional with the space (at least, 3.80 recognizes it. I haven't tried any others.)



回答5:

Actually, as http://www.gnu.org/software/make/manual/make.html#Flavors says, ?= is used for variables declared with =. If you need set default values for variables declared with :=, use construction like this:

ifeq ($(origin SOURCECODEPATH), undefined)
  SOURCECODEPATH := /home/root/source_code_path
endif