Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Add support for compilation database generated by CMake. #135

Closed
wants to merge 2 commits into from
Closed

Add support for compilation database generated by CMake. #135

wants to merge 2 commits into from

Conversation

berenm
Copy link
Contributor

@berenm berenm commented Sep 24, 2012

The compilation database file name (usually /compilation_commands.json) can
be automatically generated by CMake, when generating the makefiles, by setting the CMake
variable CMAKE_EXPORT_COMPILE_COMMANDS to 1. This can be done from inside the CMakeLists.txt
or from the command line. Then, the compilation database can be provided to SublimeClang
using the "compilation_database" parameter in the SublimeClang configuration.

The compilation database format is specified by the LLVM/Clang project:
http://clang.llvm.org/docs/JSONCompilationDatabase.html

The file contains, for each source file built, the complete command line used to built it, which
allows then SublimeClang to have the exact flags to use to parse the file, without having to
add all the include folders manually for each project.

The parsing of each command line is currently done using a regex, which tries to match any
option starting with -D, -I, -O, -U, -W, -f, -g, or -s. This is not ideal, and shlex python
module could be used instead to have a more clean split of the arguments, but it's much
faster to parse the database using this regex.

Also, the database parsing is currently done asynchronously, when the first source file is loaded.
This might be improved by doing it the earliest possible.

The compilation database file name (usually <build folder>/compilation_commands.json) can
be automatically generated by CMake, when generating the makefiles, by setting the CMake
variable CMAKE_EXPORT_COMPILE_COMMANDS to 1. This can be done from inside the CMakeLists.txt
or from the command line. Then, the compilation database can be provided to SublimeClang
using the "compilation_database" parameter in the SublimeClang configuration.

The compilation database format is specified by the LLVM/Clang project:
http://clang.llvm.org/docs/JSONCompilationDatabase.html

The file contains, for each source file built, the complete command line used to built it, which
allows then SublimeClang to have the exact flags to use to parse the file, without having to
add all the include folders manually for each project.

The parsing of each command line is currently done using a regex, which tries to match any
option starting with -D, -I, -O, -U, -W, -f, -g, or -s. This is not ideal, and shlex python
module could be used instead to have a more clean split of the arguments, but it's much
faster to parse the database using this regex.

Also, the database parsing is currently done asynchronously, when the first source file is loaded.
This might be improved by doing it the earliest possible.
@berenm
Copy link
Contributor Author

berenm commented Sep 24, 2012

Actually, I've just realised that this does not work for header files... only source files have a command associated.
It might still be possible to use this compilation database to determine most include paths automatically.

The flags retrieved from the compilation database, are merged together for each folder
containing a source file. This adds support for header files located in the same folder
as a source file, however it does not solve the missing flags for headers located in
folder which do not contain any source.

Also, this merges the compilation flags together, which is maybe not desired. Maybe a
configuration parameter to control this would be appropriate.
@berenm
Copy link
Contributor Author

berenm commented Sep 24, 2012

This new commit partially solves the issue, at least for header files located in the same folder as the sources. However, it uses the union of all the flags of all the sources inside the folder for any source or header file located in the same folder. This is maybe not optimal, as we loose the possibility to use the exact flags that are used to compile each source file.

Also it does not solve the issue of missing flags for headers located elsewhere, where no source file is located (for example in a separated include/ folder).

@quarnster
Copy link
Owner

I'm leaning towards not accepting this pull request as it's awfully CMake specific and I see no reason the same task couldn't be solved via the already implemented options script mechanism.

@berenm
Copy link
Contributor Author

berenm commented Sep 24, 2012

Well, CMake is able to generate such database. However the database format and mechanism is specified by Clang (see the link in the first comment). I even think the Clang developers are responsible for the CMake support, as Clang is built using it.

By the way, the database is currently parsed by the plugin, but it seems that the clang python bindings have a direct support for it since Clang 3.2. http://clang.llvm.org/doxygen/group__COMPILATIONDB.html I haven't looked how to use this correctly.

Anyway, as the implementation is not complete and has some obvious issues that I did not foresee, I would say this require some more work.

The script is maybe one better solution, and would simply parse the database, aggregating all the flags together... Although I liked the possibility to have the exact flags used to build each file (which could vary from file to file).

@quarnster
Copy link
Owner

Although I liked the possibility to have the exact flags used to build each file (which could vary from file to file).

The filename is provided to the options script so you don't necessarily lose that functionality.

@aukaost
Copy link
Contributor

aukaost commented Sep 24, 2012

If I may add an alternative suggestion:
Maybe it is worth adding a subfolder with useful options scripts to the project that could contain additions like this pull request.

@quarnster
Copy link
Owner

Option scripts are so very dependent on the build system used which IMO makes them better handled via gists and a link to the gist in the documentation, similar to how there are a couple of example configurations provided as gists. I'd rather not bloat the code unnecessarily, especially with code paths I do not use myself and thus wouldn't notice when it breaks.

I'm going to close this for now, but if anyone strongly disagrees with me feel free to continue the argumentation here :)

@quarnster quarnster closed this Sep 24, 2012
@quarnster
Copy link
Owner

FYI, I turned this into a gist at https://gist.github.com/3944250, with a link provided in the settings file.

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

Successfully merging this pull request may close these issues.

3 participants