Skip to content

Commit

Permalink
Try to fix include paths for new wasmer-windows target
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Nov 14, 2022
1 parent cb6fda1 commit ddf4b33
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-sys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
- name: Set up base deps on musl
if: matrix.build == 'linux-musl-x64'
run: |
apk add build-base bash musl-dev curl make libtool libffi-dev gcc gcc-multilib automake autoconf git openssl-dev g++
apk add build-base bash musl-dev curl make libtool libffi-dev gcc build-base automake autoconf git openssl-dev g++
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ regex = "1.6.0"
toml = "0.5.9"
url = "2.3.1"
libc = { version = "^0.2", default-features = false }
nuke-dir = { version = "0.1.0", optional = true }
nuke-dir = "0.1.0"
webc = { version = "3.0.1", optional = true }
isatty = "0.1.9"
tar = "0.4"

[build-dependencies]
chrono = { version = "^0.4", default-features = false, features = [ "std", "clock" ] }
Expand Down Expand Up @@ -98,7 +99,7 @@ wast = ["wasmer-wast"]
wasi = ["wasmer-wasi"]
emscripten = ["wasmer-emscripten"]
wat = ["wasmer/wat"]
webc_runner = ["wasi", "wasmer-wasi/webc_runner", "wasmer-wasi/webc_runner_rt_wasi", "wasmer-wasi/webc_runner_rt_emscripten", "nuke-dir", "webc"]
webc_runner = ["wasi", "wasmer-wasi/webc_runner", "wasmer-wasi/webc_runner_rt_wasi", "wasmer-wasi/webc_runner_rt_emscripten", "webc"]
compiler = [
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
Expand Down
155 changes: 114 additions & 41 deletions lib/cli/src/commands/create_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use clap::Parser;
use serde::{Deserialize, Serialize};
#[cfg(feature = "http")]
use std::collections::BTreeMap;
use std::default;
use std::env;
use std::fs;
use std::fs::File;
Expand Down Expand Up @@ -563,31 +564,71 @@ impl CreateExe {
include_dir.pop();
include_dir.push("include");

let mut default_include_path = None;

#[cfg(target_os = "macos")]
{
if let Ok(output) = Command::new("xcrun").arg("--show-sdk-path").output() {
default_include_path =
Some(String::from_utf8_lossy(&output.stdout).to_string());
}
}

#[cfg(target_os = "linux")]
{
default_include_path = Some("/usr/include".to_string());
}

#[cfg(target_os = "windows")]
{
// TODO: discover include path?
default_include_path = Some(
"C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17134.0\\ucrt"
.to_string(),
);
}

let default_include_path = match default_include_path {
Some(s) => Some(s),
None => {
if zig_triple.contains("windows") {
return Err(anyhow::anyhow!(
"no default include path for target windows-x64"
));
} else {
None
}
}
};

let mut cmd = Command::new(zig_binary_path);
let mut cmd_mut: &mut Command = cmd
.arg("cc")
.arg("-target")
.arg(&zig_triple)
.arg(&format!("-L{}", libwasmer_path.display()))
.arg(&format!("-l:{}", lib_filename))
// xcrun --show-sdk-path
// .arg("-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/usr/include")
.arg("-I/usr/include")
.arg(&format!("-I{}", include_dir.display()))
.arg(&format!("-I{}", header_code_path.display()));
cmd.arg("cc");
cmd.arg("-target");
cmd.arg(&zig_triple);
cmd.arg(&format!("-L{}", libwasmer_path.display()));
cmd.arg(&format!("-l:{}", lib_filename));

if let Some(default_include) = default_include_path {
cmd.arg(format!("-I{default_include}"));
}

cmd.arg(&format!("-I{}", include_dir.display()));
cmd.arg(&format!("-I{}", header_code_path.display()));

if !zig_triple.contains("windows") {
cmd_mut = cmd_mut.arg("-lunwind");
cmd.arg("-lunwind");
}
cmd_mut = cmd_mut.arg(&object_path).arg(&c_src_path);

cmd.arg(&object_path);
cmd.arg(&c_src_path);

if let Some(volume_obj) = pirita_volume_path.as_ref() {
cmd_mut = cmd_mut.arg(volume_obj.clone());
cmd.arg(volume_obj.clone());
}

println!("cmd (zig cc): {:?}", cmd_mut);
println!("cmd (zig cc): {:?}", cmd);

cmd_mut
.arg("-o")
cmd.arg("-o")
.arg(&output_path)
.output()
.context("Could not execute `zig`")?
Expand Down Expand Up @@ -1363,6 +1404,8 @@ mod http_fetch {
};
use std::convert::TryFrom;

use crate::commands::cache;

pub fn get_latest_release() -> Result<serde_json::Value> {
let mut writer = Vec::new();
let uri = Uri::try_from("https://api.github.com/repos/wasmerio/wasmer/releases").unwrap();
Expand Down Expand Up @@ -1404,6 +1447,7 @@ mod http_fetch {
mut release: serde_json::Value,
target_triple: wasmer::Triple,
) -> Result<std::path::PathBuf> {
use std::path::Path;
let check_arch = |name: &str| -> bool {
match target_triple.architecture {
wasmer_types::Architecture::X86_64 => {
Expand Down Expand Up @@ -1443,18 +1487,28 @@ mod http_fetch {
};

if let Ok(mut cache_path) = super::get_libwasmer_cache_path() {
match std::fs::read_dir(&cache_path).and_then(|r| {
println!("cache path: {}", cache_path.display());
let paths = std::fs::read_dir(&cache_path).and_then(|r| {
r.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, std::io::Error>>()
}) {
});

println!("paths: {:#?}", paths);
match paths {
Ok(mut entries) => {
entries.retain(|p| p.to_str().map(|p| check_arch(p)).unwrap_or(true));
entries.retain(|p| p.to_str().map(|p| check_vendor(p)).unwrap_or(true));
entries.retain(|p| p.to_str().map(|p| check_os(p)).unwrap_or(true));
entries.retain(|p| p.to_str().map(|p| check_env(p)).unwrap_or(true));
if entries.len() > 0 {
cache_path.push(&entries[0]);
println!("testing for cache path 2: {}", cache_path.display());
if cache_path.exists() {
let target = Path::new(
&format!("{}", cache_path.display()).replace(".tar.gz", ""),
)
.to_path_buf();
super::untar(cache_path.clone(), target)?;
eprintln!(
"Using cached tarball to cache path `{}`.",
cache_path.display()
Expand Down Expand Up @@ -1510,8 +1564,10 @@ mod http_fetch {
.last()
.unwrap_or("output")
.to_string();
let mut file = std::fs::File::create(&filename)?;
println!("Downloading {} to {}", browser_download_url, &filename);
let download_tempdir = tempdir::TempDir::new("wasmer-download")?;
let download_path = download_tempdir.path().to_path_buf().join(&filename);
let mut file = std::fs::File::create(&download_path)?;
println!("Downloading {} to {}", browser_download_url, download_path.display());
let download_thread: std::thread::JoinHandle<Result<Response, anyhow::Error>> =
std::thread::spawn(move || {
let uri = Uri::try_from(browser_download_url.as_str())?;
Expand All @@ -1536,13 +1592,17 @@ mod http_fetch {
Ok(mut cache_path) => {
cache_path.push(&filename);
if !cache_path.exists() {
if let Err(err) = std::fs::copy(&filename, &cache_path) {
eprintln!("copying from {} to {}", download_path.display(), cache_path.display());
if let Err(err) = std::fs::copy(&download_path, &cache_path) {
eprintln!(
"Could not store tarball to cache path `{}`: {}",
cache_path.display(),
err
);
} else {
eprintln!("copying to /Development");
std::fs::copy(&cache_path, "~/Development/wasmer-windows.tar.gz")
.unwrap();
eprintln!(
"Cached tarball to cache path `{}`.",
cache_path.display()
Expand All @@ -1563,6 +1623,11 @@ mod http_fetch {
match super::get_libwasmer_cache_path() {
Ok(mut cache_path) => {
cache_path.push(&filename);
println!(
"testing for cache path {}: {:?}",
cache_path.display(),
cache_path.exists()
);
if !cache_path.exists() {
if let Err(err) = std::fs::copy(&filename, &cache_path) {
eprintln!(
Expand All @@ -1575,6 +1640,9 @@ mod http_fetch {
"Cached tarball to cache path `{}`.",
cache_path.display()
);
eprintln!("copying to /Development");
std::fs::copy(&cache_path, "~/Development/wasmer-windows.tar.gz")
.unwrap();
}
}
}
Expand All @@ -1593,29 +1661,34 @@ mod http_fetch {
}

fn untar(tarball: std::path::PathBuf, target: std::path::PathBuf) -> Result<Vec<String>> {
let files = std::process::Command::new("tar")
.arg("-tf")
.arg(&tarball)
.output()
.expect("failed to execute process")
.stdout;
println!("untar: {} -> {}", tarball.display(), target.display());

let files = {
let file = File::open(&tarball)?;
let mut a = tar::Archive::new(file);
a.entries_with_seek()?
.filter_map(|entry| Some(entry.ok()?.path().ok()?.to_path_buf()))
.map(|p| format!("{}", p.display()))
.filter(|p| !p.ends_with('/'))
.collect::<Vec<_>>()
};

let files_s = String::from_utf8(files)?;
let _ = nuke_dir::nuke_dir(&target);
let _ = std::fs::remove_dir(&target);

let files = files_s
.lines()
.filter(|p| !p.ends_with('/'))
.map(|s| s.to_string())
.collect::<Vec<String>>();
if files.is_empty() {
let _ = std::fs::remove_file(&tarball);
return Ok(files);
}

let _ = std::fs::create_dir_all(&target);
let _output = std::process::Command::new("tar")
.arg("-xf")
.arg(&tarball)
.arg("-C")
.arg(&target)
.output()
.expect("failed to execute process");

println!("files ok!");

let file = File::open(&tarball)?;
let mut a = tar::Archive::new(file);
a.unpack(target)?;
println!("untar ok! {:#?}", files);
Ok(files)
}

Expand Down
3 changes: 1 addition & 2 deletions tests/integration/cli/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ fn test_no_start_wat_path() -> String {

#[test]
fn test_cross_compile_python_windows() -> anyhow::Result<()> {

let temp_dir = tempfile::TempDir::new()?;
let python_wasmer_path = temp_dir.path().join("python.exe");

Expand All @@ -35,7 +34,7 @@ fn test_cross_compile_python_windows() -> anyhow::Result<()> {
.arg("-o")
.arg(python_wasmer_path)
.output()?;

if !output.status.success() {
bail!(
"linking failed with: stdout: {}\n\nstderr: {}",
Expand Down

0 comments on commit ddf4b33

Please sign in to comment.