Create relative paths for compiler_commands.json

I am working on a project where the code is currently compiled inside a linux container.
When I instruct cmake to write the file it uses absolute paths.

cmake -S /app/opt/myApp -B /app/opt/myApp -DCMAKE_EXPORT_COMPILE_COMMANDS=YES

Which results in compiler_commands.json containing absolute paths to both the compiler and the other libraries. Once I debug the program outside of the container, my Language Server (clangd) will read the compile_commands.json file. Then return values that can only be resolved within the container.

How can i make the compile_commands.json file use relative paths?

Is there a way to make it write with paths with environment variables like %PATH%, %HOME%,etc?

AFAIK you cannot produce relative path: cf:
https://cmake.org/cmake/help/latest/variable/CMAKE_USE_RELATIVE_PATHS.html

However you can use volume mount in the container that makes the path to be the same in the container as in the host.

and then:

so I bet the container and the host are somehow “compatible” or do you do “remote debugging” from the host to the container ?

In any case making the source tree and the build appear at the same location in the container and on the host should be the simplest path to go (I do that myself for the same reason as yours).

@erk thanks for the contribution here.

That is the same thing i have found. The way we use docker containers is to provide a cross platform solution for running the container. The problem is that if you start to do very specific mounts which are not relative to the container’s path then it is no longer cross platform.

For example to run the container on windows gcc will likely be located within msys, and the Conan Packages that are pulled with the container get installed @ $HOME/.conan/data. So with this combination (docker + conan + cmake) you can buidl cross platform solutions that are just pull and run. Working in any env, but to debug those one must have the same path visible to CLANGD. Then begins the pain. As **CMAKE ** writes the compiler_commands.json it is using absolute paths. So if i just want to write cpp code on my desktop that is running the clangd language server, I cannot do it unless I do something like make a container which contains clangd, gdb, and an entire ide…Which far from elegant.

Someone else have a solution here?

CMake’s build trees are not relocatable in this way. You’ll need different compile_commands.json files for each platform (at least).