The joy of libclang in Yocto ---------------------------- Once upon a time I was asked to introduce libclang in our yocto build. The back story, abridged version, is about a certain team that requires a certain tool for certain logic to be copied and pasted from another project. In case you're not aware of it, yocto uses GCC for C and C++. As far as I know there are efforts aiming at integrating Clang to it, but in our case we are just interested in libclang, not in the compiler. The task seemed a little complex, so I started with a preliminary study, trying to figure out how difficult this could be. The compilation of the library is not complicated itself, but even restricting the build to the library alone requires around 2 hours. By tweaking the compilation a little, I could shrink down the final image size to 150 MiB, which is quite a lot, but better than the original 500 MiB. I wish the management was convinced by this data, but it clearly wasn't enough. I started to delve into the task. Let's mention the good news first: since this is about code generation, I don't need to build it for the target machine. The build is just needed for the Host and for the SDK. If you who are reading this journal entry are not experienced in Yocto, you might be wondering what that means. By Host I mean the "regular" build environment within Yocto. The SDK is a self-contained and installable build environment that enables a workstation to build software for the target platform without having Yocto installed. The software that is distributed with the SDK is effectively cross compiled as it was a different architecture With the previous paragraph hopefully giving enough context, it is time to mention that the build system of of Clang requires certain tools that are compiled on the fly, and invoked afterwards, by the very same build system. Since however this is a cross-build, the Clang build system will fail while trying to execute the executables it has just built. Those executables are in fact tailored for the SDK. What I need is to force the CMake of Clang to use those that I compiled in the host. Since the tools are made for the SDK, they can't be executed, since they're pointing at a non-existent linker script (expected to exist under /opt/infn-xr/..., where it will not be installed until the SDK is installed, so this is a chicken-egg problem. Since the tools are not meant to be found in the system root, the cmake configuration will restrict the search path to the build path Therefore I need to patch cmake configuration. Since the tools are not meant for installation, the cmake configuration will not declare them as installable. Therefore, again, I need to patch cmake configuration. Patching cmake configuration implies the invalidation of the Shared State Cache of Yocto, which means I have to endure two hours of rebuild at each attempt. Just wonderful.