Make, a linux utility, is used throughout Layer projects for automating the compilation processes and the build workflows. Today we’ll see how:
Imagine you’re a Meshery docs contributor and you want to build the docs locally, and for that, what if I tell you: Instead of running a series of commands individually in their respective order, like:
cd docs bundle install bundle exec jekyll serve --drafts --liverload --config _config.dev.yml
You can simply run a single, easy to type, command :
It lets us execute multiple interdependent commands through just a single command. But how? Let’s get right into it:
In a file named Makefile (usually found in the root directory of the projects) , you’ll find syntaxes like:
target: dependency1 dependency2 ... recipe/commands ...
Makefile is just a set of multiple rules and above is a generalized syntax of a Makefile rule.
We can specify the commands to be executed by the make utility in Makefile.
For example, let’s understand the rule below from a Makefile:
greeting: echo "Hey there!"
- The target (or rule name) “greeting” has no dependencies specified.
- The command
echo "Hey there!"is indented using a tab, which is mandatory in Makefiles.
Now, when you run
make greeting command in the directory where the Makefile resides, it will execute the command defined in the rule:
$ make greeting echo "Hey there!" # The command to be executed by the Makefile Hey there! # The result of the command
So now you know: when you run
make docs, it actually executes
cd docs; bundle install; bundle exec jekyll serve --drafts --livereload --config _config_dev.yml.
Similarly, or those of you who contribute to Layer5 website, when you run
make site command to build the site, it actually executes
1.If you want to see the commands that will be executed by the Makefile without actually running them, pass the
-n flag to the
$ make -n greeting echo "Hey there!" # The -n flag suppresses the execution
make -n docs would print
cd docs; bundle install; bundle exec jekyll serve --drafts --livereload --config _config_dev.yml, indicating the commands that would be executed by the Makefile.
$ make -s greeting Hey there! # The result of the command
-s flag, only the final output of the command is displayed, and the actual command being executed itself is not printed.
- The flags
--dry-runcan be used interchangeably.
-sis same as
In order for
make to locate and read the Makefile correctly, it is crucial to be in the corresponding directory where the Makefile is located, otherwise, you might get the
make: *** No rule to make target error.
Thanks for reading the post!
Don’t forget to explore the Makefiles of the projects you’re contributing to, add some of your rules ( don’t push them though ), run the commands and put your questions, if any, in the Replies below!