diff mbox series

[05/14] devtool: ide-sdk: fix meson compile_commands.json

Message ID 20260802195324.64533-6-adrian.freihofer@siemens.com
State New
Headers show
Series devtool ide-sdk: clang and lldb support | expand

Commit Message

AdrianF Aug. 2, 2026, 7:52 p.m. UTC
From: Adrian Freihofer <adrian.freihofer@siemens.com>

The generated meson.cross references the toolchain c/c++ binaries by
bare name (e.g. "aarch64-poky-linux-clang++"), relying on PATH being
set up by the meson wrapper script at build time. Meson stores that
command exactly as configured in compile_commands.json and
meson-info/intro-compilers.json without resolving it to an absolute
path. cpptools (via the mesonbuild extension) resolves compilerPath
using its own process PATH, which does not include the toolchain
directory, and silently falls back to a host compiler, breaking
IntelliSense.

Real builds are unaffected since the wrapper script sets up PATH at
build time, so leave the recipe's meson.cross untouched. Instead,
layer an extra --cross-file on top that only absolutizes the c/cpp
[binaries] entries, reusing the exact same flags as CC/CXX to avoid
any behavior drift. Meson merges multiple machine files, with later
files overriding matching keys from earlier ones.

Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
---
 scripts/lib/devtool/ide_plugins/ide_code.py | 41 +++++++++++++++++++++
 scripts/lib/devtool/ide_sdk.py              |  1 +
 2 files changed, 42 insertions(+)
diff mbox series

Patch

diff --git a/scripts/lib/devtool/ide_plugins/ide_code.py b/scripts/lib/devtool/ide_plugins/ide_code.py
index 96f007ca91..38f73e355c 100644
--- a/scripts/lib/devtool/ide_plugins/ide_code.py
+++ b/scripts/lib/devtool/ide_plugins/ide_code.py
@@ -128,6 +128,45 @@  class IdeVSCode(IdeBase):
     def dot_code_dir(self, modified_recipe):
         return os.path.join(modified_recipe.srctree, '.vscode')
 
+    def __gen_meson_absolute_cross_file(self, modified_recipe):
+        """Generate an extra cross file overriding c/cpp binaries with absolute paths.
+
+        The recipe's generated meson.cross references the toolchain binaries
+        (e.g. "aarch64-poky-linux-clang++") by bare name, relying on PATH
+        being set up by the meson wrapper script at build time. Meson stores
+        that command exactly as configured (it never resolves it to an
+        absolute path itself), so it ends up unresolved in
+        compile_commands.json and meson-info/intro-compilers.json. cpptools
+        (via the mesonbuild extension) resolves compilerPath using its own
+        process PATH, which does not include the toolchain directory, and
+        silently falls back to a host compiler for IntelliSense.
+
+        Real builds are unaffected since the wrapper script sets up PATH, so
+        the recipe's meson.cross is left untouched. Instead, an extra
+        --cross-file is layered on top with only the c/cpp [binaries]
+        entries absolutized, reusing the exact same flags as CC/CXX to avoid
+        any behavior drift. Meson merges multiple machine files, with later
+        files overriding matching keys from earlier ones.
+        """
+        def absolutize(cmd):
+            args = cmd.split()
+            args[0] = os.path.join(
+                modified_recipe.staging_bindir_toolchain, args[0])
+            return repr(args)
+
+        lines = ["[binaries]"]
+        if modified_recipe.cc:
+            lines.append("c = %s" % absolutize(modified_recipe.cc))
+        if modified_recipe.cxx:
+            lines.append("cpp = %s" % absolutize(modified_recipe.cxx))
+
+        os.makedirs(modified_recipe.ide_sdk_dir, exist_ok=True)
+        cross_file = os.path.join(
+            modified_recipe.ide_sdk_dir, 'meson-absolute-toolchain.cross')
+        with open(cross_file, 'w') as f:
+            f.write(os.linesep.join(lines) + os.linesep)
+        return ['--cross-file', cross_file]
+
     def __vscode_settings_meson(self, settings_dict, modified_recipe):
         if modified_recipe.build_tool is not BuildTool.MESON:
             return
@@ -135,6 +174,8 @@  class IdeVSCode(IdeBase):
 
         confopts = modified_recipe.mesonopts.split()
         confopts += modified_recipe.meson_cross_file.split()
+        if modified_recipe.meson_cross_file:
+            confopts += self.__gen_meson_absolute_cross_file(modified_recipe)
         confopts += modified_recipe.extra_oemeson.split()
         settings_dict["mesonbuild.configureOptions"] = confopts
         settings_dict["mesonbuild.buildFolder"] = modified_recipe.b
diff --git a/scripts/lib/devtool/ide_sdk.py b/scripts/lib/devtool/ide_sdk.py
index 940bb8211e..69b002b635 100755
--- a/scripts/lib/devtool/ide_sdk.py
+++ b/scripts/lib/devtool/ide_sdk.py
@@ -478,6 +478,7 @@  class RecipeModified:
         self.bblayers = recipe_d.getVar('BBLAYERS').split()
         self.bitbakepath = recipe_d.getVar('BITBAKEPATH')
         self.bpn = recipe_d.getVar('BPN')
+        self.cc = recipe_d.getVar('CC')
         self.cxx = recipe_d.getVar('CXX')
         self.d = recipe_d.getVar('D')
         self.debug_build = recipe_d.getVar('DEBUG_BUILD')