CMake linter and formatter
wefti includes a lossless CMake formatter, a linter with automatic fixes, and an unused dependency checker. All three run from the wefti command and read one .wefti.toml file. Your CMakeLists.txt files stay valid for CMake 4.5.1.
Run wefti fmt to format CMakeLists.txt and *.cmake files, and wefti lint --fix to apply safe fixes for the 12 built-in lint rules. Run wefti unused -B build to find unused find_package() calls and duplicate links. In CI, use wefti fmt --check, wefti lint --deny-warnings --sarif, and wefti unused --deny --sarif.
How does wefti fmt format CMake files?
wefti fmt changes only whitespace and, if you select it, the case of ASCII command names. Before it writes a file, an independent verifier confirms three things: the syntax tree is equivalent, the protected tokens are unchanged, and each comment stays attached to the same code. If one check fails, the file stays as it was.
The formatter reads CMakeLists.txt and *.cmake files. Recursive discovery skips .git, CMakeFiles, Testing, nested build trees, AppleDouble ._* files, symbolic links, and the exclude patterns of the project. In-place formatting does not overwrite a symbolic link that you give as an operand.
wefti fmt # format the project in place
wefti fmt --check # exit 1 if a file needs formatting
wefti fmt --diff # print unified diffs, write nothing
wefti fmt --stdin --stdin-filepath cmake/deps.cmake < cmake/deps.cmake
| Exit code | Meaning |
|---|---|
| 0 | All files are formatted, or the formatter wrote them in place |
| 1 | One or more files need formatting (with --check or --diff) |
| 2 | Invalid command line, parse or configuration error, or I/O failure |
Which .wefti.toml options control the formatter and linter?
wefti fmt and wefti lint read the nearest .wefti.toml file starting at the file's directory and walking up to the project root. The [fmt] table has four keys. An unknown key in any table is a configuration error, so a misspelled key does not fail silently.
exclude = ["third_party/**", "generated/*.cmake"]
[fmt]
indent_width = 2 # 1 to 16, default 2
line_width = 100 # 20 to 1000, default 100
command_case = "lower" # "preserve" (default), "lower", or "upper"
end_of_line = "lf" # "preserve" (default), "lf", or "crlf"
[lint]
disable = ["directory-options"]
[lint.severity]
glob-without-configure-depends = "error"
old-policy = "note"
The wefti-lsp language server reads the same file. It formats with the same engine and shows the same lint findings in your editor. wefti fmt and wefti lint ignore the [lsp] table.
Which rules does wefti lint check?
wefti lint checks CMakeLists.txt and *.cmake files against 12 built-in rules. Nine rules are warnings and three are notes by default. Ten rules have an automatic fix. Run wefti lint --list-rules to print the rules with their default severities.
| Rule ID | Default severity | What it finds | Automatic fix |
|---|---|---|---|
undefined-variable | warning | A variable reference that is unset or probably misspelled | unsafe: rename to the close match |
glob-without-configure-depends | warning | file(GLOB) or file(GLOB_RECURSE) for target sources without CONFIGURE_DEPENDS | safe: add CONFIGURE_DEPENDS |
single-config-condition | warning | A condition on CMAKE_BUILD_TYPE without a single-config generator guard | None |
unguarded-build-type | warning | An unconditional assignment or FORCE cache write that hides CMAKE_BUILD_TYPE | unsafe: remove FORCE |
directory-options | note | include_directories, link_libraries, or add_definitions that affect all targets in scope | None |
obsolete-command | warning | Obsolete or removed commands and unsupported cmake_minimum_required versions | unsafe: replacement command or higher version |
old-policy | warning | cmake_policy(SET CMPnnnn OLD) | unsafe: set to NEW |
fragile-if-expansion | warning | An unquoted expansion in if() or elseif() that breaks with empty or list values | safe (when CMP0054 is NEW for supported operands): quote the operand |
legacy-block-end-args | warning | Arguments repeated in else(), endif(), endforeach(), endwhile(), endfunction(), or endmacro() | safe: remove the arguments |
deprecated-link-scope | warning | LINK_PUBLIC or LINK_PRIVATE in target_link_libraries() (CMP0023) | safe or unsafe: replace with PUBLIC or PRIVATE |
redundant-fatal-error | note | FATAL_ERROR in cmake_minimum_required(), which CMake ignores | safe: remove FATAL_ERROR |
redundant-policy-pin | note | cmake_policy(SET CMPnnnn NEW) for a policy that the policy version already sets to NEW | safe: remove the command |
The deprecated-link-scope fix is safe only when the linted files create the target, no call uses the plain signature for the target, no argument contains $, and no external include or macro is in scope. In all other cases the fix is unsafe.
How do safe and unsafe fixes work?
A safe fix keeps the configure behavior or removes a command that has no effect. An unsafe fix can change the configure behavior. wefti lint --fix applies only safe fixes. Add --unsafe-fixes to also apply unsafe fixes.
The linter applies each fix as a byte edit on the concrete syntax tree. Comments and formatting outside each edit stay the same.
wefti lint # report findings only
wefti lint --fix # apply safe fixes in place, then report what remains
wefti lint --fix --unsafe-fixes # also apply unsafe fixes
wefti lint --dry-run # count the fixes, write nothing
wefti lint --diff --unsafe-fixes # print all fixes as a unified diff, write nothing
wefti lint --deny-warnings --sarif > wefti-lint.sarif
| Flag | Effect | Limits |
|---|---|---|
--fix | Write safe fixes, then report the remaining findings | Cannot combine with --dry-run or --diff |
--unsafe-fixes | Also apply fixes that can change behavior | Needs --fix, --dry-run, or --diff |
--dry-run | Count the fixes without writing files | Exit 1 when there is a fix to apply |
--diff | Print the fixes as a unified diff | Exit 1 when there is a fix to apply. Cannot combine with --sarif |
--deny-warnings | Exit 1 on warnings as well as errors | |
--sarif | Write SARIF 2.1.0 JSON to standard output | For GitHub code scanning and other SARIF readers |
After the fixes, the command prints a summary, for example Fixed 3 findings (3 safe, 0 unsafe) in 2 files; 1 remain.
To suppress one rule on one command, write # wefti-lint: disable=<rule> on the line before the command, inside its parentheses, or at the end of its closing line. An unknown or malformed directive gives an invalid-lint-directive finding.
How do I find unused find_package calls and links?
Run wefti unused -B build. The command configures the project again in memory and reports what the project declares but does not use. It writes no files to the build tree and starts no subprocesses.
wefti unused -B build
wefti unused -B build --only vars
wefti unused -B build --only packages,links,vars --deny
wefti unused -B build --sarif > wefti-unused.sarif
| Group | Category ID | What it reports |
|---|---|---|
packages | unused_package | A package that find_package() found but the project does not use |
packages | optional_not_found | An optional package that was not found (only with --include-optional) |
links | duplicate_link | A target that links the same target more than once |
links | empty_interface_link | A link to an in-tree INTERFACE library that has no usage requirements |
vars | unused_variable | A project-local variable that no code reads |
The default groups are packages,links. The command reports a finding only when the recorded evaluation proves it. A reference in any project listfile, also in a branch that did not run, keeps the item out of the report. Use --json for one wefti-unused-v1 report or --sarif for a SARIF 2.1.0 log. You cannot combine --json and --sarif.
The exit code is 0 when the analysis completes. It is 1 when the configure fails, or when --deny is set and there are findings. It is 2 for invalid arguments or a missing build or source directory.
How does wefti compare with cmake-format and gersemi?
wefti combines a formatter, a linter with automatic fixes, and configure-aware checks in one native binary. cmake-format and cmake-lint (from the cmakelang package) and gersemi are Python tools that read listfiles without a configure step. The table shows only features. It does not compare speed, because this project has not measured these tools.
| Feature | wefti 0.3.0 | cmake-format and cmake-lint (cmakelang) | gersemi |
|---|---|---|---|
| Implementation | Rust, one native binary | Python package | Python package |
| Install | Release archive, Homebrew, Scoop, Nix, or WinGet (once accepted in winget-pkgs) | pip install cmakelang | pip install gersemi |
| Formatter | wefti fmt | cmake-format | gersemi |
| Formatter options | Four [fmt] keys | Many layout options | Few options |
| Linter | wefti lint, 12 rules | cmake-lint | No |
| Automatic lint fixes | --fix and --unsafe-fixes | No | No linter |
| SARIF output | wefti lint and wefti unused | No | No |
| Configure-aware checks | wefti unused and wefti graph | No | No |
| Configuration file | .wefti.toml | .cmake-format.py, .yaml, or .json | .gersemirc |
| Language server with the same engine | wefti-lsp | No | No |
Use cmake-format when you need many layout options. Use gersemi when you want only a formatter with few settings. Use wefti when you want one tool for formatting, lint fixes, unused dependency checks, and SARIF output in CI. Read the upstream documentation of each tool for its current options.
How do I run the CMake linter and formatter in GitHub Actions?
Run wefti lint --deny-warnings --sarif, wefti unused --deny --sarif, and wefti fmt --check as separate steps. Then upload the SARIF files to GitHub code scanning. The upload steps run also when an earlier step fails, so the findings show on the pull request.
name: cmake-checks
on: [push, pull_request]
permissions:
contents: read
security-events: write
jobs:
cmake-checks:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Install wefti
run: |
curl -fsSLO https://raw.githubusercontent.com/wefti/wefti/v0.3.0/scripts/install.sh
bash install.sh --version 0.3.0
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
- name: Lint listfiles
run: wefti lint --deny-warnings --sarif > wefti-lint.sarif
- name: Find unused packages and links
run: |
wefti -S . -B build
wefti unused -B build --deny --sarif > wefti-unused.sarif
- name: Check formatting
run: wefti fmt --check
- name: Upload lint results
if: always() && hashFiles('wefti-lint.sarif') != ''
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: wefti-lint.sarif
category: wefti-lint
- name: Upload unused results
if: always() && hashFiles('wefti-unused.sarif') != ''
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: wefti-unused.sarif
category: wefti-unused
The install script checks the SHA-256 sum of the release archive. For other runners and package managers, read the installation guide. To share compiler checks across CI runners, read the CI cache guide.
How to format, lint, and check a CMake project
Use wefti fmt, wefti lint, and wefti unused to clean the listfiles of a CMake project and keep them clean in CI.
- Add a .wefti.toml file. Put .wefti.toml in the project root. Set the [fmt] options and the exclude patterns for vendored code.
exclude = ["third_party/**"]
[fmt]
indent_width = 2
line_width = 100
command_case = "lower"
- Format the listfiles. Run wefti fmt --diff to preview the changes. Then run wefti fmt to write them.
wefti fmt --diff
wefti fmt
- Fix lint findings. Run wefti lint --diff --unsafe-fixes to review all fixes. Run wefti lint --fix to apply the safe fixes.
wefti lint --diff --unsafe-fixes
wefti lint --fix
- Find unused dependencies. Configure the project. Then run wefti unused to find unused packages, duplicate links, and unused variables.
wefti -S . -B build
wefti unused -B build --only packages,links,vars
- Add the checks to CI. Run wefti fmt --check, wefti lint --deny-warnings --sarif, and wefti unused --deny --sarif in CI. Upload the SARIF files to code scanning.
wefti fmt --check
wefti lint --deny-warnings --sarif > wefti-lint.sarif
wefti unused -B build --deny --sarif > wefti-unused.sarif
Frequently asked questions
Is there a CMake linter that fixes problems automatically?
Yes. wefti lint checks CMakeLists.txt and *.cmake files against 12 rules. wefti lint --fix applies the safe fixes, and --unsafe-fixes also applies fixes that can change configure behavior. Use --diff to review the fixes before you write them.
Which CMake formatter does not change build behavior?
wefti fmt changes only whitespace and the optional case of ASCII command names. Before it writes a file, a verifier confirms that the syntax tree is equivalent, protected tokens are unchanged, and comments stay attached. If a check fails, the file stays the same.
How do I check CMake formatting in CI?
Run wefti fmt --check. The command exits with code 1 when a file needs formatting and writes no files. Use wefti fmt --diff to print the changes as a unified diff.
How is wefti lint different from cmake-lint and gersemi?
cmake-lint reports findings without fixes, and gersemi is a formatter only. wefti lint applies safe and unsafe fixes, writes SARIF 2.1.0 for code scanning, and shares one engine with wefti fmt and wefti-lsp. This page does not compare speed.
How do I disable a wefti lint rule?
Add the rule ID to disable in the [lint] table of .wefti.toml to turn it off for the project. To suppress one rule on one command, write # wefti-lint: disable=<rule> on the line before the command. Use [lint.severity] to change a severity.
How do I find unused find_package calls in CMake?
Run wefti unused -B build. It reports packages that find_package() found but the project does not use, duplicate links, and empty INTERFACE links. Add --only vars for unused variables, --deny to fail CI, or --sarif for code scanning.