Skip to content

Commit

Permalink
Work around the rules_rust bug
Browse files Browse the repository at this point in the history
This makes a duplicate of `versioned-export-macros` to be used
in build scripts in order to avoid the rules_rust bug:
bazelbuild/rules_rust#2210
  • Loading branch information
ulan committed Oct 27, 2023
1 parent fddcc98 commit 011d6a7
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 4 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ wasmtime-component-util = { path = "crates/component-util", version = "=13.0.1"
wasmtime-component-macro = { path = "crates/component-macro", version = "=13.0.1" }
wasmtime-asm-macros = { path = "crates/asm-macros", version = "=13.0.1" }
wasmtime-versioned-export-macros = { path = "crates/versioned-export-macros", version = "=13.0.1" }
wasmtime-versioned-export-macros-build = { path = "crates/versioned-export-macros-build", version = "=13.0.1" }
component-test-util = { path = "crates/misc/component-test-util" }
component-fuzz-util = { path = "crates/misc/component-fuzz-util" }
wiggle = { path = "crates/wiggle", version = "=13.0.1", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/fiber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ features = [

[build-dependencies]
cc = "1.0"
wasmtime-versioned-export-macros = { workspace = true }
wasmtime-versioned-export-macros-build = { workspace = true }

[dev-dependencies]
backtrace = "0.3.68"
2 changes: 1 addition & 1 deletion crates/fiber/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use wasmtime_versioned_export_macros::versioned_suffix;
use wasmtime_versioned_export_macros_build::versioned_suffix;

fn main() {
let mut build = cc::Build::new();
Expand Down
2 changes: 1 addition & 1 deletion crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ once_cell = { workspace = true }

[build-dependencies]
cc = "1.0"
wasmtime-versioned-export-macros = { workspace = true }
wasmtime-versioned-export-macros-build = { workspace = true }

[features]
async = ["wasmtime-fiber"]
Expand Down
2 changes: 1 addition & 1 deletion crates/runtime/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use wasmtime_versioned_export_macros::versioned_suffix;
use wasmtime_versioned_export_macros_build::versioned_suffix;

fn main() {
let mut build = cc::Build::new();
Expand Down
18 changes: 18 additions & 0 deletions crates/versioned-export-macros-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
authors = ["The Wasmtime Project Developers"]
description = "Macros for defining versioned exports in Wasmtime"
edition.workspace = true
license = "Apache-2.0 WITH LLVM-exception"
name = "wasmtime-versioned-export-macros-build"
repository = "https://github.com/bytecodealliance/wasmtime"
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
syn = { workspace = true, features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"

[lib]
proc-macro = true
66 changes: 66 additions & 0 deletions crates/versioned-export-macros-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! This crate defines macros to easily define and use functions with a
//! versioned suffix, to facilitate using multiple versions of the same
//! crate that generate assembly.
use quote::ToTokens;

const VERSION: &str = env!("CARGO_PKG_VERSION");

fn version(value: impl std::fmt::Display) -> String {
format!("{}_{}", value, VERSION.replace('.', "_"))
}

fn versioned_lit_str(value: impl std::fmt::Display) -> syn::LitStr {
syn::LitStr::new(version(value).as_str(), proc_macro2::Span::call_site())
}

#[proc_macro_attribute]
pub fn versioned_export(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let mut function = syn::parse_macro_input!(item as syn::ItemFn);

let export_name = versioned_lit_str(&function.sig.ident);
function
.attrs
.push(syn::parse_quote! { #[export_name = #export_name] });

function.to_token_stream().into()
}

#[proc_macro_attribute]
pub fn versioned_link(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let mut function = syn::parse_macro_input!(item as syn::ForeignItemFn);

let link_name = versioned_lit_str(&function.sig.ident);
function
.attrs
.push(syn::parse_quote! { #[link_name = #link_name] });

function.to_token_stream().into()
}

#[proc_macro]
pub fn versioned_stringify_ident(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ident = syn::parse_macro_input!(item as syn::Ident);

versioned_lit_str(ident).to_token_stream().into()
}

#[proc_macro]
pub fn versioned_suffix(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
if !item.is_empty() {
return syn::Error::new(
proc_macro2::Span::call_site(),
"`versioned_suffix!` accepts no input",
)
.to_compile_error()
.into();
};

versioned_lit_str("").to_token_stream().into()
}

0 comments on commit 011d6a7

Please sign in to comment.