How Makefile can improve your productivity as Developer

How Makefile can improve your productivity as Developer

Run commands or execute files more easily with this automation tool.

Make is a build automation tool allowing to compile programs and libraries from source code. It uses text files called Makefiles, which specify the set of commands to be executed. Make is widely used on Unix-like operating systems to build C/C++ programs.

Getting Started

As mentioned above, you need to create a Makefile (or makefile) to run commands (make must be installed on you OS beforehand). A makefile is essentially composed of statements called rules. The typical syntax of a rule is as follows:

targets: prerequisites
        commands

Here, we use the Tab character to indent the commands line.

  • targets : It can be file names separed by spaces or just a name as label of the task called phony targets. Usually, there is only one target per rule.
  • prerequisites or dependencies : As for targets, they are also file names. As the name suggest, they are required before running the commands.
  • commands : They are the instructions executed by the shell.

Let's start with the most used example in programming, printing Hello world. Create a Makefile, then add the content below:

hello:
    echo "Hello World!"

As you see it, we don't necessary need to define a prerequisite. To execute this rule, we have to be in the parent directory of the Makefile and run the make command:

$ make

By default the first rule in a Makefile is executed if there is no target specified. Here is the output from the terminal:

$ make
echo "Hello World!"
Hello World!

How Makefile could help you

Running Docker containers with Docker Compose

Let's say we have a project with a docker-compose.yml to run the project's services and another Docker Compose file for development purpose only named docker-compose.dev.yml. The command for starting the services using the latter file would be:

# shell
$ docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d

We could create a Makefile file in the project directory with the content below:

# Makefile
build:
    docker-compose build
dev:
    docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
up:
    docker-compose up -d

and use the make command like this:

# shell
$ make dev

Printing logs content

Logging files have, most of the time, large content with thousands of lines. If we want, for example, to display the n last lines or just retrieve a specific string in the logs, we have to use tail or grep command for Linux users. It is much easier to define these commands as rules in a Makefile and use them:

# Makefile
LINES_SIZE=100
logs:
    tail -$(LINES_SIZE) path/to/logfile.log

and then run the following commands:

# shell
$ make logs # using the default value
$ make logs LINES_SIZE=1000 # using the value defined

That's all!

ezgif-4-82ffcfb405.gif

It is the end!

We don't even scratch the surface of what can be done with Makefile.

If you want to deepen and know more about this tool, here are some resources: