2021-07-07 20:54:14 +00:00
|
|
|
use crate::{
|
2021-12-29 17:03:18 +00:00
|
|
|
config::{CrateConfig, ExecutableType},
|
|
|
|
error::{Error, Result},
|
2021-07-07 20:54:14 +00:00
|
|
|
};
|
2022-01-22 17:29:36 +00:00
|
|
|
use std::{io::Write, process::Command};
|
2021-07-07 20:54:14 +00:00
|
|
|
use wasm_bindgen_cli_support::Bindgen;
|
|
|
|
|
2021-12-29 16:04:18 +00:00
|
|
|
pub fn build(config: &CrateConfig) -> Result<()> {
|
2021-07-07 20:54:14 +00:00
|
|
|
/*
|
|
|
|
[1] Build the project with cargo, generating a wasm32-unknown-unknown target (is there a more specific, better target to leverage?)
|
|
|
|
[2] Generate the appropriate build folders
|
|
|
|
[3] Wasm-bindgen the .wasm fiile, and move it into the {builddir}/modules/xxxx/xxxx_bg.wasm
|
|
|
|
[4] Wasm-opt the .wasm file with whatever optimizations need to be done
|
|
|
|
[5] Link up the html page to the wasm module
|
|
|
|
*/
|
|
|
|
|
2021-12-29 17:03:18 +00:00
|
|
|
let CrateConfig {
|
2021-07-07 20:54:14 +00:00
|
|
|
out_dir,
|
|
|
|
crate_dir,
|
|
|
|
target_dir,
|
|
|
|
static_dir,
|
|
|
|
executable,
|
|
|
|
..
|
|
|
|
} = config;
|
|
|
|
|
|
|
|
let t_start = std::time::Instant::now();
|
|
|
|
|
|
|
|
// [1] Build the .wasm module
|
2022-01-22 14:19:59 +00:00
|
|
|
log::info!("🚅 Running build commands...");
|
2021-07-07 20:54:14 +00:00
|
|
|
let mut cmd = Command::new("cargo");
|
|
|
|
cmd.current_dir(&crate_dir)
|
|
|
|
.arg("build")
|
|
|
|
.arg("--target")
|
|
|
|
.arg("wasm32-unknown-unknown")
|
2022-01-22 14:19:59 +00:00
|
|
|
.stdout(std::process::Stdio::inherit())
|
2022-01-22 17:29:36 +00:00
|
|
|
.stderr(std::process::Stdio::inherit());
|
2021-07-07 20:54:14 +00:00
|
|
|
|
|
|
|
if config.release {
|
|
|
|
cmd.arg("--release");
|
|
|
|
}
|
|
|
|
|
|
|
|
match executable {
|
|
|
|
ExecutableType::Binary(name) => cmd.arg("--bin").arg(name),
|
|
|
|
ExecutableType::Lib(name) => cmd.arg("--lib").arg(name),
|
|
|
|
ExecutableType::Example(name) => cmd.arg("--example").arg(name),
|
|
|
|
};
|
|
|
|
|
2022-01-22 14:19:59 +00:00
|
|
|
let output = cmd.output()?;
|
2022-01-22 02:02:52 +00:00
|
|
|
|
2022-01-22 14:19:59 +00:00
|
|
|
if output.status.success() {
|
|
|
|
std::io::stdout().write_all(&output.stdout).unwrap();
|
2021-12-29 17:03:18 +00:00
|
|
|
} else {
|
|
|
|
log::error!("Build failed!");
|
2022-01-22 14:19:59 +00:00
|
|
|
let reason = String::from_utf8_lossy(&output.stderr).to_string();
|
2021-12-29 17:03:18 +00:00
|
|
|
return Err(Error::BuildFailed(reason));
|
|
|
|
}
|
2021-07-07 20:54:14 +00:00
|
|
|
|
|
|
|
// [2] Establish the output directory structure
|
2022-01-22 13:23:30 +00:00
|
|
|
let bindgen_outdir = out_dir.join("assets");
|
2021-07-07 20:54:14 +00:00
|
|
|
|
|
|
|
// [3] Bindgen the final binary for use easy linking
|
|
|
|
let mut bindgen_builder = Bindgen::new();
|
|
|
|
|
|
|
|
let release_type = match config.release {
|
|
|
|
true => "release",
|
|
|
|
false => "debug",
|
|
|
|
};
|
|
|
|
|
|
|
|
let input_path = match executable {
|
|
|
|
ExecutableType::Binary(name) | ExecutableType::Lib(name) => target_dir
|
|
|
|
.join(format!("wasm32-unknown-unknown/{}", release_type))
|
|
|
|
.join(format!("{}.wasm", name)),
|
|
|
|
|
|
|
|
ExecutableType::Example(name) => target_dir
|
|
|
|
.join(format!("wasm32-unknown-unknown/{}/examples", release_type))
|
|
|
|
.join(format!("{}.wasm", name)),
|
|
|
|
};
|
|
|
|
|
|
|
|
bindgen_builder
|
|
|
|
.input_path(input_path)
|
|
|
|
.web(true)?
|
|
|
|
.debug(true)
|
|
|
|
.demangle(true)
|
|
|
|
.keep_debug(true)
|
|
|
|
.remove_name_section(false)
|
|
|
|
.remove_producers_section(false)
|
|
|
|
.out_name("module")
|
|
|
|
.generate(&bindgen_outdir)?;
|
|
|
|
|
|
|
|
// [4]
|
|
|
|
// TODO: wasm-opt
|
|
|
|
|
|
|
|
// [5] Generate the html file with the module name
|
|
|
|
// TODO: support names via options
|
|
|
|
|
2022-01-22 14:19:59 +00:00
|
|
|
// log::info!("Writing to '{:#?}' directory...", out_dir);
|
2021-07-07 20:54:14 +00:00
|
|
|
let copy_options = fs_extra::dir::CopyOptions::new();
|
2022-01-22 02:02:52 +00:00
|
|
|
if static_dir.is_dir() {
|
|
|
|
match fs_extra::dir::copy(static_dir, out_dir, ©_options) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(_e) => {
|
|
|
|
log::warn!("Error copying dir: {}", _e);
|
|
|
|
}
|
2021-07-07 20:54:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let t_end = std::time::Instant::now();
|
2022-01-22 14:19:59 +00:00
|
|
|
log::info!("🏁 Done in {}ms!", (t_end - t_start).as_millis());
|
2021-07-07 20:54:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-22 13:23:30 +00:00
|
|
|
pub fn gen_page(module: &str) -> String {
|
2021-07-07 20:54:14 +00:00
|
|
|
format!(
|
|
|
|
r#"
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
|
|
|
|
<meta charset="UTF-8" />
|
|
|
|
</head>
|
|
|
|
<body>
|
2021-12-29 16:04:18 +00:00
|
|
|
<div id="main">
|
2021-12-29 17:03:18 +00:00
|
|
|
</div>
|
2021-07-07 20:54:14 +00:00
|
|
|
<!-- Note the usage of `type=module` here as this is an ES6 module -->
|
|
|
|
<script type="module">
|
|
|
|
import init from "{}";
|
2022-01-22 13:23:30 +00:00
|
|
|
init("./assets/module_bg.wasm");
|
2021-07-07 20:54:14 +00:00
|
|
|
</script>
|
2021-10-10 19:42:16 +00:00
|
|
|
<div id="dioxusroot"> </div>
|
2021-07-07 20:54:14 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"#,
|
|
|
|
module
|
|
|
|
)
|
|
|
|
}
|
2021-12-29 16:04:18 +00:00
|
|
|
|
|
|
|
// use binary_install::{Cache, Download};
|
|
|
|
|
|
|
|
// /// Attempts to find `wasm-opt` in `PATH` locally, or failing that downloads a
|
|
|
|
// /// precompiled binary.
|
|
|
|
// ///
|
|
|
|
// /// Returns `Some` if a binary was found or it was successfully downloaded.
|
|
|
|
// /// Returns `None` if a binary wasn't found in `PATH` and this platform doesn't
|
|
|
|
// /// have precompiled binaries. Returns an error if we failed to download the
|
|
|
|
// /// binary.
|
|
|
|
// pub fn find_wasm_opt(
|
|
|
|
// cache: &Cache,
|
|
|
|
// install_permitted: bool,
|
|
|
|
// ) -> Result<install::Status, failure::Error> {
|
|
|
|
// // First attempt to look up in PATH. If found assume it works.
|
|
|
|
// if let Ok(path) = which::which("wasm-opt") {
|
|
|
|
// PBAR.info(&format!("found wasm-opt at {:?}", path));
|
|
|
|
|
|
|
|
// match path.as_path().parent() {
|
|
|
|
// Some(path) => return Ok(install::Status::Found(Download::at(path))),
|
|
|
|
// None => {}
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// let version = "version_78";
|
|
|
|
// Ok(install::download_prebuilt(
|
|
|
|
// &install::Tool::WasmOpt,
|
|
|
|
// cache,
|
|
|
|
// version,
|
|
|
|
// install_permitted,
|
|
|
|
// )?)
|
|
|
|
// }
|