|
| 1 | +//! Build script. |
| 2 | +#![allow(unused_crate_dependencies)] |
| 3 | + |
| 4 | +use std::{ |
| 5 | + collections::HashMap, |
| 6 | + path::{Path, PathBuf}, |
| 7 | + process::{Command, Stdio}, |
| 8 | + str::FromStr, |
| 9 | +}; |
| 10 | + |
| 11 | +fn main() { |
| 12 | + let profile: Profile = std::env::var("PROFILE").unwrap().parse().unwrap(); |
| 13 | + let package_locations = package_locations(); |
| 14 | + |
| 15 | + // does it look like we are running under clippy or rust-analyzer |
| 16 | + // This code was inspired by |
| 17 | + // https://github.com/bytecodealliance/componentize-py/blob/139d0ed85f09095e0a4cfa112e97ce589371315e/build.rs#L35-L42 |
| 18 | + // |
| 19 | + // This doesn't detect the following things though: |
| 20 | + // - `cargo check`: https://github.com/rust-lang/cargo/issues/4001 |
| 21 | + // - `cargo doc`: https://github.com/rust-lang/cargo/issues/8811 |
| 22 | + println!("cargo::rerun-if-env-changed=JUSTCHECK"); |
| 23 | + let stub = matches!( |
| 24 | + std::env::var("CARGO_CFG_FEATURE").as_deref(), |
| 25 | + Ok("cargo-clippy") |
| 26 | + ) || std::env::var("CLIPPY_ARGS").is_ok() |
| 27 | + || std::env::var("CARGO_EXPAND_NO_RUN_NIGHTLY").is_ok() |
| 28 | + || std::env::var("DOCS_RS").is_ok() |
| 29 | + || std::env::var("JUSTCHECK").is_ok(); |
| 30 | + |
| 31 | + for feature in FEATURES { |
| 32 | + println!("processing {}", feature.name); |
| 33 | + feature.build_or_stub(stub, profile, &package_locations); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Get locations for all packages in the dependency tree. |
| 38 | +fn package_locations() -> HashMap<String, PathBuf> { |
| 39 | + let json = Command::new(std::env::var("CARGO").unwrap()) |
| 40 | + .current_dir(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()) |
| 41 | + .arg("metadata") |
| 42 | + .run(); |
| 43 | + |
| 44 | + let json: serde_json::Value = serde_json::from_str(&json).expect("valid json"); |
| 45 | + |
| 46 | + json.as_object() |
| 47 | + .unwrap() |
| 48 | + .get("packages") |
| 49 | + .unwrap() |
| 50 | + .as_array() |
| 51 | + .unwrap() |
| 52 | + .iter() |
| 53 | + .map(|val| { |
| 54 | + let package = val.as_object().unwrap(); |
| 55 | + let name = package.get("name").unwrap().as_str().unwrap().to_owned(); |
| 56 | + let manifest_path = |
| 57 | + PathBuf::from(package.get("manifest_path").unwrap().as_str().unwrap()) |
| 58 | + .parent() |
| 59 | + .unwrap() |
| 60 | + .to_owned(); |
| 61 | + (name, manifest_path) |
| 62 | + }) |
| 63 | + .collect::<HashMap<_, _>>() |
| 64 | +} |
| 65 | + |
| 66 | +/// Extension trait for [`Command`]. |
| 67 | +trait CommandExt { |
| 68 | + /// Sanitize environment variables. |
| 69 | + fn sanitize_env(&mut self) -> &mut Self; |
| 70 | + |
| 71 | + /// Run command, check status, and convert output to a string. |
| 72 | + fn run(&mut self) -> String; |
| 73 | +} |
| 74 | + |
| 75 | +impl CommandExt for Command { |
| 76 | + fn sanitize_env(&mut self) -> &mut Self { |
| 77 | + let mut cmd = self.env_clear(); |
| 78 | + |
| 79 | + // Code inspired by |
| 80 | + // https://github.com/bytecodealliance/componentize-py/blob/139d0ed85f09095e0a4cfa112e97ce589371315e/build.rs#L117-L125 |
| 81 | + for (k, v) in std::env::vars_os() { |
| 82 | + let Ok(k) = k.into_string() else { |
| 83 | + continue; |
| 84 | + }; |
| 85 | + if k.starts_with("CARGO") || k.starts_with("RUST") { |
| 86 | + continue; |
| 87 | + } |
| 88 | + cmd = cmd.env(k, v); |
| 89 | + } |
| 90 | + |
| 91 | + cmd |
| 92 | + } |
| 93 | + |
| 94 | + fn run(&mut self) -> String { |
| 95 | + let output = self |
| 96 | + .stdout(Stdio::piped()) |
| 97 | + .spawn() |
| 98 | + .unwrap() |
| 99 | + .wait_with_output() |
| 100 | + .unwrap(); |
| 101 | + |
| 102 | + assert!(output.status.success()); |
| 103 | + String::from_utf8(output.stdout).expect("valid UTF-8") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// Known cargo profile. |
| 108 | +#[derive(Debug, Clone, Copy)] |
| 109 | +enum Profile { |
| 110 | + /// Debug/dev. |
| 111 | + Debug, |
| 112 | + |
| 113 | + /// Release. |
| 114 | + Release, |
| 115 | +} |
| 116 | + |
| 117 | +impl Profile { |
| 118 | + /// Get static string for profile. |
| 119 | + fn as_str(&self) -> &'static str { |
| 120 | + match self { |
| 121 | + Self::Debug => "debug", |
| 122 | + Self::Release => "release", |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl FromStr for Profile { |
| 128 | + type Err = String; |
| 129 | + |
| 130 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 131 | + match s { |
| 132 | + "debug" => Ok(Self::Debug), |
| 133 | + "release" => Ok(Self::Release), |
| 134 | + other => Err(other.to_owned()), |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl std::fmt::Display for Profile { |
| 140 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 141 | + self.as_str().fmt(f) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/// Feature description. |
| 146 | +struct Feature { |
| 147 | + /// Lowercase feature name. |
| 148 | + name: &'static str, |
| 149 | + |
| 150 | + /// Package that contains the feature code. |
| 151 | + package: &'static str, |
| 152 | + |
| 153 | + /// `just` command prefix that compiles the feature. |
| 154 | + /// |
| 155 | + /// This will call `just prefix{profile}` within the package directory. |
| 156 | + just_cmd_prefix: &'static str, |
| 157 | + |
| 158 | + /// Path components to file in target directory. |
| 159 | + /// |
| 160 | + /// So `["foo", "bar.bin"]` will resolve to `CARGO_TARGET_DIR/wasm32-wasip2/foo/bar.bin`. |
| 161 | + just_out_file: &'static [&'static str], |
| 162 | +} |
| 163 | + |
| 164 | +impl Feature { |
| 165 | + /// Build or stub feature. |
| 166 | + fn build_or_stub( |
| 167 | + &self, |
| 168 | + stub: bool, |
| 169 | + profile: Profile, |
| 170 | + package_locations: &HashMap<String, PathBuf>, |
| 171 | + ) { |
| 172 | + let Self { |
| 173 | + name, |
| 174 | + package, |
| 175 | + just_cmd_prefix, |
| 176 | + just_out_file, |
| 177 | + } = self; |
| 178 | + |
| 179 | + let name_upper = name.to_uppercase(); |
| 180 | + if std::env::var_os(format!("CARGO_FEATURE_{name_upper}")).is_none() { |
| 181 | + // feature not selected |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); |
| 186 | + |
| 187 | + let out_file = if stub { |
| 188 | + let out_file = out_dir.join(format!("{name}.wasm")); |
| 189 | + // write empty stub file |
| 190 | + std::fs::write(&out_file, b"").unwrap(); |
| 191 | + out_file |
| 192 | + } else { |
| 193 | + let target_dir = out_dir.join(name); |
| 194 | + |
| 195 | + just_build( |
| 196 | + package_locations.get(*package).unwrap(), |
| 197 | + &format!("{just_cmd_prefix}{profile}"), |
| 198 | + &target_dir, |
| 199 | + ); |
| 200 | + |
| 201 | + just_out_file.iter().fold( |
| 202 | + target_dir.join("wasm32-wasip2").join(profile.as_str()), |
| 203 | + |path, part| path.join(part), |
| 204 | + ) |
| 205 | + }; |
| 206 | + |
| 207 | + println!( |
| 208 | + "cargo::rustc-env=BIN_PATH_{name_upper}={}", |
| 209 | + out_file.display(), |
| 210 | + ); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +/// Build a target with `just`. |
| 215 | +fn just_build(cwd: &Path, just_cmd: &str, cargo_target_dir: &Path) { |
| 216 | + Command::new("just") |
| 217 | + .current_dir(cwd) |
| 218 | + .arg(just_cmd) |
| 219 | + .sanitize_env() |
| 220 | + .env("CARGO_TARGET_DIR", cargo_target_dir.as_os_str()) |
| 221 | + .run(); |
| 222 | +} |
| 223 | + |
| 224 | +/// All supported features. |
| 225 | +/// |
| 226 | +/// This must be in-sync with the feature list in `Cargo.toml` and the imports in `src/lib.rs`. |
| 227 | +const FEATURES: &[Feature] = &[ |
| 228 | + Feature { |
| 229 | + name: "example", |
| 230 | + package: "datafusion-udf-wasm-guest", |
| 231 | + just_cmd_prefix: "build-add-one-", |
| 232 | + just_out_file: &["examples", "add_one.wasm"], |
| 233 | + }, |
| 234 | + Feature { |
| 235 | + name: "python", |
| 236 | + package: "datafusion-udf-wasm-python", |
| 237 | + just_cmd_prefix: "", |
| 238 | + just_out_file: &["datafusion_udf_wasm_python.wasm"], |
| 239 | + }, |
| 240 | +]; |
0 commit comments