DEV Community

Simone Gentili
Simone Gentili

Posted on • Edited on

A brief introduction to what Makefiles are

Why a post about Makefile

I wrote this article about the creation of a little http server in go. The purpose of that article was the go configuration. But some other items was present and one of them is the use of Makefile. A friend of mine told me that the article taken for granted the knowledge of Makefile. So here the reason of this article. I hope you enjoy the reading.

What is a Makefile?

A Makefile is a special file, containing shell commands, that you create and name Makefile. Some system use makefile (lower case). Make, instead, is an utility that is designed to start execution of a makefile. Typing make and the command in the makefile in a directory that contain that makefile, the command will be executed. With autocomplete make can really speed up the call of some command line tasks.

A list of shell commands

Because of Makefile is a list of shell commands, commands must be written for the shell which will process the makefile. A makefile that works well in one shell may not execute properly in another shell. Inside a docker container the problem not exists.

Rules

A Makefile is a list of rules. Each rule begins with a textual dependency line which defines a target followed by a colon (:) and optionally an enumeration of components (files or other targets) on which the target depends. The dependency line is arranged so that the target (left hand of the colon) depends on components (right hand of the colon). It is common to refer to components as prerequisites of the target.

some suggestion to autocomplete

https://gist.github.com/tlrobinson/1073865
https://formulae.brew.sh/formula/bash-completion

a rule

Generally each target contain one command. But you can add any command you want.

<target>: <file|target>
    <command 1>
    <command 2>
    .
    .
    .
    <command N>
Enter fullscreen mode Exit fullscreen mode

If a target depends from other targets we can write:

target1:
    <command 1>

target2: target1
    <command 2>
Enter fullscreen mode Exit fullscreen mode

More than one Makefile

If you create more than one Makefile, be certain you are in the correct directory before typing make. And if more Makefile are present in same folder, remember to pass it after make command. The option is -f and the full syntax is

$ make -f Mafefile.filename <command>
Enter fullscreen mode Exit fullscreen mode

An example with docker

The following is my default Makefile for every projects. More or less. I really love type just make bash_web, for example, instead of a more complex command. I know people who prefer to type each single character. I do not understand why.

default: up

up:
    docker-compose up -d

build:
    docker-compose up -d --build --remove-orphans

down:
    docker-compose down

bash_web:
    docker-compose exec web bash

bash_mysql:
    docker-compose exec mysql bash
Enter fullscreen mode Exit fullscreen mode

Conclusion

Do you like this post? Any suggestion or improvement? Please write it to the comments. Thank you for reading.

Top comments (0)