Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to ensure that VSCode gets the correct compile_commands.json for each subproject of the superbuild? #1596

Open
traversaro opened this issue Feb 13, 2024 · 15 comments

Comments

@traversaro
Copy link
Member

Perhaps it could make sense that if CMAKE_EXPORT_COMPILE_COMMANDS is set to ON in the robotology-superbuild, we also set it to on in all the subprojects?

@LoreMoretti

@traversaro
Copy link
Member Author

I saw that in https://cmake.org/cmake/help/v3.27/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html that:

This is initialized by the CMAKE_EXPORT_COMPILE_COMMANDS environment variable, and initializes the EXPORT_COMPILE_COMMANDS target property for all targets.

Can't we just tell people to set export CMAKE_EXPORT_COMPILE_COMMANDS=1 in their .bashrc or in their conda environment? I know that we told people not to put stuff in their .bashrc, but this seems quite a generic think that it could make sense to have in their .bashrc. In this way, we do not need to modify our code.

@traversaro
Copy link
Member Author

A second point is how to ensure that the multiple compile_commands.json of the subproject of the superbuild can be found by VSCode. I guess colcon and catkin workspaces have exactly the same problems, and indeed I found some related code for catkin: https://gist.github.com/AlexisTM/a6bb5ee488611e15114aa190b6f79404 . Apparently colcon does this automatically, see https://colcon.readthedocs.io/en/released/user/how-to.html#cmake-packages-generating-compile-commands-json . The related code is in https://github.com/colcon/colcon-cmake/blob/1509c77ecb417dd0b47e4d54b73833fa5e4c1baa/colcon_cmake/event_handler/compile_commands.py#L87-L104 .

@traversaro traversaro changed the title Propagate CMAKE_EXPORT_COMPILE_COMMANDS option? How to ensure that VSCode gets the correct compile_commands.json for each subproject of the superbuild? Feb 14, 2024
@traversaro
Copy link
Member Author

I renamed the issue to track the actual problem we want to face.

@traversaro
Copy link
Member Author

traversaro commented Feb 14, 2024

@giotherobot
Copy link
Member

I saw that in https://cmake.org/cmake/help/v3.27/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html that:

This is initialized by the CMAKE_EXPORT_COMPILE_COMMANDS environment variable, and initializes the EXPORT_COMPILE_COMMANDS target property for all targets.

Can't we just tell people to set export CMAKE_EXPORT_COMPILE_COMMANDS=1 in their .bashrc or in their conda environment? I know that we told people not to put stuff in their .bashrc, but this seems quite a generic think that it could make sense to have in their .bashrc. In this way, we do not need to modify our code.

This does not seem to work for me: this is how i tested (vscode).

  1. enable robsub conda env
  2. export CMAKE_EXPORT_COMPILE_COMMANDS=1
  3. open vscode from that terminal and build the superbuild
  4. check if the files are generated and if the cache is correct, it is not

@traversaro
Copy link
Member Author

I am not sure, but I guess the environment variable is ready only once when the project is configuring for the first time. Indeed, by checking the CMake's source code this is the case: https://github.com/Kitware/CMake/blob/v3.28.3/Modules/CMakeGenericSystem.cmake#L74-L79 .

Indeed this may be quite confusing, and explicitly passing the option may be more clear.

@giotherobot
Copy link
Member

giotherobot commented Feb 14, 2024

Tested the patch and it works, all the compile_commands.json are generated.

Then adding the following to .vscode/settings.json:

{
    "C_Cpp.default.compileCommands": "${workspaceFolder}/build/merged_compile_commands.json",
    "cmake.mergedCompileCommands": "${workspaceFolder}/build/merged_compile_commands.json"
}

allows for intellisense to work.

@traversaro
Copy link
Member Author

Great, can we document this somewhere? @LoreMoretti do you have any suggestion on where to document this?

@LoreMoretti
Copy link
Contributor

LoreMoretti commented Feb 14, 2024

Great, can we document this somewhere? @LoreMoretti do you have any suggestion on where to document this?

What about here: ami-iit/ami-commons#4

@giotherobot
Copy link
Member

Also in https://github.com/ami-iit/ami-commons/blob/master/doc/visual-studio-code.md maybe? Even though this is more specific to the superbuild

@traversaro
Copy link
Member Author

Great, can we document this somewhere? @LoreMoretti do you have any suggestion on where to document this?

What about here: ami-iit/ami-commons#4

I am a bit afraid that that issue is not in the path of things that new users will encounter when the join the AMI lab. Furthermore, this is a more robotology-superbuild problem, that is related also to users of other labs.

My suggestions are:

@giotherobot
Copy link
Member

How about shipping the settings.json file along with the superbuild repo, this would simplify the user operations needed.

@traversaro
Copy link
Member Author

How about shipping the settings.json file along with the superbuild repo, this would simplify the user operations needed.

Sure. Do you think it could make sense to also enable the CMAKE_EXPORT_COMPILE_COMMANDS option by default? If expert users have problems with this, they can always set it to OFF.

@giotherobot
Copy link
Member

At this point I would say yes, so the user has to to no action to enable this features in vscode.

@LoreMoretti
Copy link
Contributor

LoreMoretti commented Feb 14, 2024

For using clangd extension the merged_compile_commands.json of this comment should be named compile_commands.json.

I have opened an issue on clangd for it: clangd/clangd#1932

But for now, to be compatible with both intellisense and clangd we can just stick to the name compile_commands.json and add the following option for clangd:

"clangd.arguments": [
        "--compile-commands-dir=${workspaceFolder}/build/"
    ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants