Press "Enter" to skip to content

Variables

All variables in make are strings and is possible to expand variables using $( ) sequence.

$(my_bar) # This will expand to 'my_bar' value

For example:

$ cat Makefile hola := Hello default: @echo "$(hola) world!" $ make Hello world!

Defining variables

Variables get defined in different ways: – Environmental variables are loaded as make variables.

$ cat Makefile default: @echo "I'm $(USER)" $ make I'm edupo
  • Variable values can be overridden by the command line.
    $ make USER="not me" I'm not me
    

    This is useful for example for specifying your target or any parameter from your CI.

  • Some variables are automatically generated when inside a rule.
    $ cat Makefile default:
        @echo "I'm executing $@ target" $ make I'm executing default target
    

    If you are interested on automatic variables there is a reference further on this page.

  • Some variables has been assigned before you even born and they remain constant.

    $ cat Makefile default:
         @echo "I'm compiling C with '$(CC)'" $ make I'm compiling C with 'cc'
    

    Take a look to the reference of those variables. You may be familiar with some of them already.

  • Finally you can assign and build variables inside your Makefile.

    bar # Empty variable (can be used as a flag) 
    
    
    bar = value # Recursively expanded assignment 
    bar := value # Simply expanded assignment 
    bar ::= value # Posix compatible simply expanded 
    
    
    define bar
    This is my multi-line
    variable 
    endef # Multi-line variable assignment override 
    
    
    bar := value2 # Overrides command assign 
    
    
    undefine bar # You can also undefine them
    

    For your mental health if you hesitate when assigning variables use simply expanded assignment :=. The details are explained later.

Exporting variables

Sometimes in your automation adventures you may find useful to export your make variables into the environment where make execute it’s commands… Well export is your keyword.

$ cat Makefile
export PIP_INDEX_URL:=http://download.zope.org/ppix

tox: pip install tox

$ make pip install tox
Collecting tox The repository located at download.zope.org is not a trusted or secure host and is being ignored... Blah, blah, blah, I don't want to work...