Sun WorkShop TeamWare 2.1 User's Guide

Impact of the dmake Utility on Makefiles

The methods and examples shown in this section present the kinds of problems that lend themselves to solution with dmake. This section does not suggest that any one approach or example is the best. Compromises between clarity and functionality were made in many of the examples.

As procedures become more complicated, so do the makefiles that implement them. You must know which approach will yield a reasonable makefile that works. The examples in this section illustrate common code-development predicaments and some straightforward methods to simplify them using dmake.

Using Makefile Templates

If you use a makefile template from the outset of your project, custom makefiles that evolve from the makefile templates will be:

The less time you spend editing makefiles, the more time you have to develop your program or project.

Building Targets Concurrently

Large software projects typically consist of multiple independent modules that can be built concurrently. The dmake utility supports concurrent processing of targets on multiple machines over a network. This concurrency can markedly reduce the time required to build a large project.

When given a target to build, dmake checks the dependencies associated with that target, and builds those that are out of date. Building those dependencies may, in turn, entail building some of their dependencies. When distributing jobs, dmake starts every target that it can. As these targets complete, dmake starts other targets. Nested invocations of dmake are not run concurrently by default, but this can be changed (see "Restricting Parallelism " for more information).

Since dmake builds multiple targets concurrently, the output of each build is produced simultaneously. To avoid intermixing the output of various commands, dmake collects output from each build separately. The dmake utility displays the commands before they are executed. If an executed command generates any output, warnings, or errors, dmake displays the entire output for that command. Since commands started later may finish earlier, this output may be displayed in an unexpected order.

Limitations on Makefiles

Concurrent building of multiple targets places some restrictions on makefiles. Makefiles that depend on the implicit ordering of dependencies may fail when built concurrently. Targets in makefiles that modify the same files may fail if those files are modified concurrently by two different targets. Some examples of possible problems are discussed in this section.

Dependency Lists

When building targets concurrently, it is important that dependency lists be accurate. For example, if two executables use the same object file but only one specifies the dependency, then the build may cause errors when done concurrently. For example, consider the following makefile fragment:


all: prog1 prog2 
prog1: prog1.o aux.o 
	$(LINK.c) prog1.o aux.o -o prog1 
prog2: prog2.o 
	$(LINK.c) prog2.o aux.o -o prog2 

When built serially, the target aux.o is built as a dependent of prog1 and is up-to-date for the build of prog2. If built in parallel, the link of prog2 may begin before aux.o is built, and is therefore incorrect. The .KEEP_STATE feature of make detects some dependencies, but not the one shown above.

Explicit Ordering of Dependency Lists

Other examples of implicit ordering dependencies are more difficult to fix. For example, if all of the headers for a system must be constructed before anything else is built, then everything must be dependent on this construction. This causes the makefile to be more complex and increases the potential for error when new targets are added to the makefile. The user can specify the special target .WAIT in a makefile to indicate this implicit ordering of dependents. When dmake encounters the .WAIT target in a dependency list, it finishes processing all prior dependents before proceeding with the following dependents. More than one .WAIT target can be used in a dependency list. The following example shows how to use .WAIT to indicate that the headers must be constructed before anything else.

all: hdrs .WAIT libs functions  

You can add an empty rule for the .WAIT target to the makefile so that the makefile is backward-compatible.

Concurrent File Modification

You must make sure that targets built concurrently do not attempt to modify the same files at the same time. This can happen in a variety of ways. If a new suffix rule is defined that must use a temporary file, the temporary file name must be different for each target. You can accomplish this by using the dynamic macros $@ or $*. For example, a .c.o rule that performs some modification of the .c file before compiling it might be defined as:


.c.o:
	awk -f modify.awk $*.c > $*.mod.c 
	$(COMPILE.c) $*.mod.c -o $*.o 
	$(RM) $*.mod.c 

Concurrent Library Update

Another potential concurrency problem is the default rule for creating libraries that also modifies a fixed file, that is, the library. The inappropriate .c.a rule causes dmake to build each object file and then archive that object file. When dmake archives two object files in parallel, the concurrent updates will corrupt the archive file.


.c.a:
	$(COMPILE.c) -o $% $< 
	$(AR) $(ARFLAGS) $@ $% 
	$(RM) $% 

A better method is to build each object file and then archive all the object files after completion of the builds. An appropriate suffix rule and the corresponding library rule are:


.c.a:
	$(COMPILE.c) -o $% $< 
	$(COMPILE.c) -o $% $< 
lib.a: lib.a($(OBJECTS)) 
	$(AR) $(ARFLAGS) $(OBJECTS) 
	$(RM) $(OBJECTS) 

Multiple Targets

Another form of concurrent file update occurs when the same rule is defined for multiple targets. An example is a yacc(1) program that builds both a program and a header for use with lex(1). When a rule builds several target files, it is important to specify them as a group using the + notation. This is especially so in the case of a parallel build.


y.tab.c y.tab.h: parser.y 
	$(YACC.y) parser.y 

This rule is actually equivalent to the two rules:


y.tab.c: parser.y
	$(YACC.y) parser.y
y.tab.h: parser.y
	$(YACC.y) parser.y

The serial version of make builds the first rule to produce y.tab.c and then determines that y.tab.h is up-to-date and need not be built. When building in parallel, dmake checks y.tab.h before yacc has finished building y.tab.c and notices that y.tab.h does need to be built, it then starts another yacc in parallel with the first one. Since both yacc invocations are writing to the same files (y.tab.c and y.tab.h), these files are apt to be corrupted and incorrect. The correct rule uses the + construct to indicate that both targets are built simultaneously by the same rule. For example:


y.tab.c + y.tab.h: parser.y
	$(YACC.y) parser.y

Restricting Parallelism

Sometimes file collisions cannot be avoided in a makefile. An example is xstr(1), which extracts strings from a C program to implement shared strings. The xstr command writes the modified C program to the fixed file x.c and appends the strings to the fixed file strings. Since xstr must be run over each C file, the following new .c.o rule is commonly defined:


.c.o:
	$(CC) $(CPPFLAGS) -E $*.c | xstr -c - 
	$(CC) $(CFLAGS) $(TARGET_ARCH) -c x.c
	mv x.o $*.o

The dmake utility cannot concurrently build targets using this rule since the build of each target writes to the same x.c and strings files. Nor is it possible to change the files used. You can use the special target .NO_PARALLEL: to tell dmake not to build these targets concurrently. For example, if the objects being built using the .c.o rule were defined by the OBJECTS macro, the following entry would force dmake to build those targets serially:

.NO_PARALLEL: $(OBJECTS) 

If most of the objects must be built serially, it is easier and safer to force all objects to default to serial processing by including the .NO_PARALLEL: target without any dependents. Any targets that can be built in parallel can be listed as dependencies of the .PARALLEL: target:


.NO_PARALLEL:
.PARALLEL: $(LIB_OBJECT)

Nested Invocations of Distributed Make

When dmake encounters a target that invokes another dmake command, it builds that target serially, rather than concurrently. This prevents problems where two different dmake invocations attempt to build the same targets in the same directory. Such a problem might occur when two different programs are built concurrently, and each must access the same library. The only way for each dmake invocation to be sure that the library is up-to-date is for each to invoke dmake recursively to build that library. The dmake utility recognizes a nested invocation only when the $(MAKE) macro is used in the command line.

If you nest commands that you know will not collide, you can force them to be done in parallel by using the .PARALLEL: construct.

When a makefile contains many nested commands that run concurrently, the load-balancing algorithm may force too many builds to be assigned to the local machine. This may cause high loads and possibly other problems, such as running out of swap space. If such problems occur, allow the nested commands to run serially.