-
Notifications
You must be signed in to change notification settings - Fork 824
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) Implement cross-compilation API in C #2072
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b201f12
feat(c-api) Start implementing `wasm_target_t`, `wasm_triple_t` and `…
Hywan 8f42485
Merge branch 'master' into feat-c-api-cross-compilation-2
Hywan 169fe16
doc+test(c-api) Add more tests and more documentation.
Hywan 1eec258
chore(c-api) Remove warning.
Hywan bc5bbfd
feat(c-api) Implement `Debug` for `wasm_target_t`.
Hywan 14b8fb0
feat(c-api) Implement `wasm_config_set_target`.
Hywan 9fda912
feat(c-api) `wasm_config_t` and `wasm_engine_new_with_config` handle …
Hywan e405746
chore(c-api) `get_default_compiler_config` can stay private.
Hywan dc9f8fc
chore(c-api) Update headers.
Hywan fd39a74
doc(changelog) Add #2072.
Hywan 3c5692c
doc(c-api) Mark some types as unstable.
Hywan 88e5e49
chore(cargo) Update `Cargo.lock`.
Hywan 9fb174e
doc(c-api) Add more documentation.
Hywan 7e757de
test(c-api) Simplify test by using `wasm_name_new_from_string`.
Hywan 7eb0839
fix(c-api) Include feedbacks from @MarkMcCaskey!
Hywan 0826d3d
Merge branch 'master' into feat-c-api-cross-compilation-2
Hywan 4725a5d
Merge branch 'master' into feat-c-api-cross-compilation-2
Hywan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub use super::unstable::engine::wasm_config_set_target; | ||
use super::unstable::target_lexicon::wasm_target_t; | ||
use crate::error::{update_last_error, CApiError}; | ||
use cfg_if::cfg_if; | ||
use std::sync::Arc; | ||
|
@@ -93,6 +95,7 @@ pub struct wasm_config_t { | |
engine: wasmer_engine_t, | ||
#[cfg(feature = "compiler")] | ||
compiler: wasmer_compiler_t, | ||
pub(super) target: Option<Box<wasm_target_t>>, | ||
} | ||
|
||
/// Create a new default Wasmer configuration. | ||
|
@@ -396,7 +399,7 @@ pub unsafe extern "C" fn wasm_engine_delete(_engine: Option<Box<wasm_engine_t>>) | |
/// cbindgen:ignore | ||
#[no_mangle] | ||
pub extern "C" fn wasm_engine_new_with_config( | ||
config: Box<wasm_config_t>, | ||
config: Option<Box<wasm_config_t>>, | ||
) -> Option<Box<wasm_engine_t>> { | ||
#[allow(dead_code)] | ||
fn return_with_error<M>(msg: M) -> Option<Box<wasm_engine_t>> | ||
|
@@ -408,7 +411,9 @@ pub extern "C" fn wasm_engine_new_with_config( | |
}); | ||
|
||
return None; | ||
}; | ||
} | ||
|
||
let config = config?; | ||
|
||
cfg_if! { | ||
if #[cfg(feature = "compiler")] { | ||
|
@@ -447,7 +452,13 @@ pub extern "C" fn wasm_engine_new_with_config( | |
wasmer_engine_t::JIT => { | ||
cfg_if! { | ||
if #[cfg(feature = "jit")] { | ||
Arc::new(JIT::new(compiler_config).engine()) | ||
let mut builder = JIT::new(compiler_config); | ||
|
||
if let Some(target) = config.target { | ||
builder = builder.target(target.inner); | ||
} | ||
|
||
Arc::new(builder.engine()) | ||
} else { | ||
return return_with_error("Wasmer has not been compiled with the `jit` feature."); | ||
} | ||
|
@@ -456,7 +467,13 @@ pub extern "C" fn wasm_engine_new_with_config( | |
wasmer_engine_t::NATIVE => { | ||
cfg_if! { | ||
if #[cfg(feature = "native")] { | ||
Arc::new(Native::new(compiler_config).engine()) | ||
let mut builder = Native::new(compiler_config); | ||
|
||
if let Some(target) = config.target { | ||
builder = builder.target(target.inner); | ||
} | ||
|
||
Arc::new(builder.engine()) | ||
} else { | ||
return return_with_error("Wasmer has not been compiled with the `native` feature."); | ||
} | ||
|
@@ -467,7 +484,13 @@ pub extern "C" fn wasm_engine_new_with_config( | |
// There are currently no uses of the object-file engine + compiler from the C API. | ||
// So we run in headless mode. | ||
if #[cfg(feature = "object-file")] { | ||
Arc::new(ObjectFile::headless().engine()) | ||
let mut builder = ObjectFile::headless(); | ||
|
||
if let Some(target) = config.target { | ||
builder = builder.target(target.inner); | ||
} | ||
|
||
Arc::new(builder.engine()) | ||
} else { | ||
return return_with_error("Wasmer has not been compiled with the `object-file` feature."); | ||
} | ||
|
@@ -480,7 +503,13 @@ pub extern "C" fn wasm_engine_new_with_config( | |
wasmer_engine_t::JIT => { | ||
cfg_if! { | ||
if #[cfg(feature = "jit")] { | ||
Arc::new(JIT::headless().engine()) | ||
let mut builder = JIT::headless(); | ||
|
||
if let Some(target) = config.target { | ||
builder = builder.target(target.inner); | ||
} | ||
|
||
Arc::new(builder.engine()) | ||
} else { | ||
return return_with_error("Wasmer has not been compiled with the `jit` feature."); | ||
} | ||
|
@@ -489,7 +518,13 @@ pub extern "C" fn wasm_engine_new_with_config( | |
wasmer_engine_t::NATIVE => { | ||
cfg_if! { | ||
if #[cfg(feature = "native")] { | ||
Arc::new(Native::headless().engine()) | ||
let mut builder = Native::headless(); | ||
|
||
if let Some(target) = config.target { | ||
builder = builder.target(target.inner); | ||
} | ||
|
||
Arc::new(builder.engine()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These ones too |
||
} else { | ||
return return_with_error("Wasmer has not been compiled with the `native` feature."); | ||
} | ||
|
@@ -498,7 +533,13 @@ pub extern "C" fn wasm_engine_new_with_config( | |
wasmer_engine_t::OBJECT_FILE => { | ||
cfg_if! { | ||
if #[cfg(feature = "object-file")] { | ||
Arc::new(ObjectFile::headless().engine()) | ||
let mut builder = ObjectFile::headless(); | ||
|
||
if let Some(target) = config.target { | ||
builder = builder.target(target.inner); | ||
} | ||
|
||
Arc::new(builder.engine()) | ||
} else { | ||
return return_with_error("Wasmer has not been compiled with the `object-file` feature."); | ||
} | ||
|
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,49 @@ | ||
//! Unstable non-standard Wasmer-specific types for the | ||
//! `wasm_engine_t` and siblings. | ||
|
||
use super::super::engine::wasm_config_t; | ||
use super::target_lexicon::wasm_target_t; | ||
|
||
/// Unstable non-standard Wasmer-specific API to update the | ||
/// configuration to specify a particular target for the engine. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use inline_c::assert_c; | ||
/// # fn main() { | ||
/// # (assert_c! { | ||
/// # #include "tests/wasmer_wasm.h" | ||
/// # | ||
/// int main() { | ||
/// // Create the configuration. | ||
/// wasm_config_t* config = wasm_config_new(); | ||
/// | ||
/// // Set the target. | ||
/// { | ||
/// wasm_triple_t* triple = wasm_triple_new_from_host(); | ||
/// wasm_cpu_features_t* cpu_features = wasm_cpu_features_new(); | ||
/// wasm_target_t* target = wasm_target_new(triple, cpu_features); | ||
/// | ||
/// wasm_config_set_target(config, target); | ||
/// } | ||
/// | ||
/// // Create the engine. | ||
/// wasm_engine_t* engine = wasm_engine_new_with_config(config); | ||
/// | ||
/// // Check we have an engine! | ||
/// assert(engine); | ||
/// | ||
/// // Free everything. | ||
/// wasm_engine_delete(engine); | ||
/// | ||
/// return 0; | ||
/// } | ||
/// # }) | ||
/// # .success(); | ||
/// # } | ||
/// ``` | ||
#[no_mangle] | ||
pub extern "C" fn wasm_config_set_target(config: &mut wasm_config_t, target: Box<wasm_target_t>) { | ||
config.target = Some(target); | ||
} |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod engine; | ||
pub mod module; | ||
pub mod target_lexicon; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to set the target for a headless-only engine?
It's fine for symmetry I suppose but I don't think this can ever do anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was wondering if that was making sense. Using the target makes sense only when compiling, but we never know, maybe one day, someone, somewhere… :-p. Let's keep it for “the symmetry” as you said, how does it sound?