# Using clang-tidy with cross compilation

**URL:** https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435
**Category:** Usage
**Tags:** os:linux, comp:clang, tool:cmake
**Created:** [April 11, 2022, 10:09am UTC](https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435 "2022-04-11T10:09:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![AdamWRichardson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/adamwrichardson/32/2339_2.png) [@AdamWRichardson](https://discourse.cmake.org/u/AdamWRichardson)
#### Post date: [April 11, 2022, 10:09am UTC](https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435/1 "2022-04-11T10:09:53Z")

</div>

This may be a really silly issue but I’m struggling to get `clang-tidy` working while cross compiling.

I’m using the [RSIC-V GCC toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain) to generate the binaries for a small embedded project I have and as part of the build I want to enable certain static analysis tools such as `clang-tidy`. I enable the `clang-tidy` checks using

```auto
set(CMAKE_CXX_CLANG_TIDY clang-tidy;-checks=-*,readability-*,performance-*,portability-*)

```

in my top level CMakeLists.txt. I then run CMake and I get the error from `clang-tidy`:  
`error: x86-64 'interrupt' attribute only applies to functions that have only a pointer parameter optionally followed by an integer parameter [clang-diagnostic-error]`  
for the following code snippet:

```auto
__attribute__ ((interrupt)) void handler() {}

```

The specifics for this error is that the function signature for x86 differs from that of RISC-V but this just shows that clang-tidy is doing the compilation for my host machine rather than the target architecture.

Have I done anything wrong when setting up `clang-tidy` to run? Is there a way to get CMake to change the clang compiler target when running `clang-tidy`?

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [April 11, 2022, 10:30am UTC](https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435/2 "2022-04-11T10:30:31Z")

</div>

First you have to use the --target= CLI parameter for clang-tidy.

But that may not be sufficient as the header files are also for your host. You also have to define the include directories. Luckily, CMake already knows these as `CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES`.  
Before those, somehow clang then also needs its own include directory else I got strange errors (`${CLANG_ROOT_DIR}/lib/clang/${CLANG_TIDY_VERSION_STRING}/include`). To know `CLANG_TIDY_VERSION_STRING`, you have to parse `clang-tidy --version`.

Include directories for clang-tidy can be added using

```auto
list ( APPEND CMAKE_${lang}_CLANG_TIDY --extra-arg=-isystem "--extra-arg=${some_include_path}" )

```

for each directory.

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [April 11, 2022, 10:34am UTC](https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435/3 "2022-04-11T10:34:47Z")

</div>

And do yourself a favor and omit the “-checks” CLI parameter. Use a `.clang-tidy` file instead.

Advantage:

- clang-tidy integration separated from clang-tidy usage setup
- per-directory configuration possible
- IDEs also read that file for use of integrated clang-tidy displaying (e.g. QtCreator, VS 2019)
- you can actually configure the parameters of those checks

---

<div class="post-metadata">

### Author: ![AdamWRichardson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/adamwrichardson/32/2339_2.png) [@AdamWRichardson](https://discourse.cmake.org/u/AdamWRichardson)
#### Post date: [April 12, 2022, 8:21am UTC](https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435/4 "2022-04-12T08:21:24Z")

</div>

Thanks! I managed to get the include paths in a similar way to [here](https://gitlab.kitware.com/cmake/cmake/-/issues/20912) using

```auto
if(CMAKE_EXPORT_COMPILE_COMMANDS)
    set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
endif()

```

But I really don’t think this is the best way to do it. I can’t see why CMake wouldn’t be able to pass the include directories for us but maybe there’s some detail I’m missing. Even if it’s not automatic it seems like there’s a cleaner solution than this

---

<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: [April 12, 2022, 11:48am UTC](https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435/5 "2022-04-12T11:48:43Z")

</div>

The `clang-tidy` support is pretty ignorant of behaviors of `clang-tidy` itself AFAIK. Flag manipulation like this needs to be done manually as CMake doesn’t really know how well `clang-tidy` actually understands the compiler in use (and getting it to understand means that as `clang-tidy` improves, CMake has to chase it with whatever table is used to track that information).

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [April 12, 2022, 12:15pm UTC](https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435/6 "2022-04-12T12:15:15Z")

</div>

Yes, there’s also the problem of clang-tidy compatibility between several vs2019/cmake/llvm version combinations which may not be relevant at all for most developers.

Same for this problem of making clang-tidy compatible to a certain gcc-based SDK.

And then there’s also the problem that the code itself needs to be clang-compilable 😉

---

<div class="post-metadata">

### Author: ![pavel-kirienko](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pavel-kirienko/32/4403_2.png) [@pavel-kirienko](https://discourse.cmake.org/u/pavel-kirienko)
#### Post date: [March 15, 2024, 11:33pm UTC](https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435/7 "2024-03-15T23:33:35Z")

</div>

> [@hsattler](#):
>
> First you have to use the --target= CLI parameter for clang-tidy.

Could you please expand on this a little? At least the official distribution of Clang-Tidy v15 doesn’t have the target parameter.

**EDIT 2** : Oh I think you mean passing it to the clang frontend invoked by clang-tidy via `--extra-arg`. That worked for me (initially it didn’t because I invoke clang-tidy via `execvp` and I forgot that the first argument is supposed to be the path to the executable itself).
