Skip to content

mbfoss/cmake.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

cmake.nvim

Warning

Work in progress. This plugin is under active development. Behaviour, commands and configuration options may change without notice, and things may break. Use at your own risk.

A small, standalone CMake driver for Neovim (>= 0.10). Everything runs through one :CMake command with tab-completion. No dependencies.

Install

Drop it on your runtimepath (e.g. pack/plugins/opt/cmake.nvim) and, optionally:

require("cmake").setup({
  build_type = "Debug",   -- initial CMAKE_BUILD_TYPE
  build_root = "build",   -- build dir is <build_root>/<build_type>
  -- preset    = "dev",    -- start on a specific configure preset
  -- generator = "Ninja",
  -- parallel  = 8,        -- -j 8
})

setup is optional — the defaults work out of the box.

Commands

:CMake configure          configure into the build dir (seeds the File API)
:CMake build [target]     cmake --build (no target = build all)
:CMake run [target]       build then run an executable target (prompts if omitted)
:CMake test [regex]       ctest --output-on-failure (regex = ctest -R filter)
:CMake clean              build the clean target
:CMake select [type]      switch build type (prompts if omitted)
:CMake preset [name]      switch configure preset; `none` leaves preset mode
:CMake presets            list the presets the project defines
:CMake targets            list discovered targets
:CMake status             show the active preset / build type and resolved dirs

Run :CMake with no argument for a picker.

CMake presets

If the project has a CMakePresets.json (or CMakeUserPresets.json), the plugin uses it automatically — no configuration needed. The first configure preset is selected on first use; switch with :CMake preset, or pin one via setup({ preset = "dev" }).

In preset mode cmake is driven through presets, and the preset owns the generator, build type and binary dir:

:CMake configure   ->  cmake --preset dev
:CMake build app   ->  cmake --build --preset dev --target app
:CMake test        ->  ctest --preset dev

The build dir follows the preset's binaryDir, so targets, tests and :CMake run all resolve against it. build_root / build_type / generator / :CMake select are ignored here — the presets file is the source of truth. :CMake preset none drops back to the classic <build_root>/<build_type> behaviour, and setup({ use_presets = false }) disables preset mode entirely.

Build and test presets are picked up automatically when they point at the active configure preset (a same-named one wins). This matters for multi-config generators, where the build preset's configuration selects what actually gets built. When the choice would be ambiguous — several build presets for one configure preset — the plugin falls back to cmake --build <binaryDir> rather than guessing.

Supported: include, inherits, condition, hidden, cacheVariables / environment merging, and macro expansion (${sourceDir}, ${presetName}, ${generator}, ${hostSystemName}, ${fileDir}, $env{}, $penv{}, …). Hidden presets and presets whose condition fails on this host are left out of the picker and completion, matching cmake --list-presets.

Completion

:CMake <Tab> completes subcommands. After a subcommand:

  • :CMake build <Tab> / :CMake run <Tab> — target names
  • :CMake test <Tab> — CTest test names
  • :CMake select <Tab> — build types
  • :CMake preset <Tab> — configure presets

Targets and tests come from the CMake File API, so they appear once the project has been configured at least once. Before that, completion simply returns nothing rather than erroring.

How it works

  • :CMake configure drops a File API query into <build dir>/.cmake/api/v1/ and reads the reply's codemodel to enumerate targets; ctest --show-only=json-v1 supplies test names.
  • Actions run in a reused terminal split. Configure/build output is scraped into the quickfix list via your errorformat.
  • The build directory is the active preset's binaryDir, or <build_root>/<build_type> when no presets are in play.
  • Preset files are parsed in-process (so the picker and the File API query know the binaryDir before the first configure) and re-read when they change on disk. A malformed file degrades to "no presets" instead of erroring.

Configuration reference

Option Default Meaning
cmake "cmake" cmake executable
ctest "ctest" ctest executable
source_dir "." dir with the root CMakeLists.txt (relative to project root)
build_root "build" parent build dir (ignored in preset mode)
build_type "Debug" initial build type (ignored in preset mode)
build_types Debug/Release/RelWithDebInfo/MinSizeRel choices for :CMake select
use_presets true use CMakePresets.json when the project has one
preset nil configure preset to start on; nil picks the first usable one
generator nil -G generator (ignored in preset mode)
export_compile_commands true add -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
configure_args {} extra configure args
build_args {} extra build args
parallel nil -j <n> for builds
environment {} env vars for every command
quickfix true scrape build output into quickfix
run_builds_first true build a target before :CMake run
window { split = "botright", height = 15 } output terminal window

About

CMake integration for neovim

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages