From: Adrian Freihofer <[email protected]>

The configurationProvider = "mesonbuild.mesonbuild" approach in
c_cpp_properties.json worked for meson recipes built with clang (the
__gen_meson_absolute_cross_file workaround absolutized the clang
binary path in an extra cross-file so the mesonbuild extension could
find it), but failed for the gcc toolchain: the extension could not
resolve the bare compiler name in the meson.cross file and silently
fell back to the host compiler for IntelliSense. The sysroot was also
absent, so built-in headers were resolved against the host system.

Replace the extension-based approach with explicit settings that work
for both gcc and clang toolchains:
  - compilerPath: absolute path to the cross CXX binary in
    staging_bindir_toolchain, so cpptools finds the right compiler
  - compilerArgs: ["--sysroot=<recipe_sysroot>"], so built-in
    includes are resolved against the target sysroot headers
  - compileCommands: path to compile_commands.json in the meson build
    directory B, which meson/ninja generates automatically with the
    exact per-file flags (include paths, defines, -march= etc.)
  - intelliSenseMode: derived from TARGET_ARCH and TOOLCHAIN
    (linux-gcc-arm64 / linux-clang-x64, etc.)

With compileCommands pointing at the real compilation database, the
mesonbuild extension is no longer needed for IntelliSense, so the
__gen_meson_absolute_cross_file workaround is also removed.

Signed-off-by: Adrian Freihofer <[email protected]>
---
 scripts/lib/devtool/ide_plugins/ide_code.py | 66 +++++++--------------
 1 file changed, 23 insertions(+), 43 deletions(-)

diff --git a/scripts/lib/devtool/ide_plugins/ide_code.py 
b/scripts/lib/devtool/ide_plugins/ide_code.py
index 9faba3f2d1..d190858d44 100644
--- a/scripts/lib/devtool/ide_plugins/ide_code.py
+++ b/scripts/lib/devtool/ide_plugins/ide_code.py
@@ -147,45 +147,6 @@ 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
@@ -193,11 +154,16 @@ 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
+        # Prevent the extension from writing 
C_Cpp.default.configurationProvider
+        # for itself once meson-info.json appears, which would take precedence
+        # over the compileCommands set in c_cpp_properties.json.
+        settings_dict["mesonbuild.modifySettings"] = False
+        # Clear any such value a previous run (or extension version without
+        # modifySettings support) already wrote to this workspace's settings.
+        settings_dict["C_Cpp.default.configurationProvider"] = ""
 
     def __vscode_settings_cmake(self, settings_dict, modified_recipe):
         """Add cmake specific settings to settings.json.
@@ -328,8 +294,22 @@ class IdeVSCode(IdeBase):
         if modified_recipe.build_tool is BuildTool.CMAKE:
             properties_dict["configurationProvider"] = "ms-vscode.cmake-tools"
         elif modified_recipe.build_tool is BuildTool.MESON:
-            properties_dict["configurationProvider"] = "mesonbuild.mesonbuild"
-            properties_dict["compilerPath"] = 
os.path.join(modified_recipe.staging_bindir_toolchain, 
modified_recipe.cxx.split()[0])
+            # configurationProvider = "mesonbuild.mesonbuild" does not work 
because
+            # the cross-compiler is not in PATH and omits the sysroot by 
default.
+            compiler_type = 'clang' if modified_recipe.toolchain == 'clang' 
else 'gcc'
+            arch_map = {
+                'aarch64': 'arm64', 'x86_64': 'x64', 'arm': 'arm',
+                'i686': 'x86', 'i586': 'x86', 'riscv64': 'x64', 'riscv32': 
'x86',
+            }
+            intelli_arch = arch_map.get(modified_recipe.target_arch, 'x64')
+            properties_dict["compilerPath"] = os.path.join(
+                modified_recipe.staging_bindir_toolchain,
+                modified_recipe.cxx.split()[0])
+            properties_dict["compilerArgs"] = [
+                "--sysroot=" + modified_recipe.recipe_sysroot]
+            properties_dict["compileCommands"] = os.path.join(
+                modified_recipe.b, 'compile_commands.json')
+            properties_dict["intelliSenseMode"] = "linux-%s-%s" % 
(compiler_type, intelli_arch)
         elif modified_recipe.build_tool is BuildTool.KERNEL_MODULE:
             # Using e.g. configurationProvider = "ms-vscode.makefile-tools" 
was not successful
             properties_dict["compilerPath"] = 
os.path.join(modified_recipe.staging_bindir_toolchain, 
modified_recipe.kernel_cc.split()[0])
-- 
2.55.0

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#243551): 
https://lists.openembedded.org/g/openembedded-core/message/243551
Mute This Topic: https://lists.openembedded.org/mt/120780723/21656
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub 
[[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to