|
| 1 | +use heck::ToShoutySnakeCase; |
| 2 | +use std::env::var_os; |
| 3 | +use std::path::PathBuf; |
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + let out_dir = PathBuf::from(var_os("OUT_DIR").expect("OUT_DIR env var exists")); |
| 8 | + |
| 9 | + let meta = cargo_metadata::MetadataCommand::new() |
| 10 | + .exec() |
| 11 | + .expect("cargo metadata"); |
| 12 | + let test_programs_meta = meta |
| 13 | + .packages |
| 14 | + .iter() |
| 15 | + .find(|p| p.name == "test-programs") |
| 16 | + .expect("test-programs is in cargo metadata"); |
| 17 | + let test_programs_root = test_programs_meta.manifest_path.parent().unwrap(); |
| 18 | + println!( |
| 19 | + "cargo:rerun-if-changed={}", |
| 20 | + test_programs_root.as_os_str().to_str().unwrap() |
| 21 | + ); |
| 22 | + |
| 23 | + let status = Command::new("cargo") |
| 24 | + .arg("build") |
| 25 | + .arg("--target=wasm32-wasip2") |
| 26 | + .arg("--package=test-programs") |
| 27 | + .env("CARGO_TARGET_DIR", &out_dir) |
| 28 | + .env("CARGO_PROFILE_DEV_DEBUG", "2") |
| 29 | + .env("RUSTFLAGS", rustflags()) |
| 30 | + .env_remove("CARGO_ENCODED_RUSTFLAGS") |
| 31 | + .status() |
| 32 | + .expect("cargo build test programs"); |
| 33 | + assert!(status.success()); |
| 34 | + |
| 35 | + let mut generated_code = "// THIS FILE IS GENERATED CODE\n".to_string(); |
| 36 | + |
| 37 | + for binary in test_programs_meta |
| 38 | + .targets |
| 39 | + .iter() |
| 40 | + .filter(|t| t.kind == ["bin"]) |
| 41 | + { |
| 42 | + let component_path = out_dir |
| 43 | + .join("wasm32-wasip2") |
| 44 | + .join("debug") |
| 45 | + .join(format!("{}.wasm", binary.name)); |
| 46 | + |
| 47 | + let const_name = binary.name.to_shouty_snake_case(); |
| 48 | + generated_code += &format!( |
| 49 | + "pub const {const_name}: &str = {:?};\n", |
| 50 | + component_path.as_os_str().to_str().expect("path is str") |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + std::fs::write(out_dir.join("gen.rs"), generated_code).unwrap(); |
| 55 | +} |
| 56 | + |
| 57 | +fn rustflags() -> &'static str { |
| 58 | + match option_env!("RUSTFLAGS") { |
| 59 | + Some(s) if s.contains("-D warnings") => "-D warnings", |
| 60 | + _ => "", |
| 61 | + } |
| 62 | +} |
0 commit comments