CMake build phases

Building a CMake project involves three phases:

  1. Configure time (when cmake itself is running)
  2. Build time (when the generator such as make or ninja is running to compile/link the projects)
  3. Install time (when running the INSTALL target generated by CMake)

The file manipulation commands occur at configure time. For example, the following commands create folders and copy or configure files at configure time:


file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pypizza_package/pypizza/lib)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/setup.py.in ${CMAKE_CURRENT_BINARY_DIR}/pypizza_package/setup.py)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/pypizza_package DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

Also execute_process runs at configure time.

In Visual Studio the build phase occurs when you select the menu item Build/Build All, whereas the install phase occurs when you select the menu item Build/Install <project>.

To confuse things a bit, it's common to use cmake to run the build and install steps, such as in the following windows batch file:


cmake -G "Ninja" ^
    -DBUILD_SHARED_LIBS=%SHARED_LIBS% ^
    -DCMAKE_INSTALL_PREFIX:PATH="install" ^
    -DCMAKE_TOOLCHAIN_FILE="%VCPKG_ROOT_DIR%/scripts/buildsystems/vcpkg.cmake" ^
    -DVCPKG_TARGET_TRIPLET=%TRIPLET% ^
    -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS="TRUE" ^
    -DCMAKE_CXX_COMPILER=%WINDOWS_COMPILER% ^
    -DCMAKE_C_COMPILER=%WINDOWS_COMPILER% ^
    -DCMAKE_BUILD_TYPE="%CONFIG%" ^
    -DCMAKE_MAKE_PROGRAM=ninja ^
    --config %CONFIG% ^
    "%SOURCE%"

cmake --build . --target install --config %CONFIG%