Skip to contentSkip to conversation

Makefile: add support for generating JSON compilation database

PATCH Superseded Philippe Blain via GitGitGadget 5 participants 15 messages Aug 30, 2020 – Sep 3, 2020 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Makefile: add support for generating JSON compilation database

From: Philippe Blain <[email protected]>

Tools based on LibClang [1] can make use of a 'JSON Compilation Database' [2] that keeps track of the exact options used to compile a set of source files.

The Clang compiler can generate JSON fragments when compiling [3], using the -MJ flag. These JSON fragments (one per compiled source file) can then be concatenated to create the compilation database, commonly called 'compile_commands.json'.

Add support to the Makefile for generating these JSON fragments as well as the compilation database itself, if the environment variable 'GENERATE_COMPILATION_DATABASE' is set.

If this variable is set, check that $(CC) indeed supports the -MJ flag, following what is done for automatic dependencies.

All JSON fragments are placed in the 'compile_commands/' directory, and the compilation database 'compile_commands.json' is generated as a dependency of the 'all' target using a sed invocation.

[1] https://clang.llvm.org/docs/Tooling.html
[2] https://clang.llvm.org/docs/JSONCompilationDatabase.html
[3] https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mj-arg

---
Add support for generating JSON compilation database

I don't have a lot of knowledge of Make double-colon rules, or insight into why they are used for the 'all' target, but I think the approach I chose makes sense. In particular, I do not list any prerequisite for the 'compile_commands.json' file, but from what I tested it is still rebuilt anytime the 'all' target is rebuilt, which is what we want.

Note: CMakeLists.txt in contrib/buildsystems does not need to be updated to also support this feature because CMake supports it out-of-the-box [1].

[1]
https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-714%2Fphil-blain%2Fcompiledb-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-714/phil-blain/compiledb-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/714

 .gitignore |  2 ++
 Makefile   | 52 +++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 49 insertions(+), 5 deletions(-)
.gitignore +2 -0
index ee509a2ad2..f4c51300e0 100644
@@ -197,6 +197,7 @@
 /git.spec
 *.exe
 *.[aos]
+*.o.json
 *.py[co]
 .depend/
 *.gcda
@@ -218,6 +219,7 @@
 /tags
 /TAGS
 /cscope*
+/compile_commands.json
 *.hcc
 *.obj
 *.lib
Makefile +47 -5

88 lines not on this page · raw message

base-commit: d9cd4331470f4d9d78677f12dc79063dab832f53

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 5 lines · Philippe Blain via GitGitGadget

From: Philippe Blain <[email protected]>

Tools based on LibClang [1] can make use of a 'JSON Compilation Database' [2] that keeps track of the exact options used to compile a set of source files.

For additional context why this is valuable, clangd, which is a C language server protocol implementation, can use these files to determine the flags needed to compile a file so it can provide proper editor integration. As a result, editors supporting the language server protocol (such as VS Code, or Vim with a suitable plugin) can provide better searching, integration, and refactoring tools.

So I'm very much in favor of a change like this.

Makefile else · line 2456

Quoted 7 lines
Makefile +7 -0
+ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
+all:: compile_commands.json
+compile_commands.json:
+	@$(RM) $@
+	$(QUIET_GEN)sed -e '1s/^/[/' -e '$$s/,$$/]/' $(compdb_dir)*.o.json > $@+
+	@if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi
+endif

How are those commas at the end of the line added? Are they natively part of the files? If so, this seems reasonable.

Signature · 1 line
brian m. carlson: Houston, Texas, US
#2 lore raw

Hi Brian,

Quoted 9 lines · brian m. carlson
Quoted 5 lines · Philippe Blain via GitGitGadget

From: Philippe Blain <[email protected]>

Tools based on LibClang [1] can make use of a 'JSON Compilation Database' [2] that keeps track of the exact options used to compile a set of source files.

Quoted 8 lines

For additional context why this is valuable, clangd, which is a C language server protocol implementation, can use these files to determine the flags needed to compile a file so it can provide proper editor integration. As a result, editors supporting the language server protocol (such as VS Code, or Vim with a suitable plugin) can provide better searching, integration, and refactoring tools.

So I'm very much in favor of a change like this.

Thanks!

Quoted 11 lines

Makefile else · line 2456

Quoted 8 lines
Makefile +7 -0
+ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
+all:: compile_commands.json
+compile_commands.json:
+	@$(RM) $@
+	$(QUIET_GEN)sed -e '1s/^/[/' -e '$$s/,$$/]/' $(compdb_dir)*.o.json > $@+
+	@if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi
+endif

How are those commas at the end of the line added? Are they natively part of the files? If so, this seems reasonable.

Yes: the '*.o.json' files generated by the compiler contain one JSON object per file, with a trailing comma.
This 'sed' invocation turns these files into a proper JSON array by:
- adding a '[' at the beginning and a ']' at the end of the list of objects
- removing the comma after the last entry (before the closing ']')

#3 lore raw
In reply to brian m. carlson
Quoted 7 lines · brian m. carlson
Quoted 5 lines · Philippe Blain via GitGitGadget

From: Philippe Blain <[email protected]>

Tools based on LibClang [1] can make use of a 'JSON Compilation Database' [2] that keeps track of the exact options used to compile a set of source files.

Quoted 6 lines

For additional context why this is valuable, clangd, which is a C language server protocol implementation, can use these files to determine the flags needed to compile a file so it can provide proper editor integration. As a result, editors supporting the language server protocol (such as VS Code, or Vim with a suitable plugin) can provide better searching, integration, and refactoring tools.

I found that the proposed commit log was very weak to sell the change; some of what you gave above should definitely help strenthen it.

Thanks.

#4 lore raw
Quoted 16 lines · Junio C Hamano
Quoted 7 lines · brian m. carlson
Quoted 5 lines · Philippe Blain via GitGitGadget

From: Philippe Blain <[email protected]>

Tools based on LibClang [1] can make use of a 'JSON Compilation Database' [2] that keeps track of the exact options used to compile a set of source files.

Quoted 6 lines

For additional context why this is valuable, clangd, which is a C language server protocol implementation, can use these files to determine the flags needed to compile a file so it can provide proper editor integration. As a result, editors supporting the language server protocol (such as VS Code, or Vim with a suitable plugin) can provide better searching, integration, and refactoring tools.

Quoted 3 lines

I found that the proposed commit log was very weak to sell the change; some of what you gave above should definitely help strenthen it.

Likewise. Looking at the output, I'm confused how it would help with things like searching and refactoring. It might be nice to spell it out for those of us exposed to it for the first time (I tried following the links but remained unenlightened).

I'd also be curious to hear what advantages it gives to add a new Makefile knob rather than just letting interested parties add -MJ to their CFLAGS. Is it just a convenience to create the concatenated form? It seems weird that projects would need to do so themselves with sed hackery (i.e., I'd expect whatever consumes this json to be able to handle multiple files).

-Peff

#5 lore raw
Quoted 21 lines · Jeff King
Quoted 16 lines · Junio C Hamano
Quoted 13 lines · brian m. carlson
Quoted 5 lines · Philippe Blain via GitGitGadget

From: Philippe Blain <[email protected]>

Tools based on LibClang [1] can make use of a 'JSON Compilation Database' [2] that keeps track of the exact options used to compile a set of source files.

For additional context why this is valuable, clangd, which is a C language server protocol implementation, can use these files to determine the flags needed to compile a file so it can provide proper editor integration. As a result, editors supporting the language server protocol (such as VS Code, or Vim with a suitable plugin) can provide better searching, integration, and refactoring tools.

Quoted 8 lines
Quoted 3 lines

I found that the proposed commit log was very weak to sell the change; some of what you gave above should definitely help strenthen it.

Likewise. Looking at the output, I'm confused how it would help with things like searching and refactoring. It might be nice to spell it out for those of us exposed to it for the first time (I tried following the links but remained unenlightened).

OK, I'll improve the commit message. I'm not at all an expert in this subject, I just had to generate a compilation database myself to use the Sourcetrail source explorer [1] with Git so I figured I'd share what I had done. Further exploration of the topic are in [2] and [3]. Note that I did try some of the tools listed in [2] before resorting to modifying the Makefile, but these tools either did not work at all or produced wrong output (ex. strings in the JSON were not properly quoted, etc.)

Quoted 3 lines

I'd also be curious to hear what advantages it gives to add a new Makefile knob rather than just letting interested parties add -MJ to their CFLAGS. Is it just a convenience to create the concatenated form?

Unfortunately this would not work because the '-MJ' flag needs a file name to know where to put the JSON fragment.

Thanks,
Philippe.

[1] www.sourcetrail.com
[2] https://sarcasm.github.io/notes/dev/compilation-database.html
[3] https://eli.thegreenplace.net/2014/05/21/compilation-databases-for-clang-based-tools

#6 lore raw
In reply to Jeff King
Quoted 4 lines · Jeff King

Likewise. Looking at the output, I'm confused how it would help with things like searching and refactoring. It might be nice to spell it out for those of us exposed to it for the first time (I tried following the links but remained unenlightened).

Traditionally, editors had to learn about every language if they wanted to add special functionality like refactoring (e.g., renaming "struct foo" to "struct bar"), finding all the instances of a type, finding where a type or function was declared, or similar IDE features. When Microsoft developed Visual Studio Code, they decided that they did not want to implement this functionality for every language under the sun, and instead developed the Language Server Protocol[0].

With LSP, each editor needs functionality to speak its portion (either natively, as with VS Code, or with a plugin, such as Vim's ALE) and each language implements a language server to implement its part of the functionality. The protocol is capability based, so implementations can support those features which make sense for their editor or language and omit those which don't. This way, all editors can benefit and language communities can implement one program to provide features, and the problem becomes an O(M + N) problem instead of an O(M * N) problem.

In some languages, like Rust, it's pretty obvious how to compile your
project: you use cargo, the built-in build tool. There is also a standard layout to find and enumerate files within a project. However, C is not so standardized, so clangd, which is a clang-based C and C++ LSP implementation, needs help to find out which flags are needed to compile, and therefore find the header files to make sense of parsing the C code and implementing its side of the protocol. That's what this patch implements.

I use Vim and ALE extensively, and it pretty much just works for most languages, including Go and Rust, once you install the LSP server. Git is one of the few projects I work on which is still C and therefore needs help here.

Hopefully this is at least more enlightening about the functionality that clangd provides, why it's interesting, how it works, and why it's valuable.

Quoted 6 lines

I'd also be curious to hear what advantages it gives to add a new Makefile knob rather than just letting interested parties add -MJ to their CFLAGS. Is it just a convenience to create the concatenated form? It seems weird that projects would need to do so themselves with sed hackery (i.e., I'd expect whatever consumes this json to be able to handle multiple files).

I believe clangd does need the concatenated form, and at least the ALE plugin for Vim uses that specific file name to detect whether clangd should be used. The problem is that clangd doesn't know where your source code is actually located and it's very expensive to traverse an entire repository which might contain literally millions of files if you're only really interested in a handful.

[0] https://microsoft.github.io/language-server-protocol/

Signature · 1 line
brian m. carlson: Houston, Texas, US
#7 lore raw
Quoted 8 lines · brian m. carlson

Traditionally, editors had to learn about every language if they wanted to add special functionality like refactoring (e.g., renaming "struct foo" to "struct bar"), finding all the instances of a type, finding where a type or function was declared, or similar IDE features. When Microsoft developed Visual Studio Code, they decided that they did not want to implement this functionality for every language under the sun, and instead developed the Language Server Protocol[0].
[...]

Thanks for the explanation. I understand what LSP does, but the missing link for me was how "here are the command-line flags to the compiler" turned into something useful like "here's a list of identifiers". And clangd fills in that gap (presumably re-running the front-end bits of clang on the fly to pull out that kind of information).

-Peff

#8 lore raw
In reply to Philippe Blain via GitGitGadget

Makefile all:: · line 470

Quoted 8 lines

#

Makefile +6 -0
+# Define GENERATE_COMPILATION_DATABASE to generate JSON compilation database
+# entries during compilation if your compiler supports it, using the `-MJ` flag.
+# The JSON entries will be placed in the `compile_commands/` directory,
+# and the JSON compilation database can be created afterwards with
+# `make compile_commands.json`.
+#

I'm realizing that the way I'm describing how it works here is wrong: there is no separate 'make compile_commands.json' step needed (it was needed in my first draft).

I'll fix that for v2.

#9 lore raw

PATCH v2 1/1 Makefile: add support for generating JSON compilation database

In reply to Philippe Blain via GitGitGadget

From: Philippe Blain <[email protected]>

Tools based on LibClang [1] can make use of a 'JSON Compilation Database' [2] that keeps track of the exact options used to compile a set of source files.

For example, clangd [3], which is a C language server protocol implementation, can use a JSON compilation database to determine the flags needed to compile a file so it can provide proper editor integration. As a result, editors supporting the language server protocol (such as VS Code, Emacs, or Vim, with suitable plugins) can provide better searching, integration, and refactoring tools.

The Clang compiler can generate JSON fragments when compiling [4], using the -MJ flag. These JSON fragments (one per compiled source file) can then be concatenated to create the compilation database, commonly called 'compile_commands.json'.

Add support to the Makefile for generating these JSON fragments as well as the compilation database itself, if the environment variable 'GENERATE_COMPILATION_DATABASE' is set.

If this variable is set, check that $(CC) indeed supports the -MJ flag, following what is done for automatic dependencies.

All JSON fragments are placed in the 'compile_commands/' directory, and the compilation database 'compile_commands.json' is generated as a dependency of the 'all' target using a sed invocation.

[1] https://clang.llvm.org/docs/Tooling.html
[2] https://clang.llvm.org/docs/JSONCompilationDatabase.html
[3] https://clangd.llvm.org/
[4] https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mj-arg

---
Add support for generating JSON compilation database

Changes since v1:

* Added a paragraph to the commit message to better explain why this feature would help Git developers (Thanks Brian!)
* Corrected the description of the new GENERATE_COMPILATION_DATABASE Makefile knob to make it reflect its actual behavior (cf.
https://lore.kernel.org/git/[email protected]/
)

v1: I don't have a lot of knowledge of Make double-colon rules, or insight into why they are used for the 'all' target, but I think the approach I chose makes sense. In particular, I do not list any prerequisite for the 'compile_commands.json' file, but from what I tested it is still rebuilt anytime the 'all' target is rebuilt, which is what we want.

Note: CMakeLists.txt in contrib/buildsystems does not need to be updated to also support this feature because CMake supports it out-of-the-box [1].

[1]
https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-714%2Fphil-blain%2Fcompiledb-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-714/phil-blain/compiledb-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/714

Range-diff vs v1:

1: 411fefaafb ! 1: da9544e4aa Makefile: add support for generating JSON compilation database
@@ Commit message
Database' [2] that keeps track of the exact options used to compile a set of source files.

- The Clang compiler can generate JSON fragments when compiling [3],
+ For example, clangd [3], which is a C language server protocol
+ implementation, can use a JSON compilation database to determine the
+ flags needed to compile a file so it can provide proper editor
+ integration. As a result, editors supporting the language server
+ protocol (such as VS Code, Emacs, or Vim, with suitable plugins) can
+ provide better searching, integration, and refactoring tools.
+
+ The Clang compiler can generate JSON fragments when compiling [4],
using the -MJ flag. These JSON fragments (one per compiled source file) can then be concatenated to create the compilation database, commonly called 'compile_commands.json'.
@@ Commit message

[1] https://clang.llvm.org/docs/Tooling.html
[2] https://clang.llvm.org/docs/JSONCompilationDatabase.html
- [3] https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mj-arg
+ [3] https://clangd.llvm.org/
+ [4] https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mj-arg

+ Helped-by: brian m. carlson <[email protected]>

## .gitignore ##
@@ Makefile: all::
+# Define GENERATE_COMPILATION_DATABASE to generate JSON compilation database +# entries during compilation if your compiler supports it, using the -MJ flag. +# The JSON entries will be placed in the compile_commands/ directory,
-+# and the JSON compilation database can be created afterwards with -+# make compile_commands.json.
++# and the JSON compilation database 'compile_commands.json' will be created ++# at the root of the repository.
+#
# Define DEVELOPER to enable more compiler warnings. Compiler version # and family are auto detected, but could be overridden by defining

 .gitignore |  2 ++
 Makefile   | 52 +++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 49 insertions(+), 5 deletions(-)
.gitignore +2 -0
index ee509a2ad2..f4c51300e0 100644
@@ -197,6 +197,7 @@
 /git.spec
 *.exe
 *.[aos]
+*.o.json
 *.py[co]
 .depend/
 *.gcda
@@ -218,6 +219,7 @@
 /tags
 /TAGS
 /cscope*
+/compile_commands.json
 *.hcc
 *.obj
 *.lib
Makefile +47 -5

88 lines not on this page · raw message

base-commit: d9cd4331470f4d9d78677f12dc79063dab832f53

Signature · 1 line
gitgitgadget
#10 lore raw

.gitignore line 200

Quoted 9 lines · Philippe Blain via GitGitGadget
.gitignore +1 -0
index ee509a2ad2..f4c51300e0 100644
@@ -197,6 +197,7 @@
 /git.spec
 *.exe
 *.[aos]
+*.o.json

Does this naming/suffix follow the common practice followed among those projects that use the -MJ option? Even though I may have heard of it, I am not familiar with the use of the feature at all, and to such a person, naming a file after what format it is written in (i.e. 'json') feels a lot less useful than what it contains (i.e. compilation database entries).

It's like naming our source files with .txt suffix ;-)

.gitignore

Quoted 5 lines
.gitignore +5 -0
+# Define GENERATE_COMPILATION_DATABASE to generate JSON compilation database
+# entries during compilation if your compiler supports it, using the `-MJ` flag.
+# The JSON entries will be placed in the `compile_commands/` directory,
+# and the JSON compilation database 'compile_commands.json' will be created 
+# at the root of the repository. 

Likewise for the name of the directory and the resulting single database file. I just want to make sure that we are following the common convention, so people familiar with the use of the feature would feel at home, so a simple answer "yes" would suffice.

.gitignore

Quoted 7 lines
.gitignore +7 -0
+ifdef GENERATE_COMPILATION_DATABASE
+compdb_check = $(shell $(CC) $(ALL_CFLAGS) \
+	-c -MJ /dev/null \
+	-x c /dev/null -o /dev/null 2>&1; \
+	echo $$?)
+ifeq ($(compdb_check),0)
+override GENERATE_COMPILATION_DATABASE = yes

This feels strange. If the end user said to GENERATE and we find we are capable, we still override to 'yes'? What if the end user set 'no' to the GENERATE_COMPILATION_DATABASE macro? Shouldn't we be honoring that wish?

.gitignore

Quoted 4 lines
.gitignore +4 -0
+else
+override GENERATE_COMPILATION_DATABASE = no
+$(warning GENERATE_COMPILATION_DATABASE is set, but your compiler does not \
+support generating compilation database entries)

This side is perfectly understandable and I think it is a valid use of override. But I do not understand the other side.

.gitignore missing_dep_dirs = · line 2411

Quoted 12 lines
.gitignore +8 -0
@@ -2381,16 +2401,30 @@ missing_dep_dirs =
 dep_args =
 endif
 
+compdb_dir = compile_commands/
+
+ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
+missing_compdb_dir = $(compdb_dir)
+$(missing_compdb_dir):
+	@mkdir -p $@
+
+compdb_file = $(compdb_dir)$(subst .-,,$(subst /,-,$(dir $@)))$(notdir $@).json

This detail does not matter as long as the end result ensures unique output for all source files, but I am having trouble guessing what the outermost subst, which removes ".-" sequence, is trying to make prettier. Care to explain?

.gitignore missing_dep_dirs = · line 2415

Quoted 4 lines
.gitignore +4 -0
+compdb_args = -MJ $(compdb_file)
+else
+missing_compdb_dir =
+compdb_args =

These are unfortunate. I briefly wondered if we can make GIT-CFLAGS depend on these two variables so that ASM_OBJ and C_OBJ do not have to depend on them, but the actual rules need to be updated anyway, so it cannot be helped.

I do wonder if we can unify dep_args and compdb_args into a single extra_args (and similarly dep_dirs and compdb_dir to extra_dirs) so that future similar options can all piggyback on it, but this can do for now.

.gitignore else · line 2454

Quoted 9 lines
.gitignore +5 -0
@@ -2413,6 +2447,14 @@ else
 $(OBJECTS): $(LIB_H) $(GENERATED_H)
 endif
 
+ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
+all:: compile_commands.json
+compile_commands.json:
+	@$(RM) $@
+	$(QUIET_GEN)sed -e '1s/^/[/' -e '$$s/,$$/]/' $(compdb_dir)*.o.json > $@+

OK. The entire thing is concatenated and enclosed by a single pair of '[' and ']'.

If we are planning to allow developers customize compdb_dir, requiring trailing slash in the value of $(compdb_dir) is not very friendly.

Thanks.

#11 lore raw

Hi Junio,

Quoted 21 lines · Junio C Hamano
Quoted 9 lines · Philippe Blain via GitGitGadget
.gitignore +0 -0
index ee509a2ad2..f4c51300e0 100644
@@ -197,6 +197,7 @@

/git.spec
*.exe
*.[aos]
+*.o.json

Does this naming/suffix follow the common practice followed among those projects that use the -MJ option? Even though I may have heard of it, I am not familiar with the use of the feature at all, and to such a person, naming a file after what format it is written in (i.e. 'json') feels a lot less useful than what it contains (i.e. compilation database entries).

Quoted 1 line

It's like naming our source files with .txt suffix ;-)

This addition to the .gitignore is for the individual JSON files (one per source file), that are placed in the $(compdb_dir).
I think naming "rebase.o.json" the JSON file that describes how to compile "rebase.c" into "rebase.o" makes sense. I don't know what is the convention for other projects.

Quoted 10 lines

.gitignore

Quoted 5 lines
.gitignore +5 -0
+# Define GENERATE_COMPILATION_DATABASE to generate JSON compilation database
+# entries during compilation if your compiler supports it, using the `-MJ` flag.
+# The JSON entries will be placed in the `compile_commands/` directory,
+# and the JSON compilation database 'compile_commands.json' will be created 
+# at the root of the repository. 

Likewise for the name of the directory and the resulting single database file. I just want to make sure that we are following the common convention, so people familiar with the use of the feature would feel at home, so a simple answer "yes" would suffice.

The name compile_commands.json for the database itself is standard. The name of the directory where the '*.o.json' files are placed is a name I chose, and I don't feel strongly about it. I thought it made sense to name it like that, then its purpose is clear. We could make it a hidden directory if we don't want to add a new folder to the root of the repo when using this feature.

Quoted 4 lines
Quoted 4 lines
.gitignore +4 -0
+ifdef GENERATE_COMPILATION_DATABASE
+compdb_check = $(shell $(CC) $(ALL_CFLAGS) \
+	-c -MJ /dev/null \
+	-x c /dev/null -o /dev/null 2>&1; \
Quoted 8 lines

.gitignore

Quoted 3 lines
.gitignore +3 -0
+	echo $$?)
+ifeq ($(compdb_check),0)
+override GENERATE_COMPILATION_DATABASE = yes

This feels strange. If the end user said to GENERATE and we find we are capable, we still override to 'yes'? What if the end user set 'no' to the GENERATE_COMPILATION_DATABASE macro? Shouldn't we be honoring that wish?

We should. I'll tweak (and simplify) that for v3.

Quoted 4 lines
Quoted 4 lines
.gitignore +0 -0
@@ -2381,16 +2401,30 @@ missing_dep_dirs =

dep_args =
endif

Quoted 13 lines

.gitignore

Quoted 8 lines
.gitignore +8 -0
+compdb_dir = compile_commands/
+
+ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
+missing_compdb_dir = $(compdb_dir)
+$(missing_compdb_dir):
+	@mkdir -p $@
+
+compdb_file = $(compdb_dir)$(subst .-,,$(subst /,-,$(dir $@)))$(notdir $@).json

This detail does not matter as long as the end result ensures unique output for all source files, but I am having trouble guessing what the outermost subst, which removes ".-" sequence, is trying to make prettier. Care to explain?

Yes, it is because the $(dir $@) Makefile function will return ./ for source files at the base of the repo, so the JSON files get named eg. .-rebase.o.json and then they are hidden. So it's just to make them non-hidden, so as not to confuse someone that would count the number of source files and compare with the number of (non-hidden)
'*.o.json' files in $(comdb_dir) and get a different number.

Quoted 4 lines
Quoted 4 lines
.gitignore +0 -0
@@ -2413,6 +2447,14 @@ else
 $(OBJECTS): $(LIB_H) $(GENERATED_H)

endif

Quoted 8 lines

.gitignore

Quoted 5 lines
.gitignore +5 -0
+ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
+all:: compile_commands.json
+compile_commands.json:
+	@$(RM) $@
+	$(QUIET_GEN)sed -e '1s/^/[/' -e '$$s/,$$/]/' $(compdb_dir)*.o.json > $@+

OK. The entire thing is concatenated and enclosed by a single pair of '[' and ']'.

Yes, and the comma after the last entry removed for JSON compliance.

Quoted 3 lines

If we are planning to allow developers customize compdb_dir, requiring trailing slash in the value of $(compdb_dir) is not very friendly.

OK I'll change that.

Quoted 1 line

Thanks.

Thank you for your comments!

Philippe.

#12 lore raw
Quoted 4 lines · Philippe Blain

This addition to the .gitignore is for the individual JSON files (one per source file), that are placed in the $(compdb_dir).
I think naming "rebase.o.json" the JSON file that describes how to compile "rebase.c" into "rebase.o" makes sense. I don't know what is the convention for other projects.

I agree rebase.o.$somesuffix does make sense, but I do not know 'json' is a great value for $somesuffix. I wouldn't be surprised if 'cdb' or some other silly abbreviation for "compilation database" is how other people use this feature.

Those watching from the sidelines. Does anybody know if there is an established convention used by other projects? If we hear nothing by early next week, let's declare 'json' is good enough and move on.

Quoted 5 lines

The name compile_commands.json for the database itself is standard. The name of the directory where the '*.o.json' files are placed is a name I chose, and I don't feel strongly about it. I thought it made sense to name it like that, then its purpose is clear. We could make it a hidden directory if we don't want to add a new folder to the root of the repo when using this feature.

I think both of these are sensible. Again if we hear nothing about common practice, let's move on with these constants as-is.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Makefile +4 -0
+ifdef GENERATE_COMPILATION_DATABASE
+compdb_check = $(shell $(CC) $(ALL_CFLAGS) \
+	-c -MJ /dev/null \
+	-x c /dev/null -o /dev/null 2>&1; \
Quoted 8 lines

Makefile $(error please set COMPUTE_HEADER_DEPENDENCIES to yes, no, or auto \ · line 1273

Quoted 3 lines
Makefile +3 -0
+	echo $$?)
+ifeq ($(compdb_check),0)
+override GENERATE_COMPILATION_DATABASE = yes

This feels strange. If the end user said to GENERATE and we find we are capable, we still override to 'yes'? What if the end user set 'no' to the GENERATE_COMPILATION_DATABASE macro? Shouldn't we be honoring that wish?

Quoted 1 line

We should. I'll tweak (and simplify) that for v3.

I think

- GENERATE_COMPILATION_DATABASE is set to 'no': don't even probe

- GENERATE_COMPILATION_DATABASE is set to 'yes': probe and turn it to 'no' if unavailable.

- GENERATE_COMPILATION_DATABASE is set to anything else: either error out, or turn it into 'no' (I have no preference between them).

would cover all the cases.

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

+compdb_file = $(compdb_dir)$(subst .-,,$(subst /,-,$(dir $@)))$(notdir $@).json

This detail does not matter as long as the end result ensures unique output for all source files, but I am having trouble guessing what the outermost subst, which removes ".-" sequence, is trying to make prettier. Care to explain?

Quoted 5 lines

Yes, it is because the $(dir $@) Makefile function will return ./ for source files at the base of the repo, so the JSON files get named eg. .-rebase.o.json and then they are hidden. So it's just to make them non-hidden, so as not to confuse someone that would count the number of source files and compare with the number of (non-hidden)
'*.o.json' files in $(comdb_dir) and get a different number.

Hmph. Would $(subst /,-,$@) instead of "only substitute leading directory part, and concatenate the basename part unmolested" work better then? After all, by definition the basename part would not have a slash in it, so substituting all '/' to '-' in the whole pathname should do the same thing and we won't have to worry about the spurious './', no?

#13 lore raw
Quoted 19 lines · Junio C Hamano
Quoted 4 lines · Philippe Blain

This addition to the .gitignore is for the individual JSON files (one per source file), that are placed in the $(compdb_dir).
I think naming "rebase.o.json" the JSON file that describes how to compile "rebase.c" into "rebase.o" makes sense. I don't know what is the convention for other projects.

I agree rebase.o.$somesuffix does make sense, but I do not know 'json' is a great value for $somesuffix. I wouldn't be surprised if 'cdb' or some other silly abbreviation for "compilation database" is how other people use this feature.

Those watching from the sidelines. Does anybody know if there is an established convention used by other projects? If we hear nothing by early next week, let's declare 'json' is good enough and move on.

Quoted 8 lines
Quoted 5 lines

The name compile_commands.json for the database itself is standard. The name of the directory where the '*.o.json' files are placed is a name I chose, and I don't feel strongly about it. I thought it made sense to name it like that, then its purpose is clear. We could make it a hidden directory if we don't want to add a new folder to the root of the repo when using this feature.

I think both of these are sensible. Again if we hear nothing about common practice, let's move on with these constants as-is.

OK.

Quoted 20 lines
Quoted 14 lines
Quoted 13 lines

Makefile $(error please set COMPUTE_HEADER_DEPENDENCIES to yes, no, or auto \ · line 1273

Quoted 8 lines
Makefile +7 -0
+ifdef GENERATE_COMPILATION_DATABASE
+compdb_check = $(shell $(CC) $(ALL_CFLAGS) \
+	-c -MJ /dev/null \
+	-x c /dev/null -o /dev/null 2>&1; \
+	echo $$?)
+ifeq ($(compdb_check),0)
+override GENERATE_COMPILATION_DATABASE = yes

This feels strange. If the end user said to GENERATE and we find we are capable, we still override to 'yes'? What if the end user set 'no' to the GENERATE_COMPILATION_DATABASE macro? Shouldn't we be honoring that wish?

Quoted 1 line

We should. I'll tweak (and simplify) that for v3.

I think

- GENERATE_COMPILATION_DATABASE is set to 'no': don't even probe

Quoted 8 lines

- GENERATE_COMPILATION_DATABASE is set to 'yes': probe and turn it to 'no' if unavailable.

- GENERATE_COMPILATION_DATABASE is set to anything else: either error out, or turn it into 'no' (I have no preference between them).

would cover all the cases.

I agree. I'll do that.

Quoted 14 lines
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

+compdb_file = $(compdb_dir)$(subst .-,,$(subst /,-,$(dir $@)))$(notdir $@).json

This detail does not matter as long as the end result ensures unique output for all source files, but I am having trouble guessing what the outermost subst, which removes ".-" sequence, is trying to make prettier. Care to explain?

Quoted 5 lines

Yes, it is because the $(dir $@) Makefile function will return ./ for source files at the base of the repo, so the JSON files get named eg. .-rebase.o.json and then they are hidden. So it's just to make them non-hidden, so as not to confuse someone that would count the number of source files and compare with the number of (non-hidden) '*.o.json' files in $(comdb_dir) and get a different number.

Quoted 6 lines

Hmph. Would $(subst /,-,$@) instead of "only substitute leading directory part, and concatenate the basename part unmolested" work better then? After all, by definition the basename part would not have a slash in it, so substituting all '/' to '-' in the whole pathname should do the same thing and we won't have to worry about the spurious './', no?

This indeed works, and reads better. Thanks!

#14 lore raw

PATCH v3 1/1 Makefile: add support for generating JSON compilation database

In reply to Philippe Blain via GitGitGadget

From: Philippe Blain <[email protected]>

Tools based on LibClang [1] can make use of a 'JSON Compilation Database' [2] that keeps track of the exact options used to compile a set of source files.

For example, clangd [3], which is a C language server protocol implementation, can use a JSON compilation database to determine the flags needed to compile a file so it can provide proper editor integration. As a result, editors supporting the language server protocol (such as VS Code, Emacs, or Vim, with suitable plugins) can provide better searching, integration, and refactoring tools.

The Clang compiler can generate JSON fragments when compiling [4], using the -MJ flag. These JSON fragments (one per compiled source file) can then be concatenated to create the compilation database, commonly called 'compile_commands.json'.

Add support to the Makefile for generating these JSON fragments as well as the compilation database itself, if the environment variable 'GENERATE_COMPILATION_DATABASE' is set.

If this variable is set, check that $(CC) indeed supports the -MJ flag, following what is done for automatic dependencies.

All JSON fragments are placed in the 'compile_commands/' directory, and the compilation database 'compile_commands.json' is generated as a dependency of the 'all' target using a sed invocation.

[1] https://clang.llvm.org/docs/Tooling.html
[2] https://clang.llvm.org/docs/JSONCompilationDatabase.html
[3] https://clangd.llvm.org/
[4] https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mj-arg

---
Add support for generating JSON compilation database

Changes since v2: Incorporated Junio's suggestions:

* improved the logic around values of the new GENERATE_COMPILATION_DATABASE macro
* simplified the Makefile functions used to generate the path of the JSON fragments
* removed the trailing slash after the default value of compdb_dir

Changes since v1:

* Added a paragraph to the commit message to better explain why this feature would help Git developers (Thanks Brian!)
* Corrected the description of the new GENERATE_COMPILATION_DATABASE Makefile knob to make it reflect its actual behavior (cf.
https://lore.kernel.org/git/[email protected]/
)

v1: I don't have a lot of knowledge of Make double-colon rules, or insight into why they are used for the 'all' target, but I think the approach I chose makes sense. In particular, I do not list any prerequisite for the 'compile_commands.json' file, but from what I tested it is still rebuilt anytime the 'all' target is rebuilt, which is what we want.

Note: CMakeLists.txt in contrib/buildsystems does not need to be updated to also support this feature because CMake supports it out-of-the-box [1].

[1]
https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-714%2Fphil-blain%2Fcompiledb-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-714/phil-blain/compiledb-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/714

Range-diff vs v2:

1: da9544e4aa ! 1: 1955b6b390 Makefile: add support for generating JSON compilation database

     @@ Makefile: all::
       # the global variable _wpgmptr containing the absolute path of the current
       # executable (this is the case on Windows).
       #
     -+# Define GENERATE_COMPILATION_DATABASE to generate JSON compilation database
     -+# entries during compilation if your compiler supports it, using the `-MJ` flag.
     -+# The JSON entries will be placed in the `compile_commands/` directory,
     -+# and the JSON compilation database 'compile_commands.json' will be created 
     -+# at the root of the repository. 
     ++# Define GENERATE_COMPILATION_DATABASE to "yes" to generate JSON compilation
     ++# database entries during compilation if your compiler supports it, using the
     ++# `-MJ` flag. The JSON entries will be placed in the `compile_commands/`
     ++# directory, and the JSON compilation database 'compile_commands.json' will be
     ++# created at the root of the repository.
      +#
       # Define DEVELOPER to enable more compiler warnings. Compiler version
       # and family are auto detected, but could be overridden by defining
     @@ Makefile: $(error please set COMPUTE_HEADER_DEPENDENCIES to yes, no, or auto \
       endif
       endif
       
     -+ifdef GENERATE_COMPILATION_DATABASE
     ++ifndef GENERATE_COMPILATION_DATABASE
     ++GENERATE_COMPILATION_DATABASE = no
     ++endif
     ++
     ++ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
      +compdb_check = $(shell $(CC) $(ALL_CFLAGS) \
      +	-c -MJ /dev/null \
      +	-x c /dev/null -o /dev/null 2>&1; \
      +	echo $$?)
     -+ifeq ($(compdb_check),0)
     -+override GENERATE_COMPILATION_DATABASE = yes
     -+else
     ++ifneq ($(compdb_check),0)
      +override GENERATE_COMPILATION_DATABASE = no
     -+$(warning GENERATE_COMPILATION_DATABASE is set, but your compiler does not \
     ++$(warning GENERATE_COMPILATION_DATABASE is set to "yes", but your compiler does not \
      +support generating compilation database entries)
      +endif
     ++else
     ++ifneq ($(GENERATE_COMPILATION_DATABASE),no)
     ++$(error please set GENERATE_COMPILATION_DATABASE to "yes" or "no" \
     ++(not "$(GENERATE_COMPILATION_DATABASE)"))
     ++endif
      +endif
      +
       ifdef SANE_TOOL_PATH
     @@ Makefile: missing_dep_dirs =
       dep_args =
       endif
       
     -+compdb_dir = compile_commands/
     ++compdb_dir = compile_commands
      +
      +ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
      +missing_compdb_dir = $(compdb_dir)
      +$(missing_compdb_dir):
      +	@mkdir -p $@
      +
     -+compdb_file = $(compdb_dir)$(subst .-,,$(subst /,-,$(dir $@)))$(notdir $@).json
     ++compdb_file = $(compdb_dir)/$(subst /,-,[email protected])
      +compdb_args = -MJ $(compdb_file)
      +else
      +missing_compdb_dir =
     @@ Makefile: else
      +all:: compile_commands.json
      +compile_commands.json:
      +	@$(RM) $@
     -+	$(QUIET_GEN)sed -e '1s/^/[/' -e '$$s/,$$/]/' $(compdb_dir)*.o.json > $@+
     ++	$(QUIET_GEN)sed -e '1s/^/[/' -e '$$s/,$$/]/' $(compdb_dir)/*.o.json > $@+
      +	@if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi
      +endif
      +
 .gitignore |  2 ++
 Makefile   | 59 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 56 insertions(+), 5 deletions(-)
.gitignore +2 -0
index ee509a2ad2..f4c51300e0 100644
@@ -197,6 +197,7 @@
 /git.spec
 *.exe
 *.[aos]
+*.o.json
 *.py[co]
 .depend/
 *.gcda
@@ -218,6 +219,7 @@
 /tags
 /TAGS
 /cscope*
+/compile_commands.json
 *.hcc
 *.obj
 *.lib
Makefile +54 -5

95 lines not on this page · raw message

base-commit: d9cd4331470f4d9d78677f12dc79063dab832f53

Signature · 1 line
gitgitgadget