C H A P T E R  3

Program Development

This chapter briefly introduces two powerful program development tools, make and SCCS, that can be used very successfully with Fortran programming projects.

A number of good, commercially published books on using make and SCCS are currently available, including Managing Projects with make, by Andrew Oram and Steve Talbott, and Applying RCS and SCCS, by Don Bolinger and Tan Bronson. Both are from O'Reilly & Associates.


3.1 Facilitating Program Builds With the make Utility

The make utility applies intelligence to the task of program compilation and linking. Typically, a large application consists of a set of source files and INCLUDE files, requiring linking with a number of libraries. Modifying any one or more of the source files requires recompilation of that part of the program and relinking. You can automate this process by specifying the interdependencies between files that make up the application along with the commands needed to recompile and relink each piece. With these specifications in a file of directives, make ensures that only the files that need recompiling are recompiled and that relinking uses the options and libraries you need to build the executable. The following discussion provides a simple example of how to use make. For a summary, see make(1S).

3.1.1 The Makefile

A file called makefile tells make in a structured manner which source and object files depend on other files. It also defines the commands required to compile and link the files.

For example, suppose you have a program of four source files and the makefile:


demo% ls
makefile 
commonblock 
computepts.f 
pattern.f 
startupcore.f 
demo%

Assume both pattern.f and computepts.f have an INCLUDE of commonblock, and you wish to compile each.f file and link the three relocatable files, along with a series of libraries, into a program called pattern.

The makefile looks like this:


demo% cat makefile 
pattern: pattern.o computepts.o startupcore.o 
      f95 pattern.o computepts.o startupcore.o -lcore95 \ 
      -lcore -lsunwindow -lpixrect -o pattern 
pattern.o: pattern.f commonblock 
      f95 -c -u pattern.f 
computepts.o: computepts.f commonblock 
      f95 -c -u computepts.f 
startupcore.o: startupcore.f 
      f95 -c -u startupcore.f 
demo%

The first line of makefile indicates that making pattern depends on pattern.o, computepts.o, and startupcore.o. The next line and its continuations give the command for making pattern from the relocatable.o files and libraries.

Each entry in makefile is a rule expressing a target object's dependencies and the commands needed to make that object. The structure of a rule is:

target: dependencies-list
TAB build-commands

3.1.2 make Command

The make command can be invoked with no arguments, simply:


demo% make 

The make utility looks for a file named makefile or Makefile in the current directory and takes its instructions from that file.

The make utility:

3.1.3 Macros

The make utility's macro facility allows simple, parameterless string substitutions. For example, the list of relocatable files that make up the target program pattern can be expressed as a single macro string, making it easier to change.

A macro string definition has the form:

NAME = string

Use of a macro string is indicated by:

$(NAME)

which is replaced by make with the actual value of the macro string.

This example adds a macro definition naming all the object files to the beginning of makefile:


OBJ = pattern.o computepts.o startupcore.o 

Now the macro can be used in both the list of dependencies as well as on the f95 link command for target pattern in makefile:


pattern: $(OBJ)
      f95 $(OBJ) -lcore95 -lcore -lsunwindow \
      -lpixrect -o pattern 

For macro strings with single-letter names, the parentheses may be omitted.

3.1.4 Overriding of Macro Values

The initial values of make macros can be overridden with command-line options to make. For example:


FFLAGS=-u 
OBJ = pattern.o computepts.o startupcore.o
pattern: $(OBJ)
      f95 $(FFLAGS) $(OBJ) -lcore95 -lcore -lsunwindow \
      -lpixrect -o pattern 
pattern.o: pattern.f commonblock
      f95 $(FFLAGS) -c pattern.f
computepts.o:
      f95 $(FFLAGS) -c computepts.f

Now a simple make command without arguments uses the value of FFLAGS set above. However, this can be overridden from the command line:


demo% make "FFLAGS=-u -O" 

Here, the definition of the FFLAGS macro on the make command line overrides the makefile initialization, and both the -O flag and the -u flag are passed to f95. Note that "FFLAGS=" can also be used on the command to reset the macro to a null string so that it has no effect.

3.1.5 Suffix Rules in make

To make writing a makefile easier, make will use its own default rules depending on the suffix of a target file.

The default rules are in the file /usr/share/lib/make/make.rules. When recognizing default suffix rules, make passes as arguments any flags specified by the FFLAGS macro, the -c flag, and the name of the source file to be compiled. Also, the make.rules file uses the name assigned by the FC macro as the name of the Fortran compiler to be used.

The example below demonstrates this rule twice:


FC = f95
OBJ = pattern.o computepts.o startupcore.o 
FFLAGS=-u 
pattern: $(OBJ) 
      f95 $(OBJ) -lcore95 -lcore -lsunwindow \ 
      -lpixrect -o pattern 
pattern.o: pattern.f commonblock 
      f95 $(FFLAGS) -c pattern.f 
computepts.o: computepts.f commonblock 
startupcore.o: startupcore.f

make uses default rules to compile computepts.f and startupcore.f.

There are default suffix rules for .f90 files that will invoke the f95 compiler.

However, unless you define the FC macro to be f95, the default suffix rules for .f and .F files call f77 and not f95.

Furthermore, there are no suffix rules currently defined for .f95 and .F95 files, and .mod Fortran 95 module files will invoke the Modula compiler. To remedy this requires creating your own local copy of the make.rules file in the directory in which make is called, and modifying the file to add .f95 and .F95 suffix rules, and delete the suffix rules for .mod. See the make(1S) man page for details.

3.1.6 .KEEP_STATE and Special Dependency Checking

Use the special target .KEEP_STATE to check for command dependencies and hidden dependencies.

When the .KEEP_STATE: target is effective, make checks the command for building a target against the state file. If the command has changed since the last make run, make rebuilds the target.

When the .KEEP_STATE: target is effective, make reads reports from cpp(1) and other compilation processors for any "hidden" files, such as #include files. If the target is out of date with respect to any of these files, make rebuilds it.


3.2 Version Tracking and Control With SCCS

SCCS stands for Source Code Control System. SCCS provides a way to:

The basic three operations of SCCS are:

This section shows you how to use SCCS to perform these tasks, using the previous program as an example. Only basic SCCS is described and only three SCCS commands are introduced: create, edit, and delget.

3.2.1 Controlling Files With SCCS

Putting files under SCCS control involves:

3.2.1.1 Creating the SCCS Directory

To begin, you must create the SCCS subdirectory in the directory in which your program is being developed. Use this command:


demo% mkdir SCCS

SCCS must be in uppercase.

3.2.1.2 Inserting SCCS ID Keywords

Some developers put one or more SCCS ID keywords into each file, but that is optional. These keywords are later identified with a version number each time the files are checked in with an SCCS get or delget command. There are three likely places to put these strings:

The advantage of using keywords is that the version information appears in the source listing and compiled object program. If preceded by the string @(#), the keywords in the object file can be printed using the what command.

Included header files that contain only parameter and data definition statements do not generate any initialized data, so the keywords for those files usually are put in comments or in parameter statements. In some files, like ASCII data files or makefiles, the SCCS information will appear in comments.

SCCS keywords appear in the form %keyword% and are expanded into their values by the SCCS get command. The most commonly used keywords are:

%Z% expands to the identifier string @(#) recognized by the what command.
%M% expands to the name of the source file.
%I% expands to the version number of this SCCS maintained file.
%E% expands to the current date.

For example, you could identify the makefile with a make comment containing these keywords:


#      %Z%%M%       %I%       %E%

The source files, startupcore.f, computepts.f, and pattern.f, can be identified by initialized data of the form:


      CHARACTER*50 SCCSID 
      DATA SCCSID/"%Z%%M%       %I%       %E%\n"/

When this file is processed by SCCS, compiled, and the object file processed by the SCCS what command, the following is displayed:


demo% f95 -c pattern.f
...
demo% what pattern
pattern:
      pattern.f 1.2 96/06/10    

You can also create a PARAMETER named CTIME that is automatically updated whenever the file is accessed with get.


      CHARACTER*(*) CTIME
      PARAMETER ( CTIME="%E%") 

INCLUDE files can be annotated with a Fortran comment containing the SCCS stamp:


C       %Z%%M%       %I%       %E% 



Note - Use of single letter derived type component names in Fortran 95 source code files can conflict with SCCS keyword recognition. For example, the Fortran 95 structure component reference X%Y%Z when passed through SCCS will become XZ after an SCCS get. Care should be taken not to define structure components with single letters when using SCCS on Fortran 95 programs. For example, had the structure reference in the Fortran 95 program been to X%YY%Z, the %YY% would not have been interpreted by SCCS as a keyword reference. Alternatively, the SCCS get -k option will retrieve the file without expanding SCCS keyword IDs.



3.2.1.3 Creating SCCS Files

Now you can put these files under control of SCCS with the SCCS create command:


demo% sccs create makefile commonblock startupcore.f \ 
  computepts.f pattern.f 
demo%

3.2.2 Checking Files Out and In

Once your source code is under SCCS control, you use SCCS for two main tasks: to check out a file so that you can edit it, and to check in a file you have finished editing.

Check out a file with the sccs edit command. For example:


demo% sccs edit computepts.f 

SCCS then makes a writable copy of computepts.f in the current directory, and records your login name. Other users cannot check the file out while you have it checked out, but they can find out who has checked it out.

When you have completed your editing, check in the modified file with the sccs delget command. For example:


demo% sccs delget computepts.f 

This command causes the SCCS system to:

The sccs delget command is a composite of two simpler SCCS commands, delta and get. The delta command performs the first three tasks in the list above; the get command performs the last two tasks.