-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1916: feat(c-api) Add the `WASMER_VERSION*` constants, and the `wasmer_version*` functions r=Hywan a=Hywan Fix #1534. # Description ~This PR adds the `wasmer_version`. It compute a string based on the `CARGO_PKG_VERSION` environment variable value at compile-time. I know an enum could be better, but it's a start.~ This PR adds 4 C constants: ```c #define WASMER_VERSION "1.0.0-beta1" #define WASMER_VERSION_MAJOR 1 #define WASMER_VERSION_MINOR 0 #define WASMER_VERSION_PATCH 0 #define WASMER_VERSION_PRE "beta1" ``` Thay way, the user can query the version of Wasmer more easily. In addition, it adds 4 functions (when the header files aren't accessible): ```c const char* wasmer_version(void); uint8_t wasmer_version_major(void); uint8_t wasmer_version_minor(void); uint8_t wasmer_version_patch(void); const char* wasmer_version_pre(void); ``` # Review - [x] Add a short description of the the change to the CHANGELOG.md file Co-authored-by: Ivan Enderlin <[email protected]>
- Loading branch information
Showing
7 changed files
with
564 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
use lazy_static::lazy_static; | ||
use std::os::raw::c_char; | ||
|
||
const VERSION: &'static str = concat!(env!("CARGO_PKG_VERSION"), "\0"); | ||
const VERSION_PRE: &'static str = concat!(env!("CARGO_PKG_VERSION_PRE"), "\0"); | ||
|
||
lazy_static! { | ||
static ref VERSION_MAJOR: u8 = env!("CARGO_PKG_VERSION_MAJOR") | ||
.parse() | ||
.expect("Failed to parse value for `VERSION_MAJOR` from `CARGO_PKG_VERSION_MAJOR`"); | ||
static ref VERSION_MINOR: u8 = env!("CARGO_PKG_VERSION_MINOR") | ||
.parse() | ||
.expect("Failed to parse value for `VERSION_MINOR` from `CARGO_PKG_VERSION_MINOR`"); | ||
static ref VERSION_PATCH: u8 = env!("CARGO_PKG_VERSION_PATCH") | ||
.parse() | ||
.expect("Failed to parse value for `VERSION_PATCH` from `CARGO_PKG_VERSION_PATCH`"); | ||
} | ||
|
||
/// Get the version of the Wasmer C API. | ||
/// | ||
/// The `.h` files already define variables like `WASMER_VERSION*`, | ||
/// but if this file is unreachable, one can use this function to | ||
/// retrieve the full semver version of the Wasmer C API. | ||
/// | ||
/// The returned string is statically allocated. It must _not_ be | ||
/// freed! | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use inline_c::assert_c; | ||
/// # fn main() { | ||
/// # (assert_c! { | ||
/// # #include "tests/wasmer_wasm.h" | ||
/// # | ||
/// int main() { | ||
/// // Get and print the version. | ||
/// const char* version = wasmer_version(); | ||
/// printf("%s", version); | ||
/// | ||
/// // No need to free the string. It's statically allocated on | ||
/// // the Rust side. | ||
/// | ||
/// return 0; | ||
/// } | ||
/// # }) | ||
/// # .success() | ||
/// # .stdout(env!("CARGO_PKG_VERSION")); | ||
/// # } | ||
/// ``` | ||
#[no_mangle] | ||
pub unsafe extern "C" fn wasmer_version() -> *const c_char { | ||
VERSION.as_ptr() as *const _ | ||
} | ||
|
||
/// Get the major version of the Wasmer C API. | ||
/// | ||
/// See `wasmer_version` to learn more. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use inline_c::assert_c; | ||
/// # fn main() { | ||
/// # (assert_c! { | ||
/// # #include "tests/wasmer_wasm.h" | ||
/// # | ||
/// int main() { | ||
/// // Get and print the version components. | ||
/// uint8_t version_major = wasmer_version_major(); | ||
/// uint8_t version_minor = wasmer_version_minor(); | ||
/// uint8_t version_patch = wasmer_version_patch(); | ||
/// | ||
/// printf("%d.%d.%d", version_major, version_minor, version_patch); | ||
/// | ||
/// return 0; | ||
/// } | ||
/// # }) | ||
/// # .success() | ||
/// # .stdout( | ||
/// # format!( | ||
/// # "{}.{}.{}", | ||
/// # env!("CARGO_PKG_VERSION_MAJOR"), | ||
/// # env!("CARGO_PKG_VERSION_MINOR"), | ||
/// # env!("CARGO_PKG_VERSION_PATCH") | ||
/// # ) | ||
/// # ); | ||
/// # } | ||
/// ``` | ||
#[no_mangle] | ||
pub unsafe extern "C" fn wasmer_version_major() -> u8 { | ||
*VERSION_MAJOR | ||
} | ||
|
||
/// Get the minor version of the Wasmer C API. | ||
/// | ||
/// See `wasmer_version_major` to learn more and get an example. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn wasmer_version_minor() -> u8 { | ||
*VERSION_MINOR | ||
} | ||
|
||
/// Get the patch version of the Wasmer C API. | ||
/// | ||
/// See `wasmer_version_major` to learn more and get an example. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn wasmer_version_patch() -> u8 { | ||
*VERSION_PATCH | ||
} | ||
|
||
/// Get the minor version of the Wasmer C API. | ||
/// | ||
/// See `wasmer_version_major` to learn more. | ||
/// | ||
/// The returned string is statically allocated. It must _not_ be | ||
/// freed! | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use inline_c::assert_c; | ||
/// # fn main() { | ||
/// # (assert_c! { | ||
/// # #include "tests/wasmer_wasm.h" | ||
/// # | ||
/// int main() { | ||
/// // Get and print the pre version. | ||
/// const char* version_pre = wasmer_version_pre(); | ||
/// printf("%s", version_pre); | ||
/// | ||
/// // No need to free the string. It's statically allocated on | ||
/// // the Rust side. | ||
/// | ||
/// return 0; | ||
/// } | ||
/// # }) | ||
/// # .success() | ||
/// # .stdout(env!("CARGO_PKG_VERSION_PRE")); | ||
/// # } | ||
/// ``` | ||
#[no_mangle] | ||
pub unsafe extern "C" fn wasmer_version_pre() -> *const c_char { | ||
VERSION_PRE.as_ptr() as *const _ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.