All about Make utility

Make, a linux utility, is used throughout Layer5-light-small 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 : make docs :sunglasses:

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!"

Here:

  • 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 npm start.

Tips:

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 command:

$ make -n greeting
echo "Hey there!" 
# The -n flag suppresses the execution

e.g. 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.

2.

$ make -s  greeting
Hey there!  # The result of the command

With the -s flag, only the final output of the command is displayed, and the actual command being executed itself is not printed.

Note:

  • The flags -n , --just-print and --dry-run can be used interchangeably.
  • Similarly, -s is same as --silent or --quiet flags.

:warning: 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 5-light-small projects you’re contributing to, add some of your rules ( don’t push them though :eyes:), run the commands and put your questions, if any, in the Replies below!

6 Likes