next up previous
Next: Regular Expressions Up: Scripting Previous: perl

make

Not many people think of make as a scripting language, but it's ideal when a series of steps are used to process files repeatedly. Make builds files according to rules. A rule may describe how to build one type of file from another type , or how to build a specific file from a specific dependency. Make builds any intermediate dependencies required to achieve Suppose your new OpenSource program depends on 3 files: file1.c, file2.c, and main.c. To compile the program, you'd type:
$ gcc -c file1.c
$ gcc -c file2.c
$ gcc -c main.c
$ gcc -o myprogram file1.o file2.o file3.o

That would get old fast. You'd have to remember which .o files were current with respect to their sources, and you'd have to know whether the executable was current with respect to the objects.

Make automates this. By default, it knows how to build a .o file from a .c file. All you need to tell it is that myprogram depends on three object files:

# Makefile for myprogram
myprogram : file1.o file2.o main.o
    gcc -o $@ $^

Make's not limited to programming. How 'bout this for counting url hits and hosts from our web server access log:

# Makefile for access_logs
LOG:=/local/httpd/logs/access_log

default : host.counts url.counts

host.list : ${LOG}
    cut -f1 -d' ' $< >$@

url.list : ${LOG}
    perl -ne 'm/"GET (\S+)/ && print($$1,"\n")' $< >$@

%.sorted : %.list
    sort $< >$@

%.counts : %.sorted
    uniq -c $< >$@




Reece Kimball Hart
1998-03-18