diff mbox series

[v2,4/5] meson: use absolute cross-compiler paths

Message ID 20240219165525.714512-5-enguerrand.de-ribaucourt@savoirfairelinux.com
State Accepted, archived
Commit b4e00248049c2627b05eafa9313a48cf253623fa
Headers show
Series devtool: ide: Improve VSCode support | expand

Commit Message

Enguerrand de Ribaucourt Feb. 19, 2024, 4:55 p.m. UTC
Among the files generated by meson is compile_commands.json. It is not
used by bitbake during the build. However, if the devtool workspace is
opened inside an IDE, that IDE can use compile_commands.json to
configure linting and code completion. This is notably relied on by the
new devtool ide-sdk command.

The problem is that the IDE using compile_commands.json does not know
the $PATH set-up by bitbake, so it won't find the compiler. This results
in linting errors, like missing headers. We can fix this by expliciting
the absolute compiler paths in meson.cross.

The compile_commands.json specification expressly states:
"All paths specified in the command or file fields must be either
absolute or relative to this directory."
Link: https://clang.llvm.org/docs/JSONCompilationDatabase.html

An alternative way to implement this is to directly change CXX inside
bitbake.conf to make all recipes use absolute compiler paths.Since this
would affect all recipes, so I would like to have the maintainers'
opinion on this. It could make sense to use absolute compiler paths for
all toolchain binaries, we already do so for the sysroot
TOOLCHAIN_OPTIONS.

Discussions have been opened with meson/ninja maintainers to implement
this at their level:
 - https://github.com/ninja-build/ninja/issues/2383
 - https://github.com/mesonbuild/meson/issues/12834
These tools have even less information on the environment so it makes
sense for Yocto to provide the absolute paths.

Signed-off-by: Enguerrand de Ribaucourt <enguerrand.de-ribaucourt@savoirfairelinux.com>
---
 meta/classes-recipe/meson-routines.bbclass | 6 ++++++
 meta/classes-recipe/meson.bbclass          | 7 +++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

Comments

Richard Purdie Feb. 20, 2024, 12:54 p.m. UTC | #1
On Mon, 2024-02-19 at 17:55 +0100, Enguerrand de Ribaucourt wrote:
> Among the files generated by meson is compile_commands.json. It is not
> used by bitbake during the build. However, if the devtool workspace is
> opened inside an IDE, that IDE can use compile_commands.json to
> configure linting and code completion. This is notably relied on by the
> new devtool ide-sdk command.
> 
> The problem is that the IDE using compile_commands.json does not know
> the $PATH set-up by bitbake, so it won't find the compiler. This results
> in linting errors, like missing headers. We can fix this by expliciting
> the absolute compiler paths in meson.cross.
> 
> The compile_commands.json specification expressly states:
> "All paths specified in the command or file fields must be either
> absolute or relative to this directory."
> Link: https://clang.llvm.org/docs/JSONCompilationDatabase.html
> 
> An alternative way to implement this is to directly change CXX inside
> bitbake.conf to make all recipes use absolute compiler paths.Since this
> would affect all recipes, so I would like to have the maintainers'
> opinion on this. It could make sense to use absolute compiler paths for
> all toolchain binaries, we already do so for the sysroot
> TOOLCHAIN_OPTIONS.

I'd prefer not to change CXX. 

We have a lot of problems with overflows of command line lengths
already and adding extra paths to such variables will make the problem
much worse. It also does make logs harder to read with long paths added
to them.

I suspect the readability issues along are enough to make this a change
for the worse overall.

Cheers,

Richard
diff mbox series

Patch

diff --git a/meta/classes-recipe/meson-routines.bbclass b/meta/classes-recipe/meson-routines.bbclass
index a944a8fff1c..9925465ed8f 100644
--- a/meta/classes-recipe/meson-routines.bbclass
+++ b/meta/classes-recipe/meson-routines.bbclass
@@ -10,6 +10,12 @@  def meson_array(var, d):
     items = d.getVar(var).split()
     return repr(items[0] if len(items) == 1 else items)
 
+def meson_array_abspath(var, d):
+    import shutil
+    items = d.getVar(var).split()
+    items[0] = shutil.which(items[0]) or items[0]
+    return repr(items[0] if len(items) == 1 else items)
+
 # Map our ARCH values to what Meson expects:
 # http://mesonbuild.com/Reference-tables.html#cpu-families
 def meson_cpu_family(var, d):
diff --git a/meta/classes-recipe/meson.bbclass b/meta/classes-recipe/meson.bbclass
index 03fa2c06eb4..31675cf42d1 100644
--- a/meta/classes-recipe/meson.bbclass
+++ b/meta/classes-recipe/meson.bbclass
@@ -64,10 +64,13 @@  addtask write_config before do_configure
 do_write_config[vardeps] += "CC CXX AR NM STRIP READELF OBJCOPY CFLAGS CXXFLAGS LDFLAGS RUSTC RUSTFLAGS EXEWRAPPER_ENABLED"
 do_write_config() {
     # This needs to be Py to split the args into single-element lists
+    # The generated compile_commands.json file can be used by external IDEs
+    # which do not know the $PATH set-up by bitbake. They need the absolute
+    # compiler paths.
     cat >${WORKDIR}/meson.cross <<EOF
 [binaries]
-c = ${@meson_array('CC', d)}
-cpp = ${@meson_array('CXX', d)}
+c = ${@meson_array_abspath('CC', d)}
+cpp = ${@meson_array_abspath('CXX', d)}
 cython = 'cython3'
 ar = ${@meson_array('AR', d)}
 nm = ${@meson_array('NM', d)}