Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c-api: add Wasmtime version macros to wasmtime.h #5651

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crates/c-api/include/wasmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@
#include <wasmtime/trap.h>
#include <wasmtime/val.h>

/**
* \brief Wasmtime version string.
*/
#define WASMTIME_VERSION "6.0.0"
/**
* \brief Wasmtime major version number.
*/
#define WASMTIME_VERSION_MAJOR 6
/**
* \brief Wasmtime minor version number.
*/
#define WASMTIME_VERSION_MINOR 0
/**
* \brief Wasmtime patch version number.
*/
#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
);
}
}