| 7 | use std::{env, path::PathBuf, process::Command}; |
| 8 | |
| 9 | fn main() { |
| 10 | println!("cargo::rerun-if-changed=build.rs"); |
| 11 | |
| 12 | if env::var("CARGO_CFG_TARGET_ARCH").as_deref() == Ok("wasm32") { |
| 13 | return; |
| 14 | } |
| 15 | |
| 16 | if env::var_os("CARGO_FEATURE_PREBUILT").is_none() { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | let links = env::var("CARGO_MANIFEST_LINKS").expect("set `links` in [package] of Cargo.toml"); |
| 21 | let asset = format!("{links}.wasm"); |
| 22 | let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set")); |
| 23 | let dst = out_dir.join(&asset); |
| 24 | |
| 25 | if !dst.exists() { |
| 26 | let repo = env::var("CARGO_PKG_REPOSITORY") |
| 27 | .expect("set `repository` in [package] of Cargo.toml") |
| 28 | .trim_end_matches('/') |
| 29 | .to_string(); |
| 30 | let version = env::var("CARGO_PKG_VERSION").expect("CARGO_PKG_VERSION not set"); |
| 31 | let url = format!("{repo}/releases/download/v{version}/{asset}"); // Include `/v{version}` to follow the GitHub tags format. |
| 32 | |
| 33 | let status = Command::new("curl") |
| 34 | .args(["-fsSL", "-o"]) |
| 35 | .arg(&dst) |
| 36 | .arg(&url) |
| 37 | .status() |
| 38 | .expect("`curl` must be on PATH to fetch the pre-built wasm"); |
| 39 | |
| 40 | if !status.success() { |
| 41 | let _ = std::fs::remove_file(&dst); |
| 42 | panic!( |
| 43 | "failed to download {asset} from {url}\n\ |
| 44 | hint: ensure the release tag v{version} exists with `{asset}` \ |
| 45 | as an asset, or pre-place the binary at {}", |
| 46 | dst.display() |
| 47 | ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | println!("cargo::metadata=wasm={}", dst.display()); |
| 52 | } |