From 39ea7da15bd4cb1ae9cb775e345430e9f7f6a1c6 Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Sun, 29 Jan 2023 12:44:29 -0800 Subject: [PATCH] c-api: add Wasmtime version macros to wasmtime.h * Add several `WASMTIME_VERSION_*` macros to `wasmtime.h`. * Update `scripts/publish.rs` * To set these macros as per the new version in `./Cargo.toml` during `./publish bump`. * To verify the macros match the version in `./Cargo.toml` during `./publish verify`. Fix #5635 --- crates/c-api/include/wasmtime.h | 6 ++++ scripts/publish.rs | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/crates/c-api/include/wasmtime.h b/crates/c-api/include/wasmtime.h index c70fd8b71339..864094132264 100644 --- a/crates/c-api/include/wasmtime.h +++ b/crates/c-api/include/wasmtime.h @@ -196,6 +196,12 @@ #include #include +// DO NOT EDIT - automatically generated by scripts/publish.rs +#define WASMTIME_VERSION "6.0.0" +#define WASMTIME_VERSION_MAJOR 6 +#define WASMTIME_VERSION_MINOR 0 +#define WASMTIME_VERSION_PATCH 0 + #ifdef __cplusplus extern "C" { #endif diff --git a/scripts/publish.rs b/scripts/publish.rs index 7e78c72c1649..fc2c6c8bf499 100644 --- a/scripts/publish.rs +++ b/scripts/publish.rs @@ -110,6 +110,8 @@ const PUBLIC_CRATES: &[&str] = &[ "wasmtime-types", ]; +const C_HEADER_PATH: &str = "./crates/c-api/include/wasmtime.h"; + struct Workspace { version: String, } @@ -144,6 +146,8 @@ fn main() { for krate in crates.iter() { bump_version(&krate, &crates, name == "bump-patch"); } + // update C API version in wasmtime.h + update_capi_version(); // update the lock file assert!(Command::new("cargo") .arg("fetch") @@ -335,6 +339,34 @@ fn bump_version(krate: &Crate, crates: &[Crate], patch: bool) { fs::write(&krate.manifest, new_manifest).unwrap(); } +fn update_capi_version() { + let version = read_crate(None, "./Cargo.toml".as_ref()).version; + + let mut iter = version.split('.').map(|s| s.parse::().unwrap()); + let major = iter.next().expect("major version"); + let minor = iter.next().expect("minor version"); + let patch = iter.next().expect("patch version"); + + let mut new_header = String::new(); + let contents = fs::read_to_string(C_HEADER_PATH).unwrap(); + for line in contents.lines() { + if line.starts_with("#define WASMTIME_VERSION \"") { + new_header.push_str(&format!("#define WASMTIME_VERSION \"{version}\"")); + } else if line.starts_with("#define WASMTIME_VERSION_MAJOR") { + new_header.push_str(&format!("#define WASMTIME_VERSION_MAJOR {major}")); + } else if line.starts_with("#define WASMTIME_VERSION_MINOR") { + new_header.push_str(&format!("#define WASMTIME_VERSION_MINOR {minor}")); + } else if line.starts_with("#define WASMTIME_VERSION_PATCH") { + new_header.push_str(&format!("#define WASMTIME_VERSION_PATCH {patch}")); + } else { + new_header.push_str(line); + } + new_header.push_str("\n"); + } + + fs::write(&C_HEADER_PATH, new_header).unwrap(); +} + /// Performs a major version bump increment on the semver version `version`. /// /// This function will perform a semver-major-version bump on the `version` @@ -439,6 +471,8 @@ fn publish(krate: &Crate) -> bool { // directory registry generated from `cargo vendor` because the versions // referenced from `Cargo.toml` may not exist on crates.io. fn verify(crates: &[Crate]) { + verify_capi(); + drop(fs::remove_dir_all(".cargo")); drop(fs::remove_dir_all("vendor")); let vendor = Command::new("cargo") @@ -500,4 +534,34 @@ fn verify(crates: &[Crate]) { ) .unwrap(); } + + fn verify_capi() { + let version = read_crate(None, "./Cargo.toml".as_ref()).version; + + let mut iter = version.split('.').map(|s| s.parse::().unwrap()); + let major = iter.next().expect("major version"); + let minor = iter.next().expect("minor version"); + let patch = iter.next().expect("patch version"); + + let mut count = 0; + let contents = fs::read_to_string(C_HEADER_PATH).unwrap(); + for line in contents.lines() { + if line.starts_with(&format!("#define WASMTIME_VERSION \"{version}\"")) { + count += 1; + } else if line.starts_with(&format!("#define WASMTIME_VERSION_MAJOR {major}")) { + count += 1; + } else if line.starts_with(&format!("#define WASMTIME_VERSION_MINOR {minor}")) { + count += 1; + } else if line.starts_with(&format!("#define WASMTIME_VERSION_PATCH {patch}")) { + count += 1; + } + } + + assert!( + count == 4, + "invalid version macros in {}, should match \"{}\"", + C_HEADER_PATH, + version + ); + } }