Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a --target option to wasm2obj #18

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ serde = "1.0.75"
serde_derive = "1.0.75"
tempdir = "*"
faerie = "0.5.0"
target-lexicon = { version = "0.0.3", default-features = false }

[workspace]
43 changes: 35 additions & 8 deletions src/wasm2obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ extern crate wasmtime_obj;
#[macro_use]
extern crate serde_derive;
extern crate faerie;
extern crate target_lexicon;

use cranelift_codegen::settings;
use cranelift_codegen::isa;
use cranelift_codegen::settings::{self, Configurable};
use docopt::Docopt;
use faerie::Artifact;
use std::error::Error;
Expand All @@ -52,6 +54,8 @@ use std::io::prelude::*;
use std::path::Path;
use std::path::PathBuf;
use std::process;
use std::str::FromStr;
use target_lexicon::{OperatingSystem, Triple};
use wasmtime_environ::{compile_module, Module, ModuleEnvironment};
use wasmtime_obj::emit_module;

Expand All @@ -62,19 +66,21 @@ The translation is dependent on the environment chosen.
The default is a dummy environment that produces placeholder values.

Usage:
wasm2obj <file> -o <output>
wasm2obj [--target TARGET] <file> -o <output>
wasm2obj --help | --version

Options:
-v, --verbose displays the module and translated functions
-h, --help print this help message
--target <TARGET> build for the target triple; default is the host machine
--version print the Cranelift version
";

#[derive(Deserialize, Debug, Clone)]
struct Args {
arg_file: String,
arg_output: String,
arg_target: Option<String>,
}

fn read_wasm_file(path: PathBuf) -> Result<Vec<u8>, io::Error> {
Expand All @@ -93,7 +99,7 @@ fn main() {
}).unwrap_or_else(|e| e.exit());

let path = Path::new(&args.arg_file);
match handle_module(path.to_path_buf(), &args.arg_output) {
match handle_module(path.to_path_buf(), &args.arg_target, &args.arg_output) {
Ok(()) => {}
Err(message) => {
println!(" error: {}", message);
Expand All @@ -102,18 +108,39 @@ fn main() {
}
}

fn handle_module(path: PathBuf, output: &str) -> Result<(), String> {
fn handle_module(path: PathBuf, target: &Option<String>, output: &str) -> Result<(), String> {
let data = match read_wasm_file(path) {
Ok(data) => data,
Err(err) => {
return Err(String::from(err.description()));
}
};

// FIXME: Make the target a parameter.
let (flag_builder, isa_builder) = cranelift_native::builders().unwrap_or_else(|_| {
panic!("host machine is not a supported target");
});
let (flag_builder, isa_builder) = match *target {
Some(ref target) => {
let target = Triple::from_str(&target).map_err(|_| "could not parse --target")?;
let mut flag_builder = settings::builder();
match target.operating_system {
OperatingSystem::Windows => {
flag_builder.set("call_conv", "windows_fastcall").unwrap();
}
_ => {
flag_builder.set("call_conv", "system_v").unwrap();
}
};
froydnj marked this conversation as resolved.
Show resolved Hide resolved

let isa_builder = isa::lookup(target).map_err(|err| match err {
isa::LookupError::SupportDisabled => {
"support for architecture disabled at compile time"
}
isa::LookupError::Unsupported => "unsupported architecture",
})?;
(flag_builder, isa_builder)
}
None => cranelift_native::builders().unwrap_or_else(|_| {
panic!("host machine is not a supported target");
}),
};
let isa = isa_builder.finish(settings::Flags::new(flag_builder));

let mut obj = Artifact::new(isa.triple().clone(), String::from(output));
Expand Down