Thursday, March 4, 2010

What's in a Build System?

The term "build system" seems rather nebulous. Is it your build machine? Your continuous integration server? Your makefiles or project files? In my adventures, I've decided to give a specific definition to the term. For what it's worth, I'll give it a public showing here.
The build system takes source code (and possibly other resources) as input and produces a deployable artifact (or possibly multiple deployable artifacts) as output.
While the term "deployable artifact" is almost self-explanatory, I don't think it's obvious. To me, a "deployable artifact" is the set of binaries (and other resources) that can actually be installed on a production machine, or shipped to a client, and run successfully. The "deployable artifact" could be an installer program (like an msi), a set of libraries (dlls, jars, etc), or a SQL change script. It could be something totally different -- it is simply the product in its "deployable" state.

Under this definition, a makefile or project file is actually the closest thing to a "build system" that we identified earlier. However, we often want to do more than just compiling as a part of our "build". For example, in a production "build system", we might want to:
  • Update version numbers
  • Run unit tests and possibly functional tests
  • Perform style or coverage analysis
  • Coalesce the output of several projects into a single artifact
  • Publish the artifact(s) to a publicly accessible location
To make things more interesting, you probably want to do a lot of these things roughly the same way across multiple products within your organization. One-off makefiles and post-build steps in project files won't scale up to the challenge. Most organizations end up with some sort of "standard" build system, typically using one of the scripting systems designed for this kind of thing. Ant/NAnt, MSBuild, and Rake are popular options.

Since you're probably going to end up building one of these "standard" build systems, why not do it well? There are a few key properties that I believe a well executed build system should have:

1) The build system should have very few environmental dependencies

Your build system should ideally require that nothing be installed on the system other than the scripting language in which it is written. Even better, the scripting language could come along with the platform, like MSBuild (which installs with the .NET framework). If your build system makes use of third party script libraries (it probably will), you should deploy those along with your build system. If your build system uses a particular unit test runner, deploy it with your build system. Strive to place as few requirements as possible on the environment in which your system runs.

2) The build system should not be dependent on a particular continuous integration server

It's very tempting to conflate the notions of "build system" and "build server". However, that would be a violation of our first property. Every aspect of the build system should be runnable anywhere. That includes version numbering, unit test execution, and deployable package creation. Resist the urge to use your continuous integration server to define a compile, test, package workflow and implement it in the build system instead (CruiseControl.NET explicitly discourages you from using their NUnit task). You will thank me for this the first time you need to debug the way your CI server is running the unit tests for a particular product. In fact, I don't really like the term "production build system" -- developers should be executing the build system against their project, on their local machines, all the time. Plus, if you follow this principle, you'll be able to very easily change out your CI server if you find a better one.

3) The build system should make it very easy to follow standard conventions

The build system itself is a product that is used by your organization's developers. With that in mind, it should be designed for ease of use. It should come pre-packaged with subroutines that handle common build tasks your developers need. It should give them a version numbering convention for free. It should automatically find their unit tests and execute them. The build system is a software product; it needs to be easy (dare I say enjoyable?) to use.

4) The build system should be extensible and flexible

This is seemingly the opposite of property 3, but I believe that they can happily co-exist. Your build system should be an extensible platform that allows your developers to tweak it to do "non-standard" things if they need to. Implementing your build system as a suite of scripts in a well-known scripting language is an important first step, as this allows your developers to add new components or rewrite existing components if they need to. Beyond this, however, your build system should itself be pluggable and extensible, allowing users to swap out one element while keeping the others, inject an extra step, or reorder the workflow.

If you take the time to create a build system that satisfies these properties, you will have significantly reduced the friction of setting up a new project or altering an existing build. You'll make it easy to automate all your builds in a standard way and set up a CI server to consume build output. You'll make the world a little bit more efficient.