* [PATCH] Makefile: add support for generating JSON compilation database @ 2020-08-30 19:28 Philippe Blain via GitGitGadget 2020-08-30 22:10 ` brian m. carlson ` (2 more replies) 0 siblings, 3 replies; 15+ messages in thread From: Philippe Blain via GitGitGadget @ 2020-08-30 19:28 UTC (permalink / raw) To: git; +Cc: Philippe Blain, Philippe Blain From: Philippe Blain <levraiphilippeblain@gmail.com> 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 Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com> --- 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(-) diff --git a/.gitignore b/.gitignore index ee509a2ad2..f4c51300e0 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile index 65f8cfb236..954bd2aa47 100644 --- a/Makefile +++ b/Makefile @@ -462,6 +462,12 @@ 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 can be created afterwards with +# `make compile_commands.json`. +# # Define DEVELOPER to enable more compiler warnings. Compiler version # and family are auto detected, but could be overridden by defining # COMPILER_FEATURES (see config.mak.dev). You can still set @@ -1258,6 +1264,20 @@ $(error please set COMPUTE_HEADER_DEPENDENCIES to yes, no, or auto \ endif endif +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 +else +override GENERATE_COMPILATION_DATABASE = no +$(warning GENERATE_COMPILATION_DATABASE is set, but your compiler does not \ +support generating compilation database entries) +endif +endif + ifdef SANE_TOOL_PATH SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH)) BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix "$(SANE_TOOL_PATH_SQ)"|' @@ -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 +compdb_args = -MJ $(compdb_file) +else +missing_compdb_dir = +compdb_args = +endif + ASM_SRC := $(wildcard $(OBJECTS:o=S)) ASM_OBJ := $(ASM_SRC:S=o) C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS)) .SUFFIXES: -$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) - $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< -$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs) - $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< +$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) + $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< +$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) + $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< %.s: %.c GIT-CFLAGS FORCE $(QUIET_CC)$(CC) -o $@ -S $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< @@ -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 > $@+ + @if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi +endif + exec-cmd.sp exec-cmd.s exec-cmd.o: GIT-PREFIX exec-cmd.sp exec-cmd.s exec-cmd.o: EXTRA_CPPFLAGS = \ '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \ @@ -3117,7 +3159,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) $(TEST_PROGRAMS) $(RM) $(FUZZ_PROGRAMS) $(RM) $(HCC) - $(RM) -r bin-wrappers $(dep_dirs) + $(RM) -r bin-wrappers $(dep_dirs) $(compdb_dir) compile_commands.json $(RM) -r po/build/ $(RM) *.pyc *.pyo */*.pyc */*.pyo $(GENERATED_H) $(ETAGS_TARGET) tags cscope* $(RM) -r $(GIT_TARNAME) .doc-tmp-dir base-commit: d9cd4331470f4d9d78677f12dc79063dab832f53 -- gitgitgadget ^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH] Makefile: add support for generating JSON compilation database 2020-08-30 19:28 [PATCH] Makefile: add support for generating JSON compilation database Philippe Blain via GitGitGadget @ 2020-08-30 22:10 ` brian m. carlson 2020-08-31 2:37 ` Philippe Blain 2020-08-31 4:24 ` Junio C Hamano 2020-08-30 22:17 ` Philippe Blain 2020-09-01 23:09 ` [PATCH v2] " Philippe Blain via GitGitGadget 2 siblings, 2 replies; 15+ messages in thread From: brian m. carlson @ 2020-08-30 22:10 UTC (permalink / raw) To: Philippe Blain via GitGitGadget; +Cc: git, Philippe Blain [-- Attachment #1: Type: text/plain, Size: 1184 bytes --] On 2020-08-30 at 19:28:27, Philippe Blain via GitGitGadget wrote: > From: Philippe Blain <levraiphilippeblain@gmail.com> > > 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. > +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. -- brian m. carlson: Houston, Texas, US [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 263 bytes --] ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH] Makefile: add support for generating JSON compilation database 2020-08-30 22:10 ` brian m. carlson @ 2020-08-31 2:37 ` Philippe Blain 2020-08-31 4:24 ` Junio C Hamano 1 sibling, 0 replies; 15+ messages in thread From: Philippe Blain @ 2020-08-31 2:37 UTC (permalink / raw) To: brian m. carlson; +Cc: Philippe Blain via GitGitGadget, Git mailing list Hi Brian, > Le 30 août 2020 à 18:10, brian m. carlson <sandals@crustytoothpaste.net> a écrit : > > On 2020-08-30 at 19:28:27, Philippe Blain via GitGitGadget wrote: >> From: Philippe Blain <levraiphilippeblain@gmail.com> >> >> 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. Thanks! > >> +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 ']') ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH] Makefile: add support for generating JSON compilation database 2020-08-30 22:10 ` brian m. carlson 2020-08-31 2:37 ` Philippe Blain @ 2020-08-31 4:24 ` Junio C Hamano 2020-09-01 7:38 ` Jeff King 1 sibling, 1 reply; 15+ messages in thread From: Junio C Hamano @ 2020-08-31 4:24 UTC (permalink / raw) To: brian m. carlson; +Cc: Philippe Blain via GitGitGadget, git, Philippe Blain "brian m. carlson" <sandals@crustytoothpaste.net> writes: > On 2020-08-30 at 19:28:27, Philippe Blain via GitGitGadget wrote: >> From: Philippe Blain <levraiphilippeblain@gmail.com> >> >> 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. 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. ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH] Makefile: add support for generating JSON compilation database 2020-08-31 4:24 ` Junio C Hamano @ 2020-09-01 7:38 ` Jeff King 2020-09-01 13:18 ` Philippe Blain 2020-09-02 1:33 ` brian m. carlson 0 siblings, 2 replies; 15+ messages in thread From: Jeff King @ 2020-09-01 7:38 UTC (permalink / raw) To: Junio C Hamano Cc: brian m. carlson, Philippe Blain via GitGitGadget, git, Philippe Blain On Sun, Aug 30, 2020 at 09:24:03PM -0700, Junio C Hamano wrote: > "brian m. carlson" <sandals@crustytoothpaste.net> writes: > > > On 2020-08-30 at 19:28:27, Philippe Blain via GitGitGadget wrote: > >> From: Philippe Blain <levraiphilippeblain@gmail.com> > >> > >> 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. > > 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 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH] Makefile: add support for generating JSON compilation database 2020-09-01 7:38 ` Jeff King @ 2020-09-01 13:18 ` Philippe Blain 2020-09-02 1:33 ` brian m. carlson 1 sibling, 0 replies; 15+ messages in thread From: Philippe Blain @ 2020-09-01 13:18 UTC (permalink / raw) To: Jeff King Cc: Junio C Hamano, brian m. carlson, Philippe Blain via GitGitGadget, Git mailing list > Le 1 sept. 2020 à 03:38, Jeff King <peff@peff.net> a écrit : > > On Sun, Aug 30, 2020 at 09:24:03PM -0700, Junio C Hamano wrote: > >> "brian m. carlson" <sandals@crustytoothpaste.net> writes: >> >>> On 2020-08-30 at 19:28:27, Philippe Blain via GitGitGadget wrote: >>>> From: Philippe Blain <levraiphilippeblain@gmail.com> >>>> >>>> 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. >> >> 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.) > 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 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH] Makefile: add support for generating JSON compilation database 2020-09-01 7:38 ` Jeff King 2020-09-01 13:18 ` Philippe Blain @ 2020-09-02 1:33 ` brian m. carlson 2020-09-02 8:04 ` Jeff King 1 sibling, 1 reply; 15+ messages in thread From: brian m. carlson @ 2020-09-02 1:33 UTC (permalink / raw) To: Jeff King Cc: Junio C Hamano, Philippe Blain via GitGitGadget, git, Philippe Blain [-- Attachment #1: Type: text/plain, Size: 3162 bytes --] On 2020-09-01 at 07:38:27, Jeff King wrote: > 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. > 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/ -- brian m. carlson: Houston, Texas, US [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 263 bytes --] ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH] Makefile: add support for generating JSON compilation database 2020-09-02 1:33 ` brian m. carlson @ 2020-09-02 8:04 ` Jeff King 0 siblings, 0 replies; 15+ messages in thread From: Jeff King @ 2020-09-02 8:04 UTC (permalink / raw) To: brian m. carlson Cc: Junio C Hamano, Philippe Blain via GitGitGadget, git, Philippe Blain On Wed, Sep 02, 2020 at 01:33:51AM +0000, brian m. carlson wrote: > 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 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH] Makefile: add support for generating JSON compilation database 2020-08-30 19:28 [PATCH] Makefile: add support for generating JSON compilation database Philippe Blain via GitGitGadget 2020-08-30 22:10 ` brian m. carlson @ 2020-08-30 22:17 ` Philippe Blain 2020-09-01 23:09 ` [PATCH v2] " Philippe Blain via GitGitGadget 2 siblings, 0 replies; 15+ messages in thread From: Philippe Blain @ 2020-08-30 22:17 UTC (permalink / raw) To: Philippe Blain via GitGitGadget; +Cc: Git mailing list, brian m. carlson > # > +# 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. ^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v2] Makefile: add support for generating JSON compilation database 2020-08-30 19:28 [PATCH] Makefile: add support for generating JSON compilation database Philippe Blain via GitGitGadget 2020-08-30 22:10 ` brian m. carlson 2020-08-30 22:17 ` Philippe Blain @ 2020-09-01 23:09 ` Philippe Blain via GitGitGadget 2020-09-02 17:21 ` Junio C Hamano 2020-09-03 22:13 ` [PATCH v3] " Philippe Blain via GitGitGadget 2 siblings, 2 replies; 15+ messages in thread From: Philippe Blain via GitGitGadget @ 2020-09-01 23:09 UTC (permalink / raw) To: git Cc: brian m. carlson, Philippe Blain, Jeff King, Philippe Blain, Philippe Blain From: Philippe Blain <levraiphilippeblain@gmail.com> 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 Helped-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com> --- 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/FC95CFF7-F9DA-4CDA-9923-99C3432DCAD5@gmail.com/ ) 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 <sandals@crustytoothpaste.net> Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com> ## .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(-) diff --git a/.gitignore b/.gitignore index ee509a2ad2..f4c51300e0 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile index 65f8cfb236..51cd0f302b 100644 --- a/Makefile +++ b/Makefile @@ -462,6 +462,12 @@ 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 DEVELOPER to enable more compiler warnings. Compiler version # and family are auto detected, but could be overridden by defining # COMPILER_FEATURES (see config.mak.dev). You can still set @@ -1258,6 +1264,20 @@ $(error please set COMPUTE_HEADER_DEPENDENCIES to yes, no, or auto \ endif endif +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 +else +override GENERATE_COMPILATION_DATABASE = no +$(warning GENERATE_COMPILATION_DATABASE is set, but your compiler does not \ +support generating compilation database entries) +endif +endif + ifdef SANE_TOOL_PATH SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH)) BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix "$(SANE_TOOL_PATH_SQ)"|' @@ -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 +compdb_args = -MJ $(compdb_file) +else +missing_compdb_dir = +compdb_args = +endif + ASM_SRC := $(wildcard $(OBJECTS:o=S)) ASM_OBJ := $(ASM_SRC:S=o) C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS)) .SUFFIXES: -$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) - $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< -$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs) - $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< +$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) + $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< +$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) + $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< %.s: %.c GIT-CFLAGS FORCE $(QUIET_CC)$(CC) -o $@ -S $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< @@ -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 > $@+ + @if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi +endif + exec-cmd.sp exec-cmd.s exec-cmd.o: GIT-PREFIX exec-cmd.sp exec-cmd.s exec-cmd.o: EXTRA_CPPFLAGS = \ '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \ @@ -3117,7 +3159,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) $(TEST_PROGRAMS) $(RM) $(FUZZ_PROGRAMS) $(RM) $(HCC) - $(RM) -r bin-wrappers $(dep_dirs) + $(RM) -r bin-wrappers $(dep_dirs) $(compdb_dir) compile_commands.json $(RM) -r po/build/ $(RM) *.pyc *.pyo */*.pyc */*.pyo $(GENERATED_H) $(ETAGS_TARGET) tags cscope* $(RM) -r $(GIT_TARNAME) .doc-tmp-dir base-commit: d9cd4331470f4d9d78677f12dc79063dab832f53 -- gitgitgadget ^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH v2] Makefile: add support for generating JSON compilation database 2020-09-01 23:09 ` [PATCH v2] " Philippe Blain via GitGitGadget @ 2020-09-02 17:21 ` Junio C Hamano 2020-09-03 21:17 ` Philippe Blain 2020-09-03 22:13 ` [PATCH v3] " Philippe Blain via GitGitGadget 1 sibling, 1 reply; 15+ messages in thread From: Junio C Hamano @ 2020-09-02 17:21 UTC (permalink / raw) To: Philippe Blain via GitGitGadget Cc: git, brian m. carlson, Philippe Blain, Jeff King "Philippe Blain via GitGitGadget" <gitgitgadget@gmail.com> writes: > diff --git a/.gitignore b/.gitignore > index ee509a2ad2..f4c51300e0 100644 > --- a/.gitignore > +++ b/.gitignore > @@ -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 ;-) > +# 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. > +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? > +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. > @@ -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? > +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. > @@ -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. ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2] Makefile: add support for generating JSON compilation database 2020-09-02 17:21 ` Junio C Hamano @ 2020-09-03 21:17 ` Philippe Blain 2020-09-03 21:31 ` Junio C Hamano 0 siblings, 1 reply; 15+ messages in thread From: Philippe Blain @ 2020-09-03 21:17 UTC (permalink / raw) To: Junio C Hamano Cc: Philippe Blain via GitGitGadget, Git mailing list, brian m. carlson, Jeff King Hi Junio, > Le 2 sept. 2020 à 13:21, Junio C Hamano <gitster@pobox.com> a écrit : > > "Philippe Blain via GitGitGadget" <gitgitgadget@gmail.com> writes: > >> diff --git a/.gitignore b/.gitignore >> index ee509a2ad2..f4c51300e0 100644 >> --- a/.gitignore >> +++ b/.gitignore >> @@ -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 ;-) 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. >> +# 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. >> +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? We should. I'll tweak (and simplify) that for v3. >> @@ -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? 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. >> @@ -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 ']'. Yes, and the comma after the last entry removed for JSON compliance. > 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. > Thanks. Thank you for your comments! Philippe. ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2] Makefile: add support for generating JSON compilation database 2020-09-03 21:17 ` Philippe Blain @ 2020-09-03 21:31 ` Junio C Hamano 2020-09-03 22:04 ` Philippe Blain 0 siblings, 1 reply; 15+ messages in thread From: Junio C Hamano @ 2020-09-03 21:31 UTC (permalink / raw) To: Philippe Blain Cc: Philippe Blain via GitGitGadget, Git mailing list, brian m. carlson, Jeff King Philippe Blain <levraiphilippeblain@gmail.com> writes: > 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. > 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. >>> +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? > > 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. >>> +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. 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? ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2] Makefile: add support for generating JSON compilation database 2020-09-03 21:31 ` Junio C Hamano @ 2020-09-03 22:04 ` Philippe Blain 0 siblings, 0 replies; 15+ messages in thread From: Philippe Blain @ 2020-09-03 22:04 UTC (permalink / raw) To: Junio C Hamano Cc: Philippe Blain via GitGitGadget, Git mailing list, brian m. carlson, Jeff King > Le 3 sept. 2020 à 17:31, Junio C Hamano <gitster@pobox.com> a écrit : > > Philippe Blain <levraiphilippeblain@gmail.com> writes: > >> 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. > >> 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. > >>>> +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? >> >> 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. I agree. I'll do that. > >>>> +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. > > 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! ^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v3] Makefile: add support for generating JSON compilation database 2020-09-01 23:09 ` [PATCH v2] " Philippe Blain via GitGitGadget 2020-09-02 17:21 ` Junio C Hamano @ 2020-09-03 22:13 ` Philippe Blain via GitGitGadget 1 sibling, 0 replies; 15+ messages in thread From: Philippe Blain via GitGitGadget @ 2020-09-03 22:13 UTC (permalink / raw) To: git; +Cc: brian m. carlson, Jeff King, Philippe Blain, Philippe Blain From: Philippe Blain <levraiphilippeblain@gmail.com> 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 Helped-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com> --- 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/FC95CFF7-F9DA-4CDA-9923-99C3432DCAD5@gmail.com/ ) 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 /,-,$@.json) +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(-) diff --git a/.gitignore b/.gitignore index ee509a2ad2..f4c51300e0 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile index 65f8cfb236..39ca667689 100644 --- a/Makefile +++ b/Makefile @@ -462,6 +462,12 @@ all:: # the global variable _wpgmptr containing the absolute path of the current # executable (this is the case on Windows). # +# 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 # COMPILER_FEATURES (see config.mak.dev). You can still set @@ -1258,6 +1264,27 @@ $(error please set COMPUTE_HEADER_DEPENDENCIES to yes, no, or auto \ endif endif +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 $$?) +ifneq ($(compdb_check),0) +override GENERATE_COMPILATION_DATABASE = no +$(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 SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH)) BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix "$(SANE_TOOL_PATH_SQ)"|' @@ -2381,16 +2408,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 /,-,$@.json) +compdb_args = -MJ $(compdb_file) +else +missing_compdb_dir = +compdb_args = +endif + ASM_SRC := $(wildcard $(OBJECTS:o=S)) ASM_OBJ := $(ASM_SRC:S=o) C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS)) .SUFFIXES: -$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) - $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< -$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs) - $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< +$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) + $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< +$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir) + $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< %.s: %.c GIT-CFLAGS FORCE $(QUIET_CC)$(CC) -o $@ -S $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $< @@ -2413,6 +2454,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 > $@+ + @if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi +endif + exec-cmd.sp exec-cmd.s exec-cmd.o: GIT-PREFIX exec-cmd.sp exec-cmd.s exec-cmd.o: EXTRA_CPPFLAGS = \ '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \ @@ -3117,7 +3166,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) $(TEST_PROGRAMS) $(RM) $(FUZZ_PROGRAMS) $(RM) $(HCC) - $(RM) -r bin-wrappers $(dep_dirs) + $(RM) -r bin-wrappers $(dep_dirs) $(compdb_dir) compile_commands.json $(RM) -r po/build/ $(RM) *.pyc *.pyo */*.pyc */*.pyo $(GENERATED_H) $(ETAGS_TARGET) tags cscope* $(RM) -r $(GIT_TARNAME) .doc-tmp-dir base-commit: d9cd4331470f4d9d78677f12dc79063dab832f53 -- gitgitgadget ^ permalink raw reply related [flat|nested] 15+ messages in thread
end of thread, other threads:[~2020-09-03 22:13 UTC | newest] Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-08-30 19:28 [PATCH] Makefile: add support for generating JSON compilation database Philippe Blain via GitGitGadget 2020-08-30 22:10 ` brian m. carlson 2020-08-31 2:37 ` Philippe Blain 2020-08-31 4:24 ` Junio C Hamano 2020-09-01 7:38 ` Jeff King 2020-09-01 13:18 ` Philippe Blain 2020-09-02 1:33 ` brian m. carlson 2020-09-02 8:04 ` Jeff King 2020-08-30 22:17 ` Philippe Blain 2020-09-01 23:09 ` [PATCH v2] " Philippe Blain via GitGitGadget 2020-09-02 17:21 ` Junio C Hamano 2020-09-03 21:17 ` Philippe Blain 2020-09-03 21:31 ` Junio C Hamano 2020-09-03 22:04 ` Philippe Blain 2020-09-03 22:13 ` [PATCH v3] " Philippe Blain via GitGitGadget
Code repositories for project(s) associated with this public inbox https://80x24.org/mirrors/git.git This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).