From 3d098416c6888eb0bc3218843b1bc88362eee6b0 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Thu, 20 Jul 2023 15:01:37 +0200 Subject: [PATCH] common.mak: add `compdb` Makefile target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Marc Hartmayer Signed-off-by: Jan Höppner --- .gitignore | 4 ++++ common.mak | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/.gitignore b/.gitignore index 42a2a3f5..bda2ccda 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,10 @@ tags TAGS +# compile_commands.json +# (https://clang.llvm.org/docs/JSONCompilationDatabase.html) +compile_commands.json + # clangd cache (https://clangd.llvm.org/design/indexing#backgroundindex) .cache/ diff --git a/common.mak b/common.mak index b72eae41..e1b1814d 100644 --- a/common.mak +++ b/common.mak @@ -360,6 +360,7 @@ help: @echo ' all Build all tools (default target)' @echo ' install Install tools' @echo ' clean Delete all generated files' + @echo ' compdb Generate compile_commands.json for clangd' @echo '' @echo 'OPTIONS' @echo ' D=1 Build with debugging option "-Og"' @@ -375,6 +376,27 @@ help: @echo ' # make C=1 CHECK=smatch' .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 # # Create ".o.d" dependency files with the -MM compile option for all ".c" and