Make

GNU Make, or simply Make, is a utility tool designed initially to help to compile code from its source. It is provided as part of any Unix system, so chances are you have it on your Mac or Linux. On Windows, the NMake tool (https://docs.microsoft.com/en-us/cpp/build/reference/nmake-reference?view=vs-2019) can be used as a replacement.

In a nutshell, Make can run one of a few recipes from a Makefile – a tiny text file that is very easy to write. While we obviously don't need to compile Python, Make's interface is so simple, it is quite popular as a common interface for utility scripts or operations. Inside, it has nothing but shell commands. The following is an example Makefile, containing two recipes – test and upload. Both instructions are declared PHONY—this means that they don't result in a file; if we don't declare that, Make will assume that the recipe will produce a file of the same name, and if that file exists, Make will consider the recipe as already having been done and won't run it:

.PHONY: test upload

test:
echo 'testing!'

upload:
aws s3 cp ./ s3://mybucket/wikiwwii --recursive --dryrun

To run instructions, all you need is to get to the folder and type the following:

$ make test
echo 'testing!'
testing!

It is that simple. Makefiles are especially neat if you have to type a long command with many parameters, for example, uploading files using an AWS client somewhere or running PyTest with some flags.

Make requires every line after the instruction name to begin with a tab – and not whitespaces! If you're getting a Makefile:1: *** missing separator. Stop error, you most likely have some whitespaces in place of tabs. You can replace those in VS Code using a special command, or via the terminal (for example, using nano)

In order to be used by Make, the file needs to be named Makefile. If you really need to keep multiple Makefiles in the same folder, you can assign them a meaningful name, and then specify the file, using the -f flag. If needed, Makefiles could become quite complex! You can use recipes in other recipes. Documentation relating to Makefiles can be found at https://www.gnu.org/software/make/manual/html_node/Introduction.html. You are also free to use environmental variables there, so Makefiles could be quite dynamic.

One byproduct of writing Makefiles is that they work as some sort of documentation for your fellow developers – and yourself in the future. So do yourself a favor, and add these whenever you anticipate repeating any shell command more than once. And, by the way, our next tool, Cookiecutter, frequently includes Makefiles in its templates – do check them out!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.95.244