As a junior developer, I was unsure why anyone would want to use a Makefile when they could use bash or write a script in the language of their choice. I had no idea what a Makefile is and why you'd want to use one so I did some reading.
A really really quick intro to Makefiles
Makefiles are used to automate workflows and are composed of chunks of instructions (much like functions) that build on one another to accomplish multi-step processes. Many teams use Makefiles to automate processes both within single projects and across projects/teams.
1. Why use a Makefile instead of a script (bash, python, etc)?
Makefiles have the advantage (like bash) of being language agnostic. This is good for teams with a diversity of experience. For example, a front-end junior developer and a senior dev ops engineer.
Unlike bash or a regular programming language, the make application that runs Makefiles is smart about the work it does. This is because it builds dependency trees under the hood. It knows if you are using a config file to build other files. If you haven't updated the config, then it won't bother building the other files that depend on it.
While this dependency tree functionality might not seem revolutionary at first glance, it can be used to create some very powerful workflows. Check the article references at the end for some examples.
Lastly, Makefiles are extremely modular and flexible. You can use small blocks of logic and combine them to create very powerful chains that are linked together. And then you can easily re-use those blocks in other Makefiles just like you would in any other programming language.
2. What are some examples of things I can do with a Makefile?
- clean-up unwanted files
- combine the multiples steps documented in the README to get an app up and running into one simple command
- as an alias for really long commands with lots of flags (which you can change when you call the make command!)
- project templates to get you and your team writing code faster without worrying about common dependencies, tooling, and file structure
- any of the above in combination, and more!
3. How do I know when a Makefile is the right tool?
First, there are some cases where it might NOT make sense to use Makefiles:
your team doesn't use Makefiles and the time it would take to get them familiar/comfortable with it isn't justified
what you need to accomplish is not complicated and can be easily written in a common script like bash, python, etc.
Here are some cases where it does make sense:
your project involves more than one language or tool. For example, helm/k8s + docker + python + git hooks and you need a simple interface to complete tasks across those tools.
you want short, easy to remember commands to take the place of long cli commands (without giving up the ability to inject options!)
your team is working across many languages on its projects and you want a common language for automation. For example, installing and running any project locally on your machine whether it's written in python, javascript, or go.
4. What do I need to know to get started with Makefiles?
While I am still very much a beginner at Makefiles, I found these pages to be helpful:
- makefiletutorial
- How to use Makefiles to Automate Repetitive Tasks... by Justin Ellingwood
- What is a Makefile and How Does it Work? by Sachin Patil
Further resources
Here are the articles I used to write this article, each of which have some great examples of Makefiles and why you'd want to use them.
- Why you need a Makefile on your Project by Martin Guillier
- Why Use Make by Mike Bostock
- Makefiles in python projects by Krzysztof Zuraw
- Simple Makefile to automate Python projects
- How to use Makefiles to Automate Repetitive Tasks... by Justin Ellingwood
- Automation Using Makefiles by Matthew Kujija
Top comments (0)