Wednesday, 17 July 2019

Introduction to Makefile

make commands


ifeq
else
endif
...

They MUST NOT be indented with TAB characters as (almost) all lines with TAB characters as the first character on the line in a makefile are passed to the shell (/bin/sh). The shell doesn't know anything about  make commands. make commands can be indented with a set of SPACE characters but this might be misleading as recipies is what MUST be indented with TABs.


Recipies


They must be indented with TAB character in order to be passed to shell.

Targets


Targets are labels that allow make to execute a group of commands together.

Makefile:

target1:
   @echo target1 is executing
target2:
   @echo target2 is executing

We can now run make as:

$ make target1

or

$ make target2

Minimal (but working) example of Makefile:

docker-run:
@echo Building and running Docker image...
docker build -t cpp-demo . && docker run --rm --name cpp-demo cpp-demo

To run it:

$ make docker-run 

Conditional Execution


Use ifeq-endif or ifeq-else-endif blocks.

Makefile:

VAR1= test
VAR2=nottest

demo-if-else-endif:

ifeq($(VAR1), $(VAR2))
   ...
else
   ...
endif

Makefile ifeq: when are they evaluated?

Variable comparison


TEST=ON
ifeq ($(TEST),ON)
    @echo PASSED
else
    @echo FAILED
endif


To check if variable is empty:

ifeq ($(TEST),)
TEST := $(something else)
endif

Makefile set if variable is empty

 

Using Makefile in Docker projects



No comments: