|
| 1 | +#![deny(warnings)] |
| 2 | + |
| 3 | +use { |
| 4 | + anyhow::{bail, Result}, |
| 5 | + std::{ |
| 6 | + env, |
| 7 | + fs::{self, File}, |
| 8 | + io::{self, Write}, |
| 9 | + path::{Path, PathBuf}, |
| 10 | + }, |
| 11 | + tar::Builder, |
| 12 | + zstd::Encoder, |
| 13 | +}; |
| 14 | + |
| 15 | +const ZSTD_COMPRESSION_LEVEL: i32 = 19; |
| 16 | + |
| 17 | +fn main() -> Result<()> { |
| 18 | + println!("cargo:rerun-if-changed=build.rs"); |
| 19 | + |
| 20 | + if let Ok("cargo-clippy") = env::var("CARGO_CFG_FEATURE").as_deref() { |
| 21 | + stubs_for_clippy() |
| 22 | + } else { |
| 23 | + package_engine_and_core_library() |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn stubs_for_clippy() -> Result<()> { |
| 28 | + println!("cargo:warning=using stubbed engine and core library for static analysis purposes..."); |
| 29 | + |
| 30 | + let engine_path = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("engine.wasm.zstd"); |
| 31 | + |
| 32 | + if !engine_path.exists() { |
| 33 | + Encoder::new(File::create(engine_path)?, ZSTD_COMPRESSION_LEVEL)?.do_finish()?; |
| 34 | + } |
| 35 | + |
| 36 | + let core_library_path = |
| 37 | + PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("python-lib.tar.zstd"); |
| 38 | + |
| 39 | + if !core_library_path.exists() { |
| 40 | + Builder::new(Encoder::new( |
| 41 | + File::create(core_library_path)?, |
| 42 | + ZSTD_COMPRESSION_LEVEL, |
| 43 | + )?) |
| 44 | + .into_inner()? |
| 45 | + .do_finish()?; |
| 46 | + } |
| 47 | + |
| 48 | + Ok(()) |
| 49 | +} |
| 50 | + |
| 51 | +fn package_engine_and_core_library() -> Result<()> { |
| 52 | + let override_engine_path = env::var_os("SPIN_PYTHON_ENGINE_PATH"); |
| 53 | + let engine_path = if let Some(path) = override_engine_path { |
| 54 | + PathBuf::from(path) |
| 55 | + } else { |
| 56 | + let mut path = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); |
| 57 | + path.pop(); |
| 58 | + path.pop(); |
| 59 | + path.join("target/wasm32-wasi/release/spin_python_engine.wasm") |
| 60 | + }; |
| 61 | + |
| 62 | + println!("cargo:rerun-if-changed={engine_path:?}"); |
| 63 | + |
| 64 | + if engine_path.exists() { |
| 65 | + let copied_engine_path = |
| 66 | + PathBuf::from(env::var("OUT_DIR").unwrap()).join("engine.wasm.zstd"); |
| 67 | + |
| 68 | + let mut encoder = Encoder::new(File::create(copied_engine_path)?, ZSTD_COMPRESSION_LEVEL)?; |
| 69 | + io::copy(&mut File::open(engine_path)?, &mut encoder)?; |
| 70 | + encoder.do_finish()?; |
| 71 | + } else { |
| 72 | + bail!("no such file: {}", engine_path.display()) |
| 73 | + } |
| 74 | + |
| 75 | + let override_core_library_path = env::var_os("SPIN_PYTHON_CORE_LIBRARY_PATH"); |
| 76 | + let core_library_path = if let Some(path) = override_core_library_path { |
| 77 | + PathBuf::from(path) |
| 78 | + } else { |
| 79 | + let mut path = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); |
| 80 | + path.pop(); |
| 81 | + path.pop(); |
| 82 | + path.join("cpython/builddir/wasi/install/lib/python3.11") |
| 83 | + }; |
| 84 | + |
| 85 | + println!("cargo:rerun-if-changed={core_library_path:?}"); |
| 86 | + |
| 87 | + if core_library_path.exists() { |
| 88 | + let copied_core_library_path = |
| 89 | + PathBuf::from(env::var("OUT_DIR").unwrap()).join("python-lib.tar.zstd"); |
| 90 | + |
| 91 | + let mut builder = Builder::new(Encoder::new( |
| 92 | + File::create(copied_core_library_path)?, |
| 93 | + ZSTD_COMPRESSION_LEVEL, |
| 94 | + )?); |
| 95 | + |
| 96 | + add(&mut builder, &core_library_path, &core_library_path)?; |
| 97 | + |
| 98 | + builder.into_inner()?.do_finish()?; |
| 99 | + } else { |
| 100 | + bail!("no such directory: {}", core_library_path.display()) |
| 101 | + } |
| 102 | + |
| 103 | + Ok(()) |
| 104 | +} |
| 105 | + |
| 106 | +fn include(path: &Path) -> bool { |
| 107 | + !(matches!( |
| 108 | + path.extension().and_then(|e| e.to_str()), |
| 109 | + Some("a" | "pyc" | "whl") |
| 110 | + ) || matches!( |
| 111 | + path.file_name().and_then(|e| e.to_str()), |
| 112 | + Some("Makefile" | "Changelog" | "NEWS.txt") |
| 113 | + )) |
| 114 | +} |
| 115 | + |
| 116 | +fn add(builder: &mut Builder<impl Write>, root: &Path, path: &Path) -> Result<()> { |
| 117 | + if path.is_dir() { |
| 118 | + for entry in fs::read_dir(path)? { |
| 119 | + add(builder, root, &entry?.path())?; |
| 120 | + } |
| 121 | + } else if include(path) { |
| 122 | + builder.append_file(path.strip_prefix(root)?, &mut File::open(path)?)?; |
| 123 | + } |
| 124 | + |
| 125 | + Ok(()) |
| 126 | +} |
0 commit comments