Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite variable from makefile

Tags:

makefile

In my makefile I have a variable FOO:

FOO = /path/to/bar

Is it possible to overwrite this variable during the makefile call? Somthing like the following:

FOO=/path/to/foo make all
like image 511
Razer Avatar asked Apr 03 '15 15:04

Razer


2 Answers

Specify them as Var=Value before you specify the target, like make FOO=/path/to/foo all.

$ cat Makefile 
Foo = asdf

all:
    echo $(Foo)

$ make all
echo asdf
asdf
$ make Foo=bar all
echo bar
bar
like image 85
Colonel Thirty Two Avatar answered Oct 20 '22 16:10

Colonel Thirty Two


The ways that variables get assigned values is specified in the How Variables Get Their Values section of the GNU make Manual.

Variables can get values in several different ways:

  • You can specify an overriding value when you run make. See Overriding Variables.
  • You can specify a value in the makefile, either with an assignment (see Setting Variables) or with a verbatim definition (see Defining Multi-Line Variables).
  • Variables in the environment become make variables. See Variables from the Environment.
  • Several automatic variables are given new values for each rule. Each of these has a single conventional use. See Automatic Variables.
  • Several variables have constant initial values. See Variables Used by Implicit Rules.

So, as Colonel Thirty Two indicates, you can override variables set in the makefile on the command line.

You can also, if you expect this to be a variable that you want to set persistently, use the ?= assignment and then environment values for that variable will be used.

like image 17
Etan Reisner Avatar answered Oct 20 '22 18:10

Etan Reisner