Fix linking for --object-format serialized

This commit is contained in:
Felix Schütt
2022-12-23 15:04:06 +01:00
parent acdc7e2498
commit 358296f353

View File

@@ -405,7 +405,6 @@ fn conpile_atoms(
} }
/// Compile the C code. /// Compile the C code.
#[allow(dead_code)]
fn run_c_compile(path_to_c_src: &Path, output_name: &Path, target: &Triple) -> anyhow::Result<()> { fn run_c_compile(path_to_c_src: &Path, output_name: &Path, target: &Triple) -> anyhow::Result<()> {
#[cfg(not(windows))] #[cfg(not(windows))]
let c_compiler = "cc"; let c_compiler = "cc";
@@ -657,15 +656,17 @@ fn link_exe_from_dir(
directory.display() directory.display()
) )
})?; })?;
link_c_compilation()
} else { } else {
Err(anyhow::anyhow!( return Err(anyhow::anyhow!(
"ObjectFormat::Serialized + cross compilation not implemented" "ObjectFormat::Serialized + cross compilation not implemented"
)) ));
} }
} }
ObjectFormat::Symbols => { ObjectFormat::Symbols => {}
}
// compilation done, now link
let zig_binary_path = cross_compilation.zig_binary_path.as_ref().ok_or_else(|| { let zig_binary_path = cross_compilation.zig_binary_path.as_ref().ok_or_else(|| {
anyhow::anyhow!( anyhow::anyhow!(
"could not write wasmer_main.c in dir {}", "could not write wasmer_main.c in dir {}",
@@ -713,7 +714,15 @@ fn link_exe_from_dir(
cmd.arg(&format!("-femit-bin={}", out_path.display())); cmd.arg(&format!("-femit-bin={}", out_path.display()));
cmd.args(&object_paths); cmd.args(&object_paths);
cmd.arg(&library_path); cmd.arg(&library_path);
cmd.arg(directory.join("wasmer_main.c").canonicalize().unwrap()); cmd.arg(
directory
.join(match entrypoint.object_format {
ObjectFormat::Serialized => "wasmer_main.o",
ObjectFormat::Symbols => "wasmer_main.c",
})
.canonicalize()
.expect("could not find wasmer_main.c / wasmer_main.o"),
);
if zig_triple.contains("windows") { if zig_triple.contains("windows") {
let mut winsdk_path = library_path.clone(); let mut winsdk_path = library_path.clone();
@@ -755,14 +764,6 @@ fn link_exe_from_dir(
Ok(()) Ok(())
} }
}
}
/// Link compiled objects together using the system linker
fn link_c_compilation() -> Result<(), anyhow::Error> {
// TODO
Ok(())
}
/// Generate the wasmer_main.c that links all object files together /// Generate the wasmer_main.c that links all object files together
/// (depending on the object format / atoms number) /// (depending on the object format / atoms number)
@@ -771,12 +772,8 @@ fn generate_wasmer_main_c(entrypoint: &Entrypoint) -> Result<String, anyhow::Err
const WASMER_MAIN_C_SOURCE: &str = include_str!("wasmer_create_exe_main.c"); const WASMER_MAIN_C_SOURCE: &str = include_str!("wasmer_create_exe_main.c");
match entrypoint.object_format { let compile_static = entrypoint.object_format == ObjectFormat::Symbols;
ObjectFormat::Serialized => {
Ok(WASMER_MAIN_C_SOURCE.replace("// WASI_DEFINES", "#define WASI"))
}
ObjectFormat::Symbols => {
let compile_static = true;
// always with compile zig + static_defs.h // always with compile zig + static_defs.h
let atom_names = entrypoint let atom_names = entrypoint
.atoms .atoms
@@ -794,8 +791,6 @@ fn generate_wasmer_main_c(entrypoint: &Entrypoint) -> Result<String, anyhow::Err
let atom_name_uppercase = atom_name.to_uppercase(); let atom_name_uppercase = atom_name.to_uppercase();
let module_name = format!("WASMER_MODULE_{atom_name_uppercase}"); let module_name = format!("WASMER_MODULE_{atom_name_uppercase}");
extra_headers.push(format!("#include \"static_defs_{atom_name}.h\""));
write!( write!(
c_code_to_add, c_code_to_add,
" "
@@ -805,6 +800,8 @@ fn generate_wasmer_main_c(entrypoint: &Entrypoint) -> Result<String, anyhow::Err
)?; )?;
if compile_static { if compile_static {
extra_headers.push(format!("#include \"static_defs_{atom_name}.h\""));
write!(c_code_to_instantiate, " write!(c_code_to_instantiate, "
wasm_module_t *atom_{atom_name} = wasmer_object_module_new_{atom_name}(store, \"{atom_name}\"); wasm_module_t *atom_{atom_name} = wasmer_object_module_new_{atom_name}(store, \"{atom_name}\");
if (!atom_{atom_name}) {{ if (!atom_{atom_name}) {{
@@ -814,6 +811,7 @@ fn generate_wasmer_main_c(entrypoint: &Entrypoint) -> Result<String, anyhow::Err
}} }}
")?; ")?;
} else { } else {
extra_headers.push(format!("const extern unsigned char {module_name}[];\r\n"));
write!(c_code_to_instantiate, " write!(c_code_to_instantiate, "
wasm_byte_vec_t atom_{atom_name}_byte_vec = {{ wasm_byte_vec_t atom_{atom_name}_byte_vec = {{
.size = {module_name}_LENGTH, .size = {module_name}_LENGTH,
@@ -893,15 +891,12 @@ fn generate_wasmer_main_c(entrypoint: &Entrypoint) -> Result<String, anyhow::Err
Ok(return_str.replace("// INSTANTIATE_MODULES", &c_code_to_instantiate)) Ok(return_str.replace("// INSTANTIATE_MODULES", &c_code_to_instantiate))
} }
}
}
#[allow(dead_code)] #[allow(dead_code)]
pub(super) mod utils { pub(super) mod utils {
use super::{CrossCompile, CrossCompileSetup}; use super::{CrossCompile, CrossCompileSetup};
use anyhow::Context; use anyhow::Context;
use std::env;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use target_lexicon::{Architecture, OperatingSystem, Triple}; use target_lexicon::{Architecture, OperatingSystem, Triple};
use wasmer_types::{CpuFeature, Target}; use wasmer_types::{CpuFeature, Target};