Skip to content

Commit

Permalink
Merge #3117
Browse files Browse the repository at this point in the history
3117: Add tests for wasmer-cli create-{exe,obj} commands r=epilys a=epilys

Unified generated objects interfaces: both static and serialized objects
expose the same function signature to create the module:

  wasm_module_t* wasmer_object_module_new(wasm_store_t* store, const char* module_name);

Closes #3071 

Co-authored-by: Manos Pitsidianakis <[email protected]>
  • Loading branch information
bors[bot] and epilys authored Aug 19, 2022
2 parents b801e45 + 2f2b895 commit 9e1e7a5
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 310 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test-sys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ jobs:
'${{ runner.tool_cache }}/cargo-sccache/bin/sccache' -s
echo 'RUSTC_WRAPPER=${{ runner.tool_cache }}/cargo-sccache/bin/sccache' >> $GITHUB_ENV
shell: bash
- name: Test
if: matrix.run_test && matrix.os != 'windows-2019'
run: |
make
env:
TARGET: ${{ matrix.target }}
TARGET_DIR: target/${{ matrix.target }}/release
CARGO_TARGET: --target ${{ matrix.target }}
- name: Test
if: matrix.run_test && matrix.os != 'windows-2019'
run: |
Expand All @@ -215,6 +223,16 @@ jobs:
TARGET: ${{ matrix.target }}
TARGET_DIR: target/${{ matrix.target }}/release
CARGO_TARGET: --target ${{ matrix.target }}
- name: Test integration CLI
if: matrix.run_test && matrix.os != 'windows-2019'
run: |
make && make build-capi && make package-capi && make package
export WASMER_DIR=`pwd`/package
make test-integration-cli
env:
TARGET: ${{ matrix.target }}
TARGET_DIR: target/${{ matrix.target }}/release
CARGO_TARGET: --target ${{ matrix.target }}
- name: Test
if: matrix.run_test && matrix.os == 'windows-2019'
run: |
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ test-examples:
$(CARGO_BINARY) test $(CARGO_TARGET) $(compiler_features) --features wasi --examples
$(CARGO_BINARY) test $(CARGO_TARGET) --release $(compiler_features) --features wasi --examples

test-integration:
$(CARGO_BINARY) test $(CARGO_TARGET) --no-fail-fast -p wasmer-integration-tests-cli
test-integration-cli:
$(CARGO_BINARY) test $(CARGO_TARGET) --no-fail-fast -p wasmer-integration-tests-cli -- --nocapture

test-integration-ios:
$(CARGO_BINARY) test $(CARGO_TARGET) -p wasmer-integration-tests-ios
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/src/c_gen/staticlib_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ wasm_byte_vec_t generate_serialized_data() {
return module_byte_vec;
}
wasm_module_t* wasmer_static_module_new(wasm_store_t* store, const char* wasm_name) {
// wasm_name intentionally unused for now: will be used in the future.
wasm_module_t* wasmer_object_module_new(wasm_store_t* store, const char* module_name) {
// module_name intentionally unused for now: will be used in the future.
wasm_byte_vec_t module_byte_vec = generate_serialized_data();
wasm_module_t* module = wasm_module_deserialize(store, &module_byte_vec);
free(module_byte_vec.data);
Expand Down
78 changes: 36 additions & 42 deletions lib/cli/src/commands/create_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use wasmer_object::{emit_serialized, get_object_for_target};
pub type PrefixerFn = Box<dyn Fn(&[u8]) -> String + Send>;

const WASMER_MAIN_C_SOURCE: &[u8] = include_bytes!("wasmer_create_exe_main.c");
#[cfg(feature = "static-artifact-create")]
const WASMER_STATIC_MAIN_C_SOURCE: &[u8] = include_bytes!("wasmer_static_create_exe_main.c");
const WASMER_DESERIALIZE_HEADER: &str = include_str!("wasmer_deserialize_module.h");

#[derive(Debug, Clone)]
struct CrossCompile {
Expand Down Expand Up @@ -307,9 +306,39 @@ impl CreateExe {
.map_err(|err| anyhow::anyhow!(err.to_string()))?;
writer.flush()?;
drop(writer);
// Write down header file that includes deserialize function
{
let mut writer = BufWriter::new(File::create("static_defs.h")?);
writer.write_all(WASMER_DESERIALIZE_HEADER.as_bytes())?;
writer.flush()?;
}

// write C src to disk
let c_src_path = Path::new("wasmer_main.c");
#[cfg(not(windows))]
let c_src_obj = PathBuf::from("wasmer_main.o");
#[cfg(windows)]
let c_src_obj = PathBuf::from("wasmer_main.obj");

let cli_given_triple = self.target_triple.clone();
self.compile_c(wasm_object_path, cli_given_triple, output_path)?;
{
let mut c_src_file = fs::OpenOptions::new()
.create_new(true)
.write(true)
.open(&c_src_path)
.context("Failed to open C source code file")?;
c_src_file.write_all(WASMER_MAIN_C_SOURCE)?;
}
run_c_compile(c_src_path, &c_src_obj, self.target_triple.clone())
.context("Failed to compile C source code")?;
LinkCode {
object_paths: vec![c_src_obj, wasm_object_path],
output_path,
additional_libraries: self.libraries.clone(),
target: self.target_triple.clone(),
..Default::default()
}
.run()
.context("Failed to link objects together")?;
}
#[cfg(not(feature = "static-artifact-create"))]
ObjectFormat::Symbols => {
Expand Down Expand Up @@ -379,42 +408,6 @@ impl CreateExe {
Ok(())
}

fn compile_c(
&self,
wasm_object_path: PathBuf,
target_triple: Option<wasmer::Triple>,
output_path: PathBuf,
) -> anyhow::Result<()> {
// write C src to disk
let c_src_path = Path::new("wasmer_main.c");
#[cfg(not(windows))]
let c_src_obj = PathBuf::from("wasmer_main.o");
#[cfg(windows)]
let c_src_obj = PathBuf::from("wasmer_main.obj");

{
let mut c_src_file = fs::OpenOptions::new()
.create_new(true)
.write(true)
.open(&c_src_path)
.context("Failed to open C source code file")?;
c_src_file.write_all(WASMER_MAIN_C_SOURCE)?;
}
run_c_compile(c_src_path, &c_src_obj, target_triple.clone())
.context("Failed to compile C source code")?;
LinkCode {
object_paths: vec![c_src_obj, wasm_object_path],
output_path,
additional_libraries: self.libraries.clone(),
target: target_triple,
..Default::default()
}
.run()
.context("Failed to link objects together")?;

Ok(())
}

fn compile_zig(
&self,
output_path: PathBuf,
Expand Down Expand Up @@ -449,7 +442,7 @@ impl CreateExe {
.write(true)
.open(&c_src_path)
.context("Failed to open C source code file")?;
c_src_file.write_all(WASMER_STATIC_MAIN_C_SOURCE)?;
c_src_file.write_all(WASMER_MAIN_C_SOURCE)?;
}

if !header_code_path.is_dir() {
Expand Down Expand Up @@ -531,7 +524,7 @@ impl CreateExe {
.write(true)
.open(&c_src_path)
.context("Failed to open C source code file")?;
c_src_file.write_all(WASMER_STATIC_MAIN_C_SOURCE)?;
c_src_file.write_all(WASMER_MAIN_C_SOURCE)?;
}

if !header_code_path.is_dir() {
Expand Down Expand Up @@ -666,6 +659,7 @@ fn run_c_compile(
.arg("-O2")
.arg("-c")
.arg(path_to_c_src)
.arg("-I.")
.arg("-I")
.arg(get_wasmer_include_directory()?);

Expand Down
12 changes: 11 additions & 1 deletion lib/cli/src/commands/create_obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::process::Command;
use wasmer::*;
use wasmer_object::{emit_serialized, get_object_for_target};

const WASMER_SERIALIZED_HEADER: &[u8] = include_bytes!("wasmer_create_exe.h");
const WASMER_SERIALIZED_HEADER: &[u8] = include_bytes!("wasmer_deserialize_module.h");

#[derive(Debug, Parser)]
/// The options for the `wasmer create-exe` subcommand
Expand Down Expand Up @@ -152,6 +152,16 @@ impl CreateObj {
self.output.display(),
header_output.display(),
);
eprintln!("\n---\n");
eprintln!(
r#"To use, link the object file to your executable and call the `wasmer_object_module_new` function defined in the header file. For example, in the C language:
#include "{}"
wasm_module_t *module = wasmer_object_module_new(store, "my_module_name");
"#,
header_output.display(),
);

Ok(())
}
Expand Down
14 changes: 4 additions & 10 deletions lib/cli/src/commands/wasmer_create_exe_main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

#include "wasmer.h"
//#include "my_wasm.h"

#include "static_defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -11,8 +9,8 @@
// TODO: make this define templated so that the Rust code can toggle it on/off
#define WASI

extern size_t WASMER_MODULE_LENGTH asm("WASMER_MODULE_LENGTH");
extern char WASMER_MODULE_DATA asm("WASMER_MODULE_DATA");
extern wasm_module_t* wasmer_object_module_new(wasm_store_t* store, const char* module_name) asm("wasmer_object_module_new");


static void print_wasmer_error() {
int error_len = wasmer_last_error_length();
Expand Down Expand Up @@ -95,11 +93,7 @@ int main(int argc, char *argv[]) {
wasm_engine_t *engine = wasm_engine_new_with_config(config);
wasm_store_t *store = wasm_store_new(engine);

wasm_byte_vec_t module_byte_vec = {
.size = WASMER_MODULE_LENGTH,
.data = (const char*)&WASMER_MODULE_DATA,
};
wasm_module_t *module = wasm_module_deserialize(store, &module_byte_vec);
wasm_module_t *module = wasmer_object_module_new(store, "module");

if (!module) {
fprintf(stderr, "Failed to create module\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {
extern size_t WASMER_MODULE_LENGTH asm("WASMER_MODULE_LENGTH");
extern char WASMER_MODULE_DATA asm("WASMER_MODULE_DATA");

wasm_module_t* wasmer_module_new(wasm_store_t* store, const char* wasm_name) {
wasm_module_t* wasmer_object_module_new(wasm_store_t* store, const char* module_name) {
wasm_byte_vec_t module_byte_vec = {
.size = WASMER_MODULE_LENGTH,
.data = (const char*)&WASMER_MODULE_DATA,
Expand Down
Loading

0 comments on commit 9e1e7a5

Please sign in to comment.