[PATCH 04/22] rust: make build.rs generic over various ./rust/projects

marcandre.lureau@redhat.com posted 22 patches 1 month ago
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
[PATCH 04/22] rust: make build.rs generic over various ./rust/projects
Posted by marcandre.lureau@redhat.com 1 month ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Guess the name of the subdir from the manifest directory, instead of
hard-coding it. In the following commits, other crates can then link to
this file, instead of maintaining their own copy.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 rust/qemu-api/build.rs | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/rust/qemu-api/build.rs b/rust/qemu-api/build.rs
index 29d0945625..92237183ec 100644
--- a/rust/qemu-api/build.rs
+++ b/rust/qemu-api/build.rs
@@ -9,12 +9,14 @@
 use std::{env, fs::remove_file, io::Result, path::Path};
 
 fn main() -> Result<()> {
+    let manifest_dir = env!("CARGO_MANIFEST_DIR");
     let file = if let Ok(root) = env::var("MESON_BUILD_ROOT") {
-        format!("{root}/rust/qemu-api/bindings.inc.rs")
+        let sub = get_rust_subdir(manifest_dir).unwrap();
+        format!("{root}/{sub}/bindings.inc.rs")
     } else {
         // Placing bindings.inc.rs in the source directory is supported
         // but not documented or encouraged.
-        format!("{}/src/bindings.inc.rs", env!("CARGO_MANIFEST_DIR"))
+        format!("{}/src/bindings.inc.rs", manifest_dir)
     };
 
     let file = Path::new(&file);
@@ -41,3 +43,7 @@ fn main() -> Result<()> {
     println!("cargo:rerun-if-changed=build.rs");
     Ok(())
 }
+
+fn get_rust_subdir(path: &str) -> Option<&str> {
+    path.find("/rust").map(|index| &path[index + 1..])
+}
-- 
2.50.1


Re: [PATCH 04/22] rust: make build.rs generic over various ./rust/projects
Posted by Zhao Liu 1 month ago
On Wed, Aug 27, 2025 at 02:41:26PM +0400, marcandre.lureau@redhat.com wrote:
> Date: Wed, 27 Aug 2025 14:41:26 +0400
> From: marcandre.lureau@redhat.com
> Subject: [PATCH 04/22] rust: make build.rs generic over various
>  ./rust/projects
> 
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> Guess the name of the subdir from the manifest directory, instead of
> hard-coding it. In the following commits, other crates can then link to
> this file, instead of maintaining their own copy.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  rust/qemu-api/build.rs | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/rust/qemu-api/build.rs b/rust/qemu-api/build.rs
> index 29d0945625..92237183ec 100644
> --- a/rust/qemu-api/build.rs
> +++ b/rust/qemu-api/build.rs
> @@ -9,12 +9,14 @@
>  use std::{env, fs::remove_file, io::Result, path::Path};
>  
>  fn main() -> Result<()> {
> +    let manifest_dir = env!("CARGO_MANIFEST_DIR");
>      let file = if let Ok(root) = env::var("MESON_BUILD_ROOT") {
> -        format!("{root}/rust/qemu-api/bindings.inc.rs")
> +        let sub = get_rust_subdir(manifest_dir).unwrap();
> +        format!("{root}/{sub}/bindings.inc.rs")
>      } else {
>          // Placing bindings.inc.rs in the source directory is supported
>          // but not documented or encouraged.
> -        format!("{}/src/bindings.inc.rs", env!("CARGO_MANIFEST_DIR"))
> +        format!("{}/src/bindings.inc.rs", manifest_dir)

Only a nit: cargo clippy suggests this inline style:

format!("{manifest_dir}/src/bindings.inc.rs")

>      };
  
Re: [PATCH 04/22] rust: make build.rs generic over various ./rust/projects
Posted by Zhao Liu 1 month ago
On Wed, Aug 27, 2025 at 02:41:26PM +0400, marcandre.lureau@redhat.com wrote:
> Date: Wed, 27 Aug 2025 14:41:26 +0400
> From: marcandre.lureau@redhat.com
> Subject: [PATCH 04/22] rust: make build.rs generic over various
>  ./rust/projects
> 
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> Guess the name of the subdir from the manifest directory, instead of
> hard-coding it. In the following commits, other crates can then link to
> this file, instead of maintaining their own copy.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  rust/qemu-api/build.rs | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)> 

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>