common.mak: add compdb Makefile target

Add a new Makefile target 'compdb' to create the compilation database
'compile_commands.json' [1]. This file can then be used by the language
server 'clangd' [2], which is a possible backend for the so called
'Language Server Protocol' (LSP) [3] used by many IDEs and editors.

In addition, add this file to `.gitignore`.

[1] https://clang.llvm.org/docs/JSONCompilationDatabase.html
[2] https://clangd.llvm.org/
[3] https://microsoft.github.io/language-server-protocol/

Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Marc Hartmayer
2023-07-20 15:01:37 +02:00
committed by Jan Höppner
parent 7e53611e3a
commit 3d098416c6
2 changed files with 26 additions and 0 deletions

4
.gitignore vendored
View File

@@ -9,6 +9,10 @@
tags tags
TAGS TAGS
# compile_commands.json
# (https://clang.llvm.org/docs/JSONCompilationDatabase.html)
compile_commands.json
# clangd cache (https://clangd.llvm.org/design/indexing#backgroundindex) # clangd cache (https://clangd.llvm.org/design/indexing#backgroundindex)
.cache/ .cache/

View File

@@ -360,6 +360,7 @@ help:
@echo ' all Build all tools (default target)' @echo ' all Build all tools (default target)'
@echo ' install Install tools' @echo ' install Install tools'
@echo ' clean Delete all generated files' @echo ' clean Delete all generated files'
@echo ' compdb Generate compile_commands.json for clangd'
@echo '' @echo ''
@echo 'OPTIONS' @echo 'OPTIONS'
@echo ' D=1 Build with debugging option "-Og"' @echo ' D=1 Build with debugging option "-Og"'
@@ -375,6 +376,27 @@ help:
@echo ' # make C=1 CHECK=smatch' @echo ' # make C=1 CHECK=smatch'
.PHONY: help .PHONY: help
# 'compile_commands.json' generation
#
# Create the compilation database 'compile_commands.json'. See
# https://clang.llvm.org/docs/JSONCompilationDatabase.html for details.
#
.PHONY: compdb
compdb:
$(MAKE) clean
ifneq ($(shell command -v compiledb),)
compiledb $(MAKE)
else ifneq ($(shell command -v bear),)
ifeq ($(shell bear --help|grep -- '-- ...'),)
bear $(MAKE)
else
bear -- $(MAKE)
endif
else
$(error Please install either 'compiledb' or 'bear')
endif
# Automatic dependency generation # Automatic dependency generation
# #
# Create ".o.d" dependency files with the -MM compile option for all ".c" and # Create ".o.d" dependency files with the -MM compile option for all ".c" and