Sunday, March 15, 2009

Extensive usage of Make

Many guys have used make utility with makefile to compile source code. The official introduction of gnu make also introduces it as "Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files".
If we look inside how does make utility works, we'll find it can do much more than compiling code. It's so powerful to make our life much easier, and by our, i mean ordinary people, not just programmers.
The essential point is when combined with shell script, make can assist you doing a sequence of actions to perform automatically.

Typical scenario
Suppose we're writing a book "How to win lottery" which will surely be the best sell all around the world on amazon.com after it's available. Because everyone in different countries would like to have a copy of it, we also need to translate it into different languages.
And a generous, smart programmer provides us a super translation tool that is capable of translating all languages, at no cost.
The last thing is we need to share new chapters to our kind editor by placing documents at //ipaddress/book/(Sorry, I can't share the address with you since it's confidential). She will have some guys to proof reading them.

So, here is our typical working flow:
1. Write / update english version draft
2. Run the translation tool to generate a draft for a different language
3. Save the file according to its language
4. Upload the file to //ipaddress/book
5. Send a email to notify editor

A little bit boring, right ? We need to repeat this again and again when we have new chapter available or the editor asks us to correct errors. Can't we just focus on the writing the book itself ? We have two options, hire a guy to do steps two to five for us, or use make utility.

How to do?
We can define a makefile according to the steps above.

all: generateDraft translate upload sendMail

generateDraft:
draftGenerator.exe -o draft.pdf #this line generate a draft pdf file -o is the argument passed to draftGenerator

translate:
superTranslator.exe --lan $(lan) -o draft_$(lan).pdf #this line translate the draft to specified language and save it as a copy with language in filename

upload:
cp draft_$(lan).pdf //ipaddress/book

sendMail:
#send mail to editor

Then, each time after we update the draft, we can simply use "make lan=chinese"command to ask the compter do the rest for us.

The make utility is actually a parser that read the makefile and perform every actions defined there. So what we need to do is carefully design our makefile.

Summary
Well, to sum up, make utility if useful when we have:
  1. A sequence of steps to perform
  2. Frequent update
make will keep our working process consistent without forgetting to do several steps.

For example, we can define a make file to run all unit tests before comit new code to code respository.

References
Gnu make manual
Compile Apps Your Way With Custom Tasks For The Microsoft Build Engine

No comments: