Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a Makefile variable using a ENV variable or a default value

I am trying to do a simple thing:

TMPDIR ?= /tmp  test:     @echo $(TMPDIR) 

This works if I run:

$ make test /tmp 

It also works if I run:

$ make test -e TMPDIR=~/tmp /home/user/tmp 

What can I do to also have it works for:

$ TMPDIR=~/tmp make test /home/user/tmp 
like image 377
Natim Avatar asked Jun 17 '14 12:06

Natim


People also ask

How do I set an environment variable in makefile?

Re: Setting Environment variable in Makefile the "$" character is special to make. To get the shell to see one "$", you must use "$$" in the Makefile. 2.) Environment variables can only be inherited from parent to child processes, not vice versa.

What is := in makefile?

Expanded assignment = defines a recursively-expanded variable. := defines a simply-expanded variable.

How do I override a variable in makefile?

There is one way that the makefile can change a variable that you have overridden. This is to use the override directive, which is a line that looks like this: ' override variable = value ' (see The override Directive).

What is .ENV Linux?

env file. A fourth file that the operating system uses at login time is the .env file, if your .profile contains the following line: export ENV=$HOME/.env. The . env file lets you customize your individual working environment variables.


1 Answers

To follow up on my comments above, here's an example:

T ?= foo all:         : '$(T)' 

Now if I run the Makefile in various ways, it behaves as we expect (I get foo only if I don't set T either on the command line or environment):

$ make : 'foo'  $ make T=bar : 'bar'  $ T=bar make : 'bar' 
like image 155
MadScientist Avatar answered Sep 21 '22 08:09

MadScientist