# clang-format integration

**URL:** https://discourse.cmake.org/t/clang-format-integration/3358
**Category:** Code
**Tags:** comp:clang
**Created:** [May 18, 2021, 7:44am UTC](https://discourse.cmake.org/t/clang-format-integration/3358 "2021-05-18T07:44:43Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)
#### Post date: [May 18, 2021, 7:44am UTC](https://discourse.cmake.org/t/clang-format-integration/3358/1 "2021-05-18T07:44:44Z")

</div>

Is there any plans for a clang-format integration in cmake? cmake does provide a clean clang-tidy integration, but couldn’t find clang-format.

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [May 18, 2021, 1:05pm UTC](https://discourse.cmake.org/t/clang-format-integration/3358/2 "2021-05-18T13:05:28Z")

</div>

I don’t think so. `CMake` is a build tool, not a code editor…

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [May 18, 2021, 1:57pm UTC](https://discourse.cmake.org/t/clang-format-integration/3358/3 "2021-05-18T13:57:32Z")

</div>

I couldn’t find a ticket on the gitlab referring to clang-format. What type integration are you looking for?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 18, 2021, 6:28pm UTC](https://discourse.cmake.org/t/clang-format-integration/3358/4 "2021-05-18T18:28:20Z")

</div>

It’s unlikely to happen. It basically requires a cycle in the build graph[1], so it’s not representible in some build tools (notably `ninja`).

[1] The `clang-format` rule “outputs” the source file, but it also needs to run whenever the source file changes. We could make it always run, but then there’s no hope for a “nothing to do” build.

---

<div class="post-metadata">

### Author: ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)
#### Post date: [May 19, 2021, 11:34am UTC](https://discourse.cmake.org/t/clang-format-integration/3358/5 "2021-05-19T11:34:13Z")

</div>

Meson build system include clang-format as a target. See: [Release 0.50.0](https://mesonbuild.com/Release-notes-for-0-50-0.html)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 19, 2021, 1:01pm UTC](https://discourse.cmake.org/t/clang-format-integration/3358/6 "2021-05-19T13:01:35Z")

</div>

I suppose a module could be developed to do this. Something like `clang_format_add_rules(TARGETS tgt1 tgt2)` with a `clang_format_add_target(NAME clang-format)`. I really don’t know about doing it by default like Meson is doing it because `clang-format` is _terrible_ with compatibility across versions (e.g., if one dev has `clang-format-8` and another `clang-format-9`, there’s likely to be _lots_ of noise generated as each ends up running it).

---

<div class="post-metadata">

### Author: ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)
#### Post date: [May 19, 2021, 1:40pm UTC](https://discourse.cmake.org/t/clang-format-integration/3358/7 "2021-05-19T13:40:04Z")

</div>

In the absence of cmake support for clang-format, is there any way to get all source files for all targets?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 19, 2021, 1:59pm UTC](https://discourse.cmake.org/t/clang-format-integration/3358/8 "2021-05-19T13:59:58Z")

</div>

One can iterate over the `SOURCES` for each target. I would recommend explicit target enumeration, but you can iterate through the `SUBDIRECTORIES` directory property for the top-level directory to read `BUILDSYSTEM_TARGETS` for each directory instead.

---

<div class="post-metadata">

### Author: ![jwm](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jwm/32/654_2.png) [@jwm](https://discourse.cmake.org/u/jwm)
#### Post date: [May 25, 2021, 7:41pm UTC](https://discourse.cmake.org/t/clang-format-integration/3358/9 "2021-05-25T19:41:48Z")

</div>

@cmak , I wanted the same thing. @ben.boeckel walked through the philosophical/practical aspects of why this should not be done [here](https://discourse.cmake.org/t/cmake-pre-build-command/1083/11).

You can configure a pre-commit hook for `git` to run `clang-format` over just the lines changed by the commit, but I don’t like doing that because that means I’m committing code I haven’t even tried to build.

What I ended up doing was changing my automated build (Bamboo) to run `clang-format` over the entire source tree and fail if any file was modified. This makes the enforcement of formatting a CI step as Ben suggested; your build fails very quickly if you didn’t do the formatting like you should.
