Chapter 25. Caching Built Files

On multi-developer software projects, you can sometimes speed up every developer's builds a lot by allowing them to share the derived files that they build. SCons makes this easy, as well as reliable.

25.1. Specifying the Shared Cache Directory

To enable sharing of derived files, use the CacheDir function in any SConscript file:


       CacheDir('/usr/local/build_cache')
    

Note that the directory you specify must already exist and be readable and writable by all developers who will be sharing derived files. It should also be in some central location that all builds will be able to access. In environments where developers are using separate systems (like individual workstations) for builds, this directory would typically be on a shared or NFS-mounted file system.

Here's what happens: When a build has a CacheDir specified, every time a file is built, it is stored in the shared cache directory along with its MD5 build signature. [1] On subsequent builds, before an action is invoked to build a file, SCons will check the shared cache directory to see if a file with the exact same build signature already exists. If so, the derived file will not be built locally, but will be copied into the local build directory from the shared cache directory, like so:


      % scons -Q
      cc -o hello.o -c hello.c
      cc -o hello hello.o
      % scons -Q -c
      Removed hello.o
      Removed hello
      % scons -Q
      Retrieved `hello.o' from cache
      Retrieved `hello' from cache
    

Note that the CacheDir feature still calculates MD5 build sigantures for the shared cache file names even if you configure SCons to use timestamps to decide if files are up to date. (See the Chapter 6 chapter for information about the Decider function.) Consequently, using CacheDir may reduce or eliminate any potential performance improvements from using timestamps for up-to-date decisions.

Notes

[1]

Actually, the MD5 signature is used as the name of the file in the shared cache directory in which the contents are stored.