The xv6 Build System

Our fork of xv6 uses the CMake build system generator. CMake takes a description of your build system (in the form of CMakeLists.txt) and will generate a Makefile implementing that description. CMake can generate ninja files too, but this is outside the scope of this class.

It is idiomatic to output build artifacts in a separate directory. Build artifacts are simply the list of outputs generated by the build system. In most projects using CMake, the output directory is called build. This is why we first create a build directory, cd into it, and then invoke cmake.

If you do not already have a build directory, create one and cd into it!

mkdir build
cd build

Now, run the following command:

cmake .. -DCMAKE_BUILD_TYPE=Debug

Explanation of arguments:

  • ..
    • Tell cmake that the root CMakeLists.txt file is in the parent folder.
  • -DCMAKE_BUILD_TYPE=Debug
    • Tell cmake to output a build system that produces Debug builds.

After you run this command, you will see a Makefile in the build directory.

To build xv6:

make

To remove all build artifacts:

make clean

Note that this command will also delete the file system image.

Should I run cmake everytime I want to build xv6?

No, you shouldn’t need to do that. As mentioned before, cmake is a build system generator. It doesn’t build the project for you. Running make is what executes the build steps.

If you make a change to the xv6 source files (kernel, bootloader, user, include, etc), you only need to run make.

If you make a change to any CMakeLists.txt file, you will need to run both cmake and make. We need to do this as editing the CMakeLists.txt file means altering the build system description, and we need cmake to update the Makefile to reflect the change. You will typically encounter this when trying to add userspace programs that test kernel functionality.