diff mbox series

[meta-lts-mixins,wrynose/rust,03/10] rust: add rust-analyzer to the SDK behind a PACKAGECONFIG

Message ID e9f404fe137ab32bc8414bcf9c7342b3d920269a.1795997683.git.scott.murray@konsulko.com
State New
Headers show
Series Update to 1.100.0 | expand

Commit Message

Scott Murray Nov. 30, 2026, 12:52 a.m. UTC
From: Benhur Tessele <benhur.tessele@est.tech>

Add a rust-analyzer PACKAGECONFIG option which, when enabled, installs
it for the nativesdk variant and packages it as rust-tools-analyzer. The
option is disabled by default.

Two additional pieces are required for the tool to be usable:

 - The proc-macro server, rust-analyzer-proc-macro-srv, which
   rust-analyzer looks up under <sysroot>/libexec. Install it into
   ${libexecdir}; without it, analysis of any crate using a derive or
   attribute macro fails with "cannot find proc-macro server in sysroot".

 - The standard library sources, which rust-analyzer analyses from source
   rather than from the compiled rlibs. Add an RDEPENDS on ${PN}-src-lib
   so that core/std items resolve.

Both binaries link against librustc_driver from the main package, so the
package also RDEPENDS on ${PN}.

rust-analyzer is only installed for nativesdk, as a language server is
used on the SDK host rather than on a target device.

To get it into an SDK:

  SDK_FEATURES:append = " rust"
  PACKAGECONFIG:append:pn-nativesdk-rust = " rust-analyzer"
  TOOLCHAIN_HOST_TASK:append = " nativesdk-rust-tools-analyzer"

Tested by building an SDK with the option enabled and confirming
rust-analyzer resolves the standard library and expands proc-macros, and
by building with the option disabled to confirm the default configuration
is unaffected.

AI-Generated: Kiro with Claude Opus 5
Signed-off-by: Benhur Tessele <benhur.tessele@est.tech>
Signed-off-by: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
(adapted from oe-core commit bfeb93bd978c1f03947a90b137bb9237807cc842)
Signed-off-by: Scott Murray <scott.murray@konsulko.com>
---
 recipes-devtools/rust/rust_1.99.1.bb | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
diff mbox series

Patch

diff --git a/recipes-devtools/rust/rust_1.99.1.bb b/recipes-devtools/rust/rust_1.99.1.bb
index 4fa6f1a..5933578 100644
--- a/recipes-devtools/rust/rust_1.99.1.bb
+++ b/recipes-devtools/rust/rust_1.99.1.bb
@@ -24,6 +24,8 @@  RDEPENDS:${PN}:append:class-target = " gcc g++ binutils"
 
 PACKAGECONFIG ??= "llvm-shared"
 PACKAGECONFIG[llvm-shared] = ",,,"
+# Only used by the nativesdk variant; rust-analyzer runs on the SDK host
+PACKAGECONFIG[rust-analyzer] = ",,,"
 
 S = "${RUSTSRC}"
 # Separate build directory from the shared source tree so that multiple
@@ -282,17 +284,21 @@  do_test_compile () {
 ALLOW_EMPTY:${PN} = "1"
 
 PACKAGES =+ "${PN}-rustdoc ${PN}-tools-clippy ${PN}-tools-rustfmt ${PN}-src-lib"
+PACKAGES:prepend:class-nativesdk = "${@bb.utils.contains('PACKAGECONFIG', 'rust-analyzer', '${PN}-tools-analyzer ', '', d)}"
 FILES:${PN}-rustdoc = "${bindir}/rustdoc"
 FILES:${PN}-tools-clippy = "${bindir}/cargo-clippy ${bindir}/clippy-driver"
 FILES:${PN}-tools-rustfmt = "${bindir}/rustfmt"
+FILES:${PN}-tools-analyzer = "${bindir}/rust-analyzer ${libexecdir}/rust-analyzer-proc-macro-srv"
 FILES:${PN}-src-lib = "${libdir}/rustlib/src/rust"
 
 RDEPENDS:${PN}-rustdoc = "${PN}"
 RDEPENDS:${PN}-tools-clippy = "${PN}"
 RDEPENDS:${PN}-tools-rustfmt = "${PN}"
+RDEPENDS:${PN}-tools-analyzer = "${PN} ${PN}-src-lib"
 
 SUMMARY:${PN}-tools-clippy = "A collection of lints to catch common mistakes and improve your Rust code"
 SUMMARY:${PN}-tools-rustfmt = "A tool for formatting Rust code according to style guidelines"
+SUMMARY:${PN}-tools-analyzer = "A language server providing IDE features for Rust"
 
 do_install () {
     rust_do_install
@@ -314,6 +320,10 @@  rust_do_install:class-nativesdk() {
     rust_runx install
     rust_runx install clippy
     rust_runx install rustfmt
+    if ${@bb.utils.contains('PACKAGECONFIG', 'rust-analyzer', 'true', 'false', d)}; then
+        rust_runx install rust-analyzer
+        rust_runx build rust-analyzer-proc-macro-srv
+    fi
     unset PSEUDO_UNLOAD
 
     install -d ${D}${bindir}
@@ -322,6 +332,14 @@  rust_do_install:class-nativesdk() {
         patchelf --set-rpath "\$ORIGIN/../lib" ${D}${bindir}/$i
     done
 
+    if ${@bb.utils.contains('PACKAGECONFIG', 'rust-analyzer', 'true', 'false', d)}; then
+        install -d ${D}${libexecdir}
+        cp rust-build/${RUST_BUILD_SYS}/stage2-tools/${RUST_HOST_SYS}/release/rust-analyzer ${D}${bindir}
+        patchelf --set-rpath "\$ORIGIN/../lib" ${D}${bindir}/rust-analyzer
+        cp rust-build/${RUST_BUILD_SYS}/stage2-tools/${RUST_HOST_SYS}/release/rust-analyzer-proc-macro-srv ${D}${libexecdir}
+        patchelf --set-rpath "\$ORIGIN/../lib" ${D}${libexecdir}/rust-analyzer-proc-macro-srv
+    fi
+
     chown root:root ${D}/ -R
     rm ${D}${libdir}/rustlib/uninstall.sh
     rm ${D}${libdir}/rustlib/install.log