<HTML><TITLE>Jamfiles and Jambase</TITLE><BODY><CENTER><A HREF=http://www.perforce.com/jam/jam.html>Jam</a><A NAME="TOP"><H2>Using Jamfiles and Jambase</H2></A></CENTER><P>This document describes how to write Jamfiles using the Jam Jambaserules to build software products.Related documents of interest are:<UL><LI><a href="Jam.html">The Jam Executable Program</A>,which describes using the <b>jam</b> command and thelangauge used in Jambase<LI><A href="Jambase.html">Jambase Reference</A>,which summarizes the Jambase rules and variables</UL><P>Jam documentation and source are available from the<A HREF=http://public.perforce.com/public/index.html>Perforce Public Depot</a>.<HR><P><H2>Overview</H2><P><B>jam,</B> the Jam executable program,recursively builds target files from source filesusing dependency and build specifications definedin Jam rules files.<B>jam</B> parses the rules files to identify targetsand sources,examines the filesystem to determine whichtargets need updating, and issues OS commands to updatetargets.<P>A base rules file called "Jambase" is provided with theJam distribution.The Jambase file defines rules and variables which supportstandard software build operations, like compiling, linking,etc.<P>When the Jambase rules are used,<B>jam</B> reads Jambase, then reads a file called"Jamfile" in the current directory.The Jamfile describes what to do with the source files inits directory. It may also causeJamfiles in other directories to be read.<P>Under certain circumstances, the first Jamfile readalso causes a site-specific "Jamrules" file to be read.The Jamrules file is an optional set of rule and variabledefinitions used to define site-specific processing.<P><H4>The Basic Jamfile</H4><P>Jamfiles contain rule invocations, which usually look like:<PRE><I>RuleName</I> <I>targets</I> : <I>targets</I> ;</PRE>The target(s) to the left of the colon usually indicatewhat gets built, and the target(s) to the right of thecolon usually indicate what it is built from.<P><P>A Jamfile can be as simple as this:<PRE>Main myprog : main.c util.c ;</PRE>This specifies that there is a main.c and util.c file in the samedirectory as the Jamfile, and that those source files should becompiled and linked into an executable called myprog.If you cd to the directory where this Jamfile lives,you can see the exactly how <b>jam</b> wouldbuild myprog with:<PRE>jam -n</PRE>Or, you can actually build myprog with the command:<PRE>jam</PRE><P><H4>Whitespace</H4>Jamfile elements are delimited by whitespace (blanks, tabs, ornewlines). Elements to be delimited include rule names, targets,colons, and semicolons. A common mistake users make is to forget thewhitespace, e.g.,<PRE>Main myprog: main.c util.c ; #<I>WRONG!</I></PRE>Jam doesn't distinguish between a typo and a target called "myprog:",so if you get strange results, the first thingyou should check for in your Jamfile is missing whitespace.<P><H4>Filenames, Target Identifiers, and Buildable Targets</H4><P>Consider this Jamfile:<PRE>Main myprog : main.c util.c ;LinkLibraries myprog : libtree ;Library libtree : treemake.c treetrav.c ;</PRE><P>The Main rule specifies that an executable called myprog will be built.The compiled main.c and util.c objects will be linked to producemyprog.The LinkLibraries rule specifies that libtree willbe linked into myprog as well.The Library rule specifies which source files will be compiled andarchived into the libtree library.<P>The Jamfile above refers to targets like "myprog" and "libtree".However, depending on the platform you're building on, the actualfilenames of those targets could be "myprog.exe" and "libtree.lib".Most Jambase rules supply the actual filenames of targets,so that Jamfiles themselves need not make anyplatform-specific filename references.<P>The <b>jam</b> program builds up a list of unique target identifiers.Unless you are using the SubDir rules (described later),the default identifier for a file target is its filename. In the aboveexample, the target identifiers are the filenames: myprog.exe,libtree.lib, main.obj, etc.<P>While all Jambase rules refer to "targets",not all targets are buildable.There are two kinds of buildable targets:file targets and pseudotargets.File targets are objects that can be found in the filesystem.Pseudotargets are symbolic, and represent other targets.<P>You can use any buildable target on the <b>jam</b> command line tobuild a subset of defined targets. For example:<PRE>jam libtree.a</PRE>on Unix builds the libtree library and all the compiled objectsthat go in it.<P><H4>Pseudotargets</H4><P>Most Jambase rules that define file targets also define pseudotargetswhich are dependent on types of file targets.For example, Jambase defines a pseudotarget called "lib", whichis dependent on file targets created by the Library rule. Sothe command:<PRE>jam lib</PRE>used with the above example would cause the libtree library to be built.Also, there is one pseudotarget built into <b>jam</b> itself, called"all". Jambase sets "all" dependent on (almost) all other targets.<P>In the unfortunate case where you have a buildable target whose nameis the same as one of the Jambase pseudotargets, you'll have problemswith the conflicting target name.Your workaround choices are:<P><ol><lI>Change the name of your buildable file or directory that conflicts.<p><li>Modify your Jambase and change the name of the conflicting pseudotarget.(Pseudotargets are defined in Jambase using the NOTFILE rule.)<p><li>Use grist on the conflicting target name in your Jamfile. E.g., insteadof<PRE>File lib : libfoo.a ;</PRE>try<PRE>File <dir>lib : libfoo.a ;</PRE></ol><P><H4>Dependencies</H4><P>Jambase rules set dependencies on targets, so that if you update asource file, all the file targets that depend on that sourcefile, and only the ones that depend on that source file,will be updated (rebuilt) the next time you run <b>jam</b>.<P>Here are some of the dependenciesthat get set when <b>jam</b> runs on NT using the example Jamfile above:<CENTER><TABLE><TR><TD><B>Target</B><TD> <TD><B>Depends on</B></TD><TR><TD>myprog.exe<TD><TD>main.obj, util.obj, libtree.lib<TR><TD>libtree.lib<TD><TD>treemake.obj, treetrav.obj<TR><TD>treetrav.obj<TD><TD>treetrav.c</TABLE></CENTER><P>Furthermore, the Main and Library rules set up recursiveheader scanning on their source targets.So after <b>jam</b> has finished parsing the Jamfile andsetting the rule-driven dependencies, it scans the sourcefiles for "#include" lines. All #include files found duringthis scan become dependencies of the compiled object.E.g., all header files used to compile treetrav.c wouldbe made dependencies of treetrav.obj.<P>As a result, when you run <b>jam</b>, it will rebuild targetsif either the source files change or theheader files change. You can't tell by looking at a Jamfilewhich header files are dependencies, but you can easilydisplay those dependencies with:<PRE>jam -nd+3</PRE><H4>Rule Ordering</H4><P>Rules which specify dependencies, like the Main, Library, andLinkLibrary rules, can be invoked in any order. <b>jam</b>figures out the order in which targets are built fromtheir dependencies.<P>Some rules, however, set variables which are used by subsequentrule invocations, and their ordering is important.For example, the SubDir* rules (discussedlater) must be invoked in a particular order.<P><H4>Detailed Jambase Specifications</H4><P>This document describes how to use various Jambase rulesfrom a functional point of view.You can see the summary of available Jambase rules in the<a href="Jambase.html">Jambase Reference</A>.The detailed specifications for any Jambase rulecan be found by reading the rule definition itselfin the Jambase file.<P><HR><H2>Handling Directory Trees</H2>The SubDir* rules are used todefine source code directory hierarchies.With SubDir and SubInclude, you can use <b>jam</b>to build software from source files and Jamfiles spreadacross many directories, as is typical for large projects.The SubDir* rules unify an entiresource code tree so that <b>jam</b> can read inall the Jamfiles in one pass andcompute dependencies across the entire project.<P>To use the SubDir* rules, you must:<P><OL><LI> Preface the Jamfile in each directory with an invocationof the SubDir rule.<P><LI> Place at the root of the tree a file named Jamrules.This file could be empty, but inpractice it contains user-provided rules and variabledefinitions that are shared throughout thetree. Examples of such definitions are librarynames, header directories, install directories,compiler flags, etc. This file is good candidatefor automatic customizing with autoconf(GNU).<P><LI> Optionally, set an environment variable pointingto the root directory of the srouce tree. Thevariable's name is left up to you, but in theseexamples, we use TOP.</OL><P><H4>SubDir Rule</H4><P>The SubDir rule must be invoked before any rules thatrefer to the contents of the directory - it is best to putit at the top of each Jamfile. For example:<PRE># 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 ;</PRE>This compiles four files in $(TOP)/src/util, archivestwo of the objects into libtree, and links the wholething into myprog.Outputs are placed in the $(TOP)/src/utildirectory.<P>This doesn't appear to be any different fromthe previous example that didn't have a SubDir rule,but two things are happening behind the scenes:<OL><LI>The SubDir rule causes <b>jam</b> to readin the $(TOP)/Jamrules file.(The Jamrules file can alternately be named by thevariable $(xxxRULES), where xxx is the name of theroot variable, e.g., $(TOPRULES)).<P>The Jamrules file can contain variable definitionsand rule definitions specific to your codeline.It allows you to completely customize your buildenvironment without having to rewrite Jambase.Jamrules is only readin once, at the first SubDir invocation.<P><LI>The SubDir rule initializes a set of variablesthat are used by Main and other rules touniquely identify the source files in thisdirectory and assign locations to the targetsbuilt from files in this directory.<P>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 pathnamesto the root directory, computed from the numberof arguments to the first SubDir rule, e.g.,../../src/util. In either case, the SubDirrule constructs the path names that locate sourcefiles.You'll see how this is useful later.<P></UL><P>The SubDir rule takes as its first argument the rootvariable's name and takes as subsequent arguments thedirectory names leading from the root to the directory ofthe current Jamfile. Note that the name of the subdirectoryis given as individual elements: the SubDir ruledoes not use system-specific directory name syntax.<P><P><H4>SubInclude Rule</H4>The SubInclude rule is used in a Jamfile to cause anotherJamfile to be read in.Its arguments are in the same format asSubDir's.<P>The recommended practice is only to include one level ofsubdirectories at a time, and let the Jamfile in each subdirectoryinclude its own subdirectories. This allows auser to sit in any arbitrary directory of the source treeand build that subtree. For example:<PRE># This is $(TOP)/Jamfile, top level Jamfile for mondo project.SubInclude TOP src ;SubInclude TOP man ;SubInclude TOP misc ;SubInclude TOP util ;</PRE>If a directory has both subdirectories of its own as wellas files that need building, the SubIncludes should beeither before the SubDir rule or be at the end of the Jamfile- not between the SubDir and other rule invocations.For example:<PRE># This is $(TOP)/src/Jamfile:SubDir TOP src ;Main mondo : mondo.c ;LinkLibraries mondo : libmisc libutil ;SubInclude TOP src misc ;SubInclude TOP src util ;</PRE><P>(<b>jam</b> processes all the Jamfiles it reads as ifit were reading one single, large Jamfile.Build rules like Main and LinkLibraries rely on thepreceding SubDir rule to set up source file andoutput file locations, and SubIncludes rules read inJamfiles that contain SubDir rules. So if you puta SubIncludes rule between a SubDir and a Mainrule, <b>jam</b> will try to find the source filesfor the Main rule in the wrong directory.)<P><H4>Variables Used to Handle Directory Trees</H4>The following variables are set by the SubDir ruleand used by the Jambase rules that define file targets:<P><CENTER><TABLE><TR><TD VALIGN=TOP>SEARCH_SOURCE<TD><TD>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.<TR><TD VALIGN=TOP>LOCATE_SOURCE<TD><TD>Initialized by the SubDir rule to the samevalue as $(SEARCH_SOURCE), unless ALL_LOCATE_TARGETis set.$(LOCATE_SOURCE) is used by rules that buildgenerated source files (e.g., Yacc and Lex) toset location of output files.Thus the default location of built source filesis the directory of the Jamfile that defines them.<TR><TD VALIGN=TOP>LOCATE_TARGET<TD><TD>Initalized by the SubDir rule to the samevalue as $(SEARCH_SOURCE), unless ALL_LOCATE_TARGETis set.$(LOCATE_TARGET) is used by rules that buildbinary objects (e.g., Main and Library) toset location of output files.Thus the default location of built binaray filesis the directory of the Jamfile that defines them.<TR><TD VALIGN=TOP>ALL_LOCATE_TARGET<TD><TD>If $(ALL_LOCATE_TARGET) is set, LOCATE_SOURCEand and LOCATE_TARGET are set to $(ALL_LOCATE_TARGET)instead of to $(SEARCH_SOURCE). This can be used todirect built files to be written to a location outsideof the source tree, and enables building from read-onlysource trees.<TR><TD VALIGN=TOP>SOURCE_GRIST<TD><TD>The SubDir targets are formed into a stringlike "src!util" and that string is assigned toSOURCE_GRIST. Rules that define file targetsuse $(SOURCE_GRIST) to set the "grist" attributeon targets. This is used to assure uniquenessof target identifiers where filenames themselvesare not unique.For example, the target identifiers of$(TOP)/src/client/main.c and $(TOP)/src/server/main.cwould be <src!client>main.c and <src!server>main.c.</TABLE></CENTER><P>The $(LOCATE_TARGET) and $(SEARCH_SOURCE) variables are usedextensively by rules in Jambase: most rules that generatetargets (like Main, Object, etc.) set $(LOCATE) to$(LOCATE_TARGET) for the targets they generate, and rulesthat use sources (most all of them) set $(SEARCH) to be$(SEARCH_SOURCE) for the sources they use.<P>$(LOCATE) and $(SEARCH) are better explained in<A HREF="Jam.html">The Jam Executable Program</A>but in brief they tell <B>jam</B> where to create new targets andwhere to find existing ones, respectively.<P>Note that you can reset these variablesafter SubDir sets them. For example, this Jamfile buildsa program called gensrc, then runs it to create a source filecalled new.c:<PRE>SubDir TOP src util ;Main gensrc : gensrc.c ;LOCATE_SOURCE = $(NEWSRC) ;GenFile new.c : gensrc ;</PRE>By default, new.c would be written into the$(TOP)/src/util directory, but resetting LOCATE_SOURCE causesit to be written to the $(NEWSRC) directory. ($(NEWSRC) is assumedto have been set elsewhere, e.g., in Jamrules.)<P><H4>VMS Notes</H4>On VMS, the logical name table is not imported as is theenvironment on UNIX. To use the SubDir and related rules,you must set the value of the variable that names the rootdirectory. For example:<PRE>TOP = USR_DISK:[JONES.SRC] ;SubInclude TOP util ;</PRE>The variable must have a value that looks like a directoryor device. If you choose, you can use a concealed logical.For example:<PRE>TOP = TOP: ;SubInclude TOP util ;</PRE>The : at the end of TOP makes the value of $(TOP) looklike a device name, which jam respects as a directory nameand will use when trying to access files. TOP must thenbe defined from DCL:<PRE>$ define/job/translation=concealed TOP DK100:[USERS.JONES.SRC.]</PRE>Note three things: the concealed translation allows thelogical to be used as a device name; the device name inthe logical (here DK100) cannot itself be concealed logical(VMS rules, man); and the directory component of thedefinition must end in a period (more VMS rules).<P><H2>Building Executables and Libraries</H2><P>The rules that build executables and libraries are: Main, Library,and LinkLibraries.<H4>Main Rule</H4>The Main rule compiles source files and links the resultingobjects into an executable. For example:<PRE>Main myprog : main.c util.c ;</PRE>This compiles main.c and util.c and links main.o andutil.o into myprog. The object files and resultingexecutable are named appropriately for the platform.<P>Main can also be used to build shared libraries and/ordynamic link libraries, since those are also linkedobjects. E.g.:<PRE>Main driver$(SUFSHR) : driver.c ;</PRE>Normally, Main uses $(SUFEXE) to determine the suffix onthe filename of the built target. To override it,you can supply a suffix explicity.In this case,$(SUFSHR) is assumed to be the OS-specific shared librarysuffix, defined in Jamrules with somethinglike:<PRE>if $(UNIX) { SUFSHR = .so ; }else if $(NT) { SUFSHR = .dll ; }</PRE><P>Main uses the Objects rule to compile source targets.<H4>Library Rule</H4>The Library rule compiles source files, archives theresulting object files into a library, and then deletesthe object files. For example:<PRE>Library libstring : strcmp.c strcpy.c strlen.c ;Library libtree : treemake.c treetrav.c ;</PRE>This compiles five source files, archives three of theobject 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, theobjects are deleted.<P>Library uses the Objects rule to compile source files.<P><H4>LinkLibraries Rule</H4>To link executables with built libraries, usethe LinkLibraries rule. For example:<PRE>Main myprog : main.c util.c ;LinkLibraries myprog : libstring libtree ;</PRE>The LinkLibraries rule does two things: it makes thelibraries dependencies of the executable, so that they getbuilt first; and it makes the libraries show up on thecommand line that links the executable. The ordering ofthe lines above is not important, because <b>jam</b> builds targetsin the order that they are needed.<P>You can put multiple libraries on a single invocation ofthe LinkLibraries rule, or you can provide them in multipleinvocations. In both cases, the libraries appear onthe link command line in the order in which they wereencountered. You can also provide multiple executables tothe LinkLibraries rule, if they need the same libraries,e.g.:<PRE>LinkLibraries prog1 prog2 prog3 : libstring libtree ;</PRE><P><H4>Variables Used in Building Executables and Libraries</H4><CENTER><TABLE><TR><TD>AR<TD><TD>Archive command, used for Library targets.<TR><TD>SUFEXE<TD>*<TD>Suffix on filenames of executables referencedby Main and LinkLibraries.<TR><TD>LINK<TD><TD>Link command, used for Main targets.<TR><TD>LINKFLAGS<TD><TD>Linker flags.<TR><TD>LINKLIBS<TD><TD>Link libraries that aren't dependencies. (See notebelow.)<TR><TD>EXEMODE<TD>*<TD>File permissions on Main targets.<TR><TD>MODE<TD><TD>Target-specific file permissions on Main targets(set from $(EXEMODE))<TR><TD>RANLIB<TD><TD>Name of ranlib program, if any.</TABLE></CENTER><P>Variables above marked with "*" are used by the Main,Library, and LinkLibraries rules. Their values at thetime the rules are invoked are used to set target-specificvariables.<P>All other variables listed above are globally defined,and are used in actions that update Main and Librarytargets. This means that the global values of thosevariables are used, uness target-specific values havebeen set.(For instance, a target-specific MODE value is set bythe Main rule.)The target-specific values always overrideglobal values.<P>Note that there are two ways to specify link libraries forexecutables:<UL><LI>Use the LinkLibraries ruleto specify built libraries; i.e., librariesthat are built by Library rules. This assures thatthese libraries are built first, and that Main targets arerebuilt when the libraries are updated.<P><LI>Use the LINKLIBS variable to specify externallibraries; e.g., system libraries or third-party libraries.The LINKLIBS variable must be set to the the actuallink command flag that specifies the libraries.<P></UL><P>For example:<PRE><I>#In Jamrules:</I>if $(UNIX) { X11LINKLIBS = -lXext -lX11 ; }if $(NT) { X11LINKLIBS = libext.lib libX11.lib ; }<I>#In Jamfile:</I>Main xprog : xprog.c ;LINKLIBS on xprog$(SUFEXE) = $(X11LINKLIBS) ;LinkLibraries xprog : libxutil ;Library libxutil : xtop.c xbottom.c xutil.c ;</PRE>This example uses the Jam syntax "variable on target" toset a target-specific variable. In this way, only xprogwill be linked with this special $(X11LINKLIBS),even if other executables were going to be builtby the same Jamfile. Note that when you set a variableon a target, you have to specify the target identiferexactly, which in this case is the suffixed filename ofthe executable.The actual link command line on Unix, for example, wouldlook something like this:<PRE>cc -o xprog xprog.o libxutil.a -lXext -lX11</PRE><H2>Compiling</H2>Compiling of source files occurs normally as a byproductof the Main or Library rules, which call the rulesdescribed here. These rules may also be called explicitlyif the Main and Library behavior doesn't satisfy yourrequirements.<P><H4>Objects Rule</H4>The Main and Library rules call the Objects rule on source files.Compiled object files built bythe Objects rule are a dependency of the <I>obj</i>pseudotarget, so "jam obj" will build object files used inMain and Library rules.<P>Target identifiers created by the Objects rule have gristset to $(SOURCE_GRIST). So given this Jamfile:<PRE>SubDir TOP src lock ;Main locker : lock.c ;</PRE>the object file created is lock.o (or lock.obj) andits target identifier is <src!lock>lock.o(or <src!lock>lock.obj).<P>You can also call Objects directly. For example:<PRE>Objects a.c b.c c.c ;</PRE>This compiles a.c into a.o, b.c into b.o, etc. The objectfile suffix is supplied by the Objects rule.<P><H4>Object Rule</H4>Objects gets its work done by calling the Object rule oneach of the source files.You could use the Object rule directly.For example, on Unix, you could use:<PRE>Object foo.o : foo.c ;</PRE>However, the Object rule does not provide suffixes, andit does not provide the grist needed to construct targetidentifiers if you are using the SubDir* rules.A portable and robust Jamfile would need to invoke Object thus:<PRE>Object <src!util>foo$(SUFOBJ) : <src!util>foo.c ;</PRE>which is inelegant and clearly shows why using Objectsis better than using Object.<P>If there's any advantage to the Object rule, it'sthat it doesn't require that the object name bearany relationship to the source. It is thus possible tocompile the same file into different objects. For example:<PRE>Object a.o : foo.c ;Object b.o : foo.c ;Object c.o : foo.c ;</PRE>This compiles foo.c (three times) into a.o, b.o, and c.o.Later examples show how this is useful.<P>The Object rule looks at the suffix of the source file andcalls the appropriate rules to do the actual preprocessing(if any) and compiling needed to produce the output object file.The Object rule iscapable of the generating of an object file from anytype of source. For example:<PRE>Object grammar$(SUFOBJ) : grammar.y ;Object scanner$(SUFOBJ) : scanner.l ;Object fastf$(SUFOBJ) : fastf.f ;Object util$(SUFOBJ) : util.c ;</PRE>An even more elegant way to get the same result is to let theObjects rule call Object:<PRE>Objects grammar.y scanner.l fastf.f util.c ;</PRE><P>In addition to calling the compile rules, Object sets upa bunch of variables specific to the source and targetfiles. (See Variables Used in Compiling, below.)<P><H4>Cc, C++, Yacc, Lex, Fortran, As, etc. Rules</H4><P>The Object rule calls compile rules specific to the suffix ofthe source file. (You can see which suffixes are supportedby looking at the Object rule definition in Jambase.)Because the extra work done by theObject rule, it is not always useful to call the compilerules directly. But the adventurous user might attemptit. For example:<PRE>Yacc grammar.c : grammar.y ;Lex scan.c : scan.l ;Cc prog.o : prog.c ;</PRE>These examples individually run yacc(1), lex(1), and the Ccompiler on their sources.<P><H4>UserObject Rule</H4>Any files with suffixes not understood by the Object ruleare passed to the UserObject rule. The default definitionof UserObject simply emits a warning that the suffix isnot understood. This Jambase rule definition is intended to beoverridden in Jamrules with one that recognizes the project-specificsource file suffixes. For example:<PRE>#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 ; }</PRE><P>In this example, the UserObject definition in Jamrulesallows *.rc files to be handle as regular Main and Librarysources. The lock.rc file is compiled into lock.objby the "rc" command, and lock.obj is archived into a librarywith other compiled objects.<H4>LibraryFromObjects Rule</H4>Sometimes the Library rule's straightforward compiling ofsource into object modules to be archived isn't flexibleenough. 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 Objectrule for that. For example:<PRE>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 ;</PRE>This Unix-specific example compiles the same source file intotwo differentobjects, with different compile flags, and archives them.(The ObjectCcFlags rule is described shortly.)Unfortunately, the portable and robust implementation of theabove example is not as pleasant to read:<PRE>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 ;</PRE>Note that, among other things, you must supply the libraryfile suffix when using the LibraryFromObjects rule.<P><H4>MainFromObjects Rule</H4>Similar to LibraryFromObjects, MainFromObjects does thelinking part of the Main rule, but not the compiling.MainFromObjects can be used when there are noobjects at all, and everything is to be loaded fromlibraries. For example:<PRE>MainFromObjects testprog ;LinkLibraries testprog : libprog ;Library libprog : main.c util.c ;</PRE>On Unix, say, this generates a link command that looks like:<PRE>cc -o testprog libprog.a</PRE>Linking purely from libraries is something that doesn'twork everywhere: it depends on the symbol "main" beingundefined when the linker encounters the library that containsthe definition of "main".<P><H4>Variables Used in Compiling</H4>The following variables control the compiling of sourcefiles:<P><CENTER><TABLE><TR><TD VALIGN=TOP>C++<TD><TD>The C++ compiler command<TR><TD VALIGN=TOP>CC<TD><TD>The C compiler command<TR><TD VALIGN=TOP>C++FLAGS<BR>CCFLAGS<TD VALIGN=TOP><TD VALIGN=TOP>Compile flags, used tocreate or update compiled objects<TR><TD>SUBDIRC++FLAGS<BR>SUBDIRCCFLAGS<TD VALIGN=TOP><TD VALIGN=TOP>Additonal compile flagsfor source files in this directory.<TR><TD VALIGN=TOP>OPTIM<TD><TD>Compiler optimization flag. The Cc and C++actions use this as well as C++FLAGS or CCFLAGS.<TR><TD VALIGN=TOP>HDRS<TD VALIGN=TOP><TD>Non-standard header directories; i.e.,the directories the compiler will not look inby default and which therefore must be suppliedto the compile command. These directories arealso used by <b>jam</b> to scan for include files.<TR><TD VALIGN=TOP>STDHDRS<TD VALIGN=TOP><TD>Standard header directories, i.e., thedirectories the compiler searches automatically.These are not passed to the compiler, but theyare used by <b>jam</b> to scan for include files.<TR><TD>SUBDIRHDRS<TD><TD>Additional paths to add to HDRS for source filesin this directory.<TR><TD>LEX<TD><TD>The lex(1) command<TR><TD>YACC<TD><TD>The yacc(1) command</TABLE></CENTER><P>The Cc rule sets a target-specific $(CCFLAGS) to the currentvalue of $(CCFLAGS) and $(SUBDIRCCFLAGS). Similarlyfor the C++ rule. The Object rule sets a target-specific$(HDRS) to the current value of $(HDRS) and $(SUBDDIRHDRS).<P>$(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) sothey can be set independently.<P>$(HDRS) lists the directories to search for header files,and it is used in two ways: first, it is passed to the Ccompiler (with the flag -I prepended); second, it is usedby HdrRule to locate the header files whose names werefound when scanning source files. $(STDHDRS) lists theheader directories that the C compiler already knowsabout. It does not need passing to the C compiler, but isused by HdrRule.<P>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:<PRE>Library libximage : xtiff.c xjpeg.c xgif.c ;HDRS on xjpeg$(SUFOBJ) = /usr/local/src/jpeg ;CCFLAGS on xtiff$(SUFOBJ) = -DHAVE_TIFF ;</PRE>This can be done more easily with the rules that follow.<P><H4>ObjectCcFlags, ObjectC++Flags, ObjectHdrs Rules</H4>$(CCFLAGS), $(C++FLAGS) and $(HDRS) can be set on object filetargetsdirectly, but there are rules that allow these variablesto be set by referring to the original source file name,rather than to the derived object file name. ObjectCcFlagsadds object-specific flags to the $(CCFLAGS) variable,ObjectC++Flags adds object-specific flags to the$(C++FLAGS) variable, and ObjectHdrs add object-specificdirectories to the $(HDRS) variable. For example:<PRE>#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) ;</PRE>The ObjectCcFlags and ObjectHdrs rules take .c filesas targets, but actually set $(CCFLAGS) and $(HDRS) valueson the .obj (or .o) files. As a result, the actionthat updates the target .obj file uses the target-specificvalues of $(CCFLAGS) and $(HDRS).<P><H4>SubDirCcFlags, SubDirC++Flags, SubDirHdrs Rules</H4>These rules set the values of $(SUBDIRCCFLAGS), $(SUBDIRC++FLAGS)and $(SUBDIRHDRS), which are used by the Cc,C++, and Object rules when setting the target-specificvalues for $(CCFLAGS), $(C++FLAGS) and $(HDRS). The SubDirrule clears these variables out, and thus they providedirectory-specific values of $(CCFLAGS), $(C++FLAGS) and$(HDRS). For example:<PRE>#In Jamrules:GZHDRS = $(TOP)/src/gz/include ;GZFLAG = -DGZ ;#In Jamfile:SubDir TOP src gz utils ;SubDirHdrs $(GZHDRS) ;SubDirCcFlags $(GZFLAG) ;Library libgz : gizmo.c ;Main gizmo : main.c ;LinkLibraries gizmo : libgz ;</PRE>All .c files in this directory files will be compiled with$(GZFLAG) as well as the default $(CCFLAG), and the includepaths used on the compile command will be $(GZHDRS) as wellas the default $(HDRS).<H2>Header File Processing</H2>One of the functions of the Object rule is set upscanning of sourcefiles for (C style) header file inclusions. To do so, itsets the special variables $(HDRSCAN) and $(HDRRULE)as target-specific variables on the source file. Thepresence of these variables triggers a special mechanismin <B>jam</B> for scanning a file for header file inclusions andinvoking a rule with the results of the scan. The$(HDRSCAN) variable is set to an egrep(1) pattern thatmatches "#include" statements in C source files, and the$(HDRRULE) variable is set to the name of the rule thatgets invoked as such:<PRE>$(HDRRULE) source-file : included-files ;</PRE>This rule is supposed to set up the dependencies betweenthe source file and the included files. The Object ruleuses HdrRule to do the job. HdrRule itself expectsanother variable, $(HDRSEARCH), to be set to the list ofdirectories where the included files can be found. Objectdoes this as well, setting $(HDRSEARCH) to $(HDRS) and$(STDHDRS).<P>The header file scanning occurs during the "file binding"phase of <b>jam</b>, which means that the target-specificvariables (for the source file) are in effect. To accomodatenested includes, one of the HdrRule's jobs is to passthe target-specific values of $(HDRRULE), $(HDRSCAN), and$(HDRSEARCH) onto the included files, so that they will bescanned as well.<P><H4>HdrRule Rule</H4>Normally, HdrRule is not invoked directly; the Object rule(called by Main and Library) invokes it.<P>If there are special dependencies that need to be set,and which are not set by HdrRule itself, you can defineanother rule and let it invoke HdrRule. For example:<PRE>#In Jamrules:rule BuiltHeaders{DEPENDS $(>) : mkhdr$(SUFEXE) ;HdrRule $(<) : $(>) ;}#In Jamfile:Main mkhdr : mkhdr.c ;Main ugly : ugly.c ;HDRRULE on ugly.c = BuiltHeaders ;</PRE>This example just says that the files included by "ugly.c"are generated by the program "mkhdr", which can be builtfrom "mkhdr.c". During the binding phase, <b>jam</b> willscan ugly.c, and if it finds an include file, ughdr.h,for example, it will automatically invoke the rule:<PRE>BuiltHeaders ugly.c : ughdr.h ;</PRE>By calling HdrRule at the end of BuiltHeaders,all the gadgetry of HdrRule takes effect and itdoesn't need to be duplicated.<P><H4>Variables Used for Header Scanning</H4><CENTER><TABLE><TR><TD VALIGN=TOP>HDRPATTERN<TD><TD>Default scan pattern for "include" lines.<TR><TD VALIGN=TOP>HDRSCAN<TD><TD>Scan pattern to use.This is a special variable: during binding, ifboth HDRSCAN and HDRRULE are set, scanning is activatedon the target being bound.The HdrRule and Object rules sets thisto $(HDRPATTERN) on their source targets.<TR><TD VALIGN=TOP>HDRRULE<TD><TD>Name of rule to invoked on files found in headerscan. The HdrRule and Object rules set this to "HdrRule"on their source targets. This is also a special variable;it's the only <b>jam</b> variable that can hold thename of a rule to be invoked.<TR><TD VALIGN=TOP>HDRSEARCH<TD><TD>Search paths for files found during header scanning.This is set from $(HDRS) and $(STDHDRS), which aredescribed in the Compiling section.<b>jam</b> will search $(HDRSEARCH) directories forthe files found by header scans.</TABLE></CENTER><P>The Object rule sets HDRRULE and HDRSCAN specifically forthe source files to be scanned, rather than globally. Ifthey were set globally, jam would attempt to scan allfiles, even library archives and executables, for headerfile inclusions. That would be slow and probably notyield desirable results.<P><H2>Copying Files</H2><H4>File Rule</H4>The File rule copies one file to another. The target nameneedn't be the same as the source name. Forexample:<PRE>switch $(OS){case NT* : File config.h : confignt.h ;case * : File config.h : configunix.h ;}LOCATE on config.h = $(LOCATE_SOURCE) ;</PRE>This creates a config.h file from either confignt.h orconfigunix.h, depending on the current build platform.<P>The File rule does notuse the LOCATE_SOURCE variable set by theSubDir rule (although it does use SEARCH_SOURCE), whichmeans you have to set the copied file's output directoryyourself. That's done by setting the specialLOCATE variable on the target, as shown above,or with the MakeLocate rule described below.<H4>Bulk Rule</H4>The Bulk rule is a shorthand for many invocations of theFile rule when all files are going to the same directory.For example:<PRE>#In Jamrules:DISTRIB_GROB = d:\\distrib\\grob ;#In Jamfile:Bulk $(DISTRIB_GROB) : grobvals.txt grobvars.txt ;</PRE>This causes gobvals.txt and grobvars.txt to be copiedinto the $(DISTRIB_GROB) directory.<H4>HardLink Rule</H4>The Unix-only HardLink rule makes a hard link (using ln(1)) from thesource to the target, if there isn't one already. Forexample:<PRE>HardLink config.h : configunix.h ;</PRE><H4>Shell Rule</H4>The Shell rule is like the File rule, except that on Unix it makessure the first line of the target is "#!/bin/sh" and setsthe permission to make the file executable. For example:<PRE>Shell /usr/local/bin/add : add.sh ;</PRE><P>You can also use $(SHELLHEADER) to dictatewhat the first line of the copied file will be.Forexample:<PRE>Shell /usr/local/bin/add : add.awk ;SHELLHEADER on /usr/local/bin/add = "#!/bin/awk -f" ;</PRE>This installs an awk(1) script.<P><H4>Variables Used When Copying Files</H4><CENTER><TABLE><TR><TD VALIGN=TOP>FILEMODE<TD><TD>Default file permissions for copied files<TR><TD VALIGN=TOP>SHELLMODE<TD><TD>Default file permissions for Shell rule targets<TR><TD VALIGN=TOP>MODE<TD><TD>File permissions set on files copied byFile, Bulk, and Shell rules.File and Shell sets a target-specific MODE to the currentvalue of $(FILEMODE) or $(SHELLMODE), respectively.<TR><TD VALIGN=TOP>SHELLHEADER<TD><TD>String to write in first line of Shell targets(default is #!/bin/sh).</TABLE></CENTER><P><H2>Installing Files</H2>Jambase provides a set of Install* rules to copy filesinto an destination directory and set permissions on them.On Unix, the install(1) program is used.If the destination directory does not exist, <b>jam</b>creates it first.<P>All files copied with the Install* rules are dependenciesof the <i>install</i> pseudotarget, which means that thecommand "jam install" will cause the installed copies tobe updated. Also, "jam uninstall" will cause the installedcopies to be removed.<P>The Install* rules are:<CENTER><TABLE><TR><TD VALIGN=TOP><B>InstallBin</B><TD VALIGN=TOP>Copies file and sets its permission to $(EXEMODE).You must specify the suffixed executable name. E.g.:<PRE>InstallBin $(BINDIR) : thing$(SUFEXE) ;</PRE><TR><TD VALIGN=TOP><B>InstallFile</B><TD VALIGN=TOP>Copies file and sets its permission to $(FILEMODE). E.g.:<PRE>InstallFile $(DESTDIR) : readme.txt ;</PRE><TR><TD VALIGN=TOP><B>InstallLib</B><TD VALIGN=TOP>Copies file and sets its permission to $(FILEMODE).You must specify the suffixed library name. E.g.:<PRE>InstallLib $(LIBDIR) : libzoo$(SUFLIB) ;</PRE><TR><TD VALIGN=TOP><B>InstallMan</B><TD VALIGN=TOP>Copies file into the man<i>n</i>subdirectory of the target directoryand sets its permission to $(FILEMODE). E.g.,this copies foo.5 into the $(DESTDIR)/man5 directory:<PRE>InstallMan $(DESTDIR) : foo.5 ;</PRE><TR><TD VALIGN=TOP><B>InstallShell</B><TD VALIGN=TOP>Copies file and sets its permission to $(SHELLMODE). E.g.:<PRE>InstallShell $(DESTDIR) : startup ;</PRE></TABLE></CENTER><P><P><H4>Variables</H4>The following variables control the installation rules:<P><CENTER><TABLE><TR><TD>INSTALL<TD><TD>The install program (Unix only)<TR><TD>FILEMODE<TD><TD>Default file permissions on readable files.<TR><TD>EXEMODE<TD><TD>Default file permission executable files.<TR><TD>SHELLMODE<TD><TD>Default file permission on shell script files.<TR><TD>MODE<TD><TD>Target-specific file permissions</TABLE></CENTER><P><P>The Install rules set a target-specific MODE to the currentvalue of $(FILEMODE), $(EXEMODE), or $(SHELLMODE),depending on which Install rule was invoked.<P>The directory variables are just defined for convenience:they must be passed as the target to the appropriateInstall rule. The $(INSTALL) and mode variables must beset (globally) before calling the Install rules in orderto take effect.<P><H2>Miscellaneous Rules</H2><H4>Clean Rule</H4><P>The Clean rule defines files to be removed when you run "jam clean".Any site-specific build rules defined in your Jamrules should invokeClean so that outputs can be removed. E.g.,<PRE>rule ResourceCompiler{DEPENDS $(<) : $(>) ;Clean clean : $(<) ;}</PRE><P><P>Most Jambase rules invoke the Clean rule on their built targets,so "jam clean" will remove all compiled objects, libraries,executables, etc.<P><H4>MakeLocate Rule</H4>MakeLocate is a single convenient rule that creates a directory,sets LOCATE on a target to that directory, and makes the directorya dependency of the target. It is used by many Jambase rules,and can be invoked directly, e.g.:<PRE>GenFile data.tbl : hxtract data.h ;MakeLocate data.tbl : $(TABLEDIR) ;</PRE>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.<P>The MakeLocate rule invokes another Jambase rule, MkDir,to (recursively) createdirectories. MkDir uses the $(MKDIR) variable to determine theplatform-specific command that creates directories.<P><H4>RmTemps Rule</H4>Some intermediate files are meant to be temporary.The RmTemps rule can be used to cause<b>jam</b> to delete them after they are used.<P>RmTemps must be:<UL><LI>the last ruleinvoked on the permanent file that usesthe temporary file(s)<LI>invoked with the permanent file as the outputtarget and the temporary file(s) as the input target<LI>invoked with the exact target identifiers ofthe permanent file and the temporary file(s)</UL>Forexample:<PRE>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 ;</PRE>This causes big.y to be deleted after it has been used to createthe 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 executableis bigworld$(SUFEXE).<P><HR><A HREF="#TOP">Back to top.</A><P>Copyright 1997, 2000 Perforce Software, Inc.<BR>Comments to <A HREF="mailto:info@perforce.com">info@perforce.com</A><BR>Last updated: Dec 31, 2000<BR>$Id: //public/jam/src/Jamfile.html#6 $</BODY></HTML>