Skip to content

Commit

Permalink
Merge branch 'master' into cranelift-opt
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan authored Dec 14, 2020
2 parents 9fc459c + 35e0021 commit c6a693c
Show file tree
Hide file tree
Showing 11 changed files with 580 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

### Added

* [#1916](https://github.com/wasmerio/wasmer/pull/1916) Add the `WASMER_VERSION*` constants with the `wasmer_version*` functions 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`
* [#1894](https://github.com/wasmerio/wasmer/pull/1894) Added exports `wasmer::{CraneliftOptLevel, LLVMOptLevel}` to allow using `Cranelift::opt_level` and `LLVM::opt_level` directly via the `wasmer` crate
* [#1927](https://github.com/wasmerio/wasmer/pull/1927) Added mmap support in `Engine::deserialize_from_file` to speed up artifact loading

### Changed

Expand Down
11 changes: 10 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
144 changes: 144 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,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 _
}
Loading

0 comments on commit c6a693c

Please sign in to comment.