Using the Jambase

Overview

This document describes how to use the various Jambase rules from a functional point of view. You can see the summary of available Jambase rules in the Jambase Reference. The detailed specifications for any Jambase rule can be found by reading the rule definition itself in the Jambase.

Build Configuration

The build configuration rules can only be used in the Jamconfigure script and perform the necessary tests to determine whether all dependencies required for the build are present. When the Jamconfigure script is run in the build configuration step, the Jamrules file is generated. This file is loaded at the beginning of the build step.

CheckBuildC, CheckBuildC++ Rules

Once the toolset has been configured with the ConfigureC or the ConfigureC++ rule, the rules CheckBuildC and CheckBuildC++ can be used to build small test programs to find out if headers and libraries are available.

    WITH_DL = [ CheckBuildC++ "#include "
                              "int main() { dlopen(0,0); return 0; }"
                             : ""
                             : -ldl ] ; 
    if ! 
    {
        Exit "dlopen not available" ;
    }

This uses the C++ toolset to compile a small program calling a function of the dl library. The program is linked with the linker flag -ldl. The third argument passed to CheckBuildC++ is empty, but additional compiler flags could be passed here. An optional fifth argument allows to pass linker paths to the build command. The rule will return nothing if the program can not be built and a boolean-like value on success. The command will be logged in the file Jamrules.log.

Configure Rule

The Configure rule can be used to make the result of a test available to the following build steps.

    WITH_FOO = ;
    if --with-foo in $(ARGS)
    {
        WITH_FOO = yes ;
    }

    Configure WITH_FOO ; 

This tests if the command line option "--with-foo" was set and if so, the jam variable named WITH_FOO is stored in the build configuration result file. Note, that empty variables are not stored, so later in the Jamfiles it can simply be tested if WITH_FOO is set or not.

ConfigureCc, ConfigureC++ Rules

The ConfigureCc and ConfigureC++ rules can be used to configure the toolset, so that C or C++ programs can be built with the build rules like Main, Library and SharedLibrary. They do not require any arguments and have to be invoked before any other tests are done that require the toolsets to be functional.

    ConfigureCc ;
    ConfigureC++ ;

Most, if not all Jamconfigure scripts call one or both rules at the beginning. All tests will be logged in the file Jamrules.log.

EchoOption

This rule formats and prints the value and description of a configured value. It has the purpose to indicate progress during the configuration step.

    EchoOption --with-foo : "Enable foo module" : $(WITH_FOO:E="no") ;

The first argument is the option itself, the second one the description and the third one the value configured for the option. The third argument is optional and if left away, the first argument will be taken as the name of the jam variable that holds the value:

    EchoOption MY_INSTALL_BINDIR : "Program installation directory" ;

If this form is used, it will be indicated that the MY_INSTALL_BINDIR variable can be overridden with the -s command line switch, in this case something like -sMY_INSTALL_BINDIR=/some/dir.

TryCommand

The TryCommand rule takes one argument and that is a shell command to execute. It can be used to check if a required tool is in the path:

    if [ TryCommand "ls" ]
    {
        Echo The ls command is available ;
    }
    else
    {
        Echo The ls command is not available ;
    }
The rule returns nothing if the return code of the shell command indicates failure. All output of the command will be written to the logfile config.log. This is the most basic form to perform tests, but usually one of the more high-level configuration rules will be used.

Building Executables and Libraries

The rules to build targets can only be used in the Jamfiles and the Jamrules script. All build options that were configured are available in the build step. Some rules like Main, Library and SharedLibray can only work if the toolset has been configured previously.

Handling Directory Trees

The SubDir* rules are used to define source code directory hierarchies. With SubDir and SubInclude, you can use jam to build software from source files and Jamfiles spread across many directories, as is typical for large projects. The SubDir* rules unify an entire source code tree so that jam can read in all the Jamfiles in one pass and compute dependencies across the entire project.

To use the SubDir* rules, you must:

  1. Preface the Jamfile in each directory with an invocation of the SubDir rule.

  2. Optionally, set an environment variable pointing to the root directory of the source tree. The variable's name is left up to you, but in these examples, we use TOP.

SubDir Rule

The SubDir rule must be invoked before any rules that refer to the contents of the directory - it is best to put it at the top of each Jamfile. For example:

    # Jamfile in $(TOP)/src/util directory.

    SubDir TOP src util ;

    Main myprog : main.c util.c ;                   
    LinkLibraries myprog : libtree ;     
    Library libtree : treemake.c treetrav.c ;    
This compiles four files in $(TOP)/src/util, archives two of the objects into libtree, and links the whole thing into myprog. Outputs are placed in the $(TOP)/src/util directory. Two things are happening behind the scenes:
  • The SubDir rule causes jam to read in the $(TOP)/Jamrules file. (The Jamrules file can alternately be named by the variable $(xxxRULES), where xxx is the name of the root variable, e.g., $(TOPRULES)).

    Jamrules is only read in once, at the first SubDir invocation.

  • The SubDir rule initializes a set of variables that are used by Main and other rules to uniquely identify the source files in this directory and assign locations to the targets built from files in this directory.

    When you have set a root variable, e.g., $(TOP), SubDir constructs path names rooted with $(TOP), e.g., $(TOP)/src/util. Otherwise, SubDir constructs relative pathnames to the root directory, computed from the number of arguments to the first SubDir rule, e.g., ../../src/util. In either case, the SubDir rule constructs the path names that locate source files. You'll see how this is useful later.

The SubDir rule takes as its first argument the root variable's name and takes as subsequent arguments the directory names leading from the root to the directory of the current Jamfile. Note that the name of the subdirectory is given as individual elements: the SubDir rule does not use system-specific directory name syntax.

SubInclude Rule

The SubInclude rule is used in a Jamfile to cause another Jamfile to be read in. Its arguments are in the same format as SubDir's.

The recommended practice is only to include one level of subdirectories at a time, and let the Jamfile in each subdirectory include its own subdirectories. This allows a user to sit in any arbitrary directory of the source tree and build that subtree. For example:

       # This is $(TOP)/Jamfile, top level Jamfile for mondo project.

       SubInclude TOP src ;
       SubInclude TOP man ;
       SubInclude TOP misc ;
       SubInclude TOP util ;
If a directory has both subdirectories of its own as well as files that need building, the SubIncludes should be either before the SubDir rule or be at the end of the Jamfile - not between the SubDir and other rule invocations. For example:
    # This is $(TOP)/src/Jamfile:

    SubDir TOP src ;

    Main mondo : mondo.c ;
    LinkLibraries mondo : libmisc libutil ;
    
    SubInclude TOP src misc ;
    SubInclude TOP src util ;

(jam processes all the Jamfiles it reads as if it were reading one single, large Jamfile. Build rules like Main and LinkLibraries rely on the preceding SubDir rule to set up source file and output file locations, and SubIncludes rules read in Jamfiles that contain SubDir rules. So if you put a SubIncludes rule between a SubDir and a Main rule, jam will try to find the source files for the Main rule in the wrong directory.)

Variables Used to Handle Directory Trees

The following variables are set by the SubDir rule and used by the Jambase rules that define file targets:

SEARCH_SOURCE The SubDir targets (e.g., "TOP src util") are used to construct a pathname (e.g., $(TOP)/src/util), and that pathname is assigned to $(SEARCH_SOURCE). Rules like Main and Library use $(SEARCH_SOURCE) to set search paths on source files.
LOCATE_SOURCE Initialized by the SubDir rule to the same value as $(SEARCH_SOURCE), unless ALL_LOCATE_TARGET is set. $(LOCATE_SOURCE) is used by rules that build generated source files (e.g., Yacc and Lex) to set location of output files. Thus the default location of built source files is the directory of the Jamfile that defines them.
LOCATE_TARGET Initalized by the SubDir rule to the same value as $(SEARCH_SOURCE), unless ALL_LOCATE_TARGET is set. $(LOCATE_TARGET) is used by rules that build binary objects (e.g., Main and Library) to set location of output files. Thus the default location of built binaray files is the directory of the Jamfile that defines them.
ALL_LOCATE_TARGET If $(ALL_LOCATE_TARGET) is set, LOCATE_SOURCE and and LOCATE_TARGET are set to $(ALL_LOCATE_TARGET) instead of to $(SEARCH_SOURCE). This can be used to direct built files to be written to a location outside of the source tree, and enables building from read-only source trees.
SOURCE_GRIST The SubDir targets are formed into a string like "src!util" and that string is assigned to SOURCE_GRIST. Rules that define file targets use $(SOURCE_GRIST) to set the "grist" attribute on targets. This is used to assure uniqueness of target identifiers where filenames themselves are not unique. For example, the target identifiers of $(TOP)/src/client/main.c and $(TOP)/src/server/main.c would be <src!client>main.c and <src!server>main.c.

The $(LOCATE_TARGET) and $(SEARCH_SOURCE) variables are used extensively by rules in Jambase: most rules that generate targets (like Main, Object, etc.) set $(LOCATE) to $(LOCATE_TARGET) for the targets they generate, and rules that use sources (most all of them) set $(SEARCH) to be $(SEARCH_SOURCE) for the sources they use.

$(LOCATE) and $(SEARCH) are better explained in Basic Usage but in brief they tell jam where to create new targets and where to find existing ones, respectively.

Note that you can reset these variables after SubDir sets them. For example, this Jamfile builds a program called gensrc, then runs it to create a source file called new.c:


       SubDir TOP src util ;
       Main gensrc : gensrc.c ;
       LOCATE_SOURCE = $(NEWSRC) ;
       GenFile new.c : gensrc ;
       
By default, new.c would be written into the $(TOP)/src/util directory, but resetting LOCATE_SOURCE causes it to be written to the $(NEWSRC) directory. ($(NEWSRC) is assumed to have been set elsewhere, e.g., in Jamrules.)

VMS Notes

On VMS, the logical name table is not imported as is the environment on UNIX. To use the SubDir and related rules, you must set the value of the variable that names the root directory. For example:
              TOP = USR_DISK:[JONES.SRC] ;

              SubInclude TOP util ;
The variable must have a value that looks like a directory or device. If you choose, you can use a concealed logical. For example:
              TOP = TOP: ;

              SubInclude TOP util ;
The : at the end of TOP makes the value of $(TOP) look like a device name, which jam respects as a directory name and will use when trying to access files. TOP must then be defined from DCL:
              $ define/job/translation=concealed TOP DK100:[USERS.JONES.SRC.]
Note three things: the concealed translation allows the logical to be used as a device name; the device name in the logical (here DK100) cannot itself be concealed logical (VMS rules, man); and the directory component of the definition must end in a period (more VMS rules).

Main Rule

The Main rule compiles source files and links the resulting objects into an executable. For example:
        Main myprog : main.c util.c ;
    
This compiles main.c and util.c and links main.o and util.o into myprog. The object files and resulting executable are named appropriately for the platform.

Main uses the Objects rule to compile source targets.

Library Rule

The Library rule compiles source files, archives the resulting object files into a static library, and then deletes the object files. For example:
        Library libstring : strcmp.c strcpy.c strlen.c ;
        Library libtree : treemake.c treetrav.c ;
    
This compiles five source files, archives three of the object files into libstring and the other two into libtree. Actual library filenames are formed with the $(SUFLIB) suffix. Once the objects are safely in the libraries, the objects are deleted.

Library uses the Objects rule to compile source files.

SharedLibrary

The rule 'SharedLibrary' compiles source files and links the resulting objects into a shared library. For example:
        SharedLibrary example : example.c util.c ;
    
This compiles example.c and util.c and links example.o and util.o into example. The object files and resulting executable are named appropriately for the platform. Main uses the Objects rule to compile source targets.

Normally, SharedLibrary uses $(SUFLIB) and $(SUFDLL) to determine the prefix/suffix on the filename of the built target. Under windows the resulting library will be called example.dll and a import library called example.lib will be created. Under unix, one shared library called libexample.so will be created. To override it, you can supply a prefix/suffix explicity.

LinkLibraries Rule

To link executables or shared libraries with built static libraries, use the LinkLibraries rule. For example:
        Library tree : tree.c ;
        Library string : string.c ;

        Main myprog : main.c util.c ;
        LinkLibraries myprog : tree ;

        SharedLibrary mydll : dll.c stuff.c ;
        LinkLibraries mydll : string tree ;
    
The LinkLibraries rule does two things: it makes the libraries dependencies of the executable, so that they get built first; and it makes the libraries show up on the command line that links the executable. The ordering of the lines above is important, because the preceeding Main or SharedLibrary Rule define its target.

You can put multiple libraries on a single invocation of the LinkLibraries rule, or you can provide them in multiple invocations. In both cases, the libraries appear on the link command line in the order in which they were encountered.

LinkSharedLibraries Rule

To link executables or shared libraries with built shared libraries, use the LinkSharedLibraries rule. For example:
        SharedLibrary mydll : dll.c stuff.c ;

        Main myprog : main.c util.c ;
        LinkSharedLibraries myprog : mydll ;
    
The LinkSharedLibraries rule does two things: it makes the libraries dependencies of the executable, so that they get built first; and it makes the libraries show up on the command line that links the executable. The ordering of the lines above is important, because the preceeding Main or SharedLibrary Rules type its target.

You can put multiple libraries on a single invocation of the LinkSharedLibraries rule, or you can provide them in multiple invocations. In both cases, the libraries appear on the link command line in the order in which they were encountered. E. g.:

        LinkSharedLibraries myprog : dll1 dll2 ;
    

LinkLibs Rule

The LinkLibs rule adds library linker flags for a target such as an executable of shared library. The rule does so by setting the LINKLIBS variable on the targets specified in the first argument. The following example uses the library linker flags contained in MYPROG_LINKLIBS when myprog is linked:
        Main myprog : main.c util.c ;
        LinkLibs myprog :  ;
    

This rule is useful for linker flags that specify which libraries should be linked, like the -lxxx flags for most unix toolsets, because these flags must be placed at a special position in the linker command line. Use the LinkFlags rule for other linker flags.

LinkFlags Rule

The LinkFlags adds specific linker flag for a target such as an executable or shared library. For example:
        Main myprog : main.c util.c ;
        LinkFlags myprog : $(FLAGS) ;

        SharedLibrary mydll : dll.c stuff.c ;
        LinkFlags mydll : $(FLAGS) ;
    
This adds the linker flags contained in $(FLAGS) to the command line that links the targets myprog or mydll. Do not use this rule for linker flags that specify which libraries should be linked, like the -lxxx flags required by most unix toolsets. Use the LinkLibs rule for this, because these library linker flags have to be placed at a special position in the linker command line.

Variables Used in Building Executables and Libraries

AR Archive command, used for Library targets.
SUFEXE *Suffix on filenames of executables referenced by Main and LinkLibraries.
SUFDLL *Suffix on filenames of shared libraries referenced by SharedLibrary and LinkSharedLibraries.
LINK Link command, used for Main targets.
LINKFLAGS Linker flags.
LINKLIBS Link libraries that aren't dependencies. (See note below.)
EXEMODE *File permissions on Main targets.
MODE Target-specific file permissions on Main targets (set from $(EXEMODE))
RANLIB Name of ranlib program, if any.

Variables above marked with "*" are used by the build rules. Their values at the time the rules are invoked are used to set target-specific variables.

All other variables listed above are globally defined, and are used in actions that update build targets. This means that the global values of those variables are used, unless target-specific values have been set. (For instance, a target-specific MODE value is set by the Main rule.) The target-specific values always override global values.

Note that there are several ways to specify link libraries for executables:

  • Use the LinkLibraries rule to specify built libraries; i.e., libraries that are built by Library rules. This assures that these libraries are built first, and that Main targets are rebuilt when the libraries are updated.

  • Use the LinkLibs rule or LINKLIBS variable to specify external libraries; e.g., system libraries or third-party libraries. The actual link command flag that specifies the libraries has to be used.

For example:

    #In Jamrules:
        X11LINKLIBS ?= -lXext -lX11 ;

    #In Jamfile:
        Library libxutil : xtop.c xbottom.c xutil.c ;
        
        Main xprog : xprog.c ;
        LinkLibraries xprog : libxutil ;

        LinkLibs xprog : $(X11LINKLIBS) ;
        # Or: LINKLIBS on xprog$(SUFEXE) = $(X11LINKLIBS) ;
This example uses the Jam syntax "variable on target" to set a target-specific variable. In this way, only xprog will be linked with this special $(X11LINKLIBS), even if other executables were going to be built by the same Jamfile. Note that when you set a variable on a target, you have to specify the target identifer exactly, which in this case is the suffixed filename of the executable. Therefore you should always prefer to use the LinkLibs rule instead of setting the LINKLIBS variable directly. The actual link command line on Unix, for example, would look something like this:
              cc -o xprog xprog.o libxutil.a -lXext -lX11

Compiling

Compiling of source files occurs normally as a byproduct of the Main or Library rules, which call the rules described here. These rules may also be called explicitly if the Main and Library behavior doesn't satisfy your requirements.

Objects Rule

The Main and Library rules call the Objects rule on source files. Compiled object files built by the Objects rule are a dependency of the obj pseudotarget, so "jam obj" will build object files used in Main and Library rules.

Target identifiers created by the Objects rule have grist set to $(SOURCE_GRIST). So given this Jamfile:

        SubDir TOP src lock ;
        Main locker : lock.c ;
       
the object file created is lock.o (or lock.obj) and its target identifier is <src!lock>lock.o (or <src!lock>lock.obj).

You can also call Objects directly. For example:

              Objects a.c b.c c.c ;
This compiles a.c into a.o, b.c into b.o, etc. The object file suffix is supplied by the Objects rule.

Object Rule

Objects gets its work done by calling the Object rule on each of the source files. You could use the Object rule directly. For example, on Unix, you could use:
              Object foo.o : foo.c ;

However, the Object rule does not provide suffixes, and it does not provide the grist needed to construct target identifiers if you are using the SubDir* rules. A portable and robust Jamfile would need to invoke Object thus:
          Object <src!util>foo$(SUFOBJ) : <src!util>foo.c ;
    
which is inelegant and clearly shows why using Objects is better than using Object.

If there's any advantage to the Object rule, it's that it doesn't require that the object name bear any relationship to the source. It is thus possible to compile the same file into different objects. For example:


              Object a.o : foo.c ;
              Object b.o : foo.c ;
              Object c.o : foo.c ;
This compiles foo.c (three times) into a.o, b.o, and c.o. Later examples show how this is useful.

The Object rule looks at the suffix of the source file and calls the appropriate rules to do the actual preprocessing (if any) and compiling needed to produce the output object file. The Object rule is capable of the generating of an object file from any type of source. For example:

              Object grammar$(SUFOBJ) : grammar.y ;
              Object scanner$(SUFOBJ) : scanner.l ;
              Object fastf$(SUFOBJ) : fastf.f ;
              Object util$(SUFOBJ) : util.c ;
An even more elegant way to get the same result is to let the Objects rule call Object:
              Objects grammar.y scanner.l fastf.f util.c ;
    

In addition to calling the compile rules, Object sets up a bunch of variables specific to the source and target files. (See Variables Used in Compiling, below.)

Cc, C++, Yacc, Lex, Fortran, As, etc. Rules

The Object rule calls compile rules specific to the suffix of the source file. (You can see which suffixes are supported by looking at the Object rule definition in Jambase.) Because the extra work done by the Object rule, it is not always useful to call the compile rules directly. But the adventurous user might attempt it. For example:

              Yacc grammar.c : grammar.y ;
              Lex scan.c : scan.l ;
              Cc prog.o : prog.c ;
These examples individually run yacc(1), lex(1), and the C compiler on their sources.

UserObject Rule

Any files with suffixes not understood by the Object rule are passed to the UserObject rule. The default definition of UserObject simply emits a warning that the suffix is not understood. This Jambase rule definition is intended to be overridden in Jamrules with one that recognizes the project-specific source file suffixes. For example:
    #In Jamrules:

              rule UserObject
              {
                  switch $(>)
                  {
                  case *.rc   : ResourceCompiler $(<) : $(>) ;
                  case *      : ECHO "unknown suffix on" $(>) ;
                  }
              }

              rule ResourceCompiler
              {
                  DEPENDS $(<) : $(>) ;
          Clean clean : $(<) ;
              }

              actions ResourceCompiler
              {
                  rc /fo $(<) $(RCFLAGS) $(>)
              }


    #In Jamfile:

              Library liblock : lockmgr.c ;
          if $(NT) { Library liblock : lock.rc ; }

In this example, the UserObject definition in Jamrules allows *.rc files to be handle as regular Main and Library sources. The lock.rc file is compiled into lock.obj by the "rc" command, and lock.obj is archived into a library with other compiled objects.

LibraryFromObjects Rule

Sometimes the Library rule's straightforward compiling of source into object modules to be archived isn't flexible enough. The LibraryFromObjects rule does the archiving (and deleting) job of the Library rule, but not the compiling. The user can make use of the Objects or Object rule for that. For example:
              LibraryFromObjects libfoo.a : max.o min.o ;
              Object max.o : maxmin.c ;
              Object min.o : maxmin.c ;
              ObjectCcFlags max.o : -DUSEMAX ;
              ObjectCcFlags min.o : -DUSEMIN ;
This Unix-specific example compiles the same source file into two different objects, with different compile flags, and archives them. (The ObjectCcFlags rule is described shortly.) Unfortunately, the portable and robust implementation of the above example is not as pleasant to read:

          SubDir TOP foo bar ;
              LibraryFromObjects libfoo$(SUFLIB) : <foo!bar>max$(SUFOBJ)
                                       <foo!bar>min$(SUFOBJ) ;
              Object <foo!bar>min$(SUFOBJ) : <foo!bar>maxmin.c ;
              Object <foo!bar>max$(SUFOBJ) : <foo!bar>maxmin.c ;
          ObjectCcFlags <foo!bar>min$(SUFOBJ) : -DUSEMIN ;
          ObjectCcFlags <foo!bar>max$(SUFOBJ) : -DUSEMAX ;
       
Note that, among other things, you must supply the library file suffix when using the LibraryFromObjects rule.

MainFromObjects Rule

Similar to LibraryFromObjects, MainFromObjects does the linking part of the Main rule, but not the compiling. MainFromObjects can be used when there are no objects at all, and everything is to be loaded from libraries. For example:
              MainFromObjects testprog ;
              LinkLibraries testprog : libprog ;
              Library libprog : main.c util.c ;
On Unix, say, this generates a link command that looks like:
              cc -o testprog libprog.a

Linking purely from libraries is something that doesn't work everywhere: it depends on the symbol "main" being undefined when the linker encounters the library that contains the definition of "main".

Variables Used in Compiling

The following variables control the compiling of source files:

C++ The C++ compiler command
CC The C compiler command
C++FLAGS
CCFLAGS
Compile flags, used to create or update compiled objects
SUBDIRC++FLAGS
SUBDIRCCFLAGS
Additonal compile flags for source files in this directory.
OPTIM Compiler optimization flag. The Cc and C++ actions use this as well as C++FLAGS or CCFLAGS.
HDRS Non-standard header directories; i.e., the directories the compiler will not look in by default and which therefore must be supplied to the compile command. These directories are also used by jam to scan for include files.
STDHDRS Standard header directories, i.e., the directories the compiler searches automatically. These are not passed to the compiler, but they are used by jam to scan for include files.
SUBDIRHDRS Additional paths to add to HDRS for source files in this directory.
LEX The lex(1) command
YACC The yacc(1) command

The Cc rule sets a target-specific $(CCFLAGS) to the current value of $(CCFLAGS) and $(SUBDIRCCFLAGS). Similarly for the C++ rule. The Object rule sets a target-specific $(HDRS) to the current value of $(HDRS) and $(SUBDDIRHDRS).

$(CC), $(C++), $(CCFLAGS), $(C++FLAGS), $(OPTIM), and $(HDRS) all affect the compiling of C and C++ files. $(OPTIM) is separate from $(CCFLAGS) and $(C++FLAGS) so they can be set independently.

$(HDRS) lists the directories to search for header files, and it is used in two ways: first, it is passed to the C compiler (with the flag -I prepended); second, it is used by HdrRule to locate the header files whose names were found when scanning source files. $(STDHDRS) lists the header directories that the C compiler already knows about. It does not need passing to the C compiler, but is used by HdrRule.

Note that these variables, if set as target-specific variables, must be set on the target, not the source file. The target file in this case is the object file to be generated. For example:

              Library libximage : xtiff.c xjpeg.c xgif.c ;

              HDRS on xjpeg$(SUFOBJ) = /usr/local/src/jpeg ;
              CCFLAGS on xtiff$(SUFOBJ) = -DHAVE_TIFF ;
This can be done more easily with the rules that follow.

ObjectCcFlags, ObjectC++Flags, ObjectHdrs Rules

$(CCFLAGS), $(C++FLAGS) and $(HDRS) can be set on object file targets directly, but there are rules that allow these variables to be set by referring to the original source file name, rather than to the derived object file name. ObjectCcFlags adds object-specific flags to the $(CCFLAGS) variable, ObjectC++Flags adds object-specific flags to the $(C++FLAGS) variable, and ObjectHdrs add object-specific directories to the $(HDRS) variable. For example:
    #In Jamrules:
        if $(NT) { CCFLAGS_X = /DXVERSION ;
               HDRS_X = \\\\SPARKY\\X11\\INCLUDE\\X11 ;
                 }

    #In Jamfile:
              Main xviewer : viewer.c ;
              ObjectCcFlags viewer.c : $(CCFLAGS_X) ;
              ObjectHdrs viewer.c : $(HDRS_X) ;
The ObjectCcFlags and ObjectHdrs rules take .c files as targets, but actually set $(CCFLAGS) and $(HDRS) values on the .obj (or .o) files. As a result, the action that updates the target .obj file uses the target-specific values of $(CCFLAGS) and $(HDRS).

SubDirCcFlags, SubDirC++Flags, SubDirLinkFlags, SubDirHdrs, SubDirObjects Rules

SubDirCcFlags, SubDirC++Flags and SubDirHdrs set the values of $(SUBDIRCCFLAGS), $(SUBDIRC++FLAGS), $(SUBDIRLINKFLAGS) and $(SUBDIRHDRS) which are used by the Cc, C++, Link, and Object rules when setting the target-specific values for $(CCFLAGS), $(C++FLAGS), $(LINKFLAGS) and $(HDRS). The SubDir rule clears these variables out, and thus they provide directory-specific values of $(CCFLAGS), $(C++FLAGS) and $(HDRS). The SubDirObjects rule sets the default location for all object files generated by the Object rule in a specific directory. For example:
    #In Jamrules:
          GZHDRS = $(TOP)/src/gz/include ;
          GZFLAG = -DGZ ;
          OBJS = $(TOP)/objs

    #In Jamfile:
          SubDir TOP src gz utils ;

          SubDirHdrs $(GZHDRS) ;
          SubDirCcFlags $(GZFLAG) ;
          SubDirObjects $(OBJS) ;

          Library libgz : gizmo.c ;
          Main gizmo : main.c ;
          LinkLibraries gizmo : libgz ;
All .c files in this directory will be compiled with $(GZFLAG) as well as the default $(CCFLAG), and the include paths used on the compile command will be $(GZHDRS) as well as the default $(HDRS). All objects will be generated in $(TOP)/objs.

Header File Processing

One of the functions of the Object rule is set up scanning of source files for (C style) header file inclusions. To do so, it sets the special variables $(HDRSCAN) and $(HDRRULE) as target-specific variables on the source file. The presence of these variables triggers a special mechanism in jam for scanning a file for header file inclusions and invoking a rule with the results of the scan. The $(HDRSCAN) variable is set to an egrep(1) pattern that matches "#include" statements in C source files, and the $(HDRRULE) variable is set to the name of the rule that gets invoked as such:
              $(HDRRULE) source-file : included-files ;

This rule is supposed to set up the dependencies between the source file and the included files. The Object rule uses HdrRule to do the job. HdrRule itself expects another variable, $(HDRSEARCH), to be set to the list of directories where the included files can be found. Object does this as well, setting $(HDRSEARCH) to $(HDRS) and $(STDHDRS).

The header file scanning occurs during the "file binding" phase of jam, which means that the target-specific variables (for the source file) are in effect. To accomodate nested includes, one of the HdrRule's jobs is to pass the target-specific values of $(HDRRULE), $(HDRSCAN), and $(HDRSEARCH) onto the included files, so that they will be scanned as well.

HdrRule Rule

Normally, HdrRule is not invoked directly; the Object rule (called by Main and Library) invokes it.

If there are special dependencies that need to be set, and which are not set by HdrRule itself, you can define another rule and let it invoke HdrRule. For example:

    #In Jamrules:
              rule BuiltHeaders
              {
                      DEPENDS $(>) : mkhdr$(SUFEXE) ;
                      HdrRule $(<) : $(>) ;
              }

    #In Jamfile:
              Main mkhdr : mkhdr.c ;
              Main ugly : ugly.c ;

              HDRRULE on ugly.c = BuiltHeaders ;

This example just says that the files included by "ugly.c" are generated by the program "mkhdr", which can be built from "mkhdr.c". During the binding phase, jam will scan ugly.c, and if it finds an include file, ughdr.h, for example, it will automatically invoke the rule:
              BuiltHeaders ugly.c : ughdr.h ;
       
By calling HdrRule at the end of BuiltHeaders, all the gadgetry of HdrRule takes effect and it doesn't need to be duplicated.

Variables Used for Header Scanning

HDRPATTERN Default scan pattern for "include" lines.
HDRSCAN Scan pattern to use. This is a special variable: during binding, if both HDRSCAN and HDRRULE are set, scanning is activated on the target being bound. The HdrRule and Object rules sets this to $(HDRPATTERN) on their source targets.
HDRRULE Name of rule to invoked on files found in header scan. The HdrRule and Object rules set this to "HdrRule" on their source targets. This is also a special variable; it's the only jam variable that can hold the name of a rule to be invoked.
HDRSEARCH Search paths for files found during header scanning. This is set from $(HDRS) and $(STDHDRS), which are described in the Compiling section. jam will search $(HDRSEARCH) directories for the files found by header scans.

The Object rule sets HDRRULE and HDRSCAN specifically for the source files to be scanned, rather than globally. If they were set globally, jam would attempt to scan all files, even library archives and executables, for header file inclusions. That would be slow and probably not yield desirable results.

Copying Files

File Rule

The File rule copies one file to another. The target name needn't be the same as the source name. For example:
    switch $(OS)
    {
           case NT*  : File config.h : confignt.h ;
       case *    : File config.h : configunix.h ;
    }
    LOCATE on config.h = $(LOCATE_SOURCE) ;
This creates a config.h file from either confignt.h or configunix.h, depending on the current build platform.

The File rule does not use the LOCATE_SOURCE variable set by the SubDir rule (although it does use SEARCH_SOURCE), which means you have to set the copied file's output directory yourself. That's done by setting the special LOCATE variable on the target, as shown above, or with the MakeLocate rule described below.

Bulk Rule

The Bulk rule is a shorthand for many invocations of the File rule when all files are going to the same directory. For example:
    #In Jamrules:
              DISTRIB_GROB = d:\\distrib\\grob ;

    #In Jamfile:
              Bulk $(DISTRIB_GROB) : grobvals.txt grobvars.txt ;
This causes gobvals.txt and grobvars.txt to be copied into the $(DISTRIB_GROB) directory.

HardLink Rule

The Unix-only HardLink rule makes a hard link (using ln(1)) from the source to the target, if there isn't one already. For example:
              HardLink config.h : configunix.h ;

Shell Rule

The Shell rule is like the File rule, except that on Unix it makes sure the first line of the target is "#!/bin/sh" and sets the permission to make the file executable. For example:
              Shell /usr/local/bin/add : add.sh ;

You can also use $(SHELLHEADER) to dictate what the first line of the copied file will be. For example:

              Shell /usr/local/bin/add : add.awk ;
              SHELLHEADER on /usr/local/bin/add = "#!/bin/awk -f" ;

This installs an awk(1) script.

Variables Used When Copying Files

FILEMODE Default file permissions for copied files
SHELLMODE Default file permissions for Shell rule targets
MODE File permissions set on files copied by File, Bulk, and Shell rules. File and Shell sets a target-specific MODE to the current value of $(FILEMODE) or $(SHELLMODE), respectively.
SHELLHEADER String to write in first line of Shell targets (default is #!/bin/sh).

Installing Files

MakeInstall Rule

The MakeInstall rule installs a target at a given location. It works for all targets defined by the Main, Library and SharedLibrary rules.

    Main myprog : main.c ;
    MakeInstall myprog :  ;
This installs the program "myprog" to the path given by the variable MY_INSTALL_BINDIR. This rule calls one or more of the other Install* rules as appropriate.

Install Rules

Jambase provides a set of Install* rules to copy files into an destination directory and set permissions on them. On Unix, the install(1) program is used. If the destination directory does not exist, jam creates it first.

All files copied with the Install* rules are dependencies of the install pseudotarget, which means that the command "jam install" will cause the installed copies to be updated. Also, "jam uninstall" will cause the installed copies to be removed.

The Install* rules are:

InstallBin Copies file and sets its permission to $(EXEMODE). You must specify the suffixed executable name. E.g.:
InstallBin $(BINDIR) : thing$(SUFEXE) ;
           
InstallFile Copies file and sets its permission to $(FILEMODE). E.g.:
InstallFile $(DESTDIR) : readme.txt ;
           
InstallLib Copies file and sets its permission to $(FILEMODE). You must specify the suffixed library name. E.g.:
InstallLib $(LIBDIR) : libzoo$(SUFLIB) ;
           
InstallMan Copies file into the mann subdirectory of the target directory and sets its permission to $(FILEMODE). E.g., this copies foo.5 into the $(DESTDIR)/man5 directory:
InstallMan $(DESTDIR) : foo.5 ;
           
InstallShell Copies file and sets its permission to $(SHELLMODE). E.g.:
InstallShell $(DESTDIR) : startup ;
           

Variables

The following variables control the installation rules:

INSTALL The install program (Unix only)
FILEMODE Default file permissions on readable files.
EXEMODE Default file permission executable files.
SHELLMODE Default file permission on shell script files.
MODE Target-specific file permissions

The Install rules set a target-specific MODE to the current value of $(FILEMODE), $(EXEMODE), or $(SHELLMODE), depending on which Install rule was invoked.

The directory variables are just defined for convenience: they must be passed as the target to the appropriate Install rule. The $(INSTALL) and mode variables must be set (globally) before calling the Install rules in order to take effect.

Miscellaneous Rules

Clean Rule

The Clean rule defines files to be removed when you run "jam clean". Any site-specific build rules defined in your Jamrules should invoke Clean so that outputs can be removed. E.g.,

    rule ResourceCompiler
    {
       DEPENDS $(<) : $(>) ;
       Clean clean : $(<) ;
    }

Most Jambase rules invoke the Clean rule on their built targets, so "jam clean" will remove all compiled objects, libraries, executables, etc.

MakeLocate Rule

MakeLocate is a single convenient rule that creates a directory, sets LOCATE on a target to that directory, and makes the directory a dependency of the target. It is used by many Jambase rules, and can be invoked directly, e.g.:
        GenFile data.tbl : hxtract data.h ;
        MakeLocate data.tbl : $(TABLEDIR) ;
      
In this example, the File rule creates data.tbl from data.h. The MakeLocate causes data.tbl to be written into the $(TABLEDIR) directory; and if the directory doesn't exist, it is created first.

The MakeLocate rule can be used to build targets introduced by Main, Library or SharedLibrary at a specific location.

        Main myprog : main.c ;
        MakeLocate myprog : $(OUTPUT) ;
      
In the example above the program 'myprog' is built at the location specified by the variable OUTPUT.

The MakeLocate rule invokes another Jambase rule, MkDir, to (recursively) create directories. MkDir uses the $(MKDIR) variable to determine the platform-specific command that creates directories.

RmTemps Rule

Some intermediate files are meant to be temporary. The RmTemps rule can be used to cause jam to delete them after they are used.

RmTemps must be:

  • the last rule invoked on the permanent file that uses the temporary file(s)
  • invoked with the permanent file as the output target and the temporary file(s) as the input target
  • invoked with the exact target identifiers of the permanent file and the temporary file(s)
For example:
        SubDir TOP src big ;
        GenFile big.y : joinfiles part1.y part2.y part3.y ;
        Main bigworld : main.c big.y ;
        RmTemps bigworld$(SUFEXE) : <src!big>big.y ;
    
This causes big.y to be deleted after it has been used to create the bigworld executable. The exact target identifier of big.y is <src!big>big.y (the GenFile and Main rules tack on the grist automatically); the exact target identifier of the bigworld executable is bigworld$(SUFEXE).