Skip to content

Commit

Permalink
c-api: add Wasmtime version macros to wasmtime.h
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
thibaultcha committed Jan 29, 2023
1 parent 20a2169 commit 39ea7da
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/c-api/include/wasmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@
#include <wasmtime/trap.h>
#include <wasmtime/val.h>

// 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
Expand Down
64 changes: 64 additions & 0 deletions scripts/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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::<u32>().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`
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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::<u32>().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
);
}
}

0 comments on commit 39ea7da

Please sign in to comment.