Build CMake - BredaUniversityGames/JenkinsLib GitHub Wiki

cmake.build()

Configures and builds a CMake project. Supports both CMakePresets.json preset-based builds and manual generator/config/platform builds.

Usage

stages {
    git.sync()
    cmake.build()
    // ...
}

With overrides:

stages {
    git.sync()
    cmake.build(
        CMAKE_BUILD_PRESETS: ['debug-build', 'release-build']
    )
    // ...
}

Manual mode (no presets):

stages {
    git.sync()
    cmake.build(
        CMAKE_SOURCE_DIR: 'engine',
        CMAKE_GENERATOR_CHOICES: ['Ninja', 'Visual Studio 17 2022']
    )
    // ...
}

Prerequisites

  • CMake installed and on PATH on the build agent
  • Visual Studio installed (located automatically via vswhere.exe)
  • A CMakePresets.json in the project root (for preset-based builds)

Parameters

Preset Mode

When CMakePresets.json is detected, the pipeline shows preset-based parameters:

Parameter Default Description
CMAKE_SOURCE_DIR . Path to the directory containing CMakeLists.txt
CMAKE_BUILD_PRESET (first preset) Build preset (dropdown populated from CMakePresets.json)
CMAKE_ARGS (empty) Additional CMake configure arguments
CMAKE_BUILD_ARGS (empty) Additional CMake build arguments

Manual Mode

When no presets are found, the pipeline falls back to manual parameters:

Parameter Default Description
CMAKE_SOURCE_DIR . Path to the directory containing CMakeLists.txt
CMAKE_BUILD_DIR build Build output directory
CMAKE_GENERATOR Visual Studio 18 2026 CMake generator (Ninja, Visual Studio 18 2026, Visual Studio 17 2022, Visual Studio 16 2019, Unix Makefiles)
CMAKE_CONFIG Debug Build configuration (Debug, Release, RelWithDebInfo, MinSizeRel)
CMAKE_PLATFORM x64 Target platform (x64, Win32, ARM64)
CMAKE_ARGS (empty) Additional CMake configure arguments
CMAKE_BUILD_ARGS (empty) Additional CMake build arguments

How It Works

Preset Discovery

The module discovers presets in order:

  1. Override lists — Presets passed directly in the Jenkinsfile (e.g., CMAKE_BUILD_PRESETS: [...])
  2. Workspace file — Reads CMakePresets.json from the workspace (fast path, works when the workspace persists between builds)
  3. VCS remote — Fetches CMakePresets.json from the Git or Perforce remote (auto-detected from pipeline parameters)

Note: On the very first run, presets may not be available yet. Run the job once so the library can discover your presets, then subsequent runs will show preset dropdowns.

Build Flow

Preset mode:

  1. Derives the configure preset from the selected build preset
  2. Runs cmake --preset <configure-preset> (with any extra CMAKE_ARGS)
  3. Runs cmake --build --preset <build-preset> (with any extra CMAKE_BUILD_ARGS)

Manual mode:

  1. Runs cmake -S <source> -B <build> -G <generator> -DCMAKE_BUILD_TYPE=<config> (adds -A <platform> for Visual Studio generators)
  2. Runs cmake --build <build> --config <config>

VS Environment

Before running any CMake command, the module automatically:

  1. Locates the latest Visual Studio installation using vswhere.exe
  2. Runs vcvarsall.bat with the correct architecture (x64, x86, or amd64_arm64)

This ensures compilers and build tools are available without manual setup.

Context

After execution, cmake.build() sets the following on the pipeline context:

Key Preset Mode Manual Mode
ctx.buildEngine 'CMake' 'CMake'
ctx.cmakeBuildPreset Selected preset name (not set)
ctx.buildConfig (not set) Selected configuration
ctx.buildPlatform (not set) Selected platform
ctx.cmakeBuildDir (not set) Build directory path

Overrides

Override default choices by passing maps to cmake.build():

Override Key Description
CMAKE_BUILD_PRESETS List of build preset names to show in the dropdown
CMAKE_SOURCE_DIR Default source directory
CMAKE_BUILD_DIR Default build directory (manual mode)
CMAKE_GENERATOR_CHOICES List of generators for the dropdown
CMAKE_CONFIG_CHOICES List of configurations for the dropdown
CMAKE_PLATFORM_CHOICES List of platforms for the dropdown
CMAKE_ARGS Default configure arguments
CMAKE_BUILD_ARGS Default build arguments

See Also

Direct-Use Methods

These methods are available on the cmake object for advanced or custom pipelines:

Method Description
cmake.configure(config) Manual configure (sourceDir, buildDir, generator, config, platform, args)
cmake.configureWithPreset(config) Configure with a preset (preset, args)
cmake.buildProject(config) Manual build (buildDir, config, platform, buildArgs)
cmake.buildWithPreset(config) Build with a preset (preset, buildArgs)
cmake.install(config) Install (buildDir, config, prefix)
cmake.testWithPreset(config) Run CTest with a preset (preset, resultsFile)
cmake.packageProject(config) Manual CPack (buildDir, config, generator, args)
cmake.packageWithPreset(config) Run CPack with a preset (preset)
cmake.workflowWithPreset(config) Run a workflow preset (preset)

Troubleshooting

Issue Solution
No preset dropdowns on first run Run the job once so the library discovers your CMakePresets.json, then re-run
"Could not discover presets" Ensure CMakePresets.json exists in CMAKE_SOURCE_DIR and VCS credentials are configured
CMake or compiler not found Ensure CMake is on PATH and Visual Studio is installed on the build agent
Wrong Visual Studio version The module uses vswhere -latest; install the desired VS version or set a specific generator
Manual parameters shown instead of presets Ensure your CMakePresets.json contains non-hidden buildPresets
⚠️ **GitHub.com Fallback** ⚠️