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

feat(c-api) Add the WASMER_VERSION* constants, and the wasmer_version* functions #1916

Merged
merged 14 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Added

* [#1916](https://github.com/wasmerio/wasmer/pull/1916) Add the `WASMER_VERSION*` constants with the `wasmer_version` function in the Wasmer C API
* [#1867](https://github.com/wasmerio/wasmer/pull/1867) Added `Metering::get_remaining_points` and `Metering::set_remaining_points`
* [#1881](https://github.com/wasmerio/wasmer/pull/1881) Added `UnsupportedTarget` error to `CompileError`
* [#1908](https://github.com/wasmerio/wasmer/pull/1908) Implemented `TryFrom<Value<T>>` for `i32`/`u32`/`i64`/`u64`/`f32`/`f64`
Expand Down
35 changes: 29 additions & 6 deletions lib/c-api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ fn build_wasm_c_api_headers(crate_dir: &str, out_dir: &str) {
r#"// The Wasmer C/C++ header file compatible with the `wasm-c-api` standard API.
// This file is generated by lib/c-api/build.rs.

#if !defined(WASMER_WASM_H_MACROS)
#if !defined(WASMER_WASM_H_PRELUDE)

#define WASMER_WASM_H_MACROS
#define WASMER_WASM_H_PRELUDE
{pre_header}"#,
pre_header = PRE_HEADER
);
Expand All @@ -88,10 +88,12 @@ fn build_wasm_c_api_headers(crate_dir: &str, out_dir: &str) {
map_feature_as_c_define!("wasi", WASI_FEATURE_AS_C_DEFINE, pre_header);
map_feature_as_c_define!("emscripten", EMSCRIPTEN_FEATURE_AS_C_DEFINE, pre_header);

add_wasmer_version(&mut pre_header);

// Close pre header.
pre_header.push_str(
r#"
#endif // WASMER_WASM_H_MACROS
#endif // WASMER_WASM_H_PRELUDE


//
Expand Down Expand Up @@ -134,19 +136,22 @@ fn build_wasmer_headers(crate_dir: &str, out_dir: &str) {
let mut pre_header = format!(
r#"// The Wasmer C/C++ header file.

#if !defined(WASMER_H_MACROS)
#if !defined(WASMER_H_PRELUDE)

#define WASMER_H_MACROS
#define WASMER_H_PRELUDE
{pre_header}"#,
pre_header = PRE_HEADER
);

map_feature_as_c_define!("wasi", WASI_FEATURE_AS_C_DEFINE, pre_header);
map_feature_as_c_define!("emscritpen", EMSCRIPTEN_FEATURE_AS_C_DEFINE, pre_header);

add_wasmer_version(&mut pre_header);

// Close pre header.
pre_header.push_str(
r#"#endif // WASMER_H_MACROS
r#"
#endif // WASMER_H_PRELUDE


//
Expand Down Expand Up @@ -196,6 +201,24 @@ fn build_wasmer_headers(crate_dir: &str, out_dir: &str) {
}
}

fn add_wasmer_version(pre_header: &mut String) {
pre_header.push_str(&format!(
r#"
// This file corresponds to the following Wasmer version.
#define WASMER_VERSION "{full}"
#define WASMER_VERSION_MAJOR {major}
#define WASMER_VERSION_MINOR {minor}
#define WASMER_VERSION_PATCH {patch}
#define WASMER_VERSION_PRE "{pre}"
"#,
full = env!("CARGO_PKG_VERSION"),
major = env!("CARGO_PKG_VERSION_MAJOR"),
minor = env!("CARGO_PKG_VERSION_MINOR"),
patch = env!("CARGO_PKG_VERSION_PATCH"),
pre = env!("CARGO_PKG_VERSION_PRE"),
));
}

/// Create a fresh new `Builder`, already pre-configured.
fn new_builder(language: Language, crate_dir: &str, include_guard: &str, header: &str) -> Builder {
Builder::new()
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/src/wasm_c_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub mod types;
/// cbindgen:ignore
pub mod value;

pub mod version;

#[cfg(feature = "wasi")]
pub mod wasi;

Expand Down
40 changes: 40 additions & 0 deletions lib/c-api/src/wasm_c_api/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::os::raw::c_char;

static VERSION: &'static str = env!("CARGO_PKG_VERSION");

/// Get the version of Wasmer.
///
/// 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 _
}
50 changes: 47 additions & 3 deletions lib/c-api/wasmer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// The Wasmer C/C++ header file.

#if !defined(WASMER_H_MACROS)
#if !defined(WASMER_H_PRELUDE)

#define WASMER_H_MACROS
#define WASMER_H_PRELUDE

// Define the `ARCH_X86_X64` constant.
#if defined(MSVC) && defined(_M_AMD64)
Expand Down Expand Up @@ -30,7 +30,15 @@

// The `wasi` feature has been enabled for this build.
#define WASMER_WASI_ENABLED
#endif // WASMER_H_MACROS

// This file corresponds to the following Wasmer version.
#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"

#endif // WASMER_H_PRELUDE


//
Expand Down Expand Up @@ -1415,6 +1423,42 @@ wasmer_result_t wasmer_trap(const wasmer_instance_context_t *_ctx, const char *e
*/
bool wasmer_validate(const uint8_t *wasm_bytes, uint32_t wasm_bytes_len);

/**
* Get the version of Wasmer.
*
* 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"));
* # }
* ```
*/
const char *wasmer_version(void);

#if defined(WASMER_WASI_ENABLED)
/**
* Convenience function that creates a WASI import object with no arguments,
Expand Down
48 changes: 45 additions & 3 deletions lib/c-api/wasmer.hh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// The Wasmer C/C++ header file.

#if !defined(WASMER_H_MACROS)
#if !defined(WASMER_H_PRELUDE)

#define WASMER_H_MACROS
#define WASMER_H_PRELUDE

// Define the `ARCH_X86_X64` constant.
#if defined(MSVC) && defined(_M_AMD64)
Expand Down Expand Up @@ -30,7 +30,15 @@

// The `wasi` feature has been enabled for this build.
#define WASMER_WASI_ENABLED
#endif // WASMER_H_MACROS

// This file corresponds to the following Wasmer version.
#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"

#endif // WASMER_H_PRELUDE


//
Expand Down Expand Up @@ -1173,6 +1181,40 @@ wasmer_result_t wasmer_trap(const wasmer_instance_context_t *_ctx, const char *e
/// ```
bool wasmer_validate(const uint8_t *wasm_bytes, uint32_t wasm_bytes_len);

/// Get the version of Wasmer.
///
/// 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"));
/// # }
/// ```
const char *wasmer_version();

#if defined(WASMER_WASI_ENABLED)
/// Convenience function that creates a WASI import object with no arguments,
/// environment variables, preopened files, or mapped directories.
Expand Down
49 changes: 46 additions & 3 deletions lib/c-api/wasmer_wasm.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// The Wasmer C/C++ header file compatible with the `wasm-c-api` standard API.
// This file is generated by lib/c-api/build.rs.

#if !defined(WASMER_WASM_H_MACROS)
#if !defined(WASMER_WASM_H_PRELUDE)

#define WASMER_WASM_H_MACROS
#define WASMER_WASM_H_PRELUDE

// Define the `ARCH_X86_X64` constant.
#if defined(MSVC) && defined(_M_AMD64)
Expand Down Expand Up @@ -38,7 +38,14 @@
// The `wasi` feature has been enabled for this build.
#define WASMER_WASI_ENABLED

#endif // WASMER_WASM_H_MACROS
// This file corresponds to the following Wasmer version.
#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"

#endif // WASMER_WASM_H_PRELUDE


//
Expand Down Expand Up @@ -302,6 +309,42 @@ int wasmer_last_error_length(void);
*/
int wasmer_last_error_message(char *buffer, int length);

/**
* Get the version of Wasmer.
*
* 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"));
* # }
* ```
*/
const char *wasmer_version(void);

/**
* Parses in-memory bytes as either the WAT format, or a binary Wasm
* module. This is wasmer-specific.
Expand Down