automake Environment Variable Condition

半腔热情 提交于 2020-01-23 13:33:45

问题


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


来源:https://stackoverflow.com/questions/14553389/automake-environment-variable-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!