Skip to content

Commit

Permalink
WasmBuilder::build retrun path to wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed Aug 29, 2024
1 parent cdd5b12 commit 54c584c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
47 changes: 25 additions & 22 deletions utils/wasm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,25 @@ impl WasmBuilder {
}

/// Build the program and produce an output WASM binary.
pub fn build(self) {
pub fn build(self) -> Option<PathBuf> {
if env::var("__GEAR_WASM_BUILDER_NO_BUILD").is_ok() || is_intellij_sync() {
self.wasm_project.provide_dummy_wasm_binary_if_not_exist();
return;
let path = self.wasm_project.provide_dummy_wasm_binary_if_not_exist();
return Some(path);
}

if let Err(e) = self.build_project() {
eprintln!("error: {e}");
e.chain()
.skip(1)
.for_each(|cause| eprintln!("| {cause}"));
process::exit(1);
match self.build_project() {
Err(e) => {
eprintln!("error: {e}");
e.chain()
.skip(1)
.for_each(|cause| eprintln!("| {cause}"));
process::exit(1);
}
Ok(r) => r,
}
}

fn build_project(mut self) -> Result<()> {
fn build_project(mut self) -> Result<Option<PathBuf>> {
self.wasm_project.generate()?;

self.cargo
Expand Down Expand Up @@ -236,46 +239,46 @@ fn is_intellij_sync() -> bool {
const FEATURES_TO_EXCLUDE_BY_DEFAULT: &[&str] = &["std"];

/// Shorthand function to be used in `build.rs`.
pub fn build() {
pub fn build() -> Option<PathBuf> {
WasmBuilder::new()
.exclude_features(FEATURES_TO_EXCLUDE_BY_DEFAULT.to_vec())
.build();
.build()
}

/// Shorthand function to be used in `build.rs`.
pub fn build_with_metadata<T: Metadata>() {
pub fn build_with_metadata<T: Metadata>() -> Option<PathBuf> {
WasmBuilder::with_meta(T::repr())
.exclude_features(FEATURES_TO_EXCLUDE_BY_DEFAULT.to_vec())
.build();
.build()
}

/// Shorthand function to be used in `build.rs`.
pub fn build_metawasm() {
pub fn build_metawasm() -> Option<PathBuf> {
WasmBuilder::new_metawasm()
.exclude_features(FEATURES_TO_EXCLUDE_BY_DEFAULT.to_vec())
.build();
.build()
}

/// Shorthand function to be used in `build.rs`.
pub fn recommended_nightly() {
pub fn recommended_nightly() -> Option<PathBuf> {
WasmBuilder::new()
.exclude_features(FEATURES_TO_EXCLUDE_BY_DEFAULT.to_vec())
.with_recommended_toolchain()
.build();
.build()
}

/// Shorthand function to be used in `build.rs`.
pub fn recommended_nightly_with_metadata<T: Metadata>() {
pub fn recommended_nightly_with_metadata<T: Metadata>() -> Option<PathBuf> {
WasmBuilder::with_meta(T::repr())
.exclude_features(FEATURES_TO_EXCLUDE_BY_DEFAULT.to_vec())
.with_recommended_toolchain()
.build();
.build()
}

/// Shorthand function to be used in `build.rs`.
pub fn recommended_nightly_metawasm() {
pub fn recommended_nightly_metawasm() -> Option<PathBuf> {
WasmBuilder::new_metawasm()
.exclude_features(FEATURES_TO_EXCLUDE_BY_DEFAULT.to_vec())
.with_recommended_toolchain()
.build();
.build()
}
28 changes: 13 additions & 15 deletions utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ extern "C" fn metahash() {{
pub const WASM_BINARY: &[u8] = include_bytes!("{}");
#[allow(unused)]
pub const WASM_EXPORTS: &[&str] = &{:?};"#,
display_path(meta_wasm_path.clone()),
display_path(meta_wasm_path.as_path()),
Self::get_exports(&meta_wasm_path)?,
),
)
Expand Down Expand Up @@ -428,8 +428,8 @@ extern "C" fn metahash() {{
#[allow(unused)]
pub const WASM_BINARY_OPT: &[u8] = include_bytes!("{}");
{}"#,
display_path(original_copy_wasm_path),
display_path(opt_wasm_path),
display_path(original_copy_wasm_path.as_path()),
display_path(opt_wasm_path.as_path()),
metadata,
),
)
Expand All @@ -442,7 +442,7 @@ extern "C" fn metahash() {{
/// `target/wasm32-unknown-unknown/<profile>`
/// - Generate optimized and metadata WASM binaries from the built program
/// - Generate `wasm_binary.rs` source file in `OUT_DIR`
pub fn postprocess(&self) -> Result<()> {
pub fn postprocess(&self) -> Result<Option<PathBuf>> {
let file_base_name = self
.file_base_name
.as_ref()
Expand Down Expand Up @@ -516,7 +516,7 @@ extern "C" fn metahash() {{
}
}

for (wasm_path, _) in wasm_files {
for (wasm_path, _) in &wasm_files {
let code = fs::read(wasm_path)?;
let validator = CodeValidator::try_from(code)?;

Expand All @@ -531,7 +531,7 @@ extern "C" fn metahash() {{
self.force_rerun_on_next_run(&original_wasm_path)?;
}

Ok(())
Ok(wasm_files.into_iter().map(|(path, _)| path).last())
}

fn get_exports(file: &PathBuf) -> Result<Vec<String>> {
Expand Down Expand Up @@ -566,10 +566,10 @@ extern "C" fn metahash() {{
}

/// Provide a dummy WASM binary if there doesn't exist one.
pub fn provide_dummy_wasm_binary_if_not_exist(&self) {
pub fn provide_dummy_wasm_binary_if_not_exist(&self) -> PathBuf {
let wasm_binary_rs = self.out_dir.join("wasm_binary.rs");
if wasm_binary_rs.exists() {
return;
return wasm_binary_rs;
}

let content = if !self.project_type.is_metawasm() {
Expand All @@ -586,17 +586,15 @@ extern "C" fn metahash() {{
pub const WASM_EXPORTS: &[&str] = &[];
"#
};
fs::write(wasm_binary_rs.as_path(), content).unwrap_or_else(|_| {
panic!(
"Writing `{}` should not fail!",
display_path(wasm_binary_rs)
)
});
let path = wasm_binary_rs.as_path();
fs::write(path, content)
.unwrap_or_else(|_| panic!("Writing `{}` should not fail!", display_path(path)));
wasm_binary_rs
}
}

// Windows has path like `path\to\somewhere` which is incorrect for `include_*`
// Rust's macros
fn display_path(path: PathBuf) -> String {
fn display_path(path: &Path) -> String {
path.display().to_string().replace('\\', "/")
}

0 comments on commit 54c584c

Please sign in to comment.