From c93d1647855b9cae55ef1686ebb2c833685fce85 Mon Sep 17 00:00:00 2001 From: hedger Date: Sat, 1 Jun 2024 16:20:51 +0400 Subject: [PATCH] fbt, vscode: tweaks for cdb generation for clangd (#3680) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fbt, vscode: tweaks for cdb generation for clangd * .clangd: updated config * vscode: disabled auto header insertion for clangd * .clangd: updated config, ignoring format warnings * Add sublime text project * vscode: enabled clang-tidy for clangd * clangd: strict include checks for sources only Co-authored-by: あく --- .clangd | 13 ++++++++++++ .gitignore | 3 +++ .sublime-project | 21 ++++++++++++++++++ .vscode/example/settings.json | 4 +++- firmware.scons | 1 + scripts/fbt_tools/ccache.py | 1 + scripts/fbt_tools/compilation_db.py | 33 ++++++++++++++++++----------- scripts/ufbt/SConstruct | 1 + 8 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 .clangd create mode 100644 .sublime-project diff --git a/.clangd b/.clangd new file mode 100644 index 000000000..3e0024e8c --- /dev/null +++ b/.clangd @@ -0,0 +1,13 @@ +CompileFlags: + Add: + - -Wno-unknown-warning-option + - -Wno-format + Remove: + - -mword-relocations + +--- + +If: + PathMatch: .*\.h +Diagnostics: + UnusedIncludes: None diff --git a/.gitignore b/.gitignore index d60dcec3f..56c836338 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,9 @@ compile_commands.json # JetBrains IDEs .idea/ +# Sublime Text +.sublime-project.sublime-workspace + # Python VirtEnvironments .env .venv diff --git a/.sublime-project b/.sublime-project new file mode 100644 index 000000000..4912c9974 --- /dev/null +++ b/.sublime-project @@ -0,0 +1,21 @@ +{ + "folders": + [ + { + "path": ".", + } + ], + "settings": { + "LSP": { + "clangd": { + "initializationOptions": { + "clangd.compile-commands-dir": "build/latest", + "clangd.header-insertion": null, + "clangd.query-driver": "**", + "clangd.clang-tidy": true, + }, + "enabled": true, + }, + }, + }, +} diff --git a/.vscode/example/settings.json b/.vscode/example/settings.json index a59194901..9afabf926 100644 --- a/.vscode/example/settings.json +++ b/.vscode/example/settings.json @@ -19,6 +19,8 @@ "clangd.arguments": [ // We might be able to tighten this a bit more to only include the correct toolchain. "--query-driver=**", - "--compile-commands-dir=${workspaceFolder}/build/latest" + "--compile-commands-dir=${workspaceFolder}/build/latest", + "--clang-tidy", + "--header-insertion=never" ] } \ No newline at end of file diff --git a/firmware.scons b/firmware.scons index bf3f46a9b..62b1184eb 100644 --- a/firmware.scons +++ b/firmware.scons @@ -20,6 +20,7 @@ env = ENV.Clone( "fbt_resources", ], COMPILATIONDB_USE_ABSPATH=False, + COMPILATIONDB_USE_BINARY_ABSPATH=True, BUILD_DIR=fw_build_meta["build_dir"], IS_BASE_FIRMWARE=fw_build_meta["type"] == "firmware", FW_FLAVOR=fw_build_meta["flavor"], diff --git a/scripts/fbt_tools/ccache.py b/scripts/fbt_tools/ccache.py index 63577ab78..a7e546422 100644 --- a/scripts/fbt_tools/ccache.py +++ b/scripts/fbt_tools/ccache.py @@ -12,3 +12,4 @@ def generate(env): env["LINK"] = env["CXX"] env["CXX_NOCACHE"] = env["CXX"] env["CXX"] = "$CCACHE $CXX_NOCACHE" + env.AppendUnique(COMPILATIONDB_OMIT_BINARIES=["ccache"]) diff --git a/scripts/fbt_tools/compilation_db.py b/scripts/fbt_tools/compilation_db.py index 3d5e469f4..6bad96b2d 100644 --- a/scripts/fbt_tools/compilation_db.py +++ b/scripts/fbt_tools/compilation_db.py @@ -32,7 +32,7 @@ which is the name that most clang tools search for by default. import fnmatch import itertools import json -from shlex import quote +from shlex import join, split import SCons from SCons.Tool.asm import ASPPSuffixes, ASSuffixes @@ -108,6 +108,10 @@ def make_emit_compilation_DB_entry(comstr): return emit_compilation_db_entry +def __is_value_true(value): + return value in [True, 1, "True", "true"] + + def compilation_db_entry_action(target, source, env, **kw): """ Create a dictionary with evaluated command line, target, source @@ -126,16 +130,19 @@ def compilation_db_entry_action(target, source, env, **kw): env=env["__COMPILATIONDB_ENV"], ) - # We assume first non-space character is the executable - executable = command.split(" ", 1)[0] - if not (tool_path := _TOOL_PATH_CACHE.get(executable, None)): - tool_path = env.WhereIs(executable) or executable - _TOOL_PATH_CACHE[executable] = tool_path - # If there are spaces in the executable path, we need to quote it - if " " in tool_path: - tool_path = quote(tool_path) - # Replacing the executable with the full path - command = tool_path + command[len(executable) :] + cmdline = split(command) + binaries_to_omit = env["COMPILATIONDB_OMIT_BINARIES"] + while (executable := cmdline[0]) in binaries_to_omit: + cmdline.pop(0) + + if __is_value_true(env["COMPILATIONDB_USE_BINARY_ABSPATH"]): + if not (tool_path := _TOOL_PATH_CACHE.get(executable, None)): + tool_path = env.WhereIs(executable) or executable + _TOOL_PATH_CACHE[executable] = tool_path + # Replacing the executable with the full path + executable = tool_path + + command = join((executable, *cmdline[1:])) entry = { "directory": env.Dir("#").abspath, @@ -150,7 +157,7 @@ def compilation_db_entry_action(target, source, env, **kw): def write_compilation_db(target, source, env): entries = [] - use_abspath = env["COMPILATIONDB_USE_ABSPATH"] in [True, 1, "True", "true"] + use_abspath = __is_value_true(env["COMPILATIONDB_USE_ABSPATH"]) use_path_filter = env.subst("$COMPILATIONDB_PATH_FILTER") use_srcpath_filter = env.subst("$COMPILATIONDB_SRCPATH_FILTER") @@ -225,6 +232,8 @@ def generate(env, **kwargs): COMPILATIONDB_USE_ABSPATH=False, COMPILATIONDB_PATH_FILTER="", COMPILATIONDB_SRCPATH_FILTER="", + COMPILATIONDB_OMIT_BINARIES=[], + COMPILATIONDB_USE_BINARY_ABSPATH=False, ) components_by_suffix = itertools.chain( diff --git a/scripts/ufbt/SConstruct b/scripts/ufbt/SConstruct index 784d66161..26b1046ee 100644 --- a/scripts/ufbt/SConstruct +++ b/scripts/ufbt/SConstruct @@ -102,6 +102,7 @@ env = core_env.Clone( core_env.subst("$SDK_DEFINITION"), load_version_only=True ).version, APPCHECK_COMSTR="\tAPPCHK\t${SOURCE}\n\t\tTarget: ${TARGET_HW}, API: ${UFBT_API_VERSION}", + COMPILATIONDB_USE_BINARY_ABSPATH=True, ) wrap_tempfile(env, "LINKCOM")