wasmer_c_api/wasm_c_api/version.rs
1use std::os::raw::c_char;
2use std::sync::LazyLock;
3
4const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");
5const VERSION_PRE: &str = concat!(env!("CARGO_PKG_VERSION_PRE"), "\0");
6
7static VERSION_MAJOR: LazyLock<u8> = LazyLock::new(|| {
8 env!("CARGO_PKG_VERSION_MAJOR")
9 .parse()
10 .expect("Failed to parse value for `VERSION_MAJOR` from `CARGO_PKG_VERSION_MAJOR`")
11});
12static VERSION_MINOR: LazyLock<u8> = LazyLock::new(|| {
13 env!("CARGO_PKG_VERSION_MINOR")
14 .parse()
15 .expect("Failed to parse value for `VERSION_MINOR` from `CARGO_PKG_VERSION_MINOR`")
16});
17static VERSION_PATCH: LazyLock<u8> = LazyLock::new(|| {
18 env!("CARGO_PKG_VERSION_PATCH")
19 .parse()
20 .expect("Failed to parse value for `VERSION_PATCH` from `CARGO_PKG_VERSION_PATCH`")
21});
22
23/// Get the version of the Wasmer C API.
24///
25/// The `.h` files already define variables like `WASMER_VERSION*`,
26/// but if this file is unreachable, one can use this function to
27/// retrieve the full semver version of the Wasmer C API.
28///
29/// The returned string is statically allocated. It must _not_ be
30/// freed!
31///
32/// # Example
33///
34/// See the module's documentation.
35#[unsafe(no_mangle)]
36pub unsafe extern "C" fn wasmer_version() -> *const c_char {
37 VERSION.as_ptr() as *const _
38}
39
40/// Get the major version of the Wasmer C API.
41///
42/// See [`wasmer_version`] to learn more.
43///
44/// # Example
45///
46/// ```rust
47/// # use wasmer_inline_c::assert_c;
48/// # fn main() {
49/// # (assert_c! {
50/// # #include "tests/wasmer.h"
51/// #
52/// int main() {
53/// // Get and print the version components.
54/// uint8_t version_major = wasmer_version_major();
55/// uint8_t version_minor = wasmer_version_minor();
56/// uint8_t version_patch = wasmer_version_patch();
57///
58/// printf("%d.%d.%d", version_major, version_minor, version_patch);
59///
60/// return 0;
61/// }
62/// # })
63/// # .success()
64/// # .stdout(
65/// # format!(
66/// # "{}.{}.{}",
67/// # env!("CARGO_PKG_VERSION_MAJOR"),
68/// # env!("CARGO_PKG_VERSION_MINOR"),
69/// # env!("CARGO_PKG_VERSION_PATCH")
70/// # )
71/// # );
72/// # }
73/// ```
74#[unsafe(no_mangle)]
75pub unsafe extern "C" fn wasmer_version_major() -> u8 {
76 *VERSION_MAJOR
77}
78
79/// Get the minor version of the Wasmer C API.
80///
81/// See [`wasmer_version_major`] to learn more and get an example.
82#[unsafe(no_mangle)]
83pub unsafe extern "C" fn wasmer_version_minor() -> u8 {
84 *VERSION_MINOR
85}
86
87/// Get the patch version of the Wasmer C API.
88///
89/// See [`wasmer_version_major`] to learn more and get an example.
90#[unsafe(no_mangle)]
91pub unsafe extern "C" fn wasmer_version_patch() -> u8 {
92 *VERSION_PATCH
93}
94
95/// Get the minor version of the Wasmer C API.
96///
97/// See [`wasmer_version_major`] to learn more.
98///
99/// The returned string is statically allocated. It must _not_ be
100/// freed!
101///
102/// # Example
103///
104/// ```rust
105/// # use wasmer_inline_c::assert_c;
106/// # fn main() {
107/// # (assert_c! {
108/// # #include "tests/wasmer.h"
109/// #
110/// int main() {
111/// // Get and print the pre version.
112/// const char* version_pre = wasmer_version_pre();
113/// printf("%s", version_pre);
114///
115/// // No need to free the string. It's statically allocated on
116/// // the Rust side.
117///
118/// return 0;
119/// }
120/// # })
121/// # .success()
122/// # .stdout(env!("CARGO_PKG_VERSION_PRE"));
123/// # }
124/// ```
125#[unsafe(no_mangle)]
126pub unsafe extern "C" fn wasmer_version_pre() -> *const c_char {
127 VERSION_PRE.as_ptr() as *const _
128}