What is a makefile?

Hello everyone! Ever wanted to write a shell script to automate a task in your project? For example, after cloning the project, do X task and then manipulate Y file, etc. For the same thing before, I used to write shell script files. So that, after cloning the project a user may run those scripts and get the work done. But better than a shell script it is a good idea to add a makefile. Wondering? Keep reading.

What are makefiles and why are they used?

For me, these files are kind of shortcut to write multiple shell scripts in one file separated by labels and then access each script using that label name.

Best example is installing a software.
You clone it,
then execute configure,
that generates a makefile
and then you run make to execute the makefile for complete installation process.

But all projects might not need to use configure to generate a makefile, I mean what about if your project has nothing to do with installations and all? In this case, to automate some shell tasks you may create a static makefile.

Complete explanation of Makefiles is out of the scope of this post. For in-depth reading, please refer: https://www.gnu.org/software/make/manual/make.html

Demo of a makefile

I would take a quick example to illustrate the working. Lets say, we have a project that user can clone and then following tasks need to be done.

  1. Print a message of running” makefile.
  2. Create a new directory, say dump
  3. Create a file in it, say /dump/trash
  4. Add some text to it, say “Going to trash”

I repeat, these tasks could be done using a shell script file like to-do.sh or anything, but its better to use makefile for such post-downloading tasks.

Lets do it!

1. Make a test directory

$ mkdir demo
$ cd demo

2. Create a makefile

$ touch makefile

3. Add some content

create:
	    $(info Makefile is running!)
 	    mkdir dump
	    touch dump/trash
	    echo "Going to trash" >> dump/trash

4. Execute with make

$ make
Makefile is running!
mkdir dump
touch dump/trash
echo "Going to trash" >> dump/trash

make executes commands written in makefile. That’s it. Simple? So let’s create some variations:

5. Add more labels

Add one more label say drop, such that contents of makefile become as follows:

drop:
        $(info Deleting directory dump/)
        rm -rf dump/

create:
	  $(info Makefile is running!)
	  mkdir dump
	  touch dump/trash
	  echo "Going to trash" >> dump/trash

6. Use makefile with versatality

Now, users may enter 2 different commands:

$ make drop
Deleting directory dump/
rm -rf dump/

$ make create
Makefile is running!
mkdir dump
touch dump/trash
echo "Going to trash" >> dump/trash

$ make
Deleting directory dump/
rm -rf dump/

NOTE: Being on the top, drop is acting as a default target which will be called if make command is entered without any specific label.

I am keeping this post short, will keeping on updating it with as much as I’ll learn. Readers may explore more of makefiles as per their interest ๐Ÿ™‚
Here is the link: https://www.gnu.org/software/make/manual/make.html

Conclusion

Makefiles are fantastic! I would be using more of it for post-downloading tasks rather than usingshell script files.

Hope you like makefiles!

Thanks for reading! ๐Ÿ™‚

See you in the next post!

5 thoughts on “What is a makefile?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s