wasmer_integration_tests_cli/
link_code.rs1use crate::assets::*;
2use anyhow::bail;
3use std::path::PathBuf;
4use std::process::Command;
5
6#[derive(Debug)]
8pub struct LinkCode {
9 pub current_dir: PathBuf,
11 pub linker_path: PathBuf,
13 pub optimization_flag: String,
15 pub object_paths: Vec<PathBuf>,
17 pub output_path: PathBuf,
19 pub libwasmer_path: PathBuf,
21}
22
23impl Default for LinkCode {
24 fn default() -> Self {
25 #[cfg(not(windows))]
26 let linker = "cc";
27 #[cfg(windows)]
28 let linker = "clang";
29 Self {
30 current_dir: std::env::current_dir().unwrap(),
31 linker_path: PathBuf::from(linker),
32 optimization_flag: String::from("-O2"),
33 object_paths: vec![],
34 output_path: PathBuf::from("a.out"),
35 libwasmer_path: get_libwasmer_path(),
36 }
37 }
38}
39
40impl LinkCode {
41 pub fn run(&self) -> anyhow::Result<()> {
42 let mut command = Command::new(&self.linker_path);
43 let command = command
44 .current_dir(&self.current_dir)
45 .arg(&self.optimization_flag)
46 .args(
47 self.object_paths
48 .iter()
49 .map(|path| path.canonicalize().unwrap()),
50 )
51 .arg(&self.libwasmer_path.canonicalize()?);
52 #[cfg(windows)]
53 let command = command
54 .arg("-luserenv")
55 .arg("-lWs2_32")
56 .arg("-ladvapi32")
57 .arg("-lbcrypt");
58 #[cfg(not(windows))]
59 let command = command.arg("-ldl").arg("-lm").arg("-pthread");
60 let output = command.arg("-o").arg(&self.output_path).output()?;
61
62 if !output.status.success() {
63 bail!(
64 "linking failed with: stdout: {}\n\nstderr: {}",
65 std::str::from_utf8(&output.stdout)
66 .expect("stdout is not utf8! need to handle arbitrary bytes"),
67 std::str::from_utf8(&output.stderr)
68 .expect("stderr is not utf8! need to handle arbitrary bytes")
69 );
70 }
71 Ok(())
72 }
73}