Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable assignment before vs after make command

make can take variable assignments as part of his command line. Is DEBUG=1 make the same as make DEBUG=1? With this simple Makefile, both print echo 1.

$ cat Makefile
all:
        echo ${DEBUG}

It's clear in the latter case, DEBUG=1 is part of the argument to make, but the first one only seems like a variable assignment in shell. So I'd assume make gets the value differently. Some clarification about the difference will be helpful.

like image 837
qweruiop Avatar asked Feb 20 '18 15:02

qweruiop


People also ask

What is := in make?

Variables defined with ' := ' or ' ::= ' are simply expanded variables; these definitions can contain variable references which will be expanded before the definition is made.

What is $$ in Makefile?

Double dollar sign If you want a string to have a dollar sign, you can use $$ . This is how to use a shell variable in bash or sh . Note the differences between Makefile variables and Shell variables in this next example.

How do I set multiple environment variables in Linux?

To set system-wide Linux environment variables, you can edit the /etc/environment file. Instead of appending export commands to the file, append the <NAME>='<VALUE>' pair to the end of the file.


1 Answers

Is DEBUG=1 make the same as make DEBUG=1?

Not really.

  • In DEBUG=1 make: make obtains the variable from the environment. That is, the shell sets the variable DEBUG, then make is executed and it (the process running make) inherits that environment variable.
  • In make DEBUG=1: make obtains the variable from the command line (i.e.: make sets the variable itself).

Relevant differences

Variables obtained from the command line override the variables obtained from the environment. That is, in:

DEBUG=0 make DEBUG=1

DEBUG is 1, not 0.

Note as well that variables obtained from the command line override the variables set inside the makefile (unless the override directive is used), whereas environment variables don't (unless the option -e or --environment-override is passed to make).

Therefore, if the makefile sets a variable named DEBUG as in the following:

DEBUG = 0
all:
    @echo ${DEBUG}

Then:

make DEBUG=1

echoes 1, whereas:

DEBUG=1 make

echoes 0.


The origin built-in function

You can use the origin built-in function to find out where a variable comes from. Consider this modification of your makefile:

$ cat Makefile
all:
    @echo DEBUG is from $(origin DEBUG)

Then:

$ make DEBUG=1
DEBUG is from command line

but:

$ DEBUG=1 make
DEBUG is from environment
like image 68
ネロク・ゴ Avatar answered Oct 31 '22 19:10

ネロク・ゴ