Makefiles - VPATH and RCS

LeftRight

The macro VPATH tells make where, in addition to the local directory, to search for prerequisites to satisfy the make rules. VPATH has the same format as the shell PATH, a colon (:) delimited selection of directory paths. Don't include ``.'' since the current directory is always searched.

VPATH	= ./RCS:../:../RCS:$(HOME)/sources

The following example keeps the sources under RCS source control. If a source, say sub1.c, is put under RCS control and the RCS directory exists then it is kept in RCS/sub1.c,v and the compilable source only exists when checked out with ``co''.


CC = gcc CFLAGS = -g LD = $(CC) LDFLAGS = RM = rm ECHO = echo EXE = mainx SRCS = main.c sub1.c sub2.c sub3.c OBJS = ${SRCS:.c=.o} VPATH = ./RCS .SUFFIXES: .SUFFIXES: .o .c .c,v .c.o : @$(ECHO) "===== .c -> .o rule" $(CC) $(CFLAGS) -c $< .c,v.o : @$(ECHO) "===== using RCS for .c to .o" co -u $*.c 2>/dev/null $(CC) $(CFLAGS) -c $*.c $(RM) -f $*.c all : $(EXE) $(EXE) : $(OBJS) $(LD) -o $@ $(OBJS) $(OBJS) : proj.h clean : -$(RM) -f $(EXE) $(OBJS)
Suppose the directory looks like this:
% ls -l
-rw-r-----   1 rk    owen     465 Jun 17 08:29 Makefile
drwxr-x---   2 rk    owen    1024 Jun 17 08:31 RCS/
-rw-r-----   1 rk    owen      36 Jun 17 08:31 proj.h
-rw-r-----   1 rk    owen      44 Jun 17 08:27 sub2.c
Which shows that sub2.c is checked out, presumably, for modifying. The ``-r'' option below says to not use any of the pre-defined implicit rules. It was necessary in this example to force the use of the implicit rules defined in the Makefile since the pre-defined ones interfere with ours. The Makefile gives the following results:
% make -r
===== using RCS for .c to .o
co -u main.c 2>/dev/null
gcc -g -c main.c
rm -f main.c
===== using RCS for .c to .o
co -u sub1.c 2>/dev/null
gcc -g -c sub1.c
rm -f sub1.c
===== .c -> .o rule
gcc -g -c sub2.c
===== using RCS for .c to .o
co -u sub3.c 2>/dev/null
gcc -g -c sub3.c
rm -f sub3.c
gcc -o mainx main.o sub1.o sub2.o sub3.o
Notice that for sources in RCS that they are checked out, compiled, and the source is removed, since they're not necessary to keep around. Suppose we've been modifying sub2.c and rerun make.
% make
===== .c -> .o rule
gcc -g -c sub2.c
gcc -o mainx main.o sub1.o sub2.o sub3.o

LeftRight


Slide 6