Refactor compile_zig to remove duplicated code

This commit is contained in:
Felix Schütt
2022-11-18 22:25:23 +01:00
parent 3daec87b15
commit 126b354a9e

View File

@@ -237,8 +237,8 @@ impl CreateExe {
if let Some(setup) = cross_compilation.as_ref() { if let Some(setup) = cross_compilation.as_ref() {
self.compile_zig( self.compile_zig(
output_path, output_path,
wasm_module_path, &[wasm_module_path],
std::path::Path::new("static_defs.h").into(), &[std::path::Path::new("static_defs.h").into()],
setup, setup,
&[], &[],
None, None,
@@ -308,8 +308,8 @@ impl CreateExe {
if let Some(setup) = cross_compilation.as_ref() { if let Some(setup) = cross_compilation.as_ref() {
self.compile_zig( self.compile_zig(
output_path, output_path,
object_file_path, &[object_file_path],
std::path::Path::new("static_defs.h").into(), &[std::path::Path::new("static_defs.h").into()],
setup, setup,
&[], &[],
None, None,
@@ -503,8 +503,8 @@ impl CreateExe {
fn compile_zig( fn compile_zig(
&self, &self,
output_path: PathBuf, output_path: PathBuf,
object_path: PathBuf, object_paths: &[PathBuf],
mut header_code_path: PathBuf, header_code_paths: &[PathBuf],
setup: &CrossCompileSetup, setup: &CrossCompileSetup,
pirita_atoms: &[String], pirita_atoms: &[String],
pirita_main_atom: Option<&str>, pirita_main_atom: Option<&str>,
@@ -540,12 +540,16 @@ impl CreateExe {
std::fs::write(&c_src_path, WASMER_STATIC_MAIN_C_SOURCE)?; std::fs::write(&c_src_path, WASMER_STATIC_MAIN_C_SOURCE)?;
} }
if !header_code_path.is_dir() { let mut header_code_paths = header_code_paths.to_vec();
header_code_path.pop();
}
if header_code_path.display().to_string().is_empty() { for h in header_code_paths.iter_mut() {
header_code_path = std::env::current_dir()?; if !h.is_dir() {
h.pop();
}
if h.display().to_string().is_empty() {
*h = std::env::current_dir()?;
}
} }
/* Compile main function */ /* Compile main function */
@@ -559,7 +563,9 @@ impl CreateExe {
cmd.arg("-target"); cmd.arg("-target");
cmd.arg(&zig_triple); cmd.arg(&zig_triple);
cmd.arg(&format!("-I{}/", include_dir.display())); cmd.arg(&format!("-I{}/", include_dir.display()));
cmd.arg(&format!("-I{}/", header_code_path.display())); for h in header_code_paths {
cmd.arg(&format!("-I{}/", h.display()));
}
if zig_triple.contains("windows") { if zig_triple.contains("windows") {
cmd.arg("-lc++"); cmd.arg("-lc++");
} else { } else {
@@ -573,17 +579,21 @@ impl CreateExe {
cmd.arg("-fno-compiler-rt"); cmd.arg("-fno-compiler-rt");
cmd.arg(&format!("-femit-bin={}", output_path.display())); cmd.arg(&format!("-femit-bin={}", output_path.display()));
cmd.arg(&object_path); for o in object_paths {
cmd.args(o);
}
cmd.arg(&c_src_path); cmd.arg(&c_src_path);
cmd.arg(libwasmer_path.join(&lib_filename)); cmd.arg(libwasmer_path.join(&lib_filename));
if zig_triple.contains("windows") { if zig_triple.contains("windows") {
let mut libwasmer_parent = libwasmer_path.clone(); let mut libwasmer_parent = libwasmer_path.clone();
libwasmer_parent.pop(); libwasmer_parent.pop();
cmd.arg(libwasmer_parent.join("winsdk/ADVAPI32.lib")); let files_winsdk = std::fs::read_dir(libwasmer_parent.join("winsdk"))
cmd.arg(libwasmer_parent.join("winsdk/BCRYPT.lib")); .ok()
cmd.arg(libwasmer_parent.join("winsdk/KERNEL32.lib")); .map(|res| res.filter_map(|r| Some(r.ok()?.path())).collect::<Vec<_>>())
cmd.arg(libwasmer_parent.join("winsdk/USERENV.lib")); .unwrap_or_default();
cmd.arg(libwasmer_parent.join("winsdk/WS2_32.lib")); for f in files_winsdk {
cmd.arg(f);
}
} }
if let Some(volume_obj) = pirita_volume_path.as_ref() { if let Some(volume_obj) = pirita_volume_path.as_ref() {
cmd.arg(volume_obj.clone()); cmd.arg(volume_obj.clone());
@@ -770,8 +780,6 @@ impl CreateExe {
let volume_object_path = let volume_object_path =
Self::write_volume_obj(volume_bytes, target, tempdir_path)?; Self::write_volume_obj(volume_bytes, target, tempdir_path)?;
link_objects.push(volume_object_path);
#[cfg(not(windows))] #[cfg(not(windows))]
let c_src_obj = working_dir.join("wasmer_main.o"); let c_src_obj = working_dir.join("wasmer_main.o");
#[cfg(windows)] #[cfg(windows)]
@@ -791,66 +799,29 @@ impl CreateExe {
std::fs::write(&c_src_path, c_code.as_bytes()) std::fs::write(&c_src_path, c_code.as_bytes())
.context("Failed to open C source code file")?; .context("Failed to open C source code file")?;
// TODO: this branch is never hit because object format serialized +
// cross compilation doesn't work
if let Some(setup) = cross_compilation { if let Some(setup) = cross_compilation {
let CrossCompileSetup { // zig treats .o files the same as .c files
ref target, link_objects.push(c_src_path);
ref zig_binary_path,
ref library,
} = setup;
let mut libwasmer_path = library.to_path_buf(); self.compile_zig(
let zig_triple = triple_to_zig_triple(target); output_path,
&link_objects,
// Cross compilation is only possible with zig &[],
println!("Library Path: {}", libwasmer_path.display()); &setup,
println!("Using zig binary: {}", zig_binary_path.display()); &atom_names,
println!("Using zig target triple: {}", &zig_triple); Some(&entrypoint),
Some(volume_object_path),
let lib_filename = libwasmer_path )?;
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();
/* Compile main function */
let compilation = {
libwasmer_path.pop();
let mut include_dir = libwasmer_path.clone();
include_dir.pop();
include_dir.push("include");
let mut cmd = Command::new(zig_binary_path);
let mut cmd_mut: &mut Command = cmd
.arg("cc")
.arg("--verbose")
.arg("-target")
.arg(&zig_triple)
.arg(&format!("-I{}", include_dir.display()))
.arg("-lc");
if !zig_triple.contains("windows") {
cmd_mut = cmd_mut.arg("-lunwind");
}
cmd_mut
.args(link_objects.into_iter())
.arg(&c_src_path)
.arg(libwasmer_path.join(lib_filename))
.arg("-o")
.arg(&output_path)
.output()
.context("Could not execute `zig`")?
};
if !compilation.status.success() {
return Err(anyhow::anyhow!(String::from_utf8_lossy(
&compilation.stderr
)
.to_string()));
}
} else { } else {
// compile with cc instead of zig
run_c_compile(c_src_path.as_path(), &c_src_obj, self.target_triple.clone()) run_c_compile(c_src_path.as_path(), &c_src_obj, self.target_triple.clone())
.context("Failed to compile C source code")?; .context("Failed to compile C source code")?;
link_objects.push(c_src_obj); link_objects.push(c_src_obj);
link_objects.push(volume_object_path);
LinkCode { LinkCode {
object_paths: link_objects, object_paths: link_objects,
output_path, output_path,
@@ -873,8 +844,8 @@ impl CreateExe {
if let Some(setup) = cross_compilation.as_ref() { if let Some(setup) = cross_compilation.as_ref() {
self.compile_zig( self.compile_zig(
output_path, output_path,
object_file_path, &[object_file_path],
static_defs_file_path, &[static_defs_file_path],
setup, setup,
&atom_names, &atom_names,
Some(&entrypoint), Some(&entrypoint),