Jam Tutorial

Adding Jam Support

This tutorial shows how to create a project with a self-contained Jam build system. A build host needs only a native toolchain. Start with an empty directory, from now on referred to as the project root, and place sources, headers, and the files required for building with Jam there.

Copy the entry scripts and the Jam snapshot into the project root as described in Getting Jam. After that step the project root contains jam.sh, jam.bat, and jam/.

All examples in this tutorial use the script jam.sh when commands are shown. On a Windows build host, run jam.bat instead of jam.sh, with the exact same arguments. Unless noted otherwise, Jam is run from the project root directory.

Building Programs

To build a simple hello world program, add two project files named Jamconfigure and Jamfile. Configure writes Jamrules. After adding those two files the project directory contains:

- jam.sh
- jam.bat
- [jam]
- Jamconfigure
- Jamfile

Before a project can be built, it needs to be configured, so the build host properties are determined, the toolset is detected, and so on. The input for this configure step is the file named Jamconfigure. Add the following lines to that file:

SubDir MY_TOP ;
# configure basic C compiler
ConfigureCc ;
# configure basic C++ compiler
ConfigureC++ ;

The SubDir rule defines the so-called root variable, which stands for the project root directory. You can think of this for now simply as the path to your project root directory. The SubDir rule also sets the directory context for the following statements, until the next SubDir call. It will be more useful when another project is included as a subtree. So far, only basic C and C++ support needs to be configured. This allows programs, static libraries, and shared libraries to be built using standard C and C++. The configure step can now be run with this command:

./jam.sh configure

The script jam.sh must have the executable bit set. If this is not the case, add it by executing chmod +x jam.sh. On Windows, run jam.bat instead of jam.sh.

After the build configuration, targets such as the hello world program can be built. The build targets are defined in the file called Jamfile in the project root, which is picked up by Jam during the build step. Add the following lines to the Jamfile:

SubDir MY_TOP ;
Main hello : main.cpp ;

This Jamfile uses the Main rule to build a program named hello from the source file main.cpp. The SubDir rule defines the root variable and sets the directory context for the following statements, just like it was done in the Jamconfigure file. Thus, main.cpp will be looked for in the project root. If Jam is run now, a program will be built named hello on Unix or hello.exe on Windows:

./jam.sh -q

The command line parameter -q stops the build on the first error. If it is left out, the build step continues and skips the targets that fail to build.

Cleaning the Build

Jam can delete previously built targets from the project directory. Execute Jam with the clean target:

./jam.sh clean