mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-06 04:38:25 +00:00
Add target support to wasmer create-exe
This commit is contained in:
@@ -130,11 +130,13 @@ impl CreateExe {
|
|||||||
// TODO:
|
// TODO:
|
||||||
c_src_file.write_all(WASMER_MAIN_C_SOURCE)?;
|
c_src_file.write_all(WASMER_MAIN_C_SOURCE)?;
|
||||||
}
|
}
|
||||||
run_c_compile(&c_src_path, &c_src_obj).context("Failed to compile C source code")?;
|
run_c_compile(&c_src_path, &c_src_obj, self.target_triple.clone())
|
||||||
|
.context("Failed to compile C source code")?;
|
||||||
LinkCode {
|
LinkCode {
|
||||||
object_paths: vec![c_src_obj, wasm_object_path],
|
object_paths: vec![c_src_obj, wasm_object_path],
|
||||||
output_path,
|
output_path,
|
||||||
additional_libraries: self.libraries.clone(),
|
additional_libraries: self.libraries.clone(),
|
||||||
|
target: self.target_triple.clone(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
.run()
|
.run()
|
||||||
@@ -155,6 +157,7 @@ fn get_wasmer_include_directory() -> anyhow::Result<PathBuf> {
|
|||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// path to the static libwasmer
|
||||||
fn get_libwasmer_path() -> anyhow::Result<PathBuf> {
|
fn get_libwasmer_path() -> anyhow::Result<PathBuf> {
|
||||||
let mut path = PathBuf::from(env::var("WASMER_DIR")?);
|
let mut path = PathBuf::from(env::var("WASMER_DIR")?);
|
||||||
path.push("lib");
|
path.push("lib");
|
||||||
@@ -168,21 +171,31 @@ fn get_libwasmer_path() -> anyhow::Result<PathBuf> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Compile the C code.
|
/// Compile the C code.
|
||||||
fn run_c_compile(path_to_c_src: &Path, output_name: &Path) -> anyhow::Result<()> {
|
fn run_c_compile(
|
||||||
|
path_to_c_src: &Path,
|
||||||
|
output_name: &Path,
|
||||||
|
target: Option<Triple>,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
let c_compiler = "cc";
|
let c_compiler = "cc";
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
let c_compiler = "clang++";
|
let c_compiler = "clang++";
|
||||||
|
|
||||||
let output = Command::new(c_compiler)
|
let mut command = Command::new(c_compiler);
|
||||||
|
let command = command
|
||||||
.arg("-O2")
|
.arg("-O2")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(path_to_c_src)
|
.arg(path_to_c_src)
|
||||||
.arg("-I")
|
.arg("-I")
|
||||||
.arg(get_wasmer_include_directory()?)
|
.arg(get_wasmer_include_directory()?);
|
||||||
.arg("-o")
|
|
||||||
.arg(output_name)
|
let command = if let Some(target) = target {
|
||||||
.output()?;
|
command.arg("-target").arg(format!("{}", target))
|
||||||
|
} else {
|
||||||
|
command
|
||||||
|
};
|
||||||
|
|
||||||
|
let output = command.arg("-o").arg(output_name).output()?;
|
||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
bail!(
|
bail!(
|
||||||
@@ -209,8 +222,10 @@ struct LinkCode {
|
|||||||
additional_libraries: Vec<String>,
|
additional_libraries: Vec<String>,
|
||||||
/// Path to the output target.
|
/// Path to the output target.
|
||||||
output_path: PathBuf,
|
output_path: PathBuf,
|
||||||
/// Path to the static libwasmer library.
|
/// Path to the dir containing the static libwasmer library.
|
||||||
libwasmer_path: PathBuf,
|
libwasmer_path: PathBuf,
|
||||||
|
/// The target to link the executable for.
|
||||||
|
target: Option<Triple>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for LinkCode {
|
impl Default for LinkCode {
|
||||||
@@ -226,6 +241,7 @@ impl Default for LinkCode {
|
|||||||
additional_libraries: vec![],
|
additional_libraries: vec![],
|
||||||
output_path: PathBuf::from("a.out"),
|
output_path: PathBuf::from("a.out"),
|
||||||
libwasmer_path: get_libwasmer_path().unwrap(),
|
libwasmer_path: get_libwasmer_path().unwrap(),
|
||||||
|
target: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,6 +264,11 @@ impl LinkCode {
|
|||||||
.canonicalize()
|
.canonicalize()
|
||||||
.context("Failed to find libwasmer")?,
|
.context("Failed to find libwasmer")?,
|
||||||
);
|
);
|
||||||
|
let command = if let Some(target) = &self.target {
|
||||||
|
command.arg("-target").arg(format!("{}", target))
|
||||||
|
} else {
|
||||||
|
command
|
||||||
|
};
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
let command = command.arg("-luserenv").arg("-lWs2_32").arg("-ladvapi32");
|
let command = command.arg("-luserenv").arg("-lWs2_32").arg("-ladvapi32");
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
|
|||||||
Reference in New Issue
Block a user