DEV Community

Cover image for Makefiles Make It Easy
Mohamed Abdelwahed
Mohamed Abdelwahed

Posted on • Updated on

Makefiles Make It Easy

Sometimes when we developing applications we have a lot of commands for a run, test, format code, etc. and those commands may be long and we want to alias it, for many reasons we can't memorize these commands and we want to share it with other team members to make development easy for this situation we will use make utility to help us.

Lets Start

To know more about Make program we can write this command in the terminal.

man make
Enter fullscreen mode Exit fullscreen mode

you should see something like this :

make - GNU make utility to maintain groups of programs

if you faced any errors make sure that make utility installed on your machine.

as you see in man make description that make developed to make compile C programs easy and can be used in other tasks.

Install Make

for most Linux distributions you will find make installed on your machine.

  • Ubuntu run sudo apt-get install build-essential for other distributions check your distro package manager.
  • Mac OS run xcode-select --install.
  • Windows

Get your hands dirty

Let us apply make utility on a PHP lumen app for example if we want to run lumen app with PHP internal server we need to run this command php -S localhost:8000 -t public in the terminal what if we make this command shorter to convert it only to make serve.

  1. Create a file in the application root folder named makefile to tell make what to do.
  2. Open makefile and paste the next code snippet and make sure to use tabs not spaces :
serve:
        php -S localhost:8000 -t public
Enter fullscreen mode Exit fullscreen mode

3.Run make serve in the terminal instead of
php -S localhost:8000 -t public this is a basic usage for makerfile.

Lets Explain

Makefile consists of rules as the next structure of the rule :

target … : prerequisites …
        recipe
        …
        …
Enter fullscreen mode Exit fullscreen mode

makefiles have a lot of detail but we will explain what we need in our scope so let us describe make rule as :

  1. Before : this the name we will execute the rule with.
  2. After : this is a list of prerequisites rule we declared before and want to run before The rule.
  3. Next line starts with tab contain a command we want to run or script consists of several lines.

Example

#Variables
PHP = php

#Rules
serve:
        $(PHP) -S localhost:8000 -t public

test:
        ./vendor/bin/phpunit

fresh-db:
        $(PHP) artisan migrate:fresh

fresh-serve: fresh-db serve

fresh-test: fresh-db test
        echo Done;
Enter fullscreen mode Exit fullscreen mode

Expalination

  • You can run any rule just by type make rule-name terminal opened in your app root folder with makefile in it.
  • As in the first block of the file, we can declare variables to reuse them with $(VariableName) notation.
  • serve, test and fresh-db rules only execute a command you can write any bash command.
  • fresh-serve rule runs the fresh-db and serve rules as prerequisites.
  • fresh-test rule runs the fresh-db and test rules as prerequisites and bash echo command.

Conclusion

I can't cover all make features in one article I just want to explain how I use makefiles to the development process easy you use it with any technology stack you want.

References

  1. GNU make

Top comments (4)

Collapse
 
danielrbradley profile image
Daniel Bradley

Good article! Here's a nice follow-on article that goes into a bit more detail on some specific best practices for makefiles which I've just written.

Collapse
 
mahmoudwageeh94 profile image
Mahmoud Wageeh

Very good article, we need more articles like this

Collapse
 
romarioj2h profile image
Romário Huebra

Very good article, directly to the point.

Collapse
 
shawara profile image
Mahmoud Shawara

Nice article easy to understand in short time .