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

[DRAFT] add support for wasm32-unknown-emscripten #1469

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn wasm_bindgen_build(
};

let wasm_path = target_directory
.join("wasm32-unknown-unknown")
.join(data.get_target_triple())
.join(profile_name)
.join(data.crate_name())
.with_extension("wasm");
Expand Down
5 changes: 3 additions & 2 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ fn wasm_pack_local_version() -> Option<String> {
Some(output.to_string())
}

/// Run `cargo build` targetting `wasm32-unknown-unknown`.
/// Run `cargo build` targetting WebAssembly.
pub fn cargo_build_wasm(
path: &Path,
profile: BuildProfile,
target_triple: String,
extra_options: &[String],
) -> Result<()> {
let msg = format!("{}Compiling to Wasm...", emoji::CYCLONE);
Expand Down Expand Up @@ -109,7 +110,7 @@ pub fn cargo_build_wasm(
}
}

cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.arg("--target").arg(target_triple);

// The `cargo` command is executed inside the directory at `path`, so relative paths set via extra options won't work.
// To remedy the situation, all detected paths are converted to absolute paths.
Expand Down
45 changes: 24 additions & 21 deletions src/build/wasm_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ impl fmt::Display for Wasm32Check {

/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for
/// current toolchain
pub fn check_for_wasm32_target() -> Result<()> {
let msg = format!("{}Checking for the Wasm target...", emoji::TARGET);
pub fn check_for_wasm32_target(target_triple: String) -> Result<()> {
let msg = format!(
"{}Checking for the Wasm target {}...",
emoji::TARGET,
target_triple
);
PBAR.info(&msg);

// Check if wasm32 target is present, otherwise bail.
match check_wasm32_target() {
match check_wasm32_target(target_triple) {
Ok(ref wasm32_check) if wasm32_check.found => Ok(()),
Ok(wasm32_check) => bail!("{}", wasm32_check),
Err(err) => Err(err),
Expand All @@ -82,11 +86,11 @@ fn get_rustc_sysroot() -> Result<PathBuf> {
}

/// Get wasm32-unknown-unknown target libdir
fn get_rustc_wasm32_unknown_unknown_target_libdir() -> Result<PathBuf> {
fn get_rustc_target_triple_libdir(target_triple: String) -> Result<PathBuf> {
let command = Command::new("rustc")
.args(&[
"--target",
"wasm32-unknown-unknown",
target_triple.as_str(),
"--print",
"target-libdir",
])
Expand All @@ -96,27 +100,25 @@ fn get_rustc_wasm32_unknown_unknown_target_libdir() -> Result<PathBuf> {
Ok(String::from_utf8(command.stdout)?.trim().into())
} else {
Err(anyhow!(
"Getting rustc's wasm32-unknown-unknown target wasn't successful. Got {}",
"Getting rustc's {} target wasn't successful. Got {}",
target_triple,
command.status
))
}
}

fn does_wasm32_target_libdir_exist() -> bool {
let result = get_rustc_wasm32_unknown_unknown_target_libdir();
fn does_wasm32_target_libdir_exist(target_triple: String) -> bool {
let result = get_rustc_target_triple_libdir(target_triple.clone());

match result {
Ok(wasm32_target_libdir_path) => {
if wasm32_target_libdir_path.exists() {
info!(
"Found wasm32-unknown-unknown in {:?}",
wasm32_target_libdir_path
);
info!("Found {} in {:?}", target_triple, wasm32_target_libdir_path);
true
} else {
info!(
"Failed to find wasm32-unknown-unknown in {:?}",
wasm32_target_libdir_path
"Failed to find {} in {:?}",
target_triple, wasm32_target_libdir_path
);
false
}
Expand All @@ -128,11 +130,11 @@ fn does_wasm32_target_libdir_exist() -> bool {
}
}

fn check_wasm32_target() -> Result<Wasm32Check> {
fn check_wasm32_target(target_triple: String) -> Result<Wasm32Check> {
let sysroot = get_rustc_sysroot()?;
let rustc_path = which::which("rustc")?;

if does_wasm32_target_libdir_exist() {
if does_wasm32_target_libdir_exist(target_triple.clone()) {
Ok(Wasm32Check {
rustc_path,
sysroot,
Expand All @@ -144,7 +146,7 @@ fn check_wasm32_target() -> Result<Wasm32Check> {
// If sysroot contains "rustup", then we can assume we're using rustup
// and use rustup to add the wasm32-unknown-unknown target.
if sysroot.to_string_lossy().contains("rustup") {
rustup_add_wasm_target().map(|()| Wasm32Check {
rustup_add_wasm_target(target_triple).map(|()| Wasm32Check {
rustc_path,
sysroot,
found: true,
Expand All @@ -161,11 +163,12 @@ fn check_wasm32_target() -> Result<Wasm32Check> {
}
}

/// Add wasm32-unknown-unknown using `rustup`.
fn rustup_add_wasm_target() -> Result<()> {
/// Add target_triple using `rustup`.
fn rustup_add_wasm_target(target_triple: String) -> Result<()> {
let mut cmd = Command::new("rustup");
cmd.arg("target").arg("add").arg("wasm32-unknown-unknown");
child::run(cmd, "rustup").context("Adding the wasm32-unknown-unknown target with rustup")?;
cmd.arg("target").arg("add").arg(target_triple.clone());
child::run(cmd, "rustup")
.context(format!("Adding the {} target with rustup", target_triple))?;

Ok(())
}
91 changes: 72 additions & 19 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ impl Build {

/// Execute this `Build` command.
pub fn run(&mut self) -> Result<()> {
let process_steps = Build::get_process_steps(self.mode, self.no_pack, self.no_opt);
let process_steps = Build::get_process_steps(
self.mode,
self.no_pack,
self.no_opt,
self.crate_data.get_target_triple(),
);

let started = Instant::now();

Expand Down Expand Up @@ -299,6 +304,7 @@ impl Build {
mode: InstallMode,
no_pack: bool,
no_opt: bool,
target_triple: String,
) -> Vec<(&'static str, BuildStep)> {
macro_rules! steps {
($($name:ident),+) => {
Expand All @@ -322,23 +328,32 @@ impl Build {
}
}

steps.extend(steps![
step_build_wasm,
step_create_dir,
step_install_wasm_bindgen,
step_run_wasm_bindgen,
]);

if !no_opt {
steps.extend(steps![step_run_wasm_opt]);
}

if !no_pack {
if target_triple.ends_with("-emscripten") {
steps.extend(steps![
step_install_wasm_bindgen,
step_build_wasm,
step_create_dir,
step_copy_emscripten_outputs,
]);
} else {
steps.extend(steps![
step_create_json,
step_copy_readme,
step_copy_license,
step_build_wasm,
step_create_dir,
step_install_wasm_bindgen,
step_run_wasm_bindgen,
]);

if !no_opt {
steps.extend(steps![step_run_wasm_opt]);
}

if !no_pack {
steps.extend(steps![
step_create_json,
step_copy_readme,
step_copy_license,
]);
}
}

steps
Expand All @@ -361,21 +376,26 @@ impl Build {

fn step_check_for_wasm_target(&mut self) -> Result<()> {
info!("Checking for wasm-target...");
build::wasm_target::check_for_wasm32_target()?;
build::wasm_target::check_for_wasm32_target(self.crate_data.get_target_triple())?;
info!("Checking for wasm-target was successful.");
Ok(())
}

fn step_build_wasm(&mut self) -> Result<()> {
info!("Building wasm...");
build::cargo_build_wasm(&self.crate_path, self.profile.clone(), &self.extra_options)?;
build::cargo_build_wasm(
&self.crate_path,
self.profile.clone(),
self.crate_data.get_target_triple(),
&self.extra_options,
)?;

info!(
"wasm built at {:#?}.",
&self
.crate_path
.join("target")
.join("wasm32-unknown-unknown")
.join(self.crate_data.get_target_triple())
.join("release")
);
Ok(())
Expand Down Expand Up @@ -417,6 +437,13 @@ impl Build {
}

fn step_install_wasm_bindgen(&mut self) -> Result<()> {
// TODO(walkingeye): How does this work when this happens before
// building the Wasm? There is no Cargo.lock file at that point.
// TODO(walkingeye): How does this work when wasm-bindgen points to a
// local copy? It still is determining a version and installing it. This
// might be a problem I can ignore.
// TODO(walkingeye): Require a minimum version of wasm-bindgen for
// -emscripten support.
info!("Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_data)?;
let bindgen_version = lockfile.require_wasm_bindgen()?;
Expand Down Expand Up @@ -450,6 +477,32 @@ impl Build {
Ok(())
}

fn step_copy_emscripten_outputs(&mut self) -> Result<()> {
info!("Copying Emscripten outputs...");

let profile_name = match self.profile.clone() {
BuildProfile::Release | BuildProfile::Profiling => "release",
BuildProfile::Dev => "debug",
BuildProfile::Custom(profile_name) => &profile_name.clone(),
};

let wasm_path = self
.crate_data
.target_directory()
.join(self.crate_data.get_target_triple())
.join(profile_name)
.join(self.crate_data.crate_name());

// TODO(walkingeye): Actually do the copy here. Right now something
// is passing `-o /path/to/final.wasm` to the linker, which causes
// Emscripten to not output the .js at all. We can sort of override
// this was --oformat=js, but then the .js file has the .wasm extension.
// I suspect this is a bad assumption in rustc.

info!("Outputs were copied to {:#?}.", &self.out_dir);
Ok(())
}

fn step_run_wasm_opt(&mut self) -> Result<()> {
let mut args = match self
.crate_data
Expand Down
2 changes: 1 addition & 1 deletion src/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Test {

fn step_check_for_wasm_target(&mut self) -> Result<()> {
info!("Adding wasm-target...");
build::wasm_target::check_for_wasm32_target()?;
build::wasm_target::check_for_wasm32_target(self.crate_data.get_target_triple())?;
info!("Adding wasm-target was successful.");
Ok(())
}
Expand Down
Loading
Loading