31 March 2021

Makefile is a build automation tool for Unix created around April 1976 by Stuart Feldman at Bell Labs.
It use is widespread in Unix and Linux culture and is used a lot in computer science histories: from building First versions of Unix to build modern software such as linux kernel.

Makefile is a great tool because:
- text file, could be edit with your favourite editor
- dependencies: can be specified dependencies among different directive
- define rules: it is possible write in a easy way how to do things
- flexible: it is not a tool related only for building software but could be used also to help in day by day work (like know how many days left from the next weekend ;) )

In this post I will show you a basic example of using Makefile.
Example:

current_day=$(shell date +%u)
normal_day=$(shell if (test ${current_day} -lt 5); then echo "yes"; else echo "no"; fi)
countdown=$(shell expr 6 - ${current_day})

hello:
	echo "hello world!"

weekend:
ifeq "$(normal_day)" "yes"
	echo 'Not today :( ... ${countdown} left'
endif
ifeq '${current_day}' '5'
	echo 'Weekend is coming ;)'
endif
ifeq '${current_day}' '6'
	echo '!!! Saturday :) !!!'
endif
ifeq '${current_day}' '7'
	echo '!!! Sunday :) !!!'
endif

Example in a containerized environment

Copy previous example in /tmp/Makefile

$ docker run -it --mount 'type=bind,source=/tmp/Makefile,target=/tmp/Makefile' ubuntu /bin/bash
root@c6fb5678babc:/# apt update && apt install make && cd /tmp
root@c6fb5678babc:/tmp# make hello
echo "hello world!"
hello world!
root@c6fb5678babc:/tmp# make weekend
echo 'Not today :( ... 3 left'
Not today :( ... 3 left
root@c6fb5678babc:/tmp# 

References