Skip to content

Conversation

@tomasol
Copy link

@tomasol tomasol commented Nov 30, 2025

Add RustWasm::generate_to_out_dir that parses the wit folder, selects a world, generates bindings and places them in a file in the OUT_DIR. generate_to_out_dir_modify allows modifying the generated file.

Sample build.rs

use anyhow::Result;
use std::borrow::Cow;
use wit_bindgen_rust::Opts;

fn main() -> Result<()> {
    Opts {
        generate_all: true,
        additional_derive_attributes: vec![
            "serde::Serialize".to_string(),
            "serde::Deserialize".to_string(),
        ],
        ..Default::default()
    }
    .build()
    .generate_to_out_dir_modify(Some("any"), |contents| {
        let contents = String::from_utf8(contents.to_vec()).unwrap();
        let re = regex::Regex::new(r"(pub\s+enum\s+\w+)").unwrap();
        Cow::Owned(
            re.replace_all(&contents, "#[serde(rename_all = \"kebab-case\")]\n$1")
                .into_owned()
                .into_bytes(),
        )
    })
}

Then the following snipped would include it:

mod generated {
    include!(concat!(env!("OUT_DIR"), "/wit_bindgen_generated.rs"));
}

Although this should be simplified as well.

Closes #1423

Add `RustWasm::generate_to_out_dir` that parses the `wit` folder,
selects a world, generates bindings and places them in a file in the
`OUT_DIR`. `generate_to_out_dir_modify` allows modifying the generated
file.
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Was there anything else you'd like to address before merging?

@tomasol
Copy link
Author

tomasol commented Dec 11, 2025

Thanks for the quick review! Additionally I would like to add a macro to include the generated file:

#[cfg(feature = "macros")]
#[macro_export]
macro_rules! include_wit {
    ($name:literal) => {
        include!(concat!(env!("OUT_DIR"), "/", $name, ".rs"));
    };
}

so that the bindings can be included simply with wit_bindgen::include_wit!("myworld");.
Is it a good idea to add it to wit-bindgen ?

@alexcrichton
Copy link
Member

Personally I'd say that should be left out of the crate and documented in the usage of this method. I (subjectively) don't feel that clears the threshold for inclusion just yet, but if it becomes a common/frequent way of using wit-bindgen it seems ok to add.

@tomasol tomasol marked this pull request as ready for review December 11, 2025 13:02
@tomasol
Copy link
Author

tomasol commented Dec 11, 2025

Understood, in that case the PR is done.

@tschneidereit tschneidereit added this pull request to the merge queue Dec 11, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 11, 2025
@tschneidereit
Copy link
Member

Looks like an intermittent failure, so let's try again

@tschneidereit tschneidereit added this pull request to the merge queue Dec 11, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 11, 2025
@tschneidereit tschneidereit added this pull request to the merge queue Dec 11, 2025
github-merge-queue bot pushed a commit that referenced this pull request Dec 11, 2025
* Add new functions to generate rust bindings in `build.rs`

Add `RustWasm::generate_to_out_dir` that parses the `wit` folder,
selects a world, generates bindings and places them in a file in the
`OUT_DIR`. `generate_to_out_dir_modify` allows modifying the generated
file.

* Use world name as output path in `generate_to_out_dir`
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 11, 2025
@tschneidereit tschneidereit added this pull request to the merge queue Dec 11, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 11, 2025
@tschneidereit
Copy link
Member

Sigh, I think Github infra might be having a bad day. I'll try again tomorrow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rust: Generating bindings in build.rs

3 participants