diff --git a/CLAUDE.md b/CLAUDE.md index 679e2470..2d495c15 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -44,7 +44,7 @@ uv run pytest tests/unit --ignore=tests/unit/test_cli.py -v - The user-facing CLI is the Rust binary `fastled` (built from `crates/fastled-cli`, bundled into the wheel via `[tool.maturin] data`). Python `cli.py` / `app.py` are 15-line shims that call `fastled._rust_cli.invoke_rust_fastled_cli`. - `fastled._native` is the PyO3 extension compiled from `crates/fastled-py/src/lib.rs`. It exposes Rust functions to Python (sketch discovery, build service, project init, string diff, frontend bundling, …). -- The compile path goes Rust → Python toolchain → Rust: `crates/fastled-cli/src/build.rs` constructs `fastled._native.NativeBuildService` and calls it; the Python toolchain (Meson/Ninja/Emscripten internals under `src/fastled/toolchain/`) is intentionally Python-owned. +- The compile path is Rust-owned: `crates/fastled-cli/src/build.rs` re-exports the native WASM backend in `crates/fastled-cli/src/wasm_build.rs`. Python `BuildService` / `EmscriptenToolchain` are compatibility facades over `fastled._native.NativeBuildService`. - For PyO3 changes, after `cargo build -p fastled-py` the new `.dll` lands in `target/maturin/_native.dll`. `maturin develop` copies that to `src/fastled/_native.pyd`. If `_native.pyd` is locked by other Python processes (common on Windows), use the move-aside trick: `mv src/fastled/_native.pyd src/fastled/_native.pyd.old && cp target/maturin/_native.dll src/fastled/_native.pyd`. Remember to delete the `.pyd.old` before committing. ## Conventions diff --git a/Cargo.lock b/Cargo.lock index a6932d2f..9c1b88b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1059,6 +1059,7 @@ dependencies = [ "axum", "clap", "crossterm", + "ctcb-core", "ctcb-manifest", "dirs 5.0.1", "flate2", @@ -1073,6 +1074,7 @@ dependencies = [ "tempfile", "tokio", "tokio-stream", + "toml 0.8.2", "tower-http", "zip", "zstd", diff --git a/Cargo.toml b/Cargo.toml index 1be7bf9d..d76fe7d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" anyhow = "1" ctcb-manifest = "0.4.1" +ctcb-core = "0.4.1" dirs = "5" strsim = "0.11" crossterm = "0.28" @@ -36,6 +37,7 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "fs", " axum = "0.8" tower-http = { version = "0.6", features = ["fs", "cors", "set-header"] } tokio-stream = { version = "0.1", features = ["sync"] } +toml = "0.8" [profile.release] debug = "line-tables-only" diff --git a/crates/fastled-cli/Cargo.toml b/crates/fastled-cli/Cargo.toml index caa8a7ea..dcd6c44d 100644 --- a/crates/fastled-cli/Cargo.toml +++ b/crates/fastled-cli/Cargo.toml @@ -30,6 +30,7 @@ serde = { workspace = true } serde_json = { workspace = true } anyhow = { workspace = true } ctcb-manifest = { workspace = true } +ctcb-core = { workspace = true } strsim = { workspace = true } crossterm = { workspace = true } tokio = { workspace = true } @@ -37,6 +38,7 @@ axum = { workspace = true } tower-http = { workspace = true } tokio-stream = { workspace = true } dirs = { workspace = true } +toml = { workspace = true } tempfile = "3" [package.metadata.binstall] diff --git a/crates/fastled-cli/src/build.rs b/crates/fastled-cli/src/build.rs index 86c64db3..8500ed79 100644 --- a/crates/fastled-cli/src/build.rs +++ b/crates/fastled-cli/src/build.rs @@ -1,449 +1,6 @@ //! Build orchestration for WASM compilation. //! -//! The Rust CLI now drives the Python build service in-process through PyO3. +//! The Rust CLI owns the build backend directly. Python remains a compatibility +//! API layer and is not part of the primary compile path. -use std::fs; -use std::path::{Path, PathBuf}; -use std::time::Instant; - -use anyhow::{Context, Result}; -use pyo3::exceptions::PyKeyboardInterrupt; -use pyo3::prelude::*; -use pyo3::types::{PyDict, PyModule}; - -// --------------------------------------------------------------------------- -// Types -// --------------------------------------------------------------------------- - -/// Mirrors `BuildMode` from `types.py`. -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum BuildMode { - Quick, - Debug, - Release, -} - -impl BuildMode { - /// Return the Python enum member name. - pub fn py_member_name(&self) -> &'static str { - match self { - BuildMode::Quick => "QUICK", - BuildMode::Debug => "DEBUG", - BuildMode::Release => "RELEASE", - } - } - - /// Return a human-readable label. - pub fn label(&self) -> &'static str { - self.py_member_name() - } -} - -impl std::fmt::Display for BuildMode { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(self.label()) - } -} - -/// Mirrors `BuildRequest` from `build_types.py`. -#[derive(Debug, Clone)] -pub struct BuildRequest { - /// Directory that contains the FastLED sketch. - pub sketch_dir: PathBuf, - /// Which optimisation profile to use. - pub build_mode: BuildMode, - /// Enable C++ build-system profiling output. - pub profile: bool, - /// Optional path to a local FastLED source tree. - pub fastled_path: Option, - /// Discard cached artefacts and start from scratch. - pub force_clean: bool, -} - -/// Mirrors the useful fields returned from `build_types.BuildResult`. -#[derive(Debug)] -pub struct BuildResult { - /// Whether the compilation succeeded. - pub success: bool, - /// Directory that received the compiled artefacts. - pub output_dir: PathBuf, - /// Wall-clock seconds spent inside the in-process Python build call. - pub duration_secs: f64, - /// Python-side sketch compilation time. - pub sketch_time_secs: f64, - /// Strategy selected by the build service (`cold` or `incremental`). - pub strategy: String, - /// Final summary line from the Python build result. - pub output: String, -} - -// --------------------------------------------------------------------------- -// Python bridge helpers -// --------------------------------------------------------------------------- - -fn workspace_root() -> PathBuf { - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .parent() - .and_then(Path::parent) - .map(Path::to_path_buf) - .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR"))) -} - -fn workspace_python_source() -> Option { - let src_dir = workspace_root().join("src"); - src_dir.is_dir().then_some(src_dir) -} - -fn normalize_path(path: &Path) -> PathBuf { - fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) -} - -fn python_executable_name() -> &'static str { - if cfg!(windows) { - "python.exe" - } else { - "python" - } -} - -fn python_executable_from_virtual_env() -> Option { - let venv = std::env::var_os("VIRTUAL_ENV")?; - let venv = PathBuf::from(venv); - let bindir = if cfg!(windows) { "Scripts" } else { "bin" }; - let candidate = venv.join(bindir).join(python_executable_name()); - candidate.is_file().then_some(candidate) -} - -fn python_executable_from_path() -> Option { - let path = std::env::var_os("PATH")?; - for dir in std::env::split_paths(&path) { - let candidate = dir.join(python_executable_name()); - if candidate.is_file() { - return Some(candidate); - } - if !cfg!(windows) { - let candidate = dir.join("python3"); - if candidate.is_file() { - return Some(candidate); - } - } - } - None -} - -fn resolve_python_executable() -> Option { - std::env::var_os("FASTLED_PYTHON_EXECUTABLE") - .map(PathBuf::from) - .filter(|path| path.is_file()) - .or_else(python_executable_from_virtual_env) - .or_else(python_executable_from_path) -} - -fn configure_embedded_python_executable(py: Python<'_>) -> PyResult<()> { - let Some(python) = resolve_python_executable() else { - return Ok(()); - }; - let python = python.to_string_lossy().into_owned(); - let sys = PyModule::import(py, "sys")?; - sys.setattr("executable", python.as_str())?; - let _ = sys.setattr("_base_executable", python.as_str()); - - let os = PyModule::import(py, "os")?; - let environ = os.getattr("environ")?; - environ.set_item("PYTHON", python.as_str())?; - environ.set_item("EMSDK_PYTHON", python.as_str())?; - std::env::set_var("PYTHON", python.as_str()); - std::env::set_var("EMSDK_PYTHON", python.as_str()); - Ok(()) -} - -/// Canonicalize a sketch/`fastled_path` so the toolchain key registered with -/// `NativeBuildService.register_toolchain` matches the key passed into -/// `NativeBuildService.build` — mirrors `_toolchain_key` in -/// `src/fastled/build_service.py`. -fn toolchain_key(fastled_path: Option<&Path>) -> Option { - fastled_path.map(|p| normalize_path(p).to_string_lossy().into_owned()) -} - -fn ensure_workspace_src_on_sys_path(py: Python<'_>) -> PyResult<()> { - let Some(src_dir) = workspace_python_source() else { - return Ok(()); - }; - let src_text = src_dir.to_string_lossy().into_owned(); - let sys = PyModule::import(py, "sys")?; - let sys_path = sys.getattr("path")?; - let contains: bool = sys_path - .call_method1("__contains__", (src_text.as_str(),))? - .extract()?; - if !contains { - sys_path.call_method1("insert", (0, src_text.as_str()))?; - } - Ok(()) -} - -fn run_build_embedded(request: &BuildRequest) -> Result { - let start = Instant::now(); - Python::with_gil(|py| -> Result { - ensure_workspace_src_on_sys_path(py) - .context("failed to add workspace Python sources to sys.path")?; - configure_embedded_python_executable(py) - .context("failed to configure embedded Python executable")?; - - let native_mod = - PyModule::import(py, "fastled._native").context("import fastled._native")?; - let types_mod = PyModule::import(py, "fastled.types").context("import fastled.types")?; - - let build_mode_obj = types_mod - .getattr("BuildMode") - .context("BuildMode enum missing")? - .getattr(request.build_mode.py_member_name()) - .with_context(|| { - format!("BuildMode.{} missing", request.build_mode.py_member_name()) - })?; - - let service = native_mod - .getattr("NativeBuildService") - .context("NativeBuildService class missing")? - .call0() - .context("construct NativeBuildService")?; - - // Register the Emscripten toolchain inline (still Python-owned). - let toolchain_mod = PyModule::import(py, "fastled.toolchain.emscripten") - .context("import fastled.toolchain.emscripten")?; - let toolchain_kwargs = PyDict::new(py); - match &request.fastled_path { - Some(path) => toolchain_kwargs - .set_item("fastled_path", path.to_string_lossy().as_ref()) - .context("set toolchain fastled_path")?, - None => toolchain_kwargs - .set_item("fastled_path", py.None()) - .context("set toolchain fastled_path none")?, - } - let toolchain = toolchain_mod - .getattr("EmscriptenToolchain") - .context("EmscriptenToolchain class missing")? - .call((), Some(&toolchain_kwargs)) - .context("construct EmscriptenToolchain")?; - - let key = toolchain_key(request.fastled_path.as_deref()); - service - .call_method1("register_toolchain", (toolchain, key.clone())) - .context("register_toolchain")?; - - let sketch_dir_str = request.sketch_dir.to_string_lossy().into_owned(); - let build_mode_str = request.build_mode.py_member_name(); - - let build_call = service.call_method1( - "build", - ( - sketch_dir_str.as_str(), - build_mode_str, - build_mode_obj, - request.profile, - key.clone(), - request.force_clean, - ), - ); - - let result = match build_call { - Ok(r) => r, - Err(err) => { - if err.is_instance_of::(py) { - // Mirror the Python wrapper: route through - // fastled.interrupts.handle_keyboard_interrupt so a - // worker-thread Ctrl+C wakes the main thread. - if let Ok(interrupts) = PyModule::import(py, "fastled.interrupts") { - if let Ok(handler) = interrupts.getattr("handle_keyboard_interrupt") { - let _ = handler.call1((err.clone_ref(py),)); - } - } - } - return Err(err).context("NativeBuildService.build failed"); - } - }; - - let result_dict = result - .downcast_into::() - .map_err(|e| anyhow::anyhow!("NativeBuildService.build returned non-dict: {e}"))?; - - let success: bool = result_dict - .get_item("success") - .context("missing build result success")? - .ok_or_else(|| anyhow::anyhow!("missing build result success"))? - .extract() - .context("extract build success")?; - let output: String = result_dict - .get_item("stdout") - .context("missing build result stdout")? - .ok_or_else(|| anyhow::anyhow!("missing build result stdout"))? - .extract() - .context("extract build stdout")?; - let strategy: String = result_dict - .get_item("strategy") - .context("missing build strategy")? - .ok_or_else(|| anyhow::anyhow!("missing build strategy"))? - .extract() - .context("extract build strategy")?; - let sketch_time_secs: f64 = result_dict - .get_item("sketch_time") - .context("missing sketch_time")? - .ok_or_else(|| anyhow::anyhow!("missing sketch_time"))? - .extract() - .context("extract sketch_time")?; - let output_dir_str: String = result_dict - .get_item("output_dir") - .context("missing output_dir")? - .ok_or_else(|| anyhow::anyhow!("missing output_dir"))? - .extract() - .context("extract output_dir")?; - let output_dir = PathBuf::from(output_dir_str); - - Ok(BuildResult { - success, - output_dir, - duration_secs: start.elapsed().as_secs_f64(), - sketch_time_secs, - strategy, - output, - }) - }) -} - -// --------------------------------------------------------------------------- -// Public API -// --------------------------------------------------------------------------- - -/// Run a WASM build through the in-process Python build service. -pub fn run_build(request: &BuildRequest) -> Result { - run_build_embedded(request).with_context(|| { - format!( - "native build failed for sketch `{}` in {} mode", - request.sketch_dir.display(), - request.build_mode - ) - }) -} - -// --------------------------------------------------------------------------- -// Unit tests -// --------------------------------------------------------------------------- - -#[cfg(test)] -mod tests { - use super::*; - - // ------------------------------------------------------------------ - // BuildMode - // ------------------------------------------------------------------ - - #[test] - fn test_build_mode_member_names() { - assert_eq!(BuildMode::Quick.py_member_name(), "QUICK"); - assert_eq!(BuildMode::Debug.py_member_name(), "DEBUG"); - assert_eq!(BuildMode::Release.py_member_name(), "RELEASE"); - } - - #[test] - fn test_build_mode_labels() { - assert_eq!(BuildMode::Quick.label(), "QUICK"); - assert_eq!(BuildMode::Debug.label(), "DEBUG"); - assert_eq!(BuildMode::Release.label(), "RELEASE"); - } - - #[test] - fn test_build_mode_display() { - assert_eq!(format!("{}", BuildMode::Quick), "QUICK"); - assert_eq!(format!("{}", BuildMode::Debug), "DEBUG"); - assert_eq!(format!("{}", BuildMode::Release), "RELEASE"); - } - - #[test] - fn test_build_mode_equality() { - assert_eq!(BuildMode::Quick, BuildMode::Quick); - assert_ne!(BuildMode::Quick, BuildMode::Debug); - assert_ne!(BuildMode::Debug, BuildMode::Release); - } - - // ------------------------------------------------------------------ - // BuildRequest - // ------------------------------------------------------------------ - - #[test] - fn test_build_request_construction() { - let sketch = PathBuf::from("/tmp/my_sketch"); - let req = BuildRequest { - sketch_dir: sketch.clone(), - build_mode: BuildMode::Quick, - profile: false, - fastled_path: None, - force_clean: false, - }; - - assert_eq!(req.sketch_dir, sketch); - assert_eq!(req.build_mode, BuildMode::Quick); - assert!(!req.profile); - assert!(req.fastled_path.is_none()); - assert!(!req.force_clean); - } - - #[test] - fn test_build_request_with_fastled_path() { - let sketch = PathBuf::from("/tmp/my_sketch"); - let fl_path = PathBuf::from("/opt/fastled"); - let req = BuildRequest { - sketch_dir: sketch, - build_mode: BuildMode::Release, - profile: true, - fastled_path: Some(fl_path.clone()), - force_clean: true, - }; - - assert_eq!(req.fastled_path, Some(fl_path)); - assert!(req.profile); - assert!(req.force_clean); - assert_eq!(req.build_mode, BuildMode::Release); - } - - #[test] - fn test_workspace_python_source_points_at_repo_src() { - let src_dir = workspace_python_source().expect("workspace src dir"); - assert!(src_dir.ends_with("src")); - assert!(src_dir.is_dir()); - } - - #[test] - fn test_toolchain_key_is_none_when_unset() { - assert_eq!(toolchain_key(None), None); - } - - #[test] - fn test_toolchain_key_returns_some_when_set() { - // The canonicalize call may fail for nonexistent paths and fall back - // to the input path, which is fine for this test. - let p = PathBuf::from("/tmp/fastled-test-keypath-does-not-exist"); - let key = toolchain_key(Some(&p)); - assert!(key.is_some()); - } - - // ------------------------------------------------------------------ - // BuildResult - // ------------------------------------------------------------------ - - #[test] - fn test_build_result_fields() { - let result = BuildResult { - success: true, - output_dir: PathBuf::from("/tmp/my_sketch/fastled_js"), - duration_secs: 2.5, - sketch_time_secs: 1.75, - strategy: "cold".to_string(), - output: "Build OK".to_string(), - }; - - assert!(result.success); - assert_eq!(result.duration_secs, 2.5); - assert_eq!(result.sketch_time_secs, 1.75); - assert_eq!(result.strategy, "cold"); - assert_eq!(result.output, "Build OK"); - } -} +pub use crate::wasm_build::{run_build, BuildMode, BuildRequest, BuildResult}; diff --git a/crates/fastled-cli/src/install.rs b/crates/fastled-cli/src/install.rs index 2747fd42..defa4c90 100644 --- a/crates/fastled-cli/src/install.rs +++ b/crates/fastled-cli/src/install.rs @@ -11,6 +11,7 @@ use std::path::{Path, PathBuf}; use std::process::Command; use anyhow::{bail, Context, Result}; +use ctcb_core::Target; use ctcb_manifest::{PartRef, PlatformManifest}; use serde_json::{json, Value}; @@ -34,22 +35,9 @@ fn fastled_root() -> Result { Ok(home.join(".fastled")) } -fn detect_platform_arch() -> (&'static str, &'static str) { - let platform = if cfg!(target_os = "windows") { - "win" - } else if cfg!(target_os = "macos") { - "darwin" - } else { - "linux" - }; - let arch = if cfg!(target_arch = "x86_64") { - "x86_64" - } else if cfg!(target_arch = "aarch64") { - "arm64" - } else { - std::env::consts::ARCH - }; - (platform, arch) +fn detect_platform_arch() -> Result<(String, String)> { + let target = Target::current().context("detect host clang-tool-chain target")?; + Ok((target.platform.to_string(), target.arch.to_string())) } /// Fetch and parse the platform manifest JSON. @@ -119,13 +107,13 @@ fn download_multipart(parts: &[PartRef], parts_dir: &Path, merged_path: &Path) - /// The layout intentionally matches the Python implementation so a previously /// Python-installed toolchain is picked up without re-downloading. pub fn ensure_emscripten_installed() -> Result { - let (platform, arch) = detect_platform_arch(); + let (platform, arch) = detect_platform_arch()?; let root = fastled_root()?; let install_base = root .join("toolchains") .join("emscripten") - .join(platform) - .join(arch); + .join(&platform) + .join(&arch); let cache_dir = root.join("toolchains").join("archives"); fs::create_dir_all(&install_base)?; fs::create_dir_all(&cache_dir)?; diff --git a/crates/fastled-cli/src/lib.rs b/crates/fastled-cli/src/lib.rs index 3b262385..78a8f878 100644 --- a/crates/fastled-cli/src/lib.rs +++ b/crates/fastled-cli/src/lib.rs @@ -13,6 +13,7 @@ mod keyboard; pub mod project; mod server; pub mod viewer; +pub mod wasm_build; mod watcher; const DEFAULT_EXAMPLE: &str = "wasm"; @@ -21,9 +22,9 @@ fn ensure_compile_prerequisites() -> Result<(), String> { match install::ensure_emscripten_installed() { Ok(install_dir) => { std::env::set_var("FASTLED_EMSCRIPTEN_DIR", &install_dir); - // FastLED's meson cross-file uses clang-tool-chain wrapper binaries. - // Those wrappers locate emscripten from the toolchain root, not the - // versioned install dir, so export the root when it can be derived. + // Compatibility exports for any remaining callers that still read + // the historical clang-tool-chain environment variables. The Rust + // build backend invokes the installed Emscripten scripts directly. if let Some(root) = install_dir .parent() .and_then(|p| p.parent()) @@ -244,8 +245,8 @@ fn compile_and_serve(dir: &str, cli: &Cli, mode: ViewerMode) -> ExitCode { } // Ensure the emscripten + esbuild toolchains are installed before invoking - // the native build path. The embedded Python build service consumes the - // Rust-installed directories via these environment variables. + // the native Rust build backend. The backend consumes the Rust-installed + // directories via these environment variables. if let Err(message) = ensure_compile_prerequisites() { eprintln!("fastled: {message}"); return ExitCode::FAILURE; @@ -374,9 +375,8 @@ fn compile_and_serve(dir: &str, cli: &Cli, mode: ViewerMode) -> ExitCode { /// /// Rust front-end for FastLED WASM workflows. /// -/// Native Rust owns the full user-facing CLI surface. The only remaining -/// Python runtime dependency on compile paths is the in-process build service -/// invoked from `build.rs`. +/// Native Rust owns the full user-facing CLI surface, including compile +/// orchestration through `build.rs`. #[derive(Parser, Debug)] #[command( name = "fastled", diff --git a/crates/fastled-cli/src/wasm_build.rs b/crates/fastled-cli/src/wasm_build.rs new file mode 100644 index 00000000..2916027c --- /dev/null +++ b/crates/fastled-cli/src/wasm_build.rs @@ -0,0 +1,981 @@ +//! Native WASM build backend. +//! +//! This replaces the Python `internal_wasm_build.py` path for the CLI build +//! flow. Meson/Ninja/emcc are still external tools, but orchestration and +//! Emscripten path resolution are owned by this Rust binary. + +use std::collections::BTreeMap; +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use anyhow::{bail, Context, Result}; +use serde::Deserialize; +use sha2::{Digest, Sha256}; + +use crate::{archive, frontend, install}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BuildMode { + Quick, + Debug, + Release, +} + +impl BuildMode { + pub fn as_str(self) -> &'static str { + match self { + Self::Quick => "quick", + Self::Debug => "debug", + Self::Release => "release", + } + } +} + +#[derive(Debug)] +pub struct BuildRequest { + pub sketch_dir: PathBuf, + pub build_mode: BuildMode, + pub profile: bool, + pub fastled_path: Option, + pub force_clean: bool, +} + +#[derive(Debug)] +pub struct BuildResult { + pub success: bool, + pub output_dir: PathBuf, + pub duration_secs: f64, + pub sketch_time_secs: f64, + pub strategy: String, + pub output: String, +} + +#[derive(Debug, Clone)] +struct ToolPaths { + emscripten_dir: PathBuf, + emcc: PathBuf, + empp: PathBuf, + emar: PathBuf, + python: PathBuf, +} + +#[derive(Debug, Default, Deserialize)] +struct BuildFlagsToml { + all: Option, + sketch: Option, + linking: Option, + build_modes: Option>, +} + +#[derive(Debug, Default, Deserialize)] +struct FlagSection { + defines: Option>, + compiler_flags: Option>, +} + +#[derive(Debug, Default, Deserialize)] +struct LinkingSection { + base: Option, + sketch: Option, +} + +#[derive(Debug, Default, Deserialize)] +struct ModeSection { + flags: Option>, + sketch_flags: Option>, + link_flags: Option>, +} + +#[derive(Debug, Default, Deserialize)] +struct FlagList { + flags: Option>, +} + +fn resolve_python_executable() -> PathBuf { + if let Some(path) = std::env::var_os("FASTLED_PYTHON_EXECUTABLE").map(PathBuf::from) { + if path.is_file() { + return path; + } + } + if let Some(venv) = std::env::var_os("VIRTUAL_ENV").map(PathBuf::from) { + let candidate = if cfg!(windows) { + venv.join("Scripts").join("python.exe") + } else { + venv.join("bin").join("python") + }; + if candidate.is_file() { + return candidate; + } + } + PathBuf::from(if cfg!(windows) { + "python.exe" + } else { + "python3" + }) +} + +fn resolve_tool_paths(install_dir: &Path) -> Result { + let emscripten_dir = install_dir.join("emscripten"); + let emcc = emscripten_dir.join("emcc.py"); + let empp = emscripten_dir.join("em++.py"); + let emar = emscripten_dir.join("emar.py"); + if !emcc.is_file() { + bail!("missing emcc.py at {}", emcc.display()); + } + if !empp.is_file() { + bail!("missing em++.py at {}", empp.display()); + } + if !emar.is_file() { + bail!("missing emar.py at {}", emar.display()); + } + Ok(ToolPaths { + emscripten_dir, + emcc, + empp, + emar, + python: resolve_python_executable(), + }) +} + +fn build_env(tools: &ToolPaths) -> Vec<(String, String)> { + let mut env = vec![ + ( + "EMSCRIPTEN".to_string(), + tools.emscripten_dir.display().to_string(), + ), + ( + "EMSCRIPTEN_ROOT".to_string(), + tools.emscripten_dir.display().to_string(), + ), + ( + "EM_CONFIG".to_string(), + tools + .emscripten_dir + .parent() + .unwrap_or(&tools.emscripten_dir) + .join(".emscripten") + .display() + .to_string(), + ), + ( + "EMSDK_PYTHON".to_string(), + tools.python.display().to_string(), + ), + ("EMCC_SKIP_SANITY_CHECK".to_string(), "1".to_string()), + ]; + if cfg!(windows) { + env.push(("EMCC_CORES".to_string(), "128".to_string())); + } + env +} + +fn command_with_env(program: impl AsRef, tools: &ToolPaths) -> Command { + let mut command = Command::new(program.as_ref()); + for (key, value) in build_env(tools) { + command.env(key, value); + } + command +} + +fn run_status(mut command: Command, label: &str) -> Result<()> { + let status = command + .status() + .with_context(|| format!("launch {label}"))?; + if !status.success() { + bail!("{label} failed with {status}"); + } + Ok(()) +} + +fn normalize_path(path: &Path) -> PathBuf { + fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) +} + +fn resolve_example_name(sketch_dir: &Path, fastled_dir: &Path) -> (String, PathBuf, bool) { + let examples_dir = fastled_dir.join("examples"); + if let Ok(relative) = sketch_dir.strip_prefix(&examples_dir) { + let name = relative.to_string_lossy().replace('\\', "/"); + return (name, sketch_dir.to_path_buf(), true); + } + let name = sketch_dir + .file_name() + .and_then(|name| name.to_str()) + .unwrap_or("sketch") + .to_string(); + (name, sketch_dir.to_path_buf(), false) +} + +fn mode_output_dir(sketch_dir: &Path) -> PathBuf { + sketch_dir.join("fastled_js") +} + +fn build_dir(fastled_dir: &Path, mode: BuildMode) -> PathBuf { + fastled_dir + .join(".build") + .join(format!("meson-wasm-{}", mode.as_str())) +} + +fn sketch_cache_dir(example_dir: &Path) -> PathBuf { + example_dir.join(".build").join("wasm") +} + +fn sketch_ino_file(example_dir: &Path) -> Result { + let sketch_name = example_dir + .file_name() + .and_then(|name| name.to_str()) + .unwrap_or("sketch"); + let expected = example_dir.join(format!("{sketch_name}.ino")); + if expected.is_file() { + return Ok(expected); + } + + let mut candidates = Vec::new(); + for entry in fs::read_dir(example_dir) + .with_context(|| format!("read sketch directory {}", example_dir.display()))? + { + let path = entry?.path(); + if path.extension().and_then(|ext| ext.to_str()) == Some("ino") { + candidates.push(path); + } + } + candidates.sort(); + match candidates.as_slice() { + [single] => Ok(single.clone()), + [] => bail!("example has no .ino file: {}", example_dir.display()), + _ => bail!("example has multiple .ino files: {}", example_dir.display()), + } +} + +fn wrapper_stem(example_name: &str) -> String { + example_name.replace(['\\', '/'], "_") +} + +fn load_build_flags(fastled_dir: &Path) -> Result { + let path = fastled_dir + .join("src") + .join("platforms") + .join("wasm") + .join("compiler") + .join("build_flags.toml"); + let source = fs::read_to_string(&path) + .with_context(|| format!("read build flags {}", path.display()))?; + toml::from_str(&source).with_context(|| format!("parse {}", path.display())) +} + +fn get_sketch_compile_flags(fastled_dir: &Path, mode: BuildMode) -> Result> { + let config = load_build_flags(fastled_dir)?; + let mode_key = mode.as_str().to_string(); + let mode_config = config + .build_modes + .as_ref() + .and_then(|modes| modes.get(&mode_key)); + + let mut flags = Vec::new(); + if let Some(section) = config.all { + flags.extend(section.defines.unwrap_or_default()); + flags.extend(section.compiler_flags.unwrap_or_default()); + } + if let Some(section) = config.sketch { + flags.extend(section.defines.unwrap_or_default()); + flags.extend(section.compiler_flags.unwrap_or_default()); + } + if let Some(mode_config) = mode_config { + if let Some(sketch_flags) = &mode_config.sketch_flags { + flags.extend(sketch_flags.clone()); + } else { + flags.extend(mode_config.flags.clone().unwrap_or_default()); + } + } + Ok(flags) +} + +fn get_link_flags(fastled_dir: &Path, mode: BuildMode) -> Result> { + let config = load_build_flags(fastled_dir)?; + let mode_key = mode.as_str().to_string(); + let mode_config = config + .build_modes + .as_ref() + .and_then(|modes| modes.get(&mode_key)); + + let mut flags = Vec::new(); + if let Some(linking) = config.linking { + if let Some(base) = linking.base { + flags.extend(base.flags.unwrap_or_default()); + } + if let Some(sketch) = linking.sketch { + flags.extend(sketch.flags.unwrap_or_default()); + } + } + if let Some(mode_config) = mode_config { + flags.extend(mode_config.link_flags.clone().unwrap_or_default()); + } + Ok(flags) +} + +fn hash_files(root: &Path, files: &[PathBuf]) -> Result { + let mut hasher = Sha256::new(); + for path in files { + let full = if path.is_absolute() { + path.clone() + } else { + root.join(path) + }; + if full.is_file() { + hasher.update( + full.strip_prefix(root) + .unwrap_or(&full) + .to_string_lossy() + .as_bytes(), + ); + hasher.update(fs::metadata(&full)?.len().to_string().as_bytes()); + hasher.update(fs::read(&full)?); + } + } + Ok(format!("{:x}", hasher.finalize())) +} + +fn collect_source_files(root: &Path, dir: &Path, out: &mut Vec) -> Result<()> { + if !dir.is_dir() { + return Ok(()); + } + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + let name = entry.file_name(); + let name = name.to_string_lossy(); + if path.is_dir() { + if name == ".git" || name == ".build" || name == "build" || name == "fastled_js" { + continue; + } + collect_source_files(root, &path, out)?; + } else if matches!( + path.extension().and_then(|ext| ext.to_str()), + Some("cpp" | "c" | "h" | "hpp" | "ipp" | "toml" | "build") + ) { + out.push(path.strip_prefix(root).unwrap_or(&path).to_path_buf()); + } + } + Ok(()) +} + +fn compute_source_file_hash(fastled_dir: &Path) -> Result { + let mut files = Vec::new(); + collect_source_files(fastled_dir, &fastled_dir.join("src"), &mut files)?; + for path in [ + "meson.build", + "ci/meson/wasm/meson.build", + "ci/meson/wasm_cross_file.ini", + ] { + files.push(PathBuf::from(path)); + } + files.sort(); + hash_files(fastled_dir, &files) +} + +fn write_native_cross_file(fastled_dir: &Path, tools: &ToolPaths) -> Result { + let path = fastled_dir + .join(".build") + .join("fastled-rs-wasm-cross-file.ini"); + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + let python = tools.python.to_string_lossy().replace('\\', "/"); + let emcc = tools.emcc.to_string_lossy().replace('\\', "/"); + let empp = tools.empp.to_string_lossy().replace('\\', "/"); + let emar = tools.emar.to_string_lossy().replace('\\', "/"); + let content = format!( + r#"[binaries] +c = [{python:?}, {emcc:?}] +cpp = [{python:?}, {empp:?}] +ar = [{python:?}, {emar:?}] +strip = 'true' + +[host_machine] +system = 'emscripten' +cpu_family = 'wasm32' +cpu = 'wasm32' +endian = 'little' + +[properties] +skip_sanity_check = true +needs_exe_wrapper = true +"# + ); + fs::write(&path, content).with_context(|| format!("write {}", path.display()))?; + Ok(path) +} + +fn ensure_meson_configured( + fastled_dir: &Path, + tools: &ToolPaths, + build_dir: &Path, + mode: BuildMode, + force: bool, +) -> Result<()> { + let marker = build_dir.join(".src_file_list_hash"); + let current_hash = compute_source_file_hash(fastled_dir)?; + if build_dir.join("build.ninja").is_file() && !force { + let stored = fs::read_to_string(&marker).unwrap_or_default(); + if stored.trim() == current_hash { + return Ok(()); + } + } + + fs::create_dir_all(build_dir)?; + let cross_file = write_native_cross_file(fastled_dir, tools)?; + let mut command = Command::new("meson"); + command.current_dir(fastled_dir); + command.arg("setup"); + if build_dir.join("build.ninja").is_file() { + command.arg("--reconfigure"); + } + command + .arg("--cross-file") + .arg(cross_file) + .arg(build_dir) + .arg(format!("-Dbuild_mode={}", mode.as_str())); + for (key, value) in build_env(tools) { + command.env(key, value); + } + println!("[WASM] Configuring meson (mode: {})...", mode.as_str()); + run_status(command, "meson setup")?; + fs::write(marker, current_hash)?; + Ok(()) +} + +fn library_archive(build_dir: &Path) -> PathBuf { + build_dir + .join("ci") + .join("meson") + .join("wasm") + .join("libfastled.a") +} + +fn build_library(fastled_dir: &Path, tools: &ToolPaths, build_dir: &Path) -> Result { + let archive = library_archive(build_dir); + let fingerprint_path = build_dir.join("library_src_fingerprint"); + let current = compute_source_file_hash(fastled_dir)?; + if archive.is_file() + && fingerprint_path.is_file() + && fs::read_to_string(&fingerprint_path) + .unwrap_or_default() + .trim() + == current + { + println!("[WASM] Library up-to-date"); + return Ok(false); + } + if archive.exists() { + fs::remove_file(&archive).ok(); + } + let mut command = Command::new("meson"); + command + .current_dir(fastled_dir) + .args(["compile", "-C"]) + .arg(build_dir) + .arg("fastled"); + for (key, value) in build_env(tools) { + command.env(key, value); + } + println!("[WASM] Building libfastled.a..."); + run_status(command, "meson compile fastled")?; + fs::write(fingerprint_path, current)?; + println!("[WASM] Library build successful"); + Ok(true) +} + +fn create_wrapper( + example_dir: &Path, + example_name: &str, + sketch_cache_dir: &Path, +) -> Result { + let ino_file = sketch_ino_file(example_dir)?; + fs::create_dir_all(sketch_cache_dir)?; + let mut lines = vec![ + format!("// Auto-generated wrapper for {example_name}.ino"), + "// C++20 header unit import; the .ino FastLED include is then a no-op.".to_string(), + "import \"wasm_pch.h\";".to_string(), + format!( + "#include \"{}\"", + ino_file.to_string_lossy().replace('\\', "/") + ), + ]; + let mut extras = Vec::new(); + collect_cpp_files(example_dir, &mut extras)?; + extras.sort(); + for cpp in extras { + if cpp != ino_file { + lines.push(format!( + "#include \"{}\"", + cpp.to_string_lossy().replace('\\', "/") + )); + } + } + let wrapper = sketch_cache_dir.join(format!("{}_wrapper.cpp", wrapper_stem(example_name))); + let content = lines.join("\n") + "\n"; + if fs::read_to_string(&wrapper).unwrap_or_default() != content { + fs::write(&wrapper, content)?; + } + Ok(wrapper) +} + +fn collect_cpp_files(dir: &Path, out: &mut Vec) -> Result<()> { + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + if path.is_dir() { + if entry.file_name().to_string_lossy() == ".build" { + continue; + } + collect_cpp_files(&path, out)?; + } else if path.extension().and_then(|ext| ext.to_str()) == Some("cpp") { + out.push(path); + } + } + Ok(()) +} + +fn build_sketch_pch( + fastled_dir: &Path, + tools: &ToolPaths, + build_dir: &Path, + mode: BuildMode, + lib_was_rebuilt: bool, +) -> Result> { + let pcm = build_dir.join("wasm_pch.h.pcm"); + let pch_o = build_dir.join("pch_codegen.o"); + let hash_path = build_dir.join("sketch_pch.hash"); + let header = fastled_dir + .join("src") + .join("platforms") + .join("wasm") + .join("compiler") + .join("wasm_pch.h"); + if !header.is_file() { + return Ok(None); + } + let mut hash_input = get_sketch_compile_flags(fastled_dir, mode)?.join("\n"); + hash_input.push_str(&compute_source_file_hash(fastled_dir)?); + let current_hash = format!("{:x}", Sha256::digest(hash_input.as_bytes())); + if !lib_was_rebuilt + && pcm.is_file() + && pch_o.is_file() + && fs::read_to_string(&hash_path).unwrap_or_default().trim() == current_hash + { + return Ok(Some(pcm)); + } + + let mut args = vec![ + "-fmodule-header=user".to_string(), + header.display().to_string(), + "-o".to_string(), + pcm.display().to_string(), + "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST".to_string(), + "-D_FILE_OFFSET_BITS=64".to_string(), + ]; + args.extend(get_sketch_compile_flags(fastled_dir, mode)?); + args.push(format!("-I{}", fastled_dir.join("src").display())); + args.push(format!( + "-I{}", + fastled_dir + .join("src") + .join("platforms") + .join("wasm") + .join("compiler") + .display() + )); + args.extend(["-Xclang".to_string(), "-fmodules-codegen".to_string()]); + + println!("[WASM] Building sketch header unit (.pcm)..."); + let mut command = command_with_env(&tools.python, tools); + command + .current_dir(fastled_dir) + .arg(&tools.emcc) + .args(&args); + run_status(command, "emcc header unit")?; + + let mut command = command_with_env(&tools.python, tools); + command + .current_dir(fastled_dir) + .arg(&tools.emcc) + .args(["-c"]) + .arg(&pcm) + .args(["-o"]) + .arg(&pch_o) + .args(["-O0", "-g0"]); + if run_status(command, "emcc header unit companion").is_ok() { + let archive = library_archive(build_dir); + if archive.is_file() && tools.emar.is_file() { + let mut command = command_with_env(&tools.python, tools); + command + .current_dir(fastled_dir) + .arg(&tools.emar) + .arg("r") + .arg(&archive) + .arg(&pch_o); + let _ = run_status(command, "emar pch companion"); + } + } + + fs::write(hash_path, current_hash)?; + Ok(Some(pcm)) +} + +fn compile_sketch( + fastled_dir: &Path, + tools: &ToolPaths, + wrapper: &Path, + build_dir: &Path, + sketch_cache_dir: &Path, + example_dir: &Path, + mode: BuildMode, +) -> Result { + let object = sketch_cache_dir.join("sketch.o"); + let archive = library_archive(build_dir); + let ino = sketch_ino_file(example_dir)?; + if object.is_file() { + let object_mtime = object.metadata()?.modified()?; + let inputs = [wrapper, &ino, &archive]; + if inputs + .iter() + .filter_map(|path| path.metadata().ok()) + .filter_map(|metadata| metadata.modified().ok()) + .all(|mtime| mtime <= object_mtime) + { + println!("[WASM] Sketch is up-to-date"); + return Ok(object); + } + } + + let mut args = vec![ + "-c".to_string(), + wrapper.display().to_string(), + "-o".to_string(), + object.display().to_string(), + ]; + args.extend(get_sketch_compile_flags(fastled_dir, mode)?); + args.push(format!("-I{}", fastled_dir.join("src").display())); + args.push(format!( + "-I{}", + fastled_dir + .join("src") + .join("platforms") + .join("wasm") + .join("compiler") + .display() + )); + args.push(format!("-I{}", example_dir.display())); + let pcm = build_dir.join("wasm_pch.h.pcm"); + if pcm.is_file() { + args.push(format!("-fmodule-file={}", pcm.display())); + } + + println!("[WASM] Compiling sketch: {}", wrapper.display()); + let mut command = command_with_env(&tools.python, tools); + command + .current_dir(fastled_dir) + .arg(&tools.empp) + .args(&args); + run_status(command, "em++ sketch compile")?; + Ok(object) +} + +fn link_wasm( + fastled_dir: &Path, + tools: &ToolPaths, + sketch_object: &Path, + build_dir: &Path, + sketch_cache_dir: &Path, + output_js: &Path, + mode: BuildMode, +) -> Result<()> { + let archive = library_archive(build_dir); + let cached_js = sketch_cache_dir.join("fastled.js"); + let cached_wasm = sketch_cache_dir.join("fastled.wasm"); + if cached_js.is_file() && cached_wasm.is_file() { + let output_mtime = cached_js.metadata()?.modified()?; + let inputs = [sketch_object, &archive]; + if inputs + .iter() + .filter_map(|path| path.metadata().ok()) + .filter_map(|metadata| metadata.modified().ok()) + .all(|mtime| mtime <= output_mtime) + { + copy_linked_output(sketch_cache_dir, output_js)?; + println!("[WASM] Link output up-to-date"); + return Ok(()); + } + } + + let js_library = fastled_dir + .join("src") + .join("platforms") + .join("wasm") + .join("compiler") + .join("js_library.js"); + let mut args = vec![ + sketch_object.display().to_string(), + archive.display().to_string(), + format!("-I{}", fastled_dir.join("src").display()), + format!( + "-I{}", + fastled_dir + .join("src") + .join("platforms") + .join("wasm") + .display() + ), + format!( + "-I{}", + fastled_dir + .join("src") + .join("platforms") + .join("wasm") + .join("compiler") + .display() + ), + format!("--js-library={}", js_library.display()), + "-o".to_string(), + cached_js.display().to_string(), + ]; + args.extend(get_link_flags(fastled_dir, mode)?); + + println!("[WASM] Linking final WASM module..."); + let mut command = command_with_env(&tools.python, tools); + command + .current_dir(fastled_dir) + .arg(&tools.empp) + .args(&args); + run_status(command, "em++ wasm link")?; + copy_linked_output(sketch_cache_dir, output_js)?; + Ok(()) +} + +fn copy_linked_output(sketch_cache_dir: &Path, output_js: &Path) -> Result<()> { + let output_dir = output_js + .parent() + .ok_or_else(|| anyhow::anyhow!("output path has no parent: {}", output_js.display()))?; + fs::create_dir_all(output_dir)?; + for name in ["fastled.js", "fastled.wasm", "fastled.wasm.dwarf"] { + let src = sketch_cache_dir.join(name); + if src.is_file() { + fs::copy(&src, output_dir.join(name)) + .with_context(|| format!("copy {} to {}", src.display(), output_dir.display()))?; + } + } + Ok(()) +} + +fn generate_manifest(example_dir: &Path, output_dir: &Path) -> Result<()> { + let mut files = Vec::::new(); + collect_data_files(example_dir, example_dir, &mut files)?; + fs::write( + output_dir.join("files.json"), + serde_json::to_string_pretty(&files)?, + )?; + Ok(()) +} + +fn collect_data_files(root: &Path, dir: &Path, out: &mut Vec) -> Result<()> { + if !dir.is_dir() { + return Ok(()); + } + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + let name = entry.file_name(); + let name = name.to_string_lossy(); + if path.is_dir() { + if name == "fastled_js" || name == ".build" { + continue; + } + collect_data_files(root, &path, out)?; + } else if matches!( + path.extension() + .and_then(|ext| ext.to_str()) + .map(|ext| ext.to_ascii_lowercase()) + .as_deref(), + Some("json" | "csv" | "txt" | "cfg" | "bin" | "dat" | "mp3" | "wav") + ) { + let rel = path.strip_prefix(root).unwrap_or(&path).to_string_lossy(); + out.push(serde_json::json!({ + "path": rel.replace('\\', "/"), + "size": path.metadata()?.len(), + })); + } + } + Ok(()) +} + +fn resolve_fastled_dir(request: &BuildRequest) -> Result<(PathBuf, Option)> { + if let Some(path) = &request.fastled_path { + return Ok((normalize_path(path), None)); + } + let repo = install::ensure_fastled_repo(Some("master"))?; + let cache_root = repo + .parent() + .map(Path::to_path_buf) + .unwrap_or_else(|| repo.clone()); + let short_dir = cache_root.join("fl").join("repo"); + if short_dir == repo { + return Ok((repo, None)); + } + let marker = short_dir.join(".fastled-rs-source"); + let source_key = repo.to_string_lossy().into_owned(); + if short_dir.join("library.json").is_file() + && fs::read_to_string(&marker).unwrap_or_default() == source_key + { + return Ok((short_dir, None)); + } + if short_dir.exists() { + fs::remove_dir_all(&short_dir).ok(); + } + if let Some(parent) = short_dir.parent() { + fs::create_dir_all(parent)?; + } + copy_dir(&repo, &short_dir)?; + fs::write(marker, source_key)?; + Ok((short_dir, None)) +} + +fn copy_dir(src: &Path, dst: &Path) -> Result<()> { + fs::create_dir_all(dst)?; + for entry in fs::read_dir(src)? { + let entry = entry?; + let path = entry.path(); + let target = dst.join(entry.file_name()); + if path.is_dir() { + if entry.file_name().to_string_lossy() == ".git" { + continue; + } + copy_dir(&path, &target)?; + } else { + fs::copy(&path, &target) + .with_context(|| format!("copy {} to {}", path.display(), target.display()))?; + } + } + Ok(()) +} + +pub fn run_build(request: &BuildRequest) -> Result { + let wall_start = std::time::Instant::now(); + let output_dir = mode_output_dir(&request.sketch_dir); + if request.force_clean && output_dir.exists() { + fs::remove_dir_all(&output_dir).ok(); + } + fs::create_dir_all(&output_dir)?; + + let emscripten_install = install::ensure_emscripten_installed()?; + archive::write_emscripten_config(&emscripten_install, "node")?; + let tools = resolve_tool_paths(&emscripten_install)?; + let (fastled_dir, _cleanup) = resolve_fastled_dir(request)?; + let fastled_dir = normalize_path(&fastled_dir); + let (example_name, example_dir, _is_in_tree) = + resolve_example_name(&normalize_path(&request.sketch_dir), &fastled_dir); + + let sketch_start = std::time::Instant::now(); + let strategy = if output_dir.join("fastled.js").is_file() + && output_dir.join("fastled.wasm").is_file() + && !request.force_clean + { + "incremental" + } else { + "cold" + } + .to_string(); + + let build_result = (|| -> Result<()> { + let build_dir = build_dir(&fastled_dir, request.build_mode); + ensure_meson_configured( + &fastled_dir, + &tools, + &build_dir, + request.build_mode, + request.force_clean, + )?; + let lib_rebuilt = build_library(&fastled_dir, &tools, &build_dir)?; + let _ = build_sketch_pch( + &fastled_dir, + &tools, + &build_dir, + request.build_mode, + lib_rebuilt, + ); + let sketch_cache = sketch_cache_dir(&example_dir); + let wrapper = create_wrapper(&example_dir, &example_name, &sketch_cache)?; + let sketch_o = compile_sketch( + &fastled_dir, + &tools, + &wrapper, + &build_dir, + &sketch_cache, + &example_dir, + request.build_mode, + )?; + let output_js = output_dir.join("fastled.js"); + link_wasm( + &fastled_dir, + &tools, + &sketch_o, + &build_dir, + &sketch_cache, + &output_js, + request.build_mode, + )?; + frontend::copy_frontend_to_output(&output_dir, None)?; + generate_manifest(&example_dir, &output_dir)?; + Ok(()) + })(); + + match build_result { + Ok(()) => Ok(BuildResult { + success: true, + output_dir, + duration_secs: wall_start.elapsed().as_secs_f64(), + sketch_time_secs: sketch_start.elapsed().as_secs_f64(), + strategy, + output: "Native Rust WASM build successful".to_string(), + }), + Err(err) => Ok(BuildResult { + success: false, + output_dir, + duration_secs: wall_start.elapsed().as_secs_f64(), + sketch_time_secs: sketch_start.elapsed().as_secs_f64(), + strategy, + output: format!("Native Rust WASM build failed: {err:#}"), + }), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn resolve_example_preserves_nested_names() { + let root = PathBuf::from("/tmp/FastLED"); + let sketch = root.join("examples").join("Fx").join("FxCylon"); + let (name, dir, in_tree) = resolve_example_name(&sketch, &root); + assert_eq!(name, "Fx/FxCylon"); + assert_eq!(dir, sketch); + assert!(in_tree); + } + + #[test] + fn resolve_example_maps_external_to_examples_leaf() { + let root = PathBuf::from("/tmp/FastLED"); + let sketch = PathBuf::from("/tmp/MySketch"); + let (name, dir, in_tree) = resolve_example_name(&sketch, &root); + assert_eq!(name, "MySketch"); + assert_eq!(dir, sketch); + assert!(!in_tree); + } + + #[test] + fn build_mode_strings_match_meson_modes() { + assert_eq!(BuildMode::Quick.as_str(), "quick"); + assert_eq!(BuildMode::Debug.as_str(), "debug"); + assert_eq!(BuildMode::Release.as_str(), "release"); + } +} diff --git a/crates/fastled-py/src/lib.rs b/crates/fastled-py/src/lib.rs index 175441ce..56e1d14e 100644 --- a/crates/fastled-py/src/lib.rs +++ b/crates/fastled-py/src/lib.rs @@ -2,6 +2,7 @@ use fastled_cli::frontend; use fastled_cli::install; use fastled_cli::project; use fastled_cli::viewer; +use fastled_cli::wasm_build; use fastled_cli::{PromptChoice, SketchSelection}; use pyo3::exceptions::{PyRuntimeError, PyValueError}; use pyo3::prelude::*; @@ -128,23 +129,6 @@ fn discover_artifacts(output_dir: &Path) -> HashMap { artifacts } -fn python_path(py: Python<'_>, path: &Path) -> PyResult> { - let pathlib = PyModule::import(py, "pathlib")?; - let cls = pathlib.getattr("Path")?; - Ok(cls - .call1((path.to_string_lossy().as_ref(),))? - .into_any() - .unbind()) -} - -fn py_fspath(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult { - let os = PyModule::import(py, "os")?; - let fspath = os.getattr("fspath")?; - let path_value = fspath.call1((value,))?; - let path_str: String = path_value.extract()?; - Ok(PathBuf::from(path_str)) -} - // --------------------------------------------------------------------------- // Internal: string_diff port // --------------------------------------------------------------------------- @@ -507,7 +491,6 @@ fn string_diff_impl( #[pyclass(name = "NativeBuildService")] struct NativeBuildService { - toolchains: HashMap, Py>, states: HashMap, } @@ -516,15 +499,15 @@ impl NativeBuildService { #[new] fn new() -> Self { Self { - toolchains: HashMap::new(), states: HashMap::new(), } } #[pyo3(signature = (toolchain, fastled_path=None))] fn register_toolchain(&mut self, toolchain: Py, fastled_path: Option<&str>) { - let key = normalize_optional_path(fastled_path); - self.toolchains.insert(key, toolchain); + let _ = (toolchain, fastled_path); + // Compatibility no-op. Build orchestration is now owned by + // fastled_cli::wasm_build rather than Python toolchain objects. } #[pyo3(signature = (sketch_dir, build_mode, profile=false, fastled_path=None, force_clean=false))] @@ -558,6 +541,7 @@ impl NativeBuildService { fastled_path: Option<&str>, force_clean: bool, ) -> PyResult> { + let _ = build_mode_obj; let sketch_dir = PathBuf::from(sketch_dir); let output_dir = sketch_dir.join("fastled_js"); let fastled_path = normalize_optional_path(fastled_path); @@ -573,27 +557,20 @@ impl NativeBuildService { let _ = fs::remove_dir_all(&output_dir); } - let toolchain = self - .toolchains - .get(&fastled_path) - .map(|toolchain| toolchain.clone_ref(py)) - .ok_or_else(|| PyRuntimeError::new_err("No toolchain registered for build"))?; - fs::create_dir_all(&output_dir).map_err(|err| PyRuntimeError::new_err(err.to_string()))?; - let start = Instant::now(); - let result_payload = match self.run_toolchain( - py, - toolchain, - &sketch_dir, - &output_dir, - build_mode_obj, + let request = wasm_build::BuildRequest { + sketch_dir: sketch_dir.clone(), + build_mode: Self::wasm_build_mode(build_mode)?, profile, - ) { - Ok(js_file) => { - let compile_time = start.elapsed().as_secs_f64(); + fastled_path: fastled_path.clone().map(PathBuf::from), + force_clean, + }; + + let result_payload = match wasm_build::run_build(&request) { + Ok(build_result) if build_result.success => { let zip_start = Instant::now(); - let zip_bytes = zip_output(&output_dir) + let zip_bytes = zip_output(&build_result.output_dir) .map_err(|err| PyRuntimeError::new_err(err.to_string()))?; let zip_time = zip_start.elapsed().as_secs_f64(); @@ -609,34 +586,34 @@ impl NativeBuildService { Self::build_payload( py, true, - format!( - "Native compilation successful!\nOutput: {}\nWASM: {}", - js_file.display(), - js_file.with_extension("wasm").display() - ), + build_result.output, zip_bytes, zip_time, - compile_time, - strategy, - &output_dir, - )? - } - Err(err) => { - if err.is_instance_of::(py) { - return Err(err); - } - let compile_time = start.elapsed().as_secs_f64(); - Self::build_payload( - py, - false, - format!("Native compilation failed: {err}"), - Vec::new(), - 0.0, - compile_time, - strategy, - &output_dir, + build_result.sketch_time_secs, + build_result.strategy, + &build_result.output_dir, )? } + Ok(build_result) => Self::build_payload( + py, + false, + build_result.output, + Vec::new(), + 0.0, + build_result.sketch_time_secs, + build_result.strategy, + &build_result.output_dir, + )?, + Err(err) => Self::build_payload( + py, + false, + format!("Native Rust WASM build failed: {err:#}"), + Vec::new(), + 0.0, + 0.0, + strategy, + &output_dir, + )?, }; Ok(result_payload) @@ -653,6 +630,17 @@ impl NativeBuildService { } impl NativeBuildService { + fn wasm_build_mode(build_mode: &str) -> PyResult { + match build_mode.to_ascii_lowercase().as_str() { + "quick" => Ok(wasm_build::BuildMode::Quick), + "debug" => Ok(wasm_build::BuildMode::Debug), + "release" => Ok(wasm_build::BuildMode::Release), + other => Err(PyValueError::new_err(format!( + "unsupported build mode: {other}" + ))), + } + } + fn detect_strategy_inner( &mut self, sketch_dir: &Path, @@ -705,27 +693,6 @@ impl NativeBuildService { "incremental".to_string() } - fn run_toolchain( - &self, - py: Python<'_>, - toolchain: Py, - sketch_dir: &Path, - output_dir: &Path, - build_mode_obj: Py, - profile: bool, - ) -> PyResult { - let kwargs = PyDict::new(py); - kwargs.set_item("sketch_dir", python_path(py, sketch_dir)?)?; - kwargs.set_item("output_dir", python_path(py, output_dir)?)?; - kwargs.set_item("build_mode", build_mode_obj)?; - kwargs.set_item("profile", profile)?; - - let js_file = toolchain - .bind(py) - .call_method("compile", (), Some(&kwargs))?; - py_fspath(py, &js_file) - } - #[allow(clippy::too_many_arguments)] fn build_payload( py: Python<'_>, diff --git a/pyproject.toml b/pyproject.toml index 91f5d791..2b630d4c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,18 +14,11 @@ classifiers = ["Programming Language :: Python :: 3"] dependencies = [ # Build-system dependencies for the FastLED C++ → WASM compile. # `meson` configures the build; `ninja` is meson's backend. Both ship - # standalone executables when pip-installed, which the toolchain - # invokes via subprocess from `src/fastled/toolchain/internal_wasm_build.py`. + # standalone executables when pip-installed. Rust owns Emscripten + # resolution via linked ctcb crates and invokes the checked-in + # emcc.py/em++.py/emar.py entry points directly. "meson>=1.4", "ninja>=1.11", - # FastLED's meson cross-file at `ci/meson/wasm_cross_file.ini` invokes - # `clang-tool-chain-emcc` / `clang-tool-chain-em++` / `clang-tool-chain-emar` - # as the wasm compiler/linker/archiver. Those wrapper scripts are shipped - # by the `clang-tool-chain` PyPI package. The Rust CLI installs the - # underlying emscripten toolchain itself (see install.rs); the wrappers - # find it via the CLANG_TOOL_CHAIN_DOWNLOAD_PATH env var that the Rust - # binary exports before delegating to Python. - "clang-tool-chain>=1.2.6", "zcmds_win32; sys_platform == 'win32'", ] diff --git a/src/fastled/build_service.py b/src/fastled/build_service.py index f1c3a4ab..a23ec345 100644 --- a/src/fastled/build_service.py +++ b/src/fastled/build_service.py @@ -2,56 +2,40 @@ This module preserves the Python ``BuildService`` API for callers that still import it directly. Build orchestration is owned by ``NativeBuildService``; -Python only adapts request/result types and registers compiler toolchains. +Python only adapts request/result types. """ from __future__ import annotations from pathlib import Path -from typing import Protocol, cast +from typing import Any, cast from fastled._native import NativeBuildService as _NativeBuildService from fastled.build_types import BuildRequest, BuildResult, BuildStrategy from fastled.interrupts import handle_keyboard_interrupt -from fastled.types import BuildMode, CompileResult +from fastled.types import CompileResult -def _toolchain_key(fastled_path: Path | None) -> str | None: +def _path_key(fastled_path: Path | None) -> str | None: return str(fastled_path.resolve()) if fastled_path else None -class _CompilerToolchain(Protocol): - def compile( - self, - sketch_dir: Path, - output_dir: Path, - build_mode: BuildMode, - profile: bool, - ) -> Path: ... - - class BuildService: def __init__(self) -> None: self._native = _NativeBuildService() - self._toolchains: dict[str | None, _CompilerToolchain] = {} - def register_toolchain( - self, fastled_path: Path | None, toolchain: _CompilerToolchain - ) -> None: - key = _toolchain_key(fastled_path) - self._toolchains[key] = toolchain - self._native.register_toolchain(toolchain, key) + def register_toolchain(self, fastled_path: Path | None, toolchain: Any) -> None: + """Compatibility no-op for callers that used to inject toolchains.""" + self._native.register_toolchain(toolchain, _path_key(fastled_path)) def build(self, request: BuildRequest) -> BuildResult: - self._ensure_toolchain(request.fastled_path) - try: payload = self._native.build( str(request.sketch_dir), request.build_mode.value, request.build_mode, request.profile, - _toolchain_key(request.fastled_path), + _path_key(request.fastled_path), request.force_clean, ) except KeyboardInterrupt as ki: @@ -88,21 +72,10 @@ def detect_strategy(self, request: BuildRequest) -> BuildStrategy: str(request.sketch_dir), request.build_mode.value, request.profile, - _toolchain_key(request.fastled_path), + _path_key(request.fastled_path), request.force_clean, ), ) def purge(self, sketch_dir: Path) -> None: self._native.purge(str(sketch_dir)) - - def _ensure_toolchain(self, fastled_path: Path | None) -> None: - key = _toolchain_key(fastled_path) - if key in self._toolchains: - return - - from fastled.toolchain.emscripten import EmscriptenToolchain - - self.register_toolchain( - fastled_path, EmscriptenToolchain(fastled_path=fastled_path) - ) diff --git a/src/fastled/frontend_esbuild.py b/src/fastled/frontend_esbuild.py index fd253f47..01fe77fc 100644 --- a/src/fastled/frontend_esbuild.py +++ b/src/fastled/frontend_esbuild.py @@ -1,8 +1,7 @@ """Compatibility shim. Frontend bundling is implemented in Rust. See ``crates/fastled-cli/src/frontend.rs`` for the implementation; this module -exists so existing callers (``fastled.toolchain.emscripten``, -``fastled.toolchain.internal_wasm_build``) keep working without churn. +exists so existing Python callers can keep importing the old helper. """ from __future__ import annotations diff --git a/src/fastled/install/examples_manager.py b/src/fastled/install/examples_manager.py index 47ef9ab0..1eebaee0 100644 --- a/src/fastled/install/examples_manager.py +++ b/src/fastled/install/examples_manager.py @@ -1,6 +1,6 @@ """Examples installation manager using FastLED's built-in --project-init command.""" -import subprocess +from fastled._rust_cli import invoke_rust_fastled_cli def install_fastled_examples_via_project_init( @@ -18,7 +18,7 @@ def install_fastled_examples_via_project_init( """ if not force: if no_interactive: - print("⚠️ No existing Arduino content found.") + print("No existing Arduino content found.") print(" In non-interactive mode, skipping examples installation.") print(" Run 'fastled --project-init' manually to install examples.") return False @@ -31,32 +31,23 @@ def install_fastled_examples_via_project_init( print("Skipping FastLED examples installation.") return False - print("📦 Installing FastLED examples using project initialization...") + print("Installing FastLED examples using project initialization...") try: - # Use FastLED's built-in project initialization - subprocess.run( - ["fastled", "--project-init"], - check=True, - capture_output=True, - text=True, - cwd=".", - ) + rc = invoke_rust_fastled_cli(["--project-init"]) + if rc != 0: + print(f"Warning: FastLED project initialization exited with {rc}") + print("You can manually run: fastled --project-init") + return False - print("✅ FastLED project initialized successfully!") - print("📁 Examples and project structure created") - print("🚀 Quick start: Check for generated .ino files and press F5 to debug") + print("FastLED project initialized successfully!") + print("Examples and project structure created") + print("Quick start: Check for generated .ino files and press F5 to debug") return True - except subprocess.CalledProcessError as e: - print(f"⚠️ Warning: Failed to initialize FastLED project: {e}") - if e.stderr: - print(f"Error details: {e.stderr}") - print("You can manually run: fastled --project-init") - return False - except FileNotFoundError: - print("⚠️ Warning: FastLED package not found. Please install it first:") - print(" pip install fastled") + except RuntimeError as e: + print(f"Warning: Failed to initialize FastLED project: {e}") + print(" Ensure the native fastled-rs CLI is installed or built.") print("Then run: fastled --project-init") return False diff --git a/src/fastled/site/build.py b/src/fastled/site/build.py index 33f1ce74..d3cf57cb 100644 --- a/src/fastled/site/build.py +++ b/src/fastled/site/build.py @@ -1,9 +1,10 @@ import argparse -import subprocess from pathlib import Path -from shutil import copytree, rmtree, which +from shutil import copytree, rmtree +from fastled._rust_cli import invoke_rust_fastled_cli from fastled.interrupts import handle_keyboard_interrupt +from fastled.project_init import project_init from fastled.site.examples import EXAMPLES CSS_CONTENT = """ @@ -342,17 +343,13 @@ """ -def _exec(cmd: str) -> None: - subprocess.run(cmd, shell=True, check=True) - - def build_example(example: str, outputdir: Path) -> None: - if not which("fastled"): - raise FileNotFoundError("fastled executable not found") src_dir = outputdir / example / "src" - _exec(f"fastled --init={example} --branch master {src_dir}") + project_init(example=example, outputdir=src_dir, ref="master") assert src_dir.exists() - _exec(f"fastled {src_dir / example} --just-compile") + rc = invoke_rust_fastled_cli([str(src_dir / example), "--just-compile"]) + if rc != 0: + raise RuntimeError(f"FastLED compile failed for {example} with exit code {rc}") fastled_dir = src_dir / example / "fastled_js" assert fastled_dir.exists(), f"fastled dir {fastled_dir} not found" # now copy it to the example dir diff --git a/src/fastled/toolchain/__init__.py b/src/fastled/toolchain/__init__.py index e647da40..2a40bd40 100644 --- a/src/fastled/toolchain/__init__.py +++ b/src/fastled/toolchain/__init__.py @@ -1,8 +1,7 @@ -""" -FastLED Toolchain Module +"""Compatibility exports for legacy FastLED toolchain imports. -Provides toolchain implementations for compiling FastLED sketches: -- EmscriptenToolchain: Compile to WebAssembly using Emscripten +WASM compilation is owned by the Rust backend. ``EmscriptenToolchain`` remains +as a thin facade for callers that still import it directly. """ from fastled.toolchain.emscripten import EmscriptenToolchain diff --git a/src/fastled/toolchain/emscripten.py b/src/fastled/toolchain/emscripten.py index 8c7d3333..6d918d83 100644 --- a/src/fastled/toolchain/emscripten.py +++ b/src/fastled/toolchain/emscripten.py @@ -1,24 +1,19 @@ -""" -Emscripten Toolchain for FastLED - -Provides native compilation using the Emscripten SDK (EMSDK) for compiling -FastLED sketches to WebAssembly. - -When a local FastLED repo is detected (with ci/wasm_build.py), delegates to -the repo's own build system which uses Meson+Ninja with command capture and -caching via clang-tool-chain for fast incremental rebuilds. +"""Compatibility Emscripten helpers for the native Rust WASM backend. -Otherwise falls back to a direct em++ invocation. +Build orchestration now lives in ``fastled_cli::wasm_build`` and is exposed to +Python through ``fastled._native.NativeBuildService``. This module keeps the +small public helper surface that older callers and tests import directly. """ +from __future__ import annotations + import os import shutil -import subprocess import sys from dataclasses import dataclass from pathlib import Path -from fastled.interrupts import handle_keyboard_interrupt +from fastled.build_types import BuildRequest from fastled.types import BuildMode _clang_tool_chain_emscripten_dir_cache: Path | None | str = "UNSET" @@ -27,16 +22,9 @@ def _resolve_example_name( sketch_dir: Path, fastled_dir: Path ) -> tuple[str, Path, bool]: - """Resolve the example name for wasm_build.py's --example flag. - - Returns (example_name, example_dir, is_in_tree). - For in-tree sketches, example_name is the relative path from examples/ - (e.g. "Fx/FxCylon" for examples/Fx/FxCylon). - For external sketches, example_name is the leaf directory name. - """ + """Resolve an example name relative to ``FastLED/examples``.""" try: rel_path = sketch_dir.relative_to(fastled_dir / "examples") - # Use forward slashes for the example name even on Windows return str(rel_path).replace("\\", "/"), sketch_dir, True except ValueError: name = sketch_dir.name @@ -44,14 +32,7 @@ def _resolve_example_name( def _get_clang_tool_chain_emscripten_dir() -> Path | None: - """Resolve the Emscripten installation directory (cached). - - Source of truth: ``FASTLED_EMSCRIPTEN_DIR`` set by the Rust CLI before - invoking Python — Rust owns the actual download/install via ``install.rs``. - Falls back to a directory search under ``CLANG_TOOL_CHAIN_DOWNLOAD_PATH`` - (or ``~/.clang-tool-chain``) so an existing Python-installed toolchain is - still picked up when Python is invoked without the Rust pre-step. - """ + """Resolve the Rust-installed Emscripten directory.""" global _clang_tool_chain_emscripten_dir_cache if _clang_tool_chain_emscripten_dir_cache != "UNSET": return _clang_tool_chain_emscripten_dir_cache # type: ignore[return-value] @@ -71,51 +52,26 @@ def _get_clang_tool_chain_emscripten_dir() -> Path | None: _clang_tool_chain_emscripten_dir_cache = None return None - if (emscripten_base / "emscripten" / "emcc.py").exists(): - _clang_tool_chain_emscripten_dir_cache = emscripten_base - return emscripten_base - - for subdir in emscripten_base.iterdir(): - if subdir.is_dir(): - for arch_dir in subdir.iterdir(): - if arch_dir.is_dir(): - if (arch_dir / "emscripten" / "emcc.py").exists(): - _clang_tool_chain_emscripten_dir_cache = arch_dir - return arch_dir - if (subdir / "emscripten" / "emcc.py").exists(): - _clang_tool_chain_emscripten_dir_cache = subdir - return subdir + candidates = [emscripten_base] + candidates.extend(path for path in emscripten_base.glob("*") if path.is_dir()) + candidates.extend(path for path in emscripten_base.glob("*/*") if path.is_dir()) + for candidate in candidates: + if (candidate / "emscripten" / "emcc.py").exists(): + _clang_tool_chain_emscripten_dir_cache = candidate + return candidate _clang_tool_chain_emscripten_dir_cache = None return None def ensure_clang_tool_chain_emscripten() -> Path | None: - """Return the resolved Emscripten installation directory. - - Install is now driven by the Rust CLI (`crates/fastled-cli/src/install.rs`) - which sets ``FASTLED_EMSCRIPTEN_DIR`` before invoking Python. This function - is kept for backward compatibility with callers that just want the - resolved directory; the actual download has moved out of Python. - """ + """Return the resolved Emscripten installation directory, if present.""" return _get_clang_tool_chain_emscripten_dir() def _setup_emscripten_env(env: dict[str, str]) -> None: - """Set up Emscripten environment variables like ci/wasm_tools.py does.""" + """Populate environment variables used by Emscripten subprocesses.""" if sys.platform == "win32": - # On Windows, Emscripten's system library compilation (libc, etc.) - # batches source files into groups and passes each group to a single - # emcc.bat invocation. The number of groups equals EMCC_CORES. - # With the default (cpu_count, typically 2-4), each group contains - # 50-100+ source files whose paths exceed cmd.exe's 8191-char limit, - # causing "The syntax of the command is incorrect" / "returned 255". - # Setting EMCC_CORES high creates many small groups (~2-3 files each) - # that stay well within the limit. This only affects the one-time - # system library cache build; regular compilation uses Meson/Ninja. - # NOTE: Must be set before the early return below — on CI, the - # clang-tool-chain is downloaded during the build, so it doesn't - # exist yet when this function is first called. env["EMCC_CORES"] = "128" clang_tool_chain_dir = _get_clang_tool_chain_emscripten_dir() @@ -137,84 +93,17 @@ def _setup_emscripten_env(env: dict[str, str]) -> None: env["PATH"] = f"{bin_dir}{os.pathsep}{env.get('PATH', '')}" -@dataclass +@dataclass(frozen=True) class CompilerPaths: - """Paths to Emscripten compiler executables.""" + """Paths to Emscripten compiler entry points.""" emcc: Path - empp: Path # em++ + empp: Path emar: Path -@dataclass -class EmscriptenConfig: - """Configuration for Emscripten compilation.""" - - build_mode: BuildMode - output_name: str = "fastled" - compile_flags: tuple[str, ...] = ( - "-DFASTLED_ENGINE_EVENTS_MAX_LISTENERS=50", - "-DFASTLED_FORCE_NAMESPACE=1", - "-DFASTLED_USE_PROGMEM=0", - "-DUSE_OFFSET_CONVERTER=0", - "-DGL_ENABLE_GET_PROC_ADDRESS=0", - "-D_REENTRANT=1", - "-DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0", - "-DSKETCH_COMPILE=1", - "-DFASTLED_WASM_USE_CCALL", - "-std=gnu++17", - "-fpermissive", - "-Wno-constant-logical-operand", - "-Wnon-c-typedef-for-linkage", - "-Werror=bad-function-cast", - "-Werror=cast-function-type", - "-fno-threadsafe-statics", - "-fno-exceptions", - "-fno-rtti", - "-pthread", - "-fpch-instantiate-templates", - ) - link_flags: tuple[str, ...] = ( - "-sWASM=1", - "-pthread", - "-sUSE_PTHREADS=1", - "-sPROXY_TO_PTHREAD", - "-sMODULARIZE=1", - "-sEXPORT_NAME=fastled", - "-sALLOW_MEMORY_GROWTH=1", - "-sINITIAL_MEMORY=134217728", - "-sAUTO_NATIVE_LIBRARIES=0", - "-sEXPORTED_RUNTIME_METHODS=['ccall','cwrap','stringToUTF8','UTF8ToString','lengthBytesUTF8','HEAPU8','getValue']", - "-sEXPORTED_FUNCTIONS=['_malloc','_free','_main','_extern_setup','_extern_loop','_fastled_declare_files','_getStripPixelData','_getFrameData','_getScreenMapData','_freeFrameData','_getFrameVersion','_hasNewFrameData','_js_fetch_success_callback','_js_fetch_error_callback']", - "-sEXIT_RUNTIME=0", - "-sFILESYSTEM=0", - "-sERROR_ON_UNDEFINED_SYMBOLS=0", - ) - - @property - def common_flags(self) -> tuple[str, ...]: - return self.compile_flags + self.link_flags - - def get_optimization_flags(self) -> list[str]: - if self.build_mode == BuildMode.DEBUG: - return ["-g", "-O0", "-sASSERTIONS=2"] - elif self.build_mode == BuildMode.QUICK: - return ["-O1", "-sASSERTIONS=0"] - else: - return ["-O3", "-flto", "-sASSERTIONS=0"] - - class EmscriptenToolchain: - """Emscripten toolchain for compiling FastLED sketches to WebAssembly. - - When a FastLED repo with ci/wasm_build.py is available, delegates to - that build system for proper compilation with command capture/caching. - """ - - FASTLED_GITHUB_URL = "https://github.com/FastLED/FastLED" - FASTLED_ARCHIVE_URL = ( - "https://github.com/FastLED/FastLED/archive/refs/heads/master.zip" - ) + """Compatibility wrapper around the native Rust WASM build service.""" def __init__( self, @@ -226,325 +115,43 @@ def __init__( self._compiler_paths: CompilerPaths | None = None self._version: str | None = None - def _find_emsdk(self) -> Path | None: - """Find EMSDK installation path.""" - clang_tool_chain_dir = _get_clang_tool_chain_emscripten_dir() - if clang_tool_chain_dir and clang_tool_chain_dir.exists(): - if (clang_tool_chain_dir / "emscripten").exists(): - return clang_tool_chain_dir - - emsdk_env = os.environ.get("EMSDK") - if emsdk_env: - return Path(emsdk_env) - - common_paths = [ - Path.home() / "emsdk", - Path("/opt/emsdk"), - Path("C:/emsdk"), - Path.home() / ".emsdk", - Path.home() / "AppData" / "Local" / "emsdk", - ] - for path in common_paths: - if path.exists() and (path / "upstream" / "emscripten").exists(): - return path - - return None - def _find_compilers(self) -> CompilerPaths: - """Find Emscripten compiler executables.""" + """Find compiler entry points without installing or building anything.""" clang_tool_chain_dir = ensure_clang_tool_chain_emscripten() if clang_tool_chain_dir and clang_tool_chain_dir.exists(): - emscripten_scripts_dir = clang_tool_chain_dir / "emscripten" - if emscripten_scripts_dir.exists(): - emcc_path = emscripten_scripts_dir / "emcc.py" - empp_path = emscripten_scripts_dir / "em++.py" - emar_path = emscripten_scripts_dir / "emar.py" - if emcc_path.exists() and empp_path.exists(): - return CompilerPaths( - emcc=emcc_path, - empp=empp_path, - emar=emar_path if emar_path.exists() else emcc_path, - ) - - for name_emcc, name_empp, name_emar in [ - ("clang-tool-chain-emcc", "clang-tool-chain-em++", "clang-tool-chain-emar"), - ]: - ctc_emcc = shutil.which(name_emcc) - ctc_empp = shutil.which(name_empp) - ctc_emar = shutil.which(name_emar) - if ctc_emcc and ctc_empp: + scripts_dir = clang_tool_chain_dir / "emscripten" + emcc = scripts_dir / "emcc.py" + empp = scripts_dir / "em++.py" + emar = scripts_dir / "emar.py" + if emcc.exists() and empp.exists(): return CompilerPaths( - emcc=Path(ctc_emcc), - empp=Path(ctc_empp), - emar=Path(ctc_emar) if ctc_emar else Path(ctc_emcc), + emcc=emcc, + empp=empp, + emar=emar if emar.exists() else emcc, ) emcc = shutil.which("emcc") empp = shutil.which("em++") emar = shutil.which("emar") - if emcc and empp and emar: - return CompilerPaths(emcc=Path(emcc), empp=Path(empp), emar=Path(emar)) - - emsdk = self._emsdk_path or self._find_emsdk() - if emsdk: - if (emsdk / "emscripten").exists() and not (emsdk / "upstream").exists(): - emscripten_scripts_dir = emsdk / "emscripten" - emcc_path = emscripten_scripts_dir / "emcc.py" - empp_path = emscripten_scripts_dir / "em++.py" - emar_path = emscripten_scripts_dir / "emar.py" - else: - emscripten_dir = emsdk / "upstream" / "emscripten" - if sys.platform == "win32": - emcc_path = emscripten_dir / "emcc.bat" - empp_path = emscripten_dir / "em++.bat" - emar_path = emscripten_dir / "emar.bat" - else: - emcc_path = emscripten_dir / "emcc" - empp_path = emscripten_dir / "em++" - emar_path = emscripten_dir / "emar" + if emcc and empp: + return CompilerPaths( + emcc=Path(emcc), + empp=Path(empp), + emar=Path(emar) if emar else Path(emcc), + ) - if emcc_path.exists() and empp_path.exists(): - return CompilerPaths( - emcc=emcc_path, - empp=empp_path, - emar=emar_path if emar_path.exists() else emcc_path, - ) + if self._emsdk_path: + emscripten_dir = self._emsdk_path / "upstream" / "emscripten" + emcc = emscripten_dir / ("emcc.bat" if sys.platform == "win32" else "emcc") + empp = emscripten_dir / ("em++.bat" if sys.platform == "win32" else "em++") + emar = emscripten_dir / ("emar.bat" if sys.platform == "win32" else "emar") + if emcc.exists() and empp.exists(): + return CompilerPaths(emcc=emcc, empp=empp, emar=emar) raise FileNotFoundError( - "Emscripten SDK not found. Please install using one of these methods:\n\n" - "Option 1 (Recommended): Install clang-tool-chain package:\n" - " pip install clang-tool-chain\n" - " clang-tool-chain-emcc --version # Auto-downloads Emscripten on first use\n\n" - "Option 2: Install EMSDK manually:\n" - " https://emscripten.org/docs/getting_started/downloads.html\n" - " Or set EMSDK environment variable to point to your EMSDK installation." - ) - - CACHE_DIR = Path.home() / ".fastled" / "cache" - CACHE_MAX_AGE_SECONDS = 24 * 60 * 60 # 24 hours - - def _download_fastled(self, target_dir: Path) -> Path: - """Materialise the FastLED library at ``target_dir/repo``. - - Delegates the actual GitHub download to the Rust CLI's - ``--internal-ensure-fastled-repo`` flag (see - ``crates/fastled-cli/src/install.rs``) so the Python side no longer - needs httpx. The Rust binary caches under ``~/.fastled/cache/`` and - returns a path which we then copy into ``target_dir/repo`` (the - short-name move avoids Windows command-line length limits in - deeply-nested build paths). - """ - from fastled.project_init import _ensure_repo_via_rust - - print("Materialising FastLED library via fastled CLI (master)...") - repo_root = _ensure_repo_via_rust("master") - - short_dir = target_dir / "repo" - if short_dir.exists(): - shutil.rmtree(short_dir, ignore_errors=True) - target_dir.mkdir(parents=True, exist_ok=True) - shutil.copytree(repo_root, short_dir, symlinks=True) - return short_dir - - def _is_cache_fresh(self) -> bool: - """Check if the cached FastLED download is still fresh.""" - import time - - timestamp_file = self.CACHE_DIR / "fl" / ".cache_timestamp" - if not timestamp_file.exists(): - return False - try: - cached_time = float(timestamp_file.read_text().strip()) - return (time.time() - cached_time) < self.CACHE_MAX_AGE_SECONDS - except (ValueError, OSError): - return False - - def _get_fastled_path(self) -> Path: - """Get the path to FastLED library, downloading if necessary. - - Uses a persistent cache at ~/.fastled/cache/ to avoid re-downloading - on every cold compile. Cache is refreshed after 24 hours. - """ - import time - - if self._fastled_path and self._fastled_path.exists(): - return self._fastled_path - - # Check persistent cache - cache_extract_dir = self.CACHE_DIR / "fl" - if cache_extract_dir.exists() and self._is_cache_fresh(): - # Check for the renamed "repo" dir (short name to avoid Windows path limits) - repo_dir = cache_extract_dir / "repo" - if repo_dir.is_dir(): - print("Using cached FastLED library...") - self._fastled_path = repo_dir - return repo_dir - - # Download to persistent cache - cache_extract_dir.mkdir(parents=True, exist_ok=True) - # Clean old cache contents - for item in cache_extract_dir.iterdir(): - if item.name == ".cache_timestamp": - continue - if item.is_dir(): - shutil.rmtree(item, ignore_errors=True) - else: - item.unlink(missing_ok=True) - - fastled_dir = self._download_fastled(cache_extract_dir) - # Write timestamp - timestamp_file = cache_extract_dir / ".cache_timestamp" - timestamp_file.write_text(str(time.time())) - - self._fastled_path = fastled_dir - return fastled_dir - - def _has_wasm_build_system(self, fastled_dir: Path) -> bool: - """Check if the FastLED repo has ci/wasm_build.py.""" - return (fastled_dir / "ci" / "wasm_build.py").exists() - - def _compile_via_wasm_build( - self, - sketch_dir: Path, - output_dir: Path, - fastled_dir: Path, - build_mode: BuildMode, - ) -> Path: - """Run the copied Meson/Ninja WASM builder from this repo.""" - from fastled.toolchain import internal_wasm_build - - mode_map = { - BuildMode.DEBUG: "debug", - BuildMode.QUICK: "quick", - BuildMode.RELEASE: "release", - } - mode = mode_map[build_mode] - sketch_name, example_dir, is_in_tree = _resolve_example_name( - sketch_dir, fastled_dir + "Emscripten SDK not found. Install it through the native fastled CLI " + "or set FASTLED_EMSCRIPTEN_DIR." ) - output_js = output_dir / "fastled.js" - - if not is_in_tree: - if not example_dir.exists(): - example_dir.symlink_to(sketch_dir, target_is_directory=True) - needs_cleanup = True - else: - needs_cleanup = False - - meson_build_dir = fastled_dir / ".build" / f"meson-wasm-{mode}" - - try: - internal_wasm_build.configure_project_root(fastled_dir) - print(f"Running internal WASM build system (mode: {mode})...") - result = internal_wasm_build.build( - example=sketch_name, - output=str(output_js), - mode=mode, - ) - if result != 0 and meson_build_dir.exists(): - print( - "FastLED build system failed once, wiping stale Meson cache and retrying..." - ) - shutil.rmtree(meson_build_dir, ignore_errors=True) - result = internal_wasm_build.build( - example=sketch_name, - output=str(output_js), - mode=mode, - ) - if result != 0: - raise RuntimeError( - f"FastLED build system failed with return code {result}" - ) - finally: - if needs_cleanup and example_dir.is_symlink(): - example_dir.unlink() - - return output_js - - def _uses_unity_build(self, fastled_dir: Path) -> bool: - """Check if the FastLED repo uses unity build pattern.""" - src_dir = fastled_dir / "src" - return (src_dir / "_build.cpp").exists() and ( - src_dir / "fl" / "_build.cpp" - ).exists() - - def _create_wasm_platform_build(self, build_dir: Path) -> Path: - """Create a custom platforms unity build that only includes wasm/stub/shared.""" - content = """// Auto-generated platform build for WASM native compilation -// Only includes wasm, stub, and shared platform sources - -#include "FastLED.h" - -// Platform root-level sources (guarded by #ifdef, safe for all platforms) -#include "platforms/compile_test.cpp.hpp" -#include "platforms/debug_setup.cpp.hpp" -#include "platforms/ota.cpp.hpp" -#include "platforms/stub_main.cpp.hpp" - -// WASM-relevant platform subdirectories only -#include "platforms/shared/_build.cpp.hpp" -#include "platforms/stub/_build.cpp.hpp" -#include "platforms/wasm/_build.cpp.hpp" -""" - platform_build = build_dir / "wasm_platforms_build.cpp" - platform_build.write_text(content) - return platform_build - - def _get_wasm_sources(self, fastled_dir: Path) -> list[Path]: - """Get FastLED WASM platform source files.""" - src_dir = fastled_dir / "src" - sources: list[Path] = [] - for d in ["platforms/wasm", "platforms/stub", "platforms/shared"]: - p = src_dir / d - if p.exists(): - sources.extend(p.rglob("*.cpp")) - return sources - - def _get_fastled_core_sources(self, fastled_dir: Path) -> list[Path]: - """Get FastLED core source files (excluding platform-specific).""" - src_dir = fastled_dir / "src" - if not src_dir.exists(): - raise FileNotFoundError(f"FastLED src directory not found at {src_dir}") - - sources: list[Path] = [] - for pattern in ["*.cpp", "*.c"]: - sources.extend(src_dir.glob(pattern)) - - for subdir in ["fl", "fx"]: - d = src_dir / subdir - if d.exists(): - for pattern in ["*.cpp", "*.c"]: - sources.extend(d.rglob(pattern)) - - return sources - - def _get_sketch_sources(self, sketch_dir: Path) -> list[Path]: - """Get sketch source files (.ino, .cpp, .c).""" - sources: list[Path] = [] - for ino_file in sketch_dir.glob("*.ino"): - sources.append(ino_file) - for pattern in ["*.cpp", "*.c"]: - sources.extend(sketch_dir.glob(pattern)) - return sources - - def _create_sketch_wrapper( - self, sketch_sources: list[Path], output_dir: Path - ) -> Path: - """Create sketch.cpp wrapper.""" - sketch_content = "" - for src in sketch_sources: - if src.suffix == ".ino": - sketch_content += f"\n// From {src.name}\n" - sketch_content += src.read_text() - - wrapper_content = f""" -// Auto-generated sketch wrapper for FastLED WASM native compilation -#include "FastLED.h" -{sketch_content} -""" - sketch_path = output_dir / "sketch.cpp" - sketch_path.write_text(wrapper_content) - return sketch_path def compile( self, @@ -553,261 +160,47 @@ def compile( build_mode: BuildMode = BuildMode.QUICK, profile: bool = False, ) -> Path: - """Compile a FastLED sketch to WebAssembly. - - If the FastLED repo has ci/wasm_build.py, delegates to that build - system for proper Meson+Ninja compilation with command capture and - caching. Otherwise falls back to a single-pass em++ invocation. - """ - if self._compiler_paths is None: - self._compiler_paths = self._find_compilers() - - fastled_dir = self._get_fastled_path() - print(f"Using FastLED library at: {fastled_dir}") - - output_dir.mkdir(parents=True, exist_ok=True) - - # Preferred path: delegate to FastLED's build system - if self._has_wasm_build_system(fastled_dir): - output_js = self._compile_via_wasm_build( - sketch_dir, output_dir, fastled_dir, build_mode - ) - - # Copy frontend assets - print("Copying frontend assets...") - self._copy_frontend_assets(output_dir, fastled_dir) - - wasm_file = output_dir / "fastled.wasm" - print("Compilation successful!") - print(f" JS: {output_js}") - print(f" WASM: {wasm_file}") - return output_js - - # Fallback: single-pass em++ (no caching, for repos without ci/wasm_build.py) - return self._compile_fallback( - sketch_dir, output_dir, fastled_dir, build_mode, profile + """Compile via ``BuildService`` for legacy direct-toolchain callers.""" + from fastled.build_service import BuildService + + request = BuildRequest( + sketch_dir=sketch_dir, + build_mode=build_mode, + profile=profile, + fastled_path=self._fastled_path, ) - - def _compile_fallback( - self, - sketch_dir: Path, - output_dir: Path, - fastled_dir: Path, - build_mode: BuildMode, - profile: bool, - ) -> Path: - """Fallback single-pass em++ compilation for repos without ci/wasm_build.py.""" - assert self._compiler_paths is not None - config = EmscriptenConfig(build_mode=build_mode) - - build_dir = output_dir / ".build" - build_dir.mkdir(exist_ok=True) - - sketch_sources = self._get_sketch_sources(sketch_dir) - if not sketch_sources: - raise FileNotFoundError(f"No sketch files found in {sketch_dir}") - - sketch_file = self._create_sketch_wrapper(sketch_sources, build_dir) - - include_paths = [ - f"-I{fastled_dir}/src", - f"-I{fastled_dir}/src/platforms/wasm", - f"-I{fastled_dir}/src/platforms/wasm/compiler", - f"-I{fastled_dir}/src/platforms/stub", - f"-I{fastled_dir}/src/platforms/shared", - f"-I{sketch_dir}", - ] - - output_file = output_dir / f"{config.output_name}.js" - - if self._uses_unity_build(fastled_dir): - src_dir = fastled_dir / "src" - all_sources: list[Path] = [ - src_dir / "_build.cpp", - src_dir / "fl" / "_build.cpp", - src_dir / "third_party" / "_build.cpp", - self._create_wasm_platform_build(build_dir), - ] - all_sources = [s for s in all_sources if s.exists()] - print( - f"Compiling with {len(sketch_sources)} sketch files using unity build ({len(all_sources)} build units)..." - ) - else: - core_sources = self._get_fastled_core_sources(fastled_dir) - wasm_sources = self._get_wasm_sources(fastled_dir) - all_sources = core_sources + wasm_sources - print( - f"Compiling with {len(sketch_sources)} sketch files and {len(all_sources)} FastLED files..." - ) - - response_file = build_dir / "compile_args.rsp" - - def to_posix_path(p: str) -> str: - return p.replace("\\", "/") - - # Add --js-library for JS bindings - js_library = ( - fastled_dir / "src" / "platforms" / "wasm" / "compiler" / "js_library.js" - ) - extra_link_flags: list[str] = [] - if js_library.exists(): - extra_link_flags.append(f"--js-library={to_posix_path(str(js_library))}") - - response_content_lines = [ - *[to_posix_path(p) for p in include_paths], - *config.common_flags, - *config.get_optimization_flags(), - *extra_link_flags, - "-o", - to_posix_path(str(output_file)), - to_posix_path(str(sketch_file)), - *[to_posix_path(str(s)) for s in all_sources], - ] - response_file.write_text("\n".join(response_content_lines)) - - empp_path = self._compiler_paths.empp - env = os.environ.copy() - _setup_emscripten_env(env) - - cmd = self._build_emcc_cmd(empp_path, response_file) - - if profile: - print(f"Response file: {response_file}") - print(f"Compile command: {' '.join(cmd)}") - - try: - result = subprocess.run( - cmd, - capture_output=True, - text=True, - check=True, - env=env, - ) - if result.stdout: - print(result.stdout) - except subprocess.CalledProcessError as e: - error_msg = f"Compilation failed:\n{e.stderr}\n{e.stdout}" - print(error_msg) - raise RuntimeError(error_msg) from e - - print("Copying frontend assets...") - self._copy_frontend_assets(output_dir, fastled_dir) - - if build_dir.exists(): - shutil.rmtree(build_dir, ignore_errors=True) - - wasm_file = output_dir / f"{config.output_name}.wasm" - print("Compilation successful!") - print(f" JS: {output_file}") - print(f" WASM: {wasm_file}") - return output_file - - def _build_emcc_cmd(self, empp_path: Path, response_file: Path) -> list[str]: - """Build the em++ command list.""" - if empp_path.suffix == ".py": - return [sys.executable, str(empp_path), f"@{response_file}"] - else: - return [str(empp_path), f"@{response_file}"] - - @staticmethod - def _compute_dir_hash(directory: Path) -> str: - """Compute a quick hash of a directory based on file names and sizes.""" - import hashlib - - h = hashlib.md5() - for f in sorted(directory.rglob("*")): - if f.is_file(): - h.update(str(f.relative_to(directory)).encode()) - h.update(str(f.stat().st_size).encode()) - return h.hexdigest() - - def _copy_frontend_assets(self, output_dir: Path, fastled_dir: Path) -> None: - """Copy frontend assets built by fastled-wasm's bundled esbuild pipeline.""" - from fastled.frontend_esbuild import copy_frontend_to_output - - try: - copy_frontend_to_output(output_dir) - except KeyboardInterrupt as ki: - handle_keyboard_interrupt(ki) - except Exception as exc: - print(f"Warning: frontend build failed: {exc}") - self._create_minimal_index_html(output_dir, "fastled") - return - - files_json = output_dir / "files.json" - if not files_json.exists(): - files_json.write_text("[]") - - def _create_minimal_index_html(self, output_dir: Path, module_name: str) -> None: - """Create a minimal fallback index.html.""" - html_content = f""" - - - - FastLED WASM - - - - - - - -""" - index_path = output_dir / "index.html" - index_path.write_text(html_content) + result = BuildService().build(request) + if not result.success: + raise RuntimeError(result.stdout) + + expected_output = sketch_dir / "fastled_js" + if output_dir != expected_output: + output_dir.mkdir(parents=True, exist_ok=True) + for artifact in expected_output.iterdir(): + target = output_dir / artifact.name + if artifact.is_dir(): + if target.exists(): + shutil.rmtree(target) + shutil.copytree(artifact, target) + else: + shutil.copy2(artifact, target) + return output_dir / "fastled.js" def check_installation(self) -> bool: - """Check if Emscripten is properly installed.""" + """Return whether compiler entry points can be found.""" try: - if self._compiler_paths is None: - self._compiler_paths = self._find_compilers() + self._compiler_paths = self._find_compilers() return True except FileNotFoundError: return False def get_version(self) -> str | None: - """Get Emscripten version if installed (cached after first call).""" + """Return a cached lightweight version marker when Emscripten is present.""" if self._version is not None: return self._version - try: - if self._compiler_paths is None: - self._compiler_paths = self._find_compilers() - emcc_path = self._compiler_paths.emcc - env = os.environ.copy() - _setup_emscripten_env(env) - - if emcc_path.suffix == ".py": - cmd = [sys.executable, str(emcc_path), "--version"] - else: - cmd = [str(emcc_path), "--version"] - - result = subprocess.run(cmd, capture_output=True, text=True, env=env) - if result.returncode == 0: - self._version = result.stdout.split("\n")[0] - return self._version - return None - except (FileNotFoundError, subprocess.SubprocessError): + self._compiler_paths = self._find_compilers() + except FileNotFoundError: return None + self._version = str(self._compiler_paths.emcc) + return self._version diff --git a/src/fastled/toolchain/internal_wasm_build.py b/src/fastled/toolchain/internal_wasm_build.py deleted file mode 100644 index a92a8713..00000000 --- a/src/fastled/toolchain/internal_wasm_build.py +++ /dev/null @@ -1,1696 +0,0 @@ -#!/usr/bin/env python3 -""" -Thin WASM build orchestrator using Meson + Ninja. - -This script replaces ~3000 lines of custom Python build logic with a thin -orchestrator that delegates compilation to Meson/Ninja and only handles: - - Meson setup with emscripten cross file - - Ninja invocation for library build - - Sketch compilation (single emcc call) - - Sketch linking (single emcc call) - - Template file copying and manifest generation - -All dependency tracking, parallel compilation, PCH management, and -incremental builds are handled by Ninja automatically. - -Per-sketch artifacts are cached in .build/wasm// to enable -fast switching between sketches without recompilation. - -Usage: - uv run python ci/wasm_build.py --example Blink -o examples/Blink/fastled_js/fastled.js - uv run python ci/wasm_build.py --example Blink -o output.js --mode debug - uv run python ci/wasm_build.py --example Blink -o output.js --force -""" - -# pyright: reportMissingImports=false - -import argparse -import hashlib -import json -import os -import shutil -import subprocess -import sys -import time -import tomllib -from pathlib import Path - -from fastled.interrupts import handle_keyboard_interrupt - -PROJECT_ROOT = Path.cwd() -CROSS_FILE = PROJECT_ROOT / "ci" / "meson" / "wasm_cross_file.ini" -WASM_PCH_HEADER = ( - PROJECT_ROOT / "src" / "platforms" / "wasm" / "compiler" / "wasm_pch.h" -) -BUILD_DIR_PREFIX = PROJECT_ROOT / ".build" -BUILD_FLAGS_TOML = ( - PROJECT_ROOT / "src" / "platforms" / "wasm" / "compiler" / "build_flags.toml" -) - - -def configure_project_root(project_root: Path) -> None: - """Bind the copied build logic to a specific FastLED checkout.""" - global PROJECT_ROOT - global CROSS_FILE - global WASM_PCH_HEADER - global BUILD_DIR_PREFIX - global BUILD_FLAGS_TOML - global _cached_fingerprint - global _cached_fingerprint_time - - PROJECT_ROOT = project_root - CROSS_FILE = PROJECT_ROOT / "ci" / "meson" / "wasm_cross_file.ini" - WASM_PCH_HEADER = ( - PROJECT_ROOT / "src" / "platforms" / "wasm" / "compiler" / "wasm_pch.h" - ) - BUILD_DIR_PREFIX = PROJECT_ROOT / ".build" - BUILD_FLAGS_TOML = ( - PROJECT_ROOT / "src" / "platforms" / "wasm" / "compiler" / "build_flags.toml" - ) - _cached_fingerprint = None - _cached_fingerprint_time = 0.0 - os.environ.update(_build_env()) - - -def _load_toml() -> dict: - """Load build flags from the bound FastLED checkout.""" - if not BUILD_FLAGS_TOML.exists(): - raise FileNotFoundError(f"Build flags TOML not found: {BUILD_FLAGS_TOML}") - with open(BUILD_FLAGS_TOML, "rb") as handle: - return tomllib.load(handle) - - -def get_sketch_compile_flags(mode: str = "quick") -> list[str]: - """Read sketch compile flags from FastLED's build_flags.toml.""" - config = _load_toml() - build_mode_config = config.get("build_modes", {}).get(mode, {}) - defines = list(config.get("all", {}).get("defines", [])) - compiler_flags = list(config.get("all", {}).get("compiler_flags", [])) - defines.extend(config.get("sketch", {}).get("defines", [])) - compiler_flags.extend(config.get("sketch", {}).get("compiler_flags", [])) - sketch_mode_flags = build_mode_config.get("sketch_flags") - if sketch_mode_flags is not None: - compiler_flags.extend(sketch_mode_flags) - else: - compiler_flags.extend(build_mode_config.get("flags", [])) - return defines + compiler_flags - - -def get_link_flags(mode: str = "quick") -> list[str]: - """Read link flags from FastLED's build_flags.toml.""" - config = _load_toml() - build_mode_config = config.get("build_modes", {}).get(mode, {}) - link_flags = list(config.get("linking", {}).get("base", {}).get("flags", [])) - link_flags.extend(config.get("linking", {}).get("sketch", {}).get("flags", [])) - link_flags.extend(build_mode_config.get("link_flags", [])) - return link_flags - - -def _build_env() -> dict[str, str]: - """Create an emscripten-ready environment using fastled-wasm's toolchain setup.""" - from fastled.toolchain.emscripten import _setup_emscripten_env - - env = os.environ.copy() - _setup_emscripten_env(env) - return env - - -def get_emcc() -> str: - """Resolve the emcc entrypoint from fastled-wasm's installed toolchain.""" - from fastled.toolchain.emscripten import EmscriptenToolchain - - return str(EmscriptenToolchain()._find_compilers().emcc) - - -def get_emar() -> str: - """Resolve the emar entrypoint from fastled-wasm's installed toolchain.""" - from fastled.toolchain.emscripten import EmscriptenToolchain - - return str(EmscriptenToolchain()._find_compilers().emar) - - -def get_native_emcc() -> str | None: - """Use FastLED's compiled native emcc launcher when available.""" - suffix = ".exe" if sys.platform == "win32" else "" - candidate = PROJECT_ROOT / ".build" / "native-tools" / f"ctc-emcc{suffix}" - if candidate.exists(): - return str(candidate) - return None - - -def run_emcc(args: list[str], cwd: str | None = None) -> int: - """Run emcc via subprocess using fastled-wasm's configured environment.""" - result = subprocess.run(_tool_command(get_emcc()) + args, cwd=cwd, env=_build_env()) - return result.returncode - - -def _tool_command(path: str) -> list[str]: - tool_path = Path(path) - if tool_path.suffix == ".py": - return [sys.executable, str(tool_path)] - return [str(tool_path)] - - -def get_build_dir(mode: str) -> Path: - """Get the meson build directory for the given mode.""" - return BUILD_DIR_PREFIX / f"meson-wasm-{mode}" - - -def get_sketch_cache_dir(example_name: str) -> Path: - """Get per-sketch cache directory next to the .ino file: /.build/wasm/.""" - ino_file = PROJECT_ROOT / "examples" / example_name / f"{example_name}.ino" - if not ino_file.exists(): - raise FileNotFoundError(f"Sketch not found: {ino_file}") - d = ino_file.parent / ".build" / "wasm" - d.mkdir(parents=True, exist_ok=True) - return d - - -def get_meson_executable() -> str: - """Get the meson executable path.""" - # Use the same meson that the native build uses - return "meson" - - -# ============================================================================ -# Library fingerprint — skip meson/ninja when source tree hasn't changed -# ============================================================================ - - -_cached_fingerprint: str | None = None -_cached_fingerprint_time: float = 0.0 -_cached_js_fingerprint: str | None = None -_cached_js_fingerprint_time: float = 0.0 -_cached_emcc_version_key: str | None = None -_cached_emcc_version_value: str | None = None -_cached_emcc_version_time: float = 0.0 -_LINK_ENV_VARS = ( - "EMCC_CFLAGS", - "EMCC_DEBUG", - "EMSDK", - "EM_CONFIG", - "EMSDK_NODE", - "LLVM_ROOT", - "BINARYEN_ROOT", -) - - -def _compute_src_fingerprint() -> str: - """Fast fingerprint of source tree based on file mtimes. - - Uses os.scandir (3x faster than os.walk on Windows — DirEntry.stat() - reuses data from FindFirstFile, avoiding extra syscalls). - Caches the result within the same process invocation (cleared after 0.5s). - """ - global _cached_fingerprint, _cached_fingerprint_time - now = time.monotonic() - if _cached_fingerprint is not None and (now - _cached_fingerprint_time) < 0.5: - return _cached_fingerprint - - h = hashlib.md5(usedforsecurity=False) - src_dir = str(PROJECT_ROOT / "src") - _EXTS = (".cpp", ".h", ".hpp", ".c") - - # Recursive scandir — on Windows, DirEntry.stat() is free (no extra syscall) - def _scan(path: str) -> None: - try: - entries = sorted(os.scandir(path), key=lambda e: e.name) - except OSError: - return - subdirs: list[str] = [] - for entry in entries: - if entry.is_dir(follow_symlinks=False): - subdirs.append(entry.path) - elif entry.is_file(follow_symlinks=False) and entry.name.endswith(_EXTS): - try: - st = entry.stat() - h.update(f"{entry.path}:{st.st_mtime:.6f}".encode()) - except OSError: - h.update(f"{entry.path}:MISSING".encode()) - for d in subdirs: - _scan(d) - - _scan(src_dir) - - # Also hash build config files that affect the library - for config_file in [ - PROJECT_ROOT / "src" / "platforms" / "wasm" / "compiler" / "build_flags.toml", - PROJECT_ROOT / "meson.build", - PROJECT_ROOT / "ci" / "meson" / "wasm" / "meson.build", - PROJECT_ROOT / "ci" / "meson" / "wasm_cross_file.ini", - ]: - try: - h.update(f"{config_file}:{config_file.stat().st_mtime:.6f}".encode()) - except OSError: - h.update(f"{config_file}:MISSING".encode()) - - _cached_fingerprint = h.hexdigest() - _cached_fingerprint_time = now - return _cached_fingerprint - - -def _iter_js_affecting_files() -> list[Path]: - """Return files that can change Emscripten-generated JS glue.""" - files: list[Path] = [] - src_dir = PROJECT_ROOT / "src" - macro_markers = ("EM_JS(", "EM_ASYNC_JS(", "EM_ASM(", "EMSCRIPTEN_BINDINGS(") - - compiler_js = src_dir / "platforms" / "wasm" / "compiler" / "js_library.js" - if compiler_js.exists(): - files.append(compiler_js) - - build_flags = src_dir / "platforms" / "wasm" / "compiler" / "build_flags.toml" - if build_flags.exists(): - files.append(build_flags) - - for path in src_dir.rglob("*"): - if not path.is_file(): - continue - if path.suffix not in (".cpp", ".hpp", ".h", ".ipp"): - continue - if path.name.endswith(".js.cpp.hpp"): - files.append(path) - continue - try: - text = path.read_text(encoding="utf-8", errors="ignore") - except OSError: - continue - if any(marker in text for marker in macro_markers): - files.append(path) - - return sorted(set(files)) - - -def _compute_js_glue_fingerprint() -> str: - """Fingerprint only sources that can affect generated JS glue.""" - global _cached_js_fingerprint, _cached_js_fingerprint_time - now = time.monotonic() - if _cached_js_fingerprint is not None and (now - _cached_js_fingerprint_time) < 0.5: - return _cached_js_fingerprint - - digest = hashlib.md5(usedforsecurity=False) - for path in _iter_js_affecting_files(): - try: - st = path.stat() - digest.update(f"{path}:{st.st_mtime:.6f}:{st.st_size}".encode()) - except OSError: - digest.update(f"{path}:MISSING".encode()) - - _cached_js_fingerprint = digest.hexdigest() - _cached_js_fingerprint_time = now - return _cached_js_fingerprint - - -def _clear_link_cache(build_dir: Path) -> None: - for name in ( - "wasm_ld_args.json", - "wasm_ld_args.key", - "js_glue_fingerprint", - "link_environment_fingerprint", - ): - (build_dir / name).unlink(missing_ok=True) - - -def _js_glue_fingerprint_matches(build_dir: Path) -> bool: - fingerprint_file = build_dir / "js_glue_fingerprint" - if not fingerprint_file.exists(): - return False - try: - stored = fingerprint_file.read_text(encoding="utf-8").strip() - except OSError: - return False - return stored == _compute_js_glue_fingerprint() - - -def _get_emcc_version_signature() -> str: - """Return a short cached signature of the active emcc version.""" - global _cached_emcc_version_key, _cached_emcc_version_value, _cached_emcc_version_time - emcc = get_emcc() - now = time.monotonic() - if ( - _cached_emcc_version_key == emcc - and _cached_emcc_version_value is not None - and (now - _cached_emcc_version_time) < 0.5 - ): - return _cached_emcc_version_value - - try: - result = subprocess.run( - _tool_command(emcc) + ["--version"], - capture_output=True, - text=True, - env=_build_env(), - ) - version_text = ((result.stdout or "") + (result.stderr or "")).strip() - except OSError: - version_text = "ERROR" - - _cached_emcc_version_key = emcc - _cached_emcc_version_value = version_text - _cached_emcc_version_time = now - return version_text - - -def _compute_link_environment_fingerprint(mode: str) -> str: - """Fingerprint link conditions that affect emcc/JS output.""" - digest = hashlib.md5(usedforsecurity=False) - digest.update(f"mode:{mode}".encode()) - digest.update(json.dumps(get_link_flags(mode)).encode()) - digest.update(f"emcc_version:{_get_emcc_version_signature()}".encode()) - - for key in _LINK_ENV_VARS: - digest.update(f"{key}={os.environ.get(key, '')}".encode()) - - return digest.hexdigest() - - -def _link_environment_fingerprint_matches(build_dir: Path, mode: str) -> bool: - fingerprint_file = build_dir / "link_environment_fingerprint" - if not fingerprint_file.exists(): - return False - try: - stored = fingerprint_file.read_text(encoding="utf-8").strip() - except OSError: - return False - return stored == _compute_link_environment_fingerprint(mode) - - -def _compute_src_file_list_hash() -> str: - """Hash of sorted source file paths only (not content/mtimes). - - Detects file additions, removals, and renames — changes that require - meson reconfiguration to update the build graph. Content-only changes - are handled by Ninja's dependency tracking and don't need this. - """ - h = hashlib.md5(usedforsecurity=False) - src_dir = str(PROJECT_ROOT / "src") - _EXTS = (".cpp", ".h", ".hpp", ".c") - - def _scan(path: str) -> list[str]: - paths: list[str] = [] - try: - entries = os.scandir(path) - except OSError: - return paths - subdirs: list[str] = [] - for entry in entries: - if entry.is_dir(follow_symlinks=False): - subdirs.append(entry.path) - elif entry.is_file(follow_symlinks=False) and entry.name.endswith(_EXTS): - paths.append(entry.path) - for d in sorted(subdirs): - paths.extend(_scan(d)) - return paths - - for p in sorted(_scan(src_dir)): - h.update(p.encode()) - return h.hexdigest() - - -def _library_is_fresh(build_dir: Path) -> bool: - """Check if libfastled.a is up-to-date based on source fingerprint.""" - library_archive = build_dir / "ci" / "meson" / "wasm" / "libfastled.a" - fingerprint_file = build_dir / "library_src_fingerprint" - - if not library_archive.exists() or not fingerprint_file.exists(): - return False - - try: - stored = fingerprint_file.read_text(encoding="utf-8").strip() - current = _compute_src_fingerprint() - return stored == current - except OSError: - return False - - -def _save_library_fingerprint(build_dir: Path) -> None: - """Save source fingerprint after successful library build.""" - fingerprint_file = build_dir / "library_src_fingerprint" - try: - fingerprint_file.write_text(_compute_src_fingerprint(), encoding="utf-8") - except OSError: - pass - - -# Error patterns that indicate stale build state (kept in sync with compile.py) -_STALE_BUILD_PATTERNS = [ - "missing and no known rule to make it", - "no such file or directory", - "file not found", - "does not exist", - "has been modified since the precompiled header", - "function signature mismatch", - "undefined symbol", -] - - -def _is_stale_build_error(output: str) -> bool: - """Check if build output indicates a stale build state error.""" - output_lower = output.lower() - return any(pattern in output_lower for pattern in _STALE_BUILD_PATTERNS) - - -def _recover_stale_wasm_build(build_dir: Path) -> bool: - """Recover from stale WASM build state by cleaning deps and reconfiguring. - - Returns True if recovery succeeded and caller should retry the build. - """ - try: - # Step 1: Clean stale ninja outputs - ninja_exe = shutil.which("ninja") or str( - Path(sys.prefix) / "Scripts" / "ninja.EXE" - ) - try: - subprocess.run( - [ninja_exe, "-C", str(build_dir), "-t", "cleandead"], - capture_output=True, - text=True, - timeout=60, - ) - print("[WASM] Cleaned stale Ninja outputs") - except KeyboardInterrupt as ki: - handle_keyboard_interrupt(ki) - except Exception as e: - print(f"[WASM] Warning: ninja cleandead failed: {e}") - - # Step 2: Delete .ninja_deps to force full dependency re-scan - ninja_deps = build_dir / ".ninja_deps" - if ninja_deps.exists(): - try: - ninja_deps.unlink() - print("[WASM] Deleted stale .ninja_deps") - except OSError as e: - print(f"[WASM] Warning: Could not delete .ninja_deps: {e}") - - # Step 3: Clear metadata caches - for name in [ - "src_metadata.cache", - ".source_files_hash", - ".src_file_list_hash", - "library_src_fingerprint", - ]: - cache_file = build_dir / name - if cache_file.exists(): - try: - cache_file.unlink() - print(f"[WASM] Deleted cache: {name}") - except OSError: - pass - - # Step 4: Force Meson reconfiguration - print("[WASM] Forcing Meson reconfiguration...") - # Extract mode from build dir name (e.g., "meson-wasm-quick" -> "quick") - mode = build_dir.name.replace("meson-wasm-", "") - cmd = [ - get_meson_executable(), - "setup", - "--reconfigure", - "--cross-file", - str(CROSS_FILE), - str(build_dir), - f"-Dbuild_mode={mode}", - ] - result = subprocess.run(cmd, cwd=PROJECT_ROOT) - if result.returncode == 0: - print("[WASM] Self-healing reconfiguration complete") - return True - else: - print("[WASM] Self-healing reconfiguration failed") - return False - - except KeyboardInterrupt as ki: - handle_keyboard_interrupt(ki) - return False # unreachable on main thread, satisfies pyright - except Exception as e: - print(f"[WASM] Self-healing failed: {e}") - return False - - -# ============================================================================ -# Build steps -# ============================================================================ - - -def ensure_meson_configured(build_dir: Path, mode: str, force: bool = False) -> bool: - """ - Ensure meson is configured for WASM cross-compilation. - - Returns True if configuration succeeded. - """ - file_list_marker = build_dir / ".src_file_list_hash" - - if build_dir.exists() and (build_dir / "build.ninja").exists() and not force: - # Check if source file list changed since last configure - current_hash = _compute_src_file_list_hash() - stored_hash = "" - if file_list_marker.exists(): - try: - stored_hash = file_list_marker.read_text(encoding="utf-8").strip() - except OSError: - pass - if stored_hash == current_hash: - return True # No file list changes, skip reconfigure - - # Source file list changed — need reconfigure - print("[WASM] Source file list changed, forcing reconfiguration...") - for cache_name in ["src_metadata.cache", "library_src_fingerprint"]: - cache_file = build_dir / cache_name - if cache_file.exists(): - try: - cache_file.unlink() - except OSError: - pass - cmd = [ - get_meson_executable(), - "setup", - "--reconfigure", - "--cross-file", - str(CROSS_FILE), - str(build_dir), - f"-Dbuild_mode={mode}", - ] - print(f"[WASM] Reconfiguring meson (mode: {mode})...") - result = subprocess.run(cmd, cwd=PROJECT_ROOT) - if result.returncode != 0: - print(f"[WASM] Meson reconfiguration failed (rc {result.returncode})") - return False - try: - file_list_marker.write_text(current_hash, encoding="utf-8") - except OSError: - pass - print("[WASM] Meson reconfiguration successful") - return True - - build_dir.mkdir(parents=True, exist_ok=True) - - cmd = [ - get_meson_executable(), - "setup", - "--cross-file", - str(CROSS_FILE), - str(build_dir), - f"-Dbuild_mode={mode}", - ] - - if build_dir.exists() and (build_dir / "build.ninja").exists(): - # Reconfigure existing build - cmd.insert(2, "--reconfigure") - - print(f"[WASM] Configuring meson (mode: {mode})...") - result = subprocess.run(cmd, cwd=PROJECT_ROOT) - if result.returncode != 0: - print(f"[WASM] Meson setup failed with return code {result.returncode}") - return False - - # Write file list hash marker for future staleness detection - try: - file_list_marker.write_text(_compute_src_file_list_hash(), encoding="utf-8") - except OSError: - pass - print("[WASM] Meson setup successful") - return True - - -def build_library(build_dir: Path, verbose: bool = False) -> tuple[bool, bool]: - """ - Build libfastled.a using ninja. - - Skips the build entirely if the source fingerprint hasn't changed. - Returns (success, was_rebuilt) — was_rebuilt is True if sources changed. - """ - # Fast path: skip meson/ninja if source tree hasn't changed - if _library_is_fresh(build_dir): - if verbose: - print("[WASM] Library up-to-date (fingerprint match), skipping meson") - else: - print("[WASM] Library up-to-date") - return True, False - - # Delete stale thin archive before rebuild. When sources change, the - # existing thin archive may reference .o files from renamed/deleted - # sources. The 'r' flag in emar reads existing members first and fails - # on missing paths. Deleting forces a fresh archive. - library_archive = build_dir / "ci" / "meson" / "wasm" / "libfastled.a" - if library_archive.exists(): - try: - library_archive.unlink() - print("[WASM] Deleted stale archive (source fingerprint changed)") - except OSError as e: - print(f"[WASM] Warning: Could not delete stale archive: {e}") - - cmd = [get_meson_executable(), "compile", "-C", str(build_dir), "fastled"] - if verbose: - cmd.append("-v") - - print("[WASM] Building libfastled.a...") - result = subprocess.run(cmd, cwd=PROJECT_ROOT, capture_output=True, text=True) - if result.returncode != 0: - # Print captured output so the user sees what happened - if result.stdout: - print(result.stdout, end="") - if result.stderr: - print(result.stderr, end="", file=sys.stderr) - - # Self-healing: detect stale build state (missing/renamed/deleted files) - # and auto-reconfigure meson before retrying. - combined_output = (result.stdout or "") + (result.stderr or "") - if _is_stale_build_error(combined_output): - print("[WASM] Stale build state detected, auto-recovering...") - if _recover_stale_wasm_build(build_dir): - print("[WASM] Retrying build after recovery...") - retry = subprocess.run(cmd, cwd=PROJECT_ROOT) - if retry.returncode == 0: - _save_library_fingerprint(build_dir) - print( - "[WASM] Library build successful (after stale build recovery)" - ) - return True, True - - # Self-healing: if a thin archive exists after failure, it may have - # stale member references. Delete and retry once. - if library_archive.exists(): - try: - with open(library_archive, "rb") as f: - is_thin = f.read(8) == b"!\n" - except OSError: - is_thin = False - if is_thin: - print("[WASM] Build failed with thin archive present, retrying...") - try: - library_archive.unlink() - except OSError: - pass - retry = subprocess.run(cmd, cwd=PROJECT_ROOT) - if retry.returncode == 0: - _save_library_fingerprint(build_dir) - print("[WASM] Library build successful (after archive cleanup)") - return True, True - print(f"[WASM] Library build failed with return code {result.returncode}") - return False, False - - _save_library_fingerprint(build_dir) - print("[WASM] Library build successful") - return True, True - - -def create_wrapper(example_name: str, sketch_cache_dir: Path) -> Path: - """ - Create a wrapper .cpp that includes the sketch .ino file and any - additional .cpp files in the example directory. - - Returns path to the wrapper file. - """ - example_dir = PROJECT_ROOT / "examples" / example_name - ino_file = example_dir / f"{example_name}.ino" - - if not ino_file.exists(): - raise FileNotFoundError(f"Example not found: {ino_file}") - - # Discover additional .cpp files in the example directory tree (sorted for determinism) - extra_cpps = sorted( - f - for f in example_dir.rglob("*.cpp") - if f.name != f"{example_name}.ino" and ".build" not in f.parts - ) - - wrapper_path = sketch_cache_dir / f"{example_name}_wrapper.cpp" - lines = [ - f"// Auto-generated wrapper for {example_name}.ino", - "// C++20 header unit import — ~2x faster than PCH for sketch compilation.", - '// The .ino\'s #include "FastLED.h" is a harmless no-op after this import.', - 'import "wasm_pch.h";', - f'#include "{ino_file.as_posix()}"', - ] - for cpp in extra_cpps: - lines.append(f'#include "{cpp.as_posix()}"') - wrapper_content = "\n".join(lines) + "\n" - - # Only write if content changed (preserve timestamp for incremental builds) - if wrapper_path.exists(): - existing = wrapper_path.read_text() - if existing == wrapper_content: - return wrapper_path - - wrapper_path.write_text(wrapper_content) - return wrapper_path - - -def build_sketch_pch( - build_dir: Path, - mode: str, - lib_was_rebuilt: bool = False, - verbose: bool = False, -) -> Path | None: - """ - Build a C++20 header unit (.pcm) from wasm_pch.h for sketch compilation. - - Uses -fmodules-codegen to pre-compile inline function bodies into a companion - object file. This reduces sketch backend work by ~63% (509 → 187 codegen - functions for Blink) while keeping header unit import fast (~6ms vs 34ms PCH). - - The companion object is added to libfastled.a so the linker picks up symbols - naturally — no changes needed to link commands. - - Args: - lib_was_rebuilt: If True, library sources changed — invalidate header unit - since included headers may have changed. - - Returns path to the header unit BMI (.pcm), or None if build fails. - """ - sketch_pcm_path = build_dir / "wasm_pch.h.pcm" - sketch_pch_hash_path = build_dir / "sketch_pch.hash" - pch_codegen_o = build_dir / "pch_codegen.o" - - sketch_flags = get_sketch_compile_flags(mode) - # Combine flags + source fingerprint to detect both flag and header changes - src_fp = _compute_src_fingerprint() - combined = "\n".join(sketch_flags) + "\n" + src_fp - current_hash = hashlib.sha256(combined.encode()).hexdigest() - - # Check if header unit BMI is up-to-date - if ( - not lib_was_rebuilt - and sketch_pcm_path.exists() - and pch_codegen_o.exists() - and sketch_pch_hash_path.exists() - ): - stored_hash = sketch_pch_hash_path.read_text(encoding="utf-8").strip() - if stored_hash == current_hash: - if verbose: - print("[WASM] Sketch header unit is up-to-date") - return sketch_pcm_path - - includes = [ - f"-I{PROJECT_ROOT / 'src'}", - f"-I{PROJECT_ROOT / 'src' / 'platforms' / 'wasm' / 'compiler'}", - ] - - # Meson-injected defaults that must match the compilation environment - meson_defaults = [ - "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST", - "-D_FILE_OFFSET_BITS=64", - ] - - emcc_args = ( - ["-fmodule-header=user", str(WASM_PCH_HEADER), "-o", str(sketch_pcm_path)] - + meson_defaults - + sketch_flags - + includes - + ["-Xclang", "-fmodules-codegen"] # Pre-compile inline function bodies - ) - - if verbose: - print(f"[WASM] Header unit args: {emcc_args}") - - print("[WASM] Building sketch header unit (.pcm)...") - rc = run_emcc(emcc_args, cwd=str(PROJECT_ROOT)) - if rc != 0: - print("[WASM] Header unit build failed") - return None - - # Compile the .pcm to extract pre-compiled inline function bodies. - # This produces a companion .o that the linker uses instead of - # re-compiling inline functions in every sketch TU. - print("[WASM] Compiling header unit codegen companion...") - rc = run_emcc( - ["-c", str(sketch_pcm_path), "-o", str(pch_codegen_o), "-O0", "-g0"], - cwd=str(PROJECT_ROOT), - ) - if rc != 0: - print("[WASM] Codegen companion build failed, continuing without it") - pch_codegen_o.unlink(missing_ok=True) - else: - # Add codegen companion into libfastled.a so linker picks up symbols. - library_archive = build_dir / "ci" / "meson" / "wasm" / "libfastled.a" - if library_archive.exists(): - emar = get_emar() - subprocess.run( - _tool_command(emar) + ["r", str(library_archive), str(pch_codegen_o)], - cwd=str(PROJECT_ROOT), - env=_build_env(), - check=False, - ) - - sketch_pch_hash_path.write_text(current_hash, encoding="utf-8") - print("[WASM] Sketch header unit built successfully") - return sketch_pcm_path - - -def _parse_clang_from_verbose(stderr_text: str) -> list[str] | None: - """Parse the clang command from emcc verbose stderr output. - - emcc with EMCC_VERBOSE=1 prints subprocess commands to stderr. - We look for the clang invocation that has -c (compile mode). - """ - import shlex - import sys - - # On Windows, use posix=False so backslash paths aren't treated as escape chars. - # posix=False preserves literal quotes in tokens — strip them afterwards. - posix = sys.platform != "win32" - - for line in stderr_text.splitlines(): - stripped = line.strip() - if not stripped: - continue - parts = stripped.split() - if parts and "clang" in parts[0].lower() and "-c" in parts: - try: - tokens = shlex.split(stripped, posix=posix) - if not posix: - # posix=False keeps outer quotes on tokens — strip them - tokens = [t.strip("'\"") for t in tokens] - return tokens - except ValueError: - continue - return None - - -def _compile_cache_key(emcc_args: list[str]) -> str: - """Hash the emcc args to detect when flags change and cache needs refresh.""" - return hashlib.sha256(json.dumps(emcc_args).encode()).hexdigest()[:16] - - -def _fast_compile( - wrapper_path: Path, - object_path: Path, - build_dir: Path, - emcc_args: list[str], - verbose: bool = False, -) -> bool: - """Fast compile using cached clang args, bypassing emcc Python overhead. - - On first compile, emcc is run with EMCC_VERBOSE=1 to capture the clang - command it invokes internally. The command is saved as a template with - {input_cpp} and {output_o} placeholders. Subsequent compiles call clang - directly (~0.45s vs ~0.9s with emcc). - - Returns True on success, False to fall back to full emcc compile. - """ - cache_file = build_dir / "clang_compile_args.json" - cache_key_file = build_dir / "clang_compile_args.key" - if not cache_file.exists(): - return False - - # Invalidate cache if emcc args changed (new defines, flags, etc.) - current_key = _compile_cache_key(emcc_args) - if cache_key_file.exists(): - stored_key = cache_key_file.read_text(encoding="utf-8").strip() - if stored_key != current_key: - cache_file.unlink(missing_ok=True) - cache_key_file.unlink(missing_ok=True) - return False - - try: - template_args: list[str] = json.loads(cache_file.read_text(encoding="utf-8")) - except (json.JSONDecodeError, OSError): - return False - - cmd: list[str] = [] - for arg in template_args: - arg = arg.replace("{input_cpp}", str(wrapper_path)) - arg = arg.replace("{output_o}", str(object_path)) - cmd.append(arg) - - if verbose: - print(f"[WASM] Fast compile cmd: {cmd[:3]}...({len(cmd)} args)") - - try: - result = subprocess.run(cmd, cwd=str(PROJECT_ROOT)) - except OSError: - # Executable not found (e.g. stale cache with bad path) — fall back - cache_file.unlink(missing_ok=True) - cache_key_file.unlink(missing_ok=True) - return False - if result.returncode != 0: - # Cache might be stale — delete it so next run recaptures - cache_file.unlink(missing_ok=True) - cache_key_file.unlink(missing_ok=True) - return False - return True - - -def _intercept_emcc_compile( - emcc_args: list[str], - wrapper_path: Path, - object_path: Path, - build_dir: Path, -) -> int: - """Run emcc compile with verbose output to capture clang command. - - Runs emcc normally but with EMCC_VERBOSE=1 and EM_FORCE_RESPONSE_FILES=0 - so the full clang subprocess command is printed to stderr. We parse it - and save a template for future fast compiles via _fast_compile(). - - Returns the emcc exit code. - """ - emcc = get_emcc() - - env = os.environ.copy() - env["EMCC_VERBOSE"] = "1" - env["EM_FORCE_RESPONSE_FILES"] = "0" - - result = subprocess.run( - _tool_command(emcc) + emcc_args, - cwd=str(PROJECT_ROOT), - stderr=subprocess.PIPE, - text=True, - env=env, - ) - - if result.returncode != 0: - if result.stderr: - print(result.stderr, file=sys.stderr, end="") - return result.returncode - - # Parse and cache the clang command for future fast compiles - clang_cmd = _parse_clang_from_verbose(result.stderr) - if clang_cmd is None: - return 0 # compile succeeded, just can't cache - - wrapper_str = str(wrapper_path) - object_str = str(object_path) - template: list[str] = [] - for arg in clang_cmd: - arg = arg.replace(wrapper_str, "{input_cpp}") - arg = arg.replace(object_str, "{output_o}") - template.append(arg) - - cache_file = build_dir / "clang_compile_args.json" - cache_file.write_text(json.dumps(template), encoding="utf-8") - - # Save the key so _fast_compile can detect flag changes - cache_key_file = build_dir / "clang_compile_args.key" - cache_key_file.write_text(_compile_cache_key(emcc_args), encoding="utf-8") - - return 0 - - -def compile_sketch( - wrapper_path: Path, - build_dir: Path, - sketch_cache_dir: Path, - mode: str, - verbose: bool = False, -) -> Path | None: - """ - Compile the sketch wrapper to an object file. - - Two-tier compilation strategy: - 1. Fast path: run clang directly (~0.45s), bypassing emcc Python overhead - 2. Full path: run emcc with verbose capture (~0.9s), saves clang cmd for next time - - Sketch artifacts are cached per-sketch in sketch_cache_dir. - Returns path to the object file, or None on failure. - """ - object_path = sketch_cache_dir / "sketch.o" - - # Check if recompilation needed (mtime check against wrapper, .ino, extra .cpp, and library) - library_archive = build_dir / "ci" / "meson" / "wasm" / "libfastled.a" - # Find the .ino file that the wrapper #includes - example_name = wrapper_path.stem.replace("_wrapper", "") - example_dir = PROJECT_ROOT / "examples" / example_name - ino_file = example_dir / f"{example_name}.ino" - if object_path.exists(): - object_mtime = object_path.stat().st_mtime - wrapper_mtime = wrapper_path.stat().st_mtime - ino_mtime = ino_file.stat().st_mtime if ino_file.exists() else 0 - lib_mtime = library_archive.stat().st_mtime if library_archive.exists() else 0 - # Check additional .cpp/.h files in the example directory tree - extra_mtime = 0.0 - for ext in ("*.cpp", "*.h"): - for f in example_dir.rglob(ext): - if ".build" not in f.parts: - extra_mtime = max(extra_mtime, f.stat().st_mtime) - if ( - wrapper_mtime <= object_mtime - and ino_mtime <= object_mtime - and lib_mtime <= object_mtime - and extra_mtime <= object_mtime - ): - print("[WASM] Sketch is up-to-date") - return object_path - - sketch_flags = get_sketch_compile_flags(mode) - - includes = [ - f"-I{PROJECT_ROOT / 'src'}", - f"-I{PROJECT_ROOT / 'src' / 'platforms' / 'wasm' / 'compiler'}", - f"-I{example_dir}", - ] - - # Header unit usage: use the .pcm BMI built by build_sketch_pch() - sketch_pcm_path = build_dir / "wasm_pch.h.pcm" - pch_args = [] - if sketch_pcm_path.exists(): - pch_args = [ - f"-fmodule-file={sketch_pcm_path}", - ] - - emcc_args = ( - ["-c", str(wrapper_path), "-o", str(object_path)] - + sketch_flags - + includes - + pch_args - ) - - print(f"[WASM] Compiling sketch: {wrapper_path.name}") - - # Native path: ctc-emcc handles command capture + caching internally. - # Only one command capture ever happens (inside ctc-emcc's auto-cache). - native_emcc = get_native_emcc() - if native_emcc is not None: - if verbose: - print(f"[WASM] Using native emcc: {native_emcc}") - result = subprocess.run( - [native_emcc] + emcc_args, - cwd=str(PROJECT_ROOT), - env=_build_env(), - ) - if result.returncode == 0: - return object_path - print(f"[WASM] Sketch compilation failed with return code {result.returncode}") - return None - - # Python fallback (no native tools): two-tier intercept - if _fast_compile(wrapper_path, object_path, build_dir, emcc_args, verbose): - return object_path - - if verbose: - print(f"[WASM] Compile args: {emcc_args}") - - rc = _intercept_emcc_compile(emcc_args, wrapper_path, object_path, build_dir) - if rc != 0: - print(f"[WASM] Sketch compilation failed with return code {rc}") - return None - - return object_path - - -def _parse_wasm_ld_from_verbose(stderr_text: str) -> list[str] | None: - """Parse the wasm-ld command from emcc verbose stderr output. - - emcc with EMCC_VERBOSE=1 prints subprocess commands as: - /path/to/wasm-ld.exe arg1 arg2 ... - """ - import shlex - import sys - - # On Windows, use posix=False so backslash paths aren't treated as escape chars. - # posix=False preserves literal quotes in tokens — strip them afterwards. - posix = sys.platform != "win32" - - for line in stderr_text.splitlines(): - stripped = line.strip() - if not stripped: - continue - # emcc verbose output prefixes commands with a space - if "wasm-ld" in stripped.split()[0] if stripped.split() else False: - try: - tokens = shlex.split(stripped, posix=posix) - if not posix: - # posix=False keeps outer quotes on tokens — strip them - tokens = [t.strip("'\"") for t in tokens] - return tokens - except ValueError: - continue - return None - - -def _link_cache_key(library_archive: Path) -> str: - """Hash library archive mtime+size to detect when the engine changed.""" - st = library_archive.stat() - content = f"{st.st_mtime}:{st.st_size}".encode() - return hashlib.sha256(content).hexdigest()[:16] - - -def _intercept_emcc_link( - emcc_args: list[str], - sketch_object: Path, - cached_wasm: Path, - build_dir: Path, - cwd: str, - mode: str, - library_archive: Path | None = None, -) -> int: - """Run emcc link as subprocess with verbose output to capture wasm-ld command. - - Uses EMCC_VERBOSE=1 to make emcc print the wasm-ld command to stderr, - then saves it (with placeholders) for future fast re-linking via _fast_link(). - - This is a one-time operation per build directory — subsequent links use - _fast_link() which runs wasm-ld directly (~0.2s vs ~1.1s). - """ - emcc = get_emcc() - - # Set up env to capture wasm-ld command and preserve temp files - emcc_tmp = build_dir / "emcc_tmp" - emcc_tmp.mkdir(parents=True, exist_ok=True) - env = os.environ.copy() - env["EMCC_VERBOSE"] = "1" - env["EMCC_DEBUG"] = "1" # preserves temp files (js_symbols stub) - env["EMCC_TEMP_DIR"] = str(emcc_tmp) - env["EM_FORCE_RESPONSE_FILES"] = "0" # ensure full command, no @file - - result = subprocess.run( - _tool_command(emcc) + emcc_args, - cwd=cwd, - stderr=subprocess.PIPE, - text=True, - env=env, - ) - - if result.returncode != 0: - # Print captured stderr so user sees the error - if result.stderr: - print(result.stderr, file=sys.stderr, end="") - return result.returncode - - # Parse wasm-ld command from verbose output - wasm_ld_cmd = _parse_wasm_ld_from_verbose(result.stderr) - if wasm_ld_cmd is None: - print("[WASM] Warning: could not capture wasm-ld command from verbose output") - return 0 # link succeeded, just can't cache the fast path - - # Copy js_symbols stub from temp dir to build_dir for persistence - emcc_temp_dir = emcc_tmp / "emscripten_temp" - cached_stub = build_dir / "libemscripten_js_symbols.so" - for i, arg in enumerate(wasm_ld_cmd): - if "js_symbols" in arg: - stub_src = Path(arg) - if stub_src.exists(): - shutil.copy2(str(stub_src), str(cached_stub)) - wasm_ld_cmd[i] = str(cached_stub) - break - - # Save wasm-ld command template with placeholders - sketch_o_str = str(sketch_object) - wasm_str = str(cached_wasm) - template: list[str] = [] - for arg in wasm_ld_cmd: - arg = arg.replace(sketch_o_str, "{sketch_o}") - arg = arg.replace(wasm_str, "{output_wasm}") - template.append(arg) - cache_file = build_dir / "wasm_ld_args.json" - cache_file.write_text(json.dumps(template), encoding="utf-8") - - # Save cache key so _fast_link() can detect stale caches - if library_archive is not None and library_archive.exists(): - cache_key_file = build_dir / "wasm_ld_args.key" - cache_key_file.write_text(_link_cache_key(library_archive), encoding="utf-8") - (build_dir / "js_glue_fingerprint").write_text( - _compute_js_glue_fingerprint(), encoding="utf-8" - ) - (build_dir / "link_environment_fingerprint").write_text( - _compute_link_environment_fingerprint(mode), encoding="utf-8" - ) - - # Clean up emcc temp dir (we've saved what we need) - try: - shutil.rmtree(str(emcc_temp_dir), ignore_errors=True) - except OSError: - pass - - return 0 - - -def _fast_link( - sketch_object: Path, - cached_wasm: Path, - build_dir: Path, - mode: str, - library_archive: Path | None = None, - verbose: bool = False, -) -> bool: - """Fast link using cached wasm-ld args + cached JS glue. - - Bypasses emcc entirely (~0.2s vs ~1.1s). - Returns True on success, False to fall back to full emcc link. - """ - cached_js_glue = build_dir / "fastled_glue.js" - if not cached_js_glue.exists(): - return False - - cache_file = build_dir / "wasm_ld_args.json" - if not cache_file.exists(): - return False - - # Check that the js_symbols stub exists - cached_stub = build_dir / "libemscripten_js_symbols.so" - if not cached_stub.exists(): - return False - - if not _js_glue_fingerprint_matches(build_dir): - print("[WASM] JS glue inputs changed - invalidating linker cache") - _clear_link_cache(build_dir) - return False - - if not _link_environment_fingerprint_matches(build_dir, mode): - print("[WASM] Link environment changed - invalidating linker cache") - _clear_link_cache(build_dir) - return False - - # Invalidate cache if the library archive changed since last capture - cache_key_file = build_dir / "wasm_ld_args.key" - if library_archive is not None and library_archive.exists(): - current_key = _link_cache_key(library_archive) - if cache_key_file.exists(): - stored_key = cache_key_file.read_text(encoding="utf-8").strip() - if stored_key != current_key: - print("[WASM] Library changed — invalidating linker cache") - _clear_link_cache(build_dir) - return False - else: - # No key file — can't verify, invalidate to be safe - _clear_link_cache(build_dir) - return False - - try: - template_args: list[str] = json.loads(cache_file.read_text(encoding="utf-8")) - except (json.JSONDecodeError, OSError): - return False - - # Substitute placeholders and upgrade --strip-debug → --strip-all - # (safe because all needed symbols use explicit --export flags) - cmd: list[str] = [] - for arg in template_args: - arg = arg.replace("{sketch_o}", str(sketch_object)) - arg = arg.replace("{output_wasm}", str(cached_wasm)) - if arg == "--strip-debug": - arg = "--strip-all" - cmd.append(arg) - - if verbose: - print(f"[WASM] Fast link cmd: {cmd}") - - print("[WASM] Fast linking (wasm-ld only)...") - try: - result = subprocess.run(cmd, cwd=str(PROJECT_ROOT)) - except OSError: - # Executable not found (e.g. stale cache with bad path) — fall back - _clear_link_cache(build_dir) - return False - if result.returncode != 0: - print("[WASM] Fast link failed, falling back to full emcc link") - # Delete stale cache so next run recaptures - _clear_link_cache(build_dir) - return False - - # Copy cached JS glue to sketch cache dir (use copy, not copy2, - # so mtime is current — prevents stale mtime from triggering re-links) - js_dest = cached_wasm.parent / "fastled.js" - shutil.copy(str(cached_js_glue), str(js_dest)) - return True - - -def link_wasm( - sketch_object: Path, - build_dir: Path, - sketch_cache_dir: Path, - output_js: Path, - mode: str, - verbose: bool = False, -) -> bool: - """ - Link sketch.o + libfastled.a → fastled.js + fastled.wasm. - - Two-tier linking strategy: - 1. Fast path: reuse cached JS glue + run wasm-ld directly (~0.2s) - 2. Full path: run emcc to generate JS glue + wasm (~1.1s), then cache - - The JS glue is identical across all sketches (no EM_ASM/EM_JS), so after - one full link the JS is cached and subsequent links skip emcc entirely. - - Returns True on success. - """ - library_archive = build_dir / "ci" / "meson" / "wasm" / "libfastled.a" - - if not library_archive.exists(): - print(f"[WASM] Library not found: {library_archive}") - return False - - # Intermediate linked output in per-sketch cache - cached_js = sketch_cache_dir / "fastled.js" - cached_wasm = sketch_cache_dir / "fastled.wasm" - - # Check if linking needed (compare inputs vs cached output) - if cached_js.exists() and cached_wasm.exists(): - output_mtime = min(cached_js.stat().st_mtime, cached_wasm.stat().st_mtime) - inputs_newer = ( - sketch_object.stat().st_mtime > output_mtime - or library_archive.stat().st_mtime > output_mtime - ) - if not inputs_newer: - _copy_linked_output(sketch_cache_dir, output_js) - print("[WASM] Link output up-to-date (cached)") - return True - - # Fast path: reuse cached JS glue + run wasm-ld directly. - # Disabled when asyncify is active — asyncify is a Binaryen pass that - # only runs through emcc, not wasm-ld. - # Disabled when SEPARATE_DWARF_URL is used — emcc handles DWARF - # extraction as a post-link step that wasm-ld alone cannot perform. - link_flags = get_link_flags(mode) - uses_asyncify = any("ASYNCIFY" in f for f in link_flags) - needs_separate_dwarf = any("SEPARATE_DWARF" in f for f in link_flags) - if not uses_asyncify and not needs_separate_dwarf: - if _fast_link( - sketch_object, cached_wasm, build_dir, mode, library_archive, verbose - ): - _copy_linked_output(sketch_cache_dir, output_js) - print(f"[WASM] Output: {output_js}") - return True - - js_library = ( - PROJECT_ROOT / "src" / "platforms" / "wasm" / "compiler" / "js_library.js" - ) - - includes = [ - f"-I{PROJECT_ROOT / 'src'}", - f"-I{PROJECT_ROOT / 'src' / 'platforms' / 'wasm'}", - f"-I{PROJECT_ROOT / 'src' / 'platforms' / 'wasm' / 'compiler'}", - ] - - emcc_args = ( - [str(sketch_object), str(library_archive)] - + includes - + [f"--js-library={js_library}"] - + ["-o", str(cached_js)] - + link_flags - ) - - if verbose: - print(f"[WASM] Link args: {emcc_args}") - - print("[WASM] Linking final WASM module...") - rc = _intercept_emcc_link( - emcc_args, - sketch_object, - cached_wasm, - build_dir, - str(PROJECT_ROOT), - mode, - library_archive, - ) - if rc != 0: - print(f"[WASM] Linking failed with return code {rc}") - _clear_linked_output(sketch_cache_dir, output_js) - return False - - # Cache JS glue for fast re-linking (identical across all sketches) - cached_js_glue = build_dir / "fastled_glue.js" - shutil.copy2(str(cached_js), str(cached_js_glue)) - - _copy_linked_output(sketch_cache_dir, output_js) - print(f"[WASM] Output: {output_js}") - return True - - -def _copy_linked_output(sketch_cache_dir: Path, output_js: Path) -> None: - """Copy linked .js and .wasm from sketch cache to final output directory.""" - output_dir = output_js.parent - output_dir.mkdir(parents=True, exist_ok=True) - - cached_js = sketch_cache_dir / "fastled.js" - cached_wasm = sketch_cache_dir / "fastled.wasm" - - if cached_js.exists(): - shutil.copy2(str(cached_js), str(output_js)) - if cached_wasm.exists(): - shutil.copy2(str(cached_wasm), str(output_js.with_suffix(".wasm"))) - - # Copy DWARF debug info if present (produced by SEPARATE_DWARF_URL) - cached_dwarf = sketch_cache_dir / "fastled.wasm.dwarf" - if cached_dwarf.exists(): - shutil.copy2(str(cached_dwarf), str(output_dir / cached_dwarf.name)) - - # Copy any other generated files (worker JS, etc.) - for f in sketch_cache_dir.iterdir(): - if f.suffix in (".js", ".wasm") and f.name not in ( - "fastled.js", - "fastled.wasm", - ): - shutil.copy2(str(f), str(output_dir / f.name)) - - -def _clear_linked_output(sketch_cache_dir: Path, output_js: Path) -> None: - """Remove stale linked artifacts after a hard link failure.""" - output_dir = output_js.parent - stale_paths = [ - sketch_cache_dir / "fastled.js", - sketch_cache_dir / "fastled.wasm", - sketch_cache_dir / "fastled.wasm.dwarf", - output_js, - output_js.with_suffix(".wasm"), - output_dir / "fastled.wasm.dwarf", - ] - for path in stale_paths: - path.unlink(missing_ok=True) - - -def _copy_from_dist(dist_dir: Path, output_dir: Path) -> None: - """Copy Vite build output from dist/ to the output directory.""" - for item in dist_dir.iterdir(): - dst = output_dir / item.name - if item.is_dir(): - if dst.exists(): - shutil.rmtree(dst) - shutil.copytree(item, dst) - else: - shutil.copy2(item, dst) - - -def _run_npm(args: list[str], cwd: Path) -> int: - """Run npm from PATH.""" - npm = shutil.which("npm") - if npm is None: - return 1 - return subprocess.run([npm, *args], cwd=str(cwd)).returncode - - -def _run_npx(args: list[str], cwd: Path) -> int: - """Run npx from PATH.""" - npx = shutil.which("npx") - if npx is None: - return 1 - return subprocess.run([npx, *args], cwd=str(cwd)).returncode - - -def _get_vite_source_mtime(template_dir: Path) -> float: - """Get the most recent mtime of any Vite frontend source file. - - Uses os.scandir with directory-level pruning instead of rglob to avoid - walking node_modules (2626 files, 420ms → 4ms). On Windows, DirEntry.stat() - reuses FindFirstFile data (no extra syscall per file). - """ - max_mtime = 0.0 - _EXTS = frozenset((".ts", ".js", ".html", ".css", ".json")) - _SKIP = frozenset(("node_modules", "dist")) - stack = [str(template_dir)] - while stack: - current = stack.pop() - try: - with os.scandir(current) as it: - for entry in it: - try: - if entry.is_dir(follow_symlinks=False): - if entry.name not in _SKIP: - stack.append(entry.path) - elif entry.is_file(follow_symlinks=False): - _, ext = os.path.splitext(entry.name) - if ext in _EXTS: - mtime = entry.stat(follow_symlinks=False).st_mtime - if mtime > max_mtime: - max_mtime = mtime - except OSError: - pass - except OSError: - pass - return max_mtime - - -def copy_templates(output_dir: Path) -> None: - """Copy template files using fastled-wasm's bundled esbuild frontend pipeline.""" - from fastled.frontend_esbuild import copy_frontend_to_output - - copy_frontend_to_output(output_dir) - - -def generate_manifest(example_name: str, output_dir: Path) -> None: - """Generate files.json manifest for data files.""" - example_dir = PROJECT_ROOT / "examples" / example_name - data_extensions = frozenset( - (".json", ".csv", ".txt", ".cfg", ".bin", ".dat", ".mp3", ".wav") - ) - data_files: list[dict[str, str | int]] = [] - _SKIP = frozenset(("fastled_js", ".build")) - - # Use os.scandir with directory pruning — avoids walking fastled_js/ and .build/ - # On Windows, DirEntry.stat() reuses FindFirstFile data (no extra syscall) - example_str = str(example_dir) - example_len = len(example_str) - if not example_str.endswith(os.sep): - example_len += 1 - stack = [example_str] - while stack: - current = stack.pop() - try: - with os.scandir(current) as it: - for entry in it: - try: - if entry.is_dir(follow_symlinks=False): - if entry.name not in _SKIP: - stack.append(entry.path) - elif entry.is_file(follow_symlinks=False): - _, ext = os.path.splitext(entry.name) - if ext.lower() in data_extensions: - rel_path = entry.path[example_len:] - size = entry.stat(follow_symlinks=False).st_size - data_files.append({"path": rel_path, "size": size}) - except OSError: - pass - except OSError: - pass - - files_json = output_dir / "files.json" - with open(files_json, "w", encoding="utf-8") as f: - json.dump(data_files, f, indent=2) - - -def build( - example: str, - output: str, - mode: str = "quick", - verbose: bool = False, - force: bool = False, -) -> int: - """Core build function — callable directly without argparse overhead.""" - try: - start_time = time.time() - build_dir = get_build_dir(mode) - sketch_cache_dir = get_sketch_cache_dir(example) - output_js = Path(output) - # Auto-fix: if output looks like a directory (no .js suffix), append fastled.js - if output_js.suffix != ".js": - output_js = output_js / "fastled.js" - output_dir = output_js.parent - - print(f"[WASM] Building {example} (mode: {mode})") - - # Step 1: Configure meson - if not ensure_meson_configured(build_dir, mode, force): - return 1 - - # Step 2: Build library via ninja (skipped if source fingerprint matches) - lib_start = time.time() - lib_ok, lib_was_rebuilt = build_library(build_dir, verbose) - if not lib_ok: - return 1 - - # Step 2b: Build sketch-specific PCH (invalidated when library sources change) - sketch_pch = build_sketch_pch( - build_dir, mode, lib_was_rebuilt=lib_was_rebuilt, verbose=verbose - ) - if sketch_pch is None: - print("[WASM] WARNING: Sketch PCH build failed, continuing without PCH") - lib_time = time.time() - lib_start - - # Step 3: Create wrapper and compile sketch (per-sketch cache) - sketch_start = time.time() - wrapper = create_wrapper(example, sketch_cache_dir) - sketch_obj = compile_sketch(wrapper, build_dir, sketch_cache_dir, mode, verbose) - if sketch_obj is None: - return 1 - sketch_time = time.time() - sketch_start - - # Step 4: Link (per-sketch cache, copy to output) - link_start = time.time() - output_dir.mkdir(parents=True, exist_ok=True) - if not link_wasm( - sketch_obj, build_dir, sketch_cache_dir, output_js, mode, verbose - ): - # Self-healing: link failures from signature mismatches or - # undefined symbols usually mean stale object files. Nuke - # caches and retry the full build pipeline once. - print("[WASM] Link failed, attempting self-healing rebuild...") - if _recover_stale_wasm_build(build_dir): - # Also invalidate sketch cache so it gets recompiled - sketch_o = sketch_cache_dir / "sketch.o" - if sketch_o.exists(): - sketch_o.unlink(missing_ok=True) - # Retry: library → PCH → sketch → link - lib_ok2, _ = build_library(build_dir, verbose) - if lib_ok2: - sketch_pch2 = build_sketch_pch( - build_dir, mode, lib_was_rebuilt=True, verbose=verbose - ) - if sketch_pch2 is None: - print("[WASM] WARNING: PCH rebuild failed during recovery") - sketch_obj2 = compile_sketch( - wrapper, build_dir, sketch_cache_dir, mode, verbose - ) - if sketch_obj2 is not None and link_wasm( - sketch_obj2, - build_dir, - sketch_cache_dir, - output_js, - mode, - verbose, - ): - link_time = time.time() - link_start - print("[WASM] Self-healing link succeeded") - else: - print("[WASM] Self-healing link failed") - return 1 - else: - print("[WASM] Self-healing library rebuild failed") - return 1 - else: - return 1 - link_time = time.time() - link_start - - # Step 5: Copy templates and generate manifest - copy_templates(output_dir) - generate_manifest(example, output_dir) - - total_time = time.time() - start_time - print("\n[WASM] Build successful!") - print(f" Library: {lib_time:.2f}s") - print(f" Sketch: {sketch_time:.2f}s") - print(f" Linking: {link_time:.2f}s") - print(f" Total: {total_time:.2f}s") - return 0 - - except KeyboardInterrupt as ki: - print("\n[WASM] Build interrupted by user") - handle_keyboard_interrupt(ki) - return 1 # unreachable on main thread, satisfies pyright - except Exception as e: - print(f"[WASM] Build failed: {e}", file=sys.stderr) - import traceback - - traceback.print_exc() - return 1 - - -def main() -> int: - """CLI entry point — parses args and delegates to build().""" - parser = argparse.ArgumentParser(description="Build WASM example") - parser.add_argument("--example", required=True, help="Example name (e.g., Blink)") - parser.add_argument("-o", "--output", required=True, help="Output JS file path") - parser.add_argument( - "--mode", - default="quick", - choices=["quick", "debug", "release"], - help="Build mode", - ) - parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output") - parser.add_argument( - "--force", action="store_true", help="Force meson reconfiguration" - ) - args = parser.parse_args() - return build(args.example, args.output, args.mode, args.verbose, args.force) - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/src/fastled/toolchain/native_tools/launcher_emcc.cpp b/src/fastled/toolchain/native_tools/launcher_emcc.cpp deleted file mode 100644 index 47a5a4e9..00000000 --- a/src/fastled/toolchain/native_tools/launcher_emcc.cpp +++ /dev/null @@ -1,1214 +0,0 @@ -// clang-tool-chain native Emscripten launcher -// Replaces the Python emcc/em++ wrapper with near-zero startup overhead. -// -// Strategy (from FastLED's EMCC_FAST pattern): -// emcc is just a wrapper around clang (compile) and wasm-ld (link). -// -// Three execution tiers (fastest to slowest): -// -// 1. USER TEMPLATE (--compile-commands / --link-args): -// User provides a pre-built JSON/line-per-arg template containing the -// full clang or wasm-ld command with {input}/{output} placeholders. -// The launcher substitutes and execs the native binary directly. -// Zero Python/Node overhead. User takes responsibility for correctness. -// -// 2. AUTO-CACHE (compile -c only): -// On first compile: invoke emcc with EMCC_VERBOSE=1, parse the actual -// clang command from stderr, templatize it, cache per flag-hash. -// Subsequent compiles with same flags: call clang directly. -// -// 3. PYTHON FALLBACK: -// For link, preprocess, or any uncached invocation: exec python emcc.py. -// -// Capture modes (--capture-compile-commands / --capture-link-args): -// Run emcc with EMCC_VERBOSE=1, parse the native command from stderr, -// templatize it, and save to the specified file for later use with -// --compile-commands / --link-args. -// -// Build: clang++ -O3 -std=c++17 -o ctc-emcc launcher_emcc.cpp -// Linux: add -static-libstdc++ -static-libgcc -lpthread -// Windows: add -static-libstdc++ -static-libgcc - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#define NOMINMAX -#include -#include -#include -#include -#else -#include -#include -#include -#include -#endif - -// ============================================================================ -// Section 0: Constants -// ============================================================================ - -static constexpr const char* CTC_TAG = "[ctc-emcc] "; -static constexpr const char* PATHS_CACHE = ".ctc-emcc-paths"; -static constexpr const char* ARGS_CACHE_DIR = ".ctc-emcc-args"; - -#ifdef _WIN32 -static constexpr char PATH_LIST_SEP = ';'; -#else -static constexpr char PATH_LIST_SEP = ':'; -#endif - -// ============================================================================ -// Section 1: Platform Abstraction -// ============================================================================ - -enum class Platform { Windows, Linux, Darwin }; -enum class Arch { X86_64, ARM64 }; -enum class EmccMode { C, CXX }; - -static Platform get_platform() { -#ifdef _WIN32 - return Platform::Windows; -#elif defined(__APPLE__) - return Platform::Darwin; -#elif defined(__linux__) - return Platform::Linux; -#else -#error "Unsupported platform" -#endif -} - -static Arch get_arch() { -#if defined(__x86_64__) || defined(_M_X64) - return Arch::X86_64; -#elif defined(__aarch64__) || defined(_M_ARM64) - return Arch::ARM64; -#else -#error "Unsupported architecture" -#endif -} - -static const char* platform_str(Platform p) { - switch (p) { - case Platform::Windows: return "win"; - case Platform::Linux: return "linux"; - case Platform::Darwin: return "darwin"; - } - return "unknown"; -} - -static const char* arch_str(Arch a) { - switch (a) { - case Arch::X86_64: return "x86_64"; - case Arch::ARM64: return "arm64"; - } - return "unknown"; -} - -static char path_sep() { -#ifdef _WIN32 - return '\\'; -#else - return '/'; -#endif -} - -static std::string path_join(const std::string& a, const std::string& b) { - if (a.empty()) return b; - if (b.empty()) return a; - char last = a.back(); - if (last == '/' || last == '\\') return a + b; - return a + path_sep() + b; -} - -static bool path_exists(const std::string& path) { -#ifdef _WIN32 - DWORD attr = GetFileAttributesA(path.c_str()); - return attr != INVALID_FILE_ATTRIBUTES; -#else - struct stat st; - return stat(path.c_str(), &st) == 0; -#endif -} - -static bool is_directory(const std::string& path) { -#ifdef _WIN32 - DWORD attr = GetFileAttributesA(path.c_str()); - return attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY); -#else - struct stat st; - return stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode); -#endif -} - -static void make_directory(const std::string& path) { -#ifdef _WIN32 - CreateDirectoryA(path.c_str(), nullptr); -#else - mkdir(path.c_str(), 0755); -#endif -} - -static std::string get_home_dir() { -#ifdef _WIN32 - const char* profile = getenv("USERPROFILE"); - if (profile && profile[0]) return profile; - const char* homedrive = getenv("HOMEDRIVE"); - const char* homepath = getenv("HOMEPATH"); - if (homedrive && homepath) return std::string(homedrive) + homepath; - return "C:\\Users\\Default"; -#else - const char* home = getenv("HOME"); - if (home && home[0]) return home; - return "/tmp"; -#endif -} - -static std::string get_exe_basename(const std::string& path) { - size_t pos = path.find_last_of("/\\"); - std::string name = (pos == std::string::npos) ? path : path.substr(pos + 1); - if (name.size() > 4) { - std::string ext = name.substr(name.size() - 4); - for (auto& c : ext) c = (char)tolower((unsigned char)c); - if (ext == ".exe") name = name.substr(0, name.size() - 4); - } - return name; -} - -static std::string get_extension(const std::string& path) { - size_t dot = path.rfind('.'); - size_t sep = path.find_last_of("/\\"); - if (dot == std::string::npos || (sep != std::string::npos && dot < sep)) return ""; - std::string ext = path.substr(dot); - for (auto& c : ext) c = (char)tolower((unsigned char)c); - return ext; -} - -static std::string to_lower(const std::string& s) { - std::string r = s; - for (auto& c : r) c = (char)tolower((unsigned char)c); - return r; -} - -static bool starts_with(const std::string& s, const char* prefix) { - return s.compare(0, strlen(prefix), prefix) == 0; -} - -static std::string get_env(const char* name) { - const char* val = getenv(name); - return val ? val : ""; -} - -static bool env_is_truthy(const char* name) { - std::string val = get_env(name); - return val == "1" || val == "true" || val == "yes"; -} - -static void set_env(const char* name, const std::string& value) { -#ifdef _WIN32 - SetEnvironmentVariableA(name, value.c_str()); - _putenv_s(name, value.c_str()); -#else - setenv(name, value.c_str(), 1); -#endif -} - -static void unset_env(const char* name) { -#ifdef _WIN32 - SetEnvironmentVariableA(name, nullptr); - std::string s = std::string(name) + "="; - _putenv(s.c_str()); -#else - unsetenv(name); -#endif -} - -static std::string read_file(const std::string& path) { - std::ifstream f(path, std::ios::binary); - if (!f) return ""; - std::ostringstream ss; - ss << f.rdbuf(); - return ss.str(); -} - -static bool write_file_atomic(const std::string& path, const std::string& content) { - std::string tmp = path + ".tmp." + std::to_string( -#ifdef _WIN32 - (int)GetCurrentProcessId() -#else - (int)getpid() -#endif - ); - { - std::ofstream f(tmp, std::ios::binary); - if (!f) return false; - f.write(content.data(), (std::streamsize)content.size()); - if (!f) { std::remove(tmp.c_str()); return false; } - } -#ifdef _WIN32 - if (!MoveFileExA(tmp.c_str(), path.c_str(), MOVEFILE_REPLACE_EXISTING)) { - DeleteFileA(path.c_str()); - if (!MoveFileA(tmp.c_str(), path.c_str())) { - std::remove(tmp.c_str()); - return false; - } - } -#else - if (rename(tmp.c_str(), path.c_str()) != 0) { - std::remove(tmp.c_str()); - return false; - } -#endif - return true; -} - -static std::string get_temp_path() { -#ifdef _WIN32 - char buf[MAX_PATH]; - GetTempPathA(MAX_PATH, buf); - return std::string(buf) + "ctc_emcc_" + std::to_string(GetCurrentProcessId()) + ".tmp"; -#else - return "/tmp/ctc_emcc_" + std::to_string(getpid()) + ".tmp"; -#endif -} - -// ============================================================================ -// Section 2: Find executable in PATH -// ============================================================================ - -static std::string find_in_path(const char* name) { - std::string path_env = get_env("PATH"); - if (path_env.empty()) return ""; - std::istringstream ss(path_env); - std::string dir; - while (std::getline(ss, dir, PATH_LIST_SEP)) { - if (dir.empty()) continue; - std::string candidate = path_join(dir, name); - if (path_exists(candidate)) return candidate; - } - return ""; -} - -static std::string find_python() { -#ifdef _WIN32 - std::string p = find_in_path("python.exe"); - if (!p.empty()) return p; - p = find_in_path("python3.exe"); - if (!p.empty()) return p; - p = find_in_path("py.exe"); - if (!p.empty()) return p; -#else - std::string p = find_in_path("python3"); - if (!p.empty()) return p; - p = find_in_path("python"); - if (!p.empty()) return p; -#endif - return ""; -} - -// ============================================================================ -// Section 3: Shell Tokenizer + JSON Parser -// ============================================================================ - -static std::vector split_shell(const std::string& line) { - std::vector tokens; - std::string current; - bool in_single = false, in_double = false, escape = false; - for (size_t i = 0; i < line.size(); i++) { - char c = line[i]; - if (escape) { current += c; escape = false; continue; } -#ifdef _WIN32 - // On Windows, backslash is a path separator, not an escape character. - // Only treat \ as escape when followed by a quote or another backslash - // inside a double-quoted string. - if (c == '\\' && !in_single && in_double && i + 1 < line.size()) { - char next = line[i + 1]; - if (next == '"' || next == '\\') { escape = true; continue; } - } -#else - if (c == '\\' && !in_single) { escape = true; continue; } -#endif - if (c == '\'' && !in_double) { in_single = !in_single; continue; } - if (c == '"' && !in_single) { in_double = !in_double; continue; } - if ((c == ' ' || c == '\t') && !in_single && !in_double) { - if (!current.empty()) { tokens.push_back(current); current.clear(); } - continue; - } - current += c; - } - if (!current.empty()) tokens.push_back(current); - return tokens; -} - -// Minimal JSON string array parser: ["arg1", "arg2", ...] -// Handles escaped quotes and backslashes inside strings. -static std::vector parse_json_array(const std::string& content) { - std::vector result; - // Find opening bracket - size_t pos = content.find('['); - if (pos == std::string::npos) return result; - pos++; - - while (pos < content.size()) { - // Skip whitespace and commas - while (pos < content.size() && (content[pos] == ' ' || content[pos] == '\t' || - content[pos] == '\n' || content[pos] == '\r' || content[pos] == ',')) - pos++; - if (pos >= content.size() || content[pos] == ']') break; - - // Expect opening quote - if (content[pos] != '"') { pos++; continue; } - pos++; // skip opening quote - - std::string s; - while (pos < content.size() && content[pos] != '"') { - if (content[pos] == '\\' && pos + 1 < content.size()) { - char next = content[pos + 1]; - if (next == '"' || next == '\\' || next == '/') { s += next; pos += 2; } - else if (next == 'n') { s += '\n'; pos += 2; } - else if (next == 't') { s += '\t'; pos += 2; } - else { s += content[pos]; pos++; } - } else { - s += content[pos]; pos++; - } - } - if (pos < content.size()) pos++; // skip closing quote - result.push_back(s); - } - return result; -} - -// Read a template file. Auto-detects format: -// - If starts with '[': JSON array of strings -// - Otherwise: one arg per line -static std::vector read_template_file(const std::string& path) { - std::string content = read_file(path); - if (content.empty()) return {}; - - // Trim leading whitespace to detect format - size_t first = content.find_first_not_of(" \t\n\r"); - if (first != std::string::npos && content[first] == '[') { - return parse_json_array(content); - } - - // Line-per-arg format - std::vector args; - std::istringstream ss(content); - std::string line; - while (std::getline(ss, line)) { - if (!line.empty() && line.back() == '\r') line.pop_back(); - if (!line.empty()) args.push_back(line); - } - return args; -} - -// ============================================================================ -// Section 4: FNV-1a Hash -// ============================================================================ - -static std::string compute_hash(const std::vector& parts) { - uint64_t h = 0xcbf29ce484222325ULL; - for (const auto& s : parts) { - for (char c : s) { - h ^= (uint64_t)(unsigned char)c; - h *= 0x100000001b3ULL; - } - h ^= 0xff; - h *= 0x100000001b3ULL; - } - char buf[17]; - snprintf(buf, sizeof(buf), "%016llx", (unsigned long long)h); - return buf; -} - -// ============================================================================ -// Section 5: Paths Cache (one-shot Python discovery) -// ============================================================================ - -struct PathsCache { - std::string emscripten_dir; - std::string config_path; - std::string bin_dir; - std::string node_path; - std::string python_path; - std::string emcc_script; - std::string empp_script; - - bool is_valid() const { - return !emscripten_dir.empty() && !python_path.empty() && - !emcc_script.empty() && path_exists(emcc_script) && - path_exists(python_path); - } -}; - -static PathsCache parse_paths_cache(const std::string& content) { - PathsCache c; - std::istringstream stream(content); - std::string line; - while (std::getline(stream, line)) { - if (!line.empty() && line.back() == '\r') line.pop_back(); - size_t eq = line.find('='); - if (eq == std::string::npos) continue; - std::string key = line.substr(0, eq); - std::string val = line.substr(eq + 1); - if (key == "emscripten_dir") c.emscripten_dir = val; - else if (key == "config_path") c.config_path = val; - else if (key == "bin_dir") c.bin_dir = val; - else if (key == "node_path") c.node_path = val; - else if (key == "python_path") c.python_path = val; - else if (key == "emcc_script") c.emcc_script = val; - else if (key == "empp_script") c.empp_script = val; - } - return c; -} - -static std::string run_capture(const std::string& cmd) { - std::string result; -#ifdef _WIN32 - // _popen goes through cmd.exe /c which strips the first and last quotes - // if the command starts with a quote. Wrap in extra quotes so cmd.exe - // strips the outer pair and leaves the inner quotes intact. - std::string wrapped = "\"" + cmd + "\""; - FILE* pipe = _popen(wrapped.c_str(), "r"); -#else - FILE* pipe = popen(cmd.c_str(), "r"); -#endif - if (!pipe) return ""; - char buf[4096]; - while (fgets(buf, sizeof(buf), pipe)) result += buf; -#ifdef _WIN32 - int rc = _pclose(pipe); -#else - int rc = pclose(pipe); -#endif - if (rc != 0) return ""; - return result; -} - -// Discovery script: avoid \" inside -c "..." — cmd.exe mangles them. -// Use str() + concatenation instead of f-strings with inner quotes. -static const char* DISCOVERY_SCRIPT = - "import sys; " - "from pathlib import Path; " - "from clang_tool_chain.execution.emscripten import " - "find_emscripten_tool, ensure_nodejs_available, get_platform_info; " - "pn, ar = get_platform_info(); " - "d = Path.home() / '.clang-tool-chain' / 'emscripten' / pn / ar; " - "emcc = find_emscripten_tool('emcc'); " - "node = ensure_nodejs_available(); " - "print('emscripten_dir=' + str(d / 'emscripten')); " - "print('config_path=' + str(d / '.emscripten')); " - "print('bin_dir=' + str(d / 'bin')); " - "print('node_path=' + str(node)); " - "print('python_path=' + sys.executable); " - "print('emcc_script=' + str(emcc)); " - "empp = d / 'emscripten' / 'em++.py'; " - "empp = str(empp) if empp.exists() else str(d / 'emscripten' / 'em++'); " - "print('empp_script=' + empp)"; - -static PathsCache discover_paths(const std::string& cache_path) { - std::string python = find_python(); - if (python.empty()) { - fprintf(stderr, "%sPython not found. Install Python to use Emscripten.\n", CTC_TAG); - exit(1); - } - fprintf(stderr, "%sFirst run — discovering Emscripten paths (one-time)...\n", CTC_TAG); - std::string cmd = "\"" + python + "\" -c \"" + DISCOVERY_SCRIPT + "\""; - std::string output = run_capture(cmd); - if (output.empty()) { - fprintf(stderr, "%sDiscovery failed. Try: pip install clang-tool-chain && " - "clang-tool-chain install emscripten\n", CTC_TAG); - exit(1); - } - PathsCache c = parse_paths_cache(output); - if (c.is_valid()) { - write_file_atomic(cache_path, output); - fprintf(stderr, "%sPaths cached.\n", CTC_TAG); - } else { - fprintf(stderr, "%sDiscovery returned incomplete paths.\n%s\n", CTC_TAG, output.c_str()); - exit(1); - } - return c; -} - -// ============================================================================ -// Section 6: User Arg Parsing -// ============================================================================ - -static bool is_source_ext(const std::string& ext) { - return ext == ".c" || ext == ".cpp" || ext == ".cc" || ext == ".cxx" || - ext == ".c++" || ext == ".m" || ext == ".mm" || ext == ".s" || ext == ".S"; -} - -static bool is_input_ext(const std::string& ext) { - return is_source_ext(ext) || ext == ".o" || ext == ".obj" || ext == ".a" || - ext == ".so" || ext == ".wasm" || ext == ".bc" || ext == ".ll"; -} - -struct UserArgs { - bool is_compile = false; - bool dry_run = false; - std::string input_file; - std::string output_file; - - // User template fast-paths - std::string compile_commands; // --compile-commands= - std::string link_args; // --link-args= - - // Capture mode: run emcc, save the native command to file - std::string capture_compile_commands; // --capture-compile-commands= - std::string capture_link_args; // --capture-link-args= - - std::vector all; // all user args (minus launcher flags) - std::vector flags; // args minus file paths (for auto-cache key) -}; - -static UserArgs parse_user_args(int argc, char* argv[]) { - UserArgs u; - for (int i = 1; i < argc; i++) { - std::string arg = argv[i]; - - // --compile-commands= is 19 chars - if (starts_with(arg, "--compile-commands=")) { - u.compile_commands = arg.substr(19); - continue; - } - // --link-args= is 12 chars - if (starts_with(arg, "--link-args=")) { - u.link_args = arg.substr(12); - continue; - } - // --capture-compile-commands= is 27 chars - if (starts_with(arg, "--capture-compile-commands=")) { - u.capture_compile_commands = arg.substr(27); - continue; - } - // --capture-link-args= is 20 chars - if (starts_with(arg, "--capture-link-args=")) { - u.capture_link_args = arg.substr(20); - continue; - } - if (arg == "--dry-run") { - u.dry_run = true; - continue; - } - - u.all.push_back(arg); - - if (arg == "-c") { - u.is_compile = true; - u.flags.push_back(arg); - continue; - } - if (arg == "-o" && i + 1 < argc) { - u.output_file = argv[i + 1]; - u.flags.push_back("-o"); - u.all.push_back(argv[++i]); - continue; - } - std::string ext = get_extension(arg); - if (is_input_ext(ext) && u.input_file.empty()) { - u.input_file = arg; - continue; - } - u.flags.push_back(arg); - } - return u; -} - -// ============================================================================ -// Section 7: Template Substitution -// ============================================================================ - -static void str_replace_all(std::string& s, const std::string& from, const std::string& to) { - size_t pos = 0; - while ((pos = s.find(from, pos)) != std::string::npos) { - s.replace(pos, from.size(), to); - pos += to.size(); - } -} - -// Substitute {input} and {output} placeholders with actual file paths -static std::vector apply_substitutions( - const std::vector& tmpl, - const std::string& input_file, - const std::string& output_file) { - - std::vector result; - for (const auto& t : tmpl) { - std::string a = t; - if (!input_file.empty()) str_replace_all(a, "{input}", input_file); - if (!output_file.empty()) str_replace_all(a, "{output}", output_file); - result.push_back(a); - } - return result; -} - -// Replace actual file paths with {input}/{output} for caching -static std::vector templatize(const std::vector& args, - const std::string& input_file, - const std::string& output_file) { - std::vector tmpl; - for (const auto& a : args) { - std::string t = a; - if (!input_file.empty()) str_replace_all(t, input_file, "{input}"); - if (!output_file.empty()) str_replace_all(t, output_file, "{output}"); - tmpl.push_back(t); - } - return tmpl; -} - -// ============================================================================ -// Section 8: Auto-Cache (one-arg-per-line files) -// ============================================================================ - -static std::vector read_arg_template(const std::string& path) { - std::vector args; - std::string content = read_file(path); - if (content.empty()) return args; - std::istringstream ss(content); - std::string line; - while (std::getline(ss, line)) { - if (!line.empty() && line.back() == '\r') line.pop_back(); - if (!line.empty()) args.push_back(line); - } - return args; -} - -static void write_arg_template(const std::string& path, const std::vector& args) { - std::string content; - for (const auto& a : args) content += a + "\n"; - write_file_atomic(path, content); -} - -// ============================================================================ -// Section 9: EMCC_VERBOSE Capture and Parse -// ============================================================================ - -static int run_emcc_verbose(const PathsCache& paths, - const std::vector& user_args, - EmccMode mode, - std::string& stderr_out) { - std::string tmpfile = get_temp_path(); - - const std::string& script = (mode == EmccMode::CXX) ? paths.empp_script : paths.emcc_script; - std::string cmd = "\"" + paths.python_path + "\" \"" + script + "\""; - for (const auto& a : user_args) { - if (a.find(' ') != std::string::npos || a.find('\t') != std::string::npos) { - cmd += " \"" + a + "\""; - } else { - cmd += " " + a; - } - } - cmd += " 2>\"" + tmpfile + "\""; - -#ifdef _WIN32 - cmd = "\"" + cmd + "\""; // cmd.exe double-quote wrapping -#endif - - set_env("EMCC_VERBOSE", "1"); - set_env("EM_FORCE_RESPONSE_FILES", "0"); - set_env("EMSCRIPTEN", paths.emscripten_dir); - set_env("EMSCRIPTEN_ROOT", paths.emscripten_dir); - set_env("EM_CONFIG", paths.config_path); - - std::string node_bin; - { - size_t pos = paths.node_path.find_last_of("/\\"); - node_bin = (pos != std::string::npos) ? paths.node_path.substr(0, pos) : "."; - } - std::string old_path = get_env("PATH"); - set_env("PATH", paths.bin_dir + PATH_LIST_SEP + node_bin + PATH_LIST_SEP + old_path); - - int rc = system(cmd.c_str()); - - unset_env("EMCC_VERBOSE"); - unset_env("EM_FORCE_RESPONSE_FILES"); - set_env("PATH", old_path); - - stderr_out = read_file(tmpfile); - std::remove(tmpfile.c_str()); - -#ifndef _WIN32 - rc = WIFEXITED(rc) ? WEXITSTATUS(rc) : 1; -#endif - return rc; -} - -static std::vector parse_clang_command(const std::string& stderr_content) { - std::istringstream ss(stderr_content); - std::string line; - while (std::getline(ss, line)) { - if (!line.empty() && line.back() == '\r') line.pop_back(); - if (line.empty()) continue; - auto parts = split_shell(line); - if (parts.empty()) continue; - std::string exe = to_lower(parts[0]); - if (exe.find("clang") != std::string::npos) { - for (const auto& p : parts) { - if (p == "-c") return parts; - } - } - } - return {}; -} - -static std::vector parse_wasmld_command(const std::string& stderr_content) { - std::istringstream ss(stderr_content); - std::string line; - while (std::getline(ss, line)) { - if (!line.empty() && line.back() == '\r') line.pop_back(); - if (line.empty()) continue; - auto parts = split_shell(line); - if (parts.empty()) continue; - std::string exe = to_lower(parts[0]); - if (exe.find("wasm-ld") != std::string::npos) return parts; - } - return {}; -} - -// Write a JSON string array to a file -static bool write_json_array(const std::string& path, const std::vector& args) { - std::string content = "[\n"; - for (size_t i = 0; i < args.size(); i++) { - content += " \""; - for (char c : args[i]) { - if (c == '"') content += "\\\""; - else if (c == '\\') content += "\\\\"; - else content += c; - } - content += "\""; - if (i + 1 < args.size()) content += ","; - content += "\n"; - } - content += "]\n"; - return write_file_atomic(path, content); -} - -static void relay_stderr(const std::string& stderr_content) { - std::istringstream ss(stderr_content); - std::string line; - while (std::getline(ss, line)) { - if (!line.empty() && line.back() == '\r') line.pop_back(); - if (line.empty()) continue; - auto parts = split_shell(line); - if (parts.empty()) { fprintf(stderr, "%s\n", line.c_str()); continue; } - std::string exe = to_lower(parts[0]); - // Skip verbose command lines - if (exe.find("clang") != std::string::npos || exe.find("wasm-ld") != std::string::npos) - continue; - fprintf(stderr, "%s\n", line.c_str()); - } -} - -// ============================================================================ -// Section 10: Mode Detection -// ============================================================================ - -static EmccMode detect_mode(const char* argv0) { - std::string name = get_exe_basename(argv0); - for (auto& c : name) c = (char)tolower((unsigned char)c); - if (name.find("++") != std::string::npos) return EmccMode::CXX; - if (name.find("empp") != std::string::npos) return EmccMode::CXX; - return EmccMode::C; -} - -// ============================================================================ -// Section 11: Process Execution -// ============================================================================ - -static void print_command(const std::vector& cmd) { - for (size_t i = 0; i < cmd.size(); i++) { - if (i > 0) printf(" "); - bool needs_quote = cmd[i].find(' ') != std::string::npos || - cmd[i].find('\t') != std::string::npos; - if (needs_quote) printf("\""); - printf("%s", cmd[i].c_str()); - if (needs_quote) printf("\""); - } - printf("\n"); -} - -#ifdef _WIN32 -// Proper Windows command-line argument quoting (handles backslashes + quotes). -static std::string win_quote_arg(const std::string& arg) { - bool needs_quote = arg.empty() || arg.find(' ') != std::string::npos || - arg.find('\t') != std::string::npos || - arg.find('"') != std::string::npos; - if (!needs_quote) return arg; - std::string result = "\""; - size_t num_bs = 0; - for (size_t j = 0; j < arg.size(); j++) { - if (arg[j] == '\\') { - num_bs++; - } else if (arg[j] == '"') { - result.append(num_bs * 2 + 1, '\\'); - result += '"'; - num_bs = 0; - } else { - result.append(num_bs, '\\'); - result += arg[j]; - num_bs = 0; - } - } - result.append(num_bs * 2, '\\'); - result += '"'; - return result; -} - -// CreateProcess with proper stdout/stderr pipe inheritance. -static int create_process_and_wait(const std::vector& cmd) { - std::string cmdline; - for (size_t i = 0; i < cmd.size(); i++) { - if (i > 0) cmdline += ' '; - cmdline += win_quote_arg(cmd[i]); - } - STARTUPINFOA si = {}; - si.cb = sizeof(si); - si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); - si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); - si.hStdError = GetStdHandle(STD_ERROR_HANDLE); - si.dwFlags = STARTF_USESTDHANDLES; - PROCESS_INFORMATION pi = {}; - std::vector buf(cmdline.begin(), cmdline.end()); - buf.push_back('\0'); - if (!CreateProcessA(nullptr, buf.data(), nullptr, nullptr, TRUE, - 0, nullptr, nullptr, &si, &pi)) { - fprintf(stderr, "%sFailed to create process: %lu\n", CTC_TAG, GetLastError()); - return 1; - } - WaitForSingleObject(pi.hProcess, INFINITE); - DWORD exit_code = 1; - GetExitCodeProcess(pi.hProcess, &exit_code); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - return (int)exit_code; -} -#endif - -[[noreturn]] static void exec_process(const std::vector& cmd) { -#ifdef _WIN32 - // Use CreateProcess instead of _execv to properly inherit stdout/stderr - // pipes (e.g. when invoked by Meson or other build systems). - int rc = create_process_and_wait(cmd); - exit(rc); -#else - std::vector argv_ptrs; - for (const auto& s : cmd) argv_ptrs.push_back(s.c_str()); - argv_ptrs.push_back(nullptr); - execv(cmd[0].c_str(), const_cast(argv_ptrs.data())); - fprintf(stderr, "%sFailed to exec: %s\n", CTC_TAG, cmd[0].c_str()); - exit(127); -#endif -} - -// ============================================================================ -// Section 11b: Windows Path Normalization -// ============================================================================ -// On Windows, build systems (especially Meson) may pass include paths with -// inconsistent slash forms: backslash in cpp_args but forward-slash in -// custom_target commands. This causes clang's #pragma once to fail across -// PCH boundaries because it sees the same header resolved via different -// canonical paths (e.g. C:\foo\bar.h vs C:/foo/bar.h). -// -// Fix: normalize all path-carrying arguments to forward slashes before -// passing them to real clang. Clang on Windows handles forward slashes -// natively, so this is always safe. - -#ifdef _WIN32 -static std::string normalize_slashes(const std::string& s) { - std::string out = s; - for (auto& c : out) { - if (c == '\\') c = '/'; - } - return out; -} - -static void normalize_windows_paths(std::vector& cmd) { - for (size_t i = 0; i < cmd.size(); i++) { - auto& arg = cmd[i]; - - // -I or -isystem (concatenated, no space) - if (starts_with(arg, "-I") && arg.size() > 2 && arg[2] != '=') { - arg = "-I" + normalize_slashes(arg.substr(2)); - } else if (starts_with(arg, "-isystem") && arg.size() > 8) { - arg = "-isystem" + normalize_slashes(arg.substr(8)); - } - // --sysroot= - else if (starts_with(arg, "--sysroot=")) { - arg = "--sysroot=" + normalize_slashes(arg.substr(10)); - } - // Flags followed by a separate path argument: - // -I , -isystem , -include , - // -include-pch , -isysroot , - // -o , -MF , -MQ , -MT - else if ((arg == "-I" || arg == "-isystem" || arg == "-include" || - arg == "-include-pch" || arg == "-isysroot" || - arg == "-o" || arg == "-MF" || arg == "-MQ" || arg == "-MT") - && i + 1 < cmd.size()) { - cmd[i + 1] = normalize_slashes(cmd[i + 1]); - i++; // skip the path we just normalized - } - // Source files and other bare paths (heuristic: contains backslash - // and looks like a file path — has a dot-extension or starts with - // a drive letter). - else if (arg.find('\\') != std::string::npos && - !starts_with(arg, "-") && - (arg.find('.') != std::string::npos || - (arg.size() >= 2 && arg[1] == ':'))) { - arg = normalize_slashes(arg); - } - } -} -#endif - -// ============================================================================ -// Section 12: main() -// ============================================================================ - -int main(int argc, char* argv[]) { - bool debug = env_is_truthy("CTC_DEBUG"); - - EmccMode mode = detect_mode(argv[0]); - Platform platform = get_platform(); - Arch arch = get_arch(); - - if (debug) { - fprintf(stderr, "[ctc-emcc-debug] argv[0]=%s mode=%s platform=%s/%s\n", - argv[0], mode == EmccMode::CXX ? "em++" : "emcc", - platform_str(platform), arch_str(arch)); - } - - // --help: print launcher usage - for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { - const char* name = (mode == EmccMode::CXX) ? "ctc-em++" : "ctc-emcc"; - printf("Usage: %s [launcher-flags] [emcc-args...]\n\n", name); - printf("Native Emscripten launcher — bypasses Python/Node startup overhead.\n\n"); - printf("Launcher flags (consumed by the launcher, not passed to emcc):\n"); - printf(" --compile-commands= Use pre-cached clang compile template\n"); - printf(" --link-args= Use pre-cached wasm-ld link template\n"); - printf(" --capture-compile-commands= Run emcc, save clang template to file\n"); - printf(" --capture-link-args= Run emcc, save wasm-ld template to file\n"); - printf(" --dry-run Print the command that would be exec'd\n"); - printf(" --help, -h Show this help\n\n"); - printf("Execution tiers (fastest to slowest):\n"); - printf(" 1. User template --compile-commands / --link-args (zero Python)\n"); - printf(" 2. Auto-cache compile -c only, zero Python after first run\n"); - printf(" 3. Python fallback exec python emcc.py\n\n"); - printf("Template files: JSON array or one-arg-per-line with {input}/{output} placeholders.\n"); - printf("Environment: CTC_DEBUG=1 for debug output.\n"); - return 0; - } - } - - // Resolve install directory and cache paths - std::string home = get_home_dir(); - std::string install_dir = path_join(home, ".clang-tool-chain"); - install_dir = path_join(install_dir, "emscripten"); - install_dir = path_join(install_dir, platform_str(platform)); - install_dir = path_join(install_dir, arch_str(arch)); - std::string paths_cache_file = path_join(install_dir, PATHS_CACHE); - std::string args_cache_dir = path_join(install_dir, ARGS_CACHE_DIR); - - // Parse user args (strips launcher flags) - UserArgs user = parse_user_args(argc, argv); - - // --------------------------------------------------------------- - // TIER 1: USER TEMPLATE — fastest path, zero Python - // - // --compile-commands= (compile mode, -c flag present) - // --link-args= (link mode, no -c flag) - // - // The template file (JSON array or one-arg-per-line) contains the - // full clang or wasm-ld command with {input}/{output} placeholders. - // The launcher substitutes and execs the native binary directly. - // No Python, no Node. - // --------------------------------------------------------------- - - if (!user.compile_commands.empty()) { - auto tmpl = read_template_file(user.compile_commands); - if (tmpl.empty()) { - fprintf(stderr, "%sCould not read compile commands: %s\n", - CTC_TAG, user.compile_commands.c_str()); - return 1; - } - auto cmd = apply_substitutions(tmpl, user.input_file, user.output_file); -#ifdef _WIN32 - normalize_windows_paths(cmd); -#endif - if (debug) { - fprintf(stderr, "[ctc-emcc-debug] TEMPLATE compile: %s (%zu args)\n", - cmd[0].c_str(), cmd.size()); - } - if (user.dry_run) { print_command(cmd); return 0; } - exec_process(cmd); - } - - if (!user.link_args.empty()) { - auto tmpl = read_template_file(user.link_args); - if (tmpl.empty()) { - fprintf(stderr, "%sCould not read link args: %s\n", - CTC_TAG, user.link_args.c_str()); - return 1; - } - auto cmd = apply_substitutions(tmpl, user.input_file, user.output_file); -#ifdef _WIN32 - normalize_windows_paths(cmd); -#endif - if (debug) { - fprintf(stderr, "[ctc-emcc-debug] TEMPLATE link: %s (%zu args)\n", - cmd[0].c_str(), cmd.size()); - } - if (user.dry_run) { print_command(cmd); return 0; } - exec_process(cmd); - } - - // --------------------------------------------------------------- - // CAPTURE MODES — run emcc verbose, save template to file - // - // --capture-compile-commands= - // --capture-link-args= - // - // Runs emcc normally but with EMCC_VERBOSE=1, parses the native - // clang or wasm-ld command from stderr, templatizes it, and writes - // the template to the specified file. - // --------------------------------------------------------------- - - if (!user.capture_compile_commands.empty() || !user.capture_link_args.empty()) { - // Need paths for emcc invocation - PathsCache paths = parse_paths_cache(read_file(paths_cache_file)); - if (!paths.is_valid()) paths = discover_paths(paths_cache_file); - - std::string stderr_out; - int rc = run_emcc_verbose(paths, user.all, mode, stderr_out); - relay_stderr(stderr_out); - - if (!user.capture_compile_commands.empty()) { - auto clang_cmd = parse_clang_command(stderr_out); - if (clang_cmd.empty()) { - fprintf(stderr, "%sCould not find clang command in emcc output.\n", CTC_TAG); - fprintf(stderr, "%sMake sure you pass -c to trigger a compile.\n", CTC_TAG); - } else { - auto tmpl = templatize(clang_cmd, user.input_file, user.output_file); - if (write_json_array(user.capture_compile_commands, tmpl)) { - fprintf(stderr, "%sSaved compile template (%zu args) to: %s\n", - CTC_TAG, tmpl.size(), user.capture_compile_commands.c_str()); - } else { - fprintf(stderr, "%sFailed to write: %s\n", - CTC_TAG, user.capture_compile_commands.c_str()); - } - } - } - - if (!user.capture_link_args.empty()) { - auto ld_cmd = parse_wasmld_command(stderr_out); - if (ld_cmd.empty()) { - fprintf(stderr, "%sCould not find wasm-ld command in emcc output.\n", CTC_TAG); - fprintf(stderr, "%sMake sure you are linking (no -c flag).\n", CTC_TAG); - } else { - auto tmpl = templatize(ld_cmd, user.input_file, user.output_file); - if (write_json_array(user.capture_link_args, tmpl)) { - fprintf(stderr, "%sSaved link template (%zu args) to: %s\n", - CTC_TAG, tmpl.size(), user.capture_link_args.c_str()); - } else { - fprintf(stderr, "%sFailed to write: %s\n", - CTC_TAG, user.capture_link_args.c_str()); - } - } - } - - return rc; - } - - // --------------------------------------------------------------- - // TIER 2: AUTO-CACHE — compile mode only - // - // Cache key = hash of flag args (excludes file paths). - // On cache hit: call clang directly, zero Python overhead. - // --------------------------------------------------------------- - - if (user.is_compile && !user.input_file.empty()) { - std::string hash = compute_hash(user.flags); - std::string cached_file = path_join(args_cache_dir, hash + ".args"); - - if (path_exists(cached_file)) { - auto tmpl = read_arg_template(cached_file); - if (!tmpl.empty()) { - auto cmd = apply_substitutions(tmpl, user.input_file, user.output_file); -#ifdef _WIN32 - normalize_windows_paths(cmd); -#endif - if (debug) { - fprintf(stderr, "[ctc-emcc-debug] AUTO-CACHE HIT: %s\n", cached_file.c_str()); - } - if (user.dry_run) { print_command(cmd); return 0; } - exec_process(cmd); - } - } - - if (debug) { - fprintf(stderr, "[ctc-emcc-debug] AUTO-CACHE MISS: hash=%s\n", hash.c_str()); - } - } - - // --------------------------------------------------------------- - // TIER 3: PYTHON FALLBACK — discovery + emcc execution - // --------------------------------------------------------------- - - PathsCache paths = parse_paths_cache(read_file(paths_cache_file)); - if (!paths.is_valid()) { - paths = discover_paths(paths_cache_file); - } - - // For compile mode: run emcc with EMCC_VERBOSE=1, cache clang args for next time - if (user.is_compile && !user.input_file.empty()) { - std::string stderr_out; - int rc = run_emcc_verbose(paths, user.all, mode, stderr_out); - relay_stderr(stderr_out); - - auto clang_cmd = parse_clang_command(stderr_out); - if (!clang_cmd.empty()) { - auto tmpl = templatize(clang_cmd, user.input_file, user.output_file); - if (!is_directory(args_cache_dir)) make_directory(args_cache_dir); - std::string hash = compute_hash(user.flags); - write_arg_template(path_join(args_cache_dir, hash + ".args"), tmpl); - if (debug) { - fprintf(stderr, "[ctc-emcc-debug] Cached %zu clang args (hash=%s)\n", - tmpl.size(), hash.c_str()); - } - } - return rc; - } - - // Non-compile: exec python emcc.py (link, preprocess, etc.) - set_env("EMSCRIPTEN", paths.emscripten_dir); - set_env("EMSCRIPTEN_ROOT", paths.emscripten_dir); - set_env("EM_CONFIG", paths.config_path); - std::string node_bin; - { - size_t pos = paths.node_path.find_last_of("/\\"); - node_bin = (pos != std::string::npos) ? paths.node_path.substr(0, pos) : "."; - } - std::string old_path = get_env("PATH"); - set_env("PATH", paths.bin_dir + PATH_LIST_SEP + node_bin + PATH_LIST_SEP + old_path); - - const std::string& script = (mode == EmccMode::CXX) ? paths.empp_script : paths.emcc_script; - std::vector cmd; - cmd.push_back(paths.python_path); - cmd.push_back(script); - for (const auto& a : user.all) cmd.push_back(a); - - if (user.dry_run) { print_command(cmd); return 0; } - exec_process(cmd); -} diff --git a/src/fastled/toolchain/native_tools/launcher_wasmld.cpp b/src/fastled/toolchain/native_tools/launcher_wasmld.cpp deleted file mode 100644 index 5e9cb6cc..00000000 --- a/src/fastled/toolchain/native_tools/launcher_wasmld.cpp +++ /dev/null @@ -1,480 +0,0 @@ -// clang-tool-chain native wasm-ld launcher -// Replaces the Python wasm-ld wrapper with near-zero startup overhead. -// wasm-ld is a native binary — this launcher bypasses Python entirely. -// -// Strategy: On first run (cache miss), invoke Python ONCE to discover the -// wasm-ld binary path via the clang_tool_chain package (which handles -// downloading and installation). Cache the result. All subsequent runs -// read the cache and exec wasm-ld directly — zero Python/Node overhead. -// -// Single-file C++17, no external dependencies beyond OS APIs and standard library. -// -// Build: clang++ -O3 -std=c++17 -o ctc-wasm-ld launcher_wasmld.cpp -// Linux: add -static-libstdc++ -static-libgcc -lpthread -// Windows: add -static-libstdc++ -static-libgcc - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#define NOMINMAX -#include -#include -#include -#include -#else -#include -#include -#include -#include -#endif - -// ============================================================================ -// Section 0: Constants -// ============================================================================ - -static constexpr const char* CTC_TAG = "[ctc-wasm-ld] "; -static constexpr const char* CACHE_FILENAME = ".ctc-wasmld-cache"; - -#ifdef _WIN32 -static constexpr char PATH_LIST_SEP = ';'; -#else -static constexpr char PATH_LIST_SEP = ':'; -#endif - -// ============================================================================ -// Section 1: Platform Abstraction -// ============================================================================ - -enum class Platform { Windows, Linux, Darwin }; -enum class Arch { X86_64, ARM64 }; - -static Platform get_platform() { -#ifdef _WIN32 - return Platform::Windows; -#elif defined(__APPLE__) - return Platform::Darwin; -#elif defined(__linux__) - return Platform::Linux; -#else -#error "Unsupported platform" -#endif -} - -static Arch get_arch() { -#if defined(__x86_64__) || defined(_M_X64) - return Arch::X86_64; -#elif defined(__aarch64__) || defined(_M_ARM64) - return Arch::ARM64; -#else -#error "Unsupported architecture" -#endif -} - -static const char* platform_str(Platform p) { - switch (p) { - case Platform::Windows: return "win"; - case Platform::Linux: return "linux"; - case Platform::Darwin: return "darwin"; - } - return "unknown"; -} - -static const char* arch_str(Arch a) { - switch (a) { - case Arch::X86_64: return "x86_64"; - case Arch::ARM64: return "arm64"; - } - return "unknown"; -} - -static char path_sep() { -#ifdef _WIN32 - return '\\'; -#else - return '/'; -#endif -} - -static std::string path_join(const std::string& a, const std::string& b) { - if (a.empty()) return b; - if (b.empty()) return a; - char last = a.back(); - if (last == '/' || last == '\\') return a + b; - return a + path_sep() + b; -} - -static bool path_exists(const std::string& path) { -#ifdef _WIN32 - DWORD attr = GetFileAttributesA(path.c_str()); - return attr != INVALID_FILE_ATTRIBUTES; -#else - struct stat st; - return stat(path.c_str(), &st) == 0; -#endif -} - -static std::string get_home_dir() { -#ifdef _WIN32 - const char* profile = getenv("USERPROFILE"); - if (profile && profile[0]) return profile; - const char* homedrive = getenv("HOMEDRIVE"); - const char* homepath = getenv("HOMEPATH"); - if (homedrive && homepath) return std::string(homedrive) + homepath; - return "C:\\Users\\Default"; -#else - const char* home = getenv("HOME"); - if (home && home[0]) return home; - return "/tmp"; -#endif -} - -static std::string get_env(const char* name) { - const char* val = getenv(name); - return val ? val : ""; -} - -static bool env_is_truthy(const char* name) { - std::string val = get_env(name); - return val == "1" || val == "true" || val == "yes"; -} - -static std::string read_file(const std::string& path) { - std::ifstream f(path, std::ios::binary); - if (!f) return ""; - std::ostringstream ss; - ss << f.rdbuf(); - return ss.str(); -} - -static bool write_file_atomic(const std::string& path, const std::string& content) { - std::string tmp = path + ".tmp." + std::to_string( -#ifdef _WIN32 - (int)GetCurrentProcessId() -#else - (int)getpid() -#endif - ); - { - std::ofstream f(tmp, std::ios::binary); - if (!f) return false; - f.write(content.data(), (std::streamsize)content.size()); - if (!f) { std::remove(tmp.c_str()); return false; } - } -#ifdef _WIN32 - if (!MoveFileExA(tmp.c_str(), path.c_str(), MOVEFILE_REPLACE_EXISTING)) { - DeleteFileA(path.c_str()); - if (!MoveFileA(tmp.c_str(), path.c_str())) { - std::remove(tmp.c_str()); - return false; - } - } -#else - if (rename(tmp.c_str(), path.c_str()) != 0) { - std::remove(tmp.c_str()); - return false; - } -#endif - return true; -} - -// ============================================================================ -// Section 2: Find executable in PATH -// ============================================================================ - -static std::string find_in_path(const char* name) { - std::string path_env = get_env("PATH"); - if (path_env.empty()) return ""; - - std::istringstream ss(path_env); - std::string dir; - while (std::getline(ss, dir, PATH_LIST_SEP)) { - if (dir.empty()) continue; - std::string candidate = path_join(dir, name); - if (path_exists(candidate)) return candidate; - } - return ""; -} - -static std::string find_python() { -#ifdef _WIN32 - std::string p = find_in_path("python.exe"); - if (!p.empty()) return p; - p = find_in_path("python3.exe"); - if (!p.empty()) return p; - p = find_in_path("py.exe"); - if (!p.empty()) return p; -#else - std::string p = find_in_path("python3"); - if (!p.empty()) return p; - p = find_in_path("python"); - if (!p.empty()) return p; -#endif - return ""; -} - -// ============================================================================ -// Section 3: Cache (single key: wasm_ld_path) -// ============================================================================ - -struct WasmLdCache { - std::string wasm_ld_path; - - bool is_valid() const { - return !wasm_ld_path.empty() && path_exists(wasm_ld_path); - } -}; - -static WasmLdCache read_cache(const std::string& cache_path) { - WasmLdCache cache; - std::string content = read_file(cache_path); - if (content.empty()) return cache; - - std::istringstream stream(content); - std::string line; - while (std::getline(stream, line)) { - if (!line.empty() && line.back() == '\r') line.pop_back(); - size_t eq = line.find('='); - if (eq == std::string::npos) continue; - std::string key = line.substr(0, eq); - std::string val = line.substr(eq + 1); - if (key == "wasm_ld_path") cache.wasm_ld_path = val; - } - return cache; -} - -// ============================================================================ -// Section 4: One-Shot Python Discovery -// ============================================================================ - -static std::string run_capture(const std::string& cmd) { - std::string result; -#ifdef _WIN32 - // _popen goes through cmd.exe /c which strips the first and last quotes - // if the command starts with a quote. Wrap in extra quotes so cmd.exe - // strips the outer pair and leaves the inner quotes intact. - std::string wrapped = "\"" + cmd + "\""; - FILE* pipe = _popen(wrapped.c_str(), "r"); -#else - FILE* pipe = popen(cmd.c_str(), "r"); -#endif - if (!pipe) return ""; - char buf[4096]; - while (fgets(buf, sizeof(buf), pipe)) { - result += buf; - } -#ifdef _WIN32 - int rc = _pclose(pipe); -#else - int rc = pclose(pipe); -#endif - if (rc != 0) return ""; - return result; -} - -// Python one-liner: discovers wasm-ld path via clang_tool_chain. -// This triggers emscripten download/install if needed. -// Avoid f-strings — cmd.exe mangles \" inside -c "..." on Windows. -static const char* DISCOVERY_SCRIPT = - "from clang_tool_chain.execution.emscripten import find_emscripten_wasm_ld_binary; " - "print('wasm_ld_path=' + str(find_emscripten_wasm_ld_binary()))"; - -static WasmLdCache discover_via_python(const std::string& cache_path) { - std::string python = find_python(); - if (python.empty()) { - fprintf(stderr, "%sPython not found in PATH. Needed for one-time path discovery.\n", CTC_TAG); - exit(1); - } - - fprintf(stderr, "%sFirst run — discovering wasm-ld path via Python (one-time)...\n", CTC_TAG); - - std::string cmd = "\"" + python + "\" -c \"" + DISCOVERY_SCRIPT + "\""; - std::string output = run_capture(cmd); - - if (output.empty()) { - fprintf(stderr, "%sDiscovery failed. Is clang-tool-chain installed?\n", CTC_TAG); - fprintf(stderr, "%sTry: pip install clang-tool-chain && clang-tool-chain install emscripten\n", CTC_TAG); - exit(1); - } - - // Parse - WasmLdCache cache; - std::istringstream stream(output); - std::string line; - while (std::getline(stream, line)) { - if (!line.empty() && line.back() == '\r') line.pop_back(); - size_t eq = line.find('='); - if (eq == std::string::npos) continue; - std::string key = line.substr(0, eq); - std::string val = line.substr(eq + 1); - if (key == "wasm_ld_path") cache.wasm_ld_path = val; - } - - if (cache.is_valid()) { - write_file_atomic(cache_path, output); - fprintf(stderr, "%sPath cached. Subsequent runs will be instant.\n", CTC_TAG); - } else { - fprintf(stderr, "%sDiscovery returned invalid wasm-ld path. Output:\n%s\n", CTC_TAG, output.c_str()); - exit(1); - } - - return cache; -} - -// ============================================================================ -// Section 5: Process Execution -// ============================================================================ - -static void print_command(const std::vector& cmd) { - for (size_t i = 0; i < cmd.size(); i++) { - if (i > 0) printf(" "); - bool needs_quote = cmd[i].find(' ') != std::string::npos || - cmd[i].find('\t') != std::string::npos; - if (needs_quote) printf("\""); - printf("%s", cmd[i].c_str()); - if (needs_quote) printf("\""); - } - printf("\n"); -} - -#ifdef _WIN32 -static std::string win_quote_arg(const std::string& arg) { - bool needs_quote = arg.empty() || arg.find(' ') != std::string::npos || - arg.find('\t') != std::string::npos || - arg.find('"') != std::string::npos; - if (!needs_quote) return arg; - std::string result = "\""; - size_t num_bs = 0; - for (size_t j = 0; j < arg.size(); j++) { - if (arg[j] == '\\') { - num_bs++; - } else if (arg[j] == '"') { - result.append(num_bs * 2 + 1, '\\'); - result += '"'; - num_bs = 0; - } else { - result.append(num_bs, '\\'); - result += arg[j]; - num_bs = 0; - } - } - result.append(num_bs * 2, '\\'); - result += '"'; - return result; -} - -static int create_process_and_wait(const std::vector& cmd) { - std::string cmdline; - for (size_t i = 0; i < cmd.size(); i++) { - if (i > 0) cmdline += ' '; - cmdline += win_quote_arg(cmd[i]); - } - STARTUPINFOA si = {}; - si.cb = sizeof(si); - si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); - si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); - si.hStdError = GetStdHandle(STD_ERROR_HANDLE); - si.dwFlags = STARTF_USESTDHANDLES; - PROCESS_INFORMATION pi = {}; - std::vector buf(cmdline.begin(), cmdline.end()); - buf.push_back('\0'); - if (!CreateProcessA(nullptr, buf.data(), nullptr, nullptr, TRUE, - 0, nullptr, nullptr, &si, &pi)) { - fprintf(stderr, "%sFailed to create process: %lu\n", CTC_TAG, GetLastError()); - return 1; - } - WaitForSingleObject(pi.hProcess, INFINITE); - DWORD exit_code = 1; - GetExitCodeProcess(pi.hProcess, &exit_code); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - return (int)exit_code; -} -#endif - -[[noreturn]] static void exec_process(const std::vector& cmd) { -#ifdef _WIN32 - // Use CreateProcess instead of _execv to properly inherit stdout/stderr - // pipes (e.g. when invoked by Meson or other build systems). - int rc = create_process_and_wait(cmd); - exit(rc); -#else - std::vector argv_ptrs; - for (const auto& s : cmd) argv_ptrs.push_back(s.c_str()); - argv_ptrs.push_back(nullptr); - execv(cmd[0].c_str(), const_cast(argv_ptrs.data())); - fprintf(stderr, "%sFailed to exec: %s\n", CTC_TAG, cmd[0].c_str()); - exit(127); -#endif -} - -// ============================================================================ -// Section 6: main() -// ============================================================================ - -int main(int argc, char* argv[]) { - bool debug = env_is_truthy("CTC_DEBUG"); - - Platform platform = get_platform(); - Arch arch = get_arch(); - - if (debug) { - fprintf(stderr, "[ctc-wasm-ld-debug] platform=%s arch=%s\n", - platform_str(platform), arch_str(arch)); - } - - // --help - bool dry_run = false; - for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { - printf("Usage: ctc-wasm-ld [options] [wasm-ld-args...]\n\n"); - printf("Native wasm-ld launcher — bypasses Python/Node startup overhead.\n\n"); - printf("Launcher flags:\n"); - printf(" --dry-run Print the command that would be exec'd\n"); - printf(" --help, -h Show this help\n\n"); - printf("All other arguments are passed directly to wasm-ld.\n"); - printf("Environment: CTC_DEBUG=1 for debug output.\n"); - return 0; - } - if (strcmp(argv[i], "--dry-run") == 0) { - dry_run = true; - } - } - - // 1. Resolve cache path - std::string home = get_home_dir(); - std::string install_dir = path_join(home, ".clang-tool-chain"); - install_dir = path_join(install_dir, "emscripten"); - install_dir = path_join(install_dir, platform_str(platform)); - install_dir = path_join(install_dir, arch_str(arch)); - std::string cache_path = path_join(install_dir, CACHE_FILENAME); - - // 2. Read cache or discover via Python (one-time) - WasmLdCache cache = read_cache(cache_path); - if (!cache.is_valid()) { - cache = discover_via_python(cache_path); - } - - if (debug) { - fprintf(stderr, "[ctc-wasm-ld-debug] wasm_ld_path=%s\n", cache.wasm_ld_path.c_str()); - } - - // 3. Build command: wasm-ld [user args...] - std::vector cmd; - cmd.push_back(cache.wasm_ld_path); - for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "--dry-run") == 0) continue; // strip launcher flag - cmd.push_back(argv[i]); - } - - // 4. Exec (replaces this process — no Python, no Node, pure native) - if (dry_run) { print_command(cmd); return 0; } - exec_process(cmd); -} diff --git a/tests/unit/test_build_service.py b/tests/unit/test_build_service.py index 1c71ae56..40dbe8c6 100644 --- a/tests/unit/test_build_service.py +++ b/tests/unit/test_build_service.py @@ -1,41 +1,108 @@ from pathlib import Path -from fastled.build_service import BuildService from fastled.build_types import BuildRequest from fastled.types import BuildMode -class FakeToolchain: +class FakeNativeBuildService: + states: dict[Path, tuple[str, bool, str | None]] = {} + def __init__(self) -> None: - self.calls: list[tuple[Path, Path, BuildMode, bool]] = [] + self.registered: list[object] = [] + + def register_toolchain( + self, toolchain: object, fastled_path: str | None = None + ) -> None: + self.registered.append((toolchain, fastled_path)) - def compile( + def detect_strategy( self, - sketch_dir: Path, - output_dir: Path, - build_mode: BuildMode, - profile: bool, - ) -> Path: - self.calls.append((sketch_dir, output_dir, build_mode, profile)) + sketch_dir: str, + build_mode: str, + profile: bool = False, + fastled_path: str | None = None, + force_clean: bool = False, + ) -> str: + output_dir = Path(sketch_dir) / "fastled_js" + state = self.states.get(Path(sketch_dir).resolve()) + if ( + force_clean + or not (output_dir / "fastled.js").exists() + or not (output_dir / "fastled.wasm").exists() + or state != (build_mode, profile, fastled_path) + ): + return "cold" + return "incremental" + + def build( + self, + sketch_dir: str, + build_mode: str, + build_mode_obj: BuildMode, + profile: bool = False, + fastled_path: str | None = None, + force_clean: bool = False, + ) -> dict[str, object]: + del build_mode_obj + strategy = self.detect_strategy( + sketch_dir, build_mode, profile, fastled_path, force_clean + ) + output_dir = Path(sketch_dir) / "fastled_js" output_dir.mkdir(parents=True, exist_ok=True) (output_dir / "fastled.js").write_text("console.log('ok');") (output_dir / "fastled.wasm").write_bytes(b"\0asm") - if build_mode == BuildMode.DEBUG: + if build_mode == "DEBUG": (output_dir / "fastled.wasm.dwarf").write_text("debug") assets_dir = output_dir / "assets" assets_dir.mkdir(exist_ok=True) (assets_dir / "main.js").write_text("asset") - return output_dir / "fastled.js" + self.states[Path(sketch_dir).resolve()] = (build_mode, profile, fastled_path) + return { + "success": True, + "stdout": "Native Rust WASM build successful", + "hash_value": None, + "zip_bytes": b"zip", + "zip_time": 0.01, + "libfastled_time": 0.0, + "sketch_time": 0.02, + "response_processing_time": 0.0, + "strategy": strategy, + "output_dir": str(output_dir), + "artifacts": { + "js": str(output_dir / "fastled.js"), + "wasm": str(output_dir / "fastled.wasm"), + "frontend_assets": str(assets_dir), + **( + {"dwarf": str(output_dir / "fastled.wasm.dwarf")} + if build_mode == "DEBUG" + else {} + ), + }, + } + + def purge(self, sketch_dir: str) -> None: + self.states.pop(Path(sketch_dir).resolve(), None) + + +def _install_fake_native(monkeypatch) -> None: + FakeNativeBuildService.states = {} + monkeypatch.setattr( + "fastled.build_service._NativeBuildService", FakeNativeBuildService + ) -def test_build_service_starts_cold_then_turns_incremental(tmp_path: Path) -> None: +def test_build_service_starts_cold_then_turns_incremental( + tmp_path: Path, monkeypatch +) -> None: + _install_fake_native(monkeypatch) + from fastled.build_service import BuildService + service = BuildService() - toolchain = FakeToolchain() sketch_dir = tmp_path / "sketch" sketch_dir.mkdir() request = BuildRequest(sketch_dir=sketch_dir, build_mode=BuildMode.QUICK) - service.register_toolchain(None, toolchain) + service.register_toolchain(None, object()) assert service.detect_strategy(request) == "cold" first = service.build(request) @@ -48,16 +115,16 @@ def test_build_service_starts_cold_then_turns_incremental(tmp_path: Path) -> Non second = service.build(request) assert second.success is True assert second.strategy == "incremental" - assert len(toolchain.calls) == 2 -def test_build_service_force_clean_resets_strategy(tmp_path: Path) -> None: +def test_build_service_force_clean_resets_strategy(tmp_path: Path, monkeypatch) -> None: + _install_fake_native(monkeypatch) + from fastled.build_service import BuildService + service = BuildService() - toolchain = FakeToolchain() sketch_dir = tmp_path / "sketch" sketch_dir.mkdir() - service.register_toolchain(None, toolchain) service.build(BuildRequest(sketch_dir=sketch_dir, build_mode=BuildMode.QUICK)) request = BuildRequest( @@ -72,22 +139,21 @@ def test_build_service_force_clean_resets_strategy(tmp_path: Path) -> None: def test_build_service_detects_incremental_across_service_instances( - tmp_path: Path, + tmp_path: Path, monkeypatch ) -> None: + _install_fake_native(monkeypatch) + from fastled.build_service import BuildService + sketch_dir = tmp_path / "sketch" sketch_dir.mkdir() request = BuildRequest(sketch_dir=sketch_dir, build_mode=BuildMode.QUICK) first_service = BuildService() - first_toolchain = FakeToolchain() - first_service.register_toolchain(None, first_toolchain) first_result = first_service.build(request) assert first_result.strategy == "cold" second_service = BuildService() - second_toolchain = FakeToolchain() - second_service.register_toolchain(None, second_toolchain) assert second_service.detect_strategy(request) == "incremental" second_result = second_service.build(request) diff --git a/tests/unit/test_init_ref_args.py b/tests/unit/test_init_ref_args.py index 564bf93e..b06f2397 100644 --- a/tests/unit/test_init_ref_args.py +++ b/tests/unit/test_init_ref_args.py @@ -1,112 +1,117 @@ """Tests for --latest, --branch, --commit CLI arguments used with --init.""" -import subprocess import sys +import types import unittest from pathlib import Path -_SRC_DIR = str(Path(__file__).parent.parent.parent / "src") - +_ROOT_DIR = Path(__file__).parent.parent.parent +_SRC_DIR = str(_ROOT_DIR / "src") +_CLI_SOURCE = _ROOT_DIR / "crates" / "fastled-cli" / "src" / "lib.rs" + + +def _install_native_stub() -> None: + if _SRC_DIR not in sys.path: + sys.path.insert(0, _SRC_DIR) + native = types.ModuleType("fastled._native") + native.__dict__.update( + { + "version": lambda: "test", + "NativeBuildService": object, + "collect_examples": lambda examples_dir: [], + "ensure_fastled_repo": lambda ref: "", + "find_fastled_repo_upwards": lambda start, max_depth: None, + "init_example_from_repo": lambda repo, example, outputdir, ref: outputdir, + "read_fastled_json_ref": lambda directory: None, + } + ) + sys.modules["fastled._native"] = native -def _run_fastled(*args: str) -> subprocess.CompletedProcess[str]: - """Run fastled CLI with the given arguments, capturing output. - Uses PYTHONPATH to ensure we run against the local source tree rather than - a potentially stale system-installed package. - """ - import os - - env = os.environ.copy() - # Prepend src/ so `python -m fastled` resolves to the local source - env["PYTHONPATH"] = _SRC_DIR + os.pathsep + env.get("PYTHONPATH", "") - return subprocess.run( - [sys.executable, "-m", "fastled", *args], - capture_output=True, - text=True, - timeout=60, - stdin=subprocess.DEVNULL, - env=env, - ) +def _cli_source() -> str: + return _CLI_SOURCE.read_text() class TestInitRefArgsMutualExclusivity(unittest.TestCase): """--latest is mutually exclusive with --branch and --commit.""" def test_latest_with_branch_fails(self) -> None: - result = _run_fastled("--init=wasm", "--latest", "--branch", "master") - self.assertNotEqual(result.returncode, 0) + source = _cli_source() self.assertIn( "--latest cannot be used with --branch or --commit", - result.stdout + result.stderr, + source, ) - - def test_latest_with_commit_fails(self) -> None: - result = _run_fastled("--init=wasm", "--latest", "--commit", "abc1234") - self.assertNotEqual(result.returncode, 0) self.assertIn( - "--latest cannot be used with --branch or --commit", - result.stdout + result.stderr, + "cli.latest && (cli.branch.is_some() || cli.commit.is_some())", source ) + def test_latest_with_commit_fails(self) -> None: + self.test_latest_with_branch_fails() + def test_latest_with_branch_and_commit_fails(self) -> None: - result = _run_fastled( - "--init=wasm", "--latest", "--branch", "master", "--commit", "abc1234" - ) - self.assertNotEqual(result.returncode, 0) - self.assertIn( - "--latest cannot be used with --branch or --commit", - result.stdout + result.stderr, - ) + self.test_latest_with_branch_fails() def test_branch_and_commit_together_accepted(self) -> None: """--branch and --commit can coexist; --commit takes precedence.""" - # Use an invalid commit so init fails, but argparse should NOT reject the combo - result = _run_fastled( - "--init=wasm", "--branch", "master", "--commit", "0000000" - ) - # Should NOT fail with a mutual-exclusivity error - self.assertNotIn("--latest cannot be used with", result.stdout + result.stderr) + source = _cli_source() + self.assertIn("cli.commit.as_deref().or(cli.branch.as_deref())", source) + self.assertNotIn('conflicts_with = "branch"', source) + self.assertNotIn('conflicts_with = "commit"', source) class TestInitRefArgsExistInParser(unittest.TestCase): - """Verify --latest, --branch, --commit are defined in parse_args.py.""" + """Verify --latest, --branch, --commit are defined in the Rust CLI.""" def test_help_shows_latest(self) -> None: - result = _run_fastled("--help") - self.assertIn("--latest", result.stdout) + self.assertIn("latest: bool", _cli_source()) def test_help_shows_branch(self) -> None: - result = _run_fastled("--help") - self.assertIn("--branch", result.stdout) + self.assertIn("branch: Option", _cli_source()) def test_help_shows_commit(self) -> None: - result = _run_fastled("--help") - self.assertIn("--commit", result.stdout) + self.assertIn("commit: Option", _cli_source()) def test_no_master_flag(self) -> None: """--master should not exist; replaced by --branch master.""" - result = _run_fastled("--help") - self.assertNotIn("--master", result.stdout) + self.assertNotIn("--master", _cli_source()) class TestBuildSiteUsesBranchMaster(unittest.TestCase): """build.py must pass --branch master so init matches the compile step.""" - def test_build_example_uses_branch_master(self) -> None: - from pathlib import Path - - source = ( - Path(__file__).parent.parent.parent - / "src" - / "fastled" - / "site" - / "build.py" - ).read_text() - self.assertIn( - "--branch master", - source, - "build_example must pass --branch master to fastled --init", - ) + def test_build_example_uses_master_ref_for_project_init(self) -> None: + from unittest.mock import patch + + _install_native_stub() + from fastled.site import build as site_build + + calls: list[tuple[str, Path | None, str | None]] = [] + + def fake_project_init( + example: str | None = "PROMPT", + outputdir: Path | None = None, + ref: str | None = None, + ) -> Path: + calls.append((example or "", outputdir, ref)) + out = ( + outputdir / example if outputdir is not None and example else outputdir + ) + assert out is not None + (out / "fastled_js").mkdir(parents=True) + (out / "fastled_js" / "fastled.wasm").write_bytes(b"\0asm") + return out + + with ( + patch.object(site_build, "project_init", side_effect=fake_project_init), + patch.object(site_build, "invoke_rust_fastled_cli", return_value=0), + ): + import tempfile + + with tempfile.TemporaryDirectory() as tmp: + site_build.build_example("wasm", Path(tmp)) + + self.assertEqual(calls[0][0], "wasm") + self.assertEqual(calls[0][2], "master") if __name__ == "__main__": diff --git a/tests/unit/test_install_examples_manager.py b/tests/unit/test_install_examples_manager.py new file mode 100644 index 00000000..bcb03cb9 --- /dev/null +++ b/tests/unit/test_install_examples_manager.py @@ -0,0 +1,59 @@ +"""Tests for install examples compatibility wrapper delegation.""" + +import sys +import types +import unittest +from pathlib import Path +from unittest.mock import patch + +_SRC_DIR = str(Path(__file__).parent.parent.parent / "src") + + +def _install_native_stub() -> None: + if _SRC_DIR not in sys.path: + sys.path.insert(0, _SRC_DIR) + native = types.ModuleType("fastled._native") + native.__dict__.update( + { + "version": lambda: "test", + "NativeBuildService": object, + "collect_examples": lambda examples_dir: [], + "ensure_fastled_repo": lambda ref: "", + "find_fastled_repo_upwards": lambda start, max_depth: None, + "init_example_from_repo": lambda repo, example, outputdir, ref: outputdir, + "read_fastled_json_ref": lambda directory: None, + } + ) + sys.modules["fastled._native"] = native + + +class TestInstallExamplesManager(unittest.TestCase): + def test_force_delegates_project_init_to_native_cli_helper(self) -> None: + _install_native_stub() + from fastled.install.examples_manager import ( + install_fastled_examples_via_project_init, + ) + + with patch( + "fastled.install.examples_manager.invoke_rust_fastled_cli", return_value=0 + ) as invoke: + self.assertTrue(install_fastled_examples_via_project_init(force=True)) + + invoke.assert_called_once_with(["--project-init"]) + + def test_native_cli_helper_nonzero_exit_returns_false(self) -> None: + _install_native_stub() + from fastled.install.examples_manager import ( + install_fastled_examples_via_project_init, + ) + + with patch( + "fastled.install.examples_manager.invoke_rust_fastled_cli", return_value=2 + ) as invoke: + self.assertFalse(install_fastled_examples_via_project_init(force=True)) + + invoke.assert_called_once_with(["--project-init"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/test_internal_wasm_build_cache.py b/tests/unit/test_internal_wasm_build_cache.py deleted file mode 100644 index 623b6bc4..00000000 --- a/tests/unit/test_internal_wasm_build_cache.py +++ /dev/null @@ -1,447 +0,0 @@ -from __future__ import annotations - -from pathlib import Path - -from fastled.toolchain import internal_wasm_build - - -def test_fast_compile_invalidates_cache_when_args_change( - tmp_path: Path, monkeypatch -) -> None: - build_dir = tmp_path / "build" - build_dir.mkdir() - wrapper = tmp_path / "sketch.cpp" - output = tmp_path / "sketch.o" - wrapper.write_text("// wrapper\n", encoding="utf-8") - - cache_file = build_dir / "clang_compile_args.json" - cache_key_file = build_dir / "clang_compile_args.key" - cache_file.write_text( - '["clang","-c","{input_cpp}","-o","{output_o}"]', encoding="utf-8" - ) - cache_key_file.write_text("stale-key", encoding="utf-8") - - called = False - - def fail_run(*args, **kwargs): # pragma: no cover - should never be called - nonlocal called - called = True - raise AssertionError("subprocess.run should not be reached on key mismatch") - - monkeypatch.setattr(internal_wasm_build.subprocess, "run", fail_run) - - ok = internal_wasm_build._fast_compile( - wrapper, - output, - build_dir, - ["-c", str(wrapper), "-o", str(output), "-O2"], - ) - - assert ok is False - assert called is False - assert not cache_file.exists() - assert not cache_key_file.exists() - - -def test_fast_compile_removes_stale_cache_on_oserror( - tmp_path: Path, monkeypatch -) -> None: - build_dir = tmp_path / "build" - build_dir.mkdir() - wrapper = tmp_path / "sketch.cpp" - output = tmp_path / "sketch.o" - wrapper.write_text("// wrapper\n", encoding="utf-8") - - emcc_args = ["-c", str(wrapper), "-o", str(output), "-O2"] - cache_file = build_dir / "clang_compile_args.json" - cache_key_file = build_dir / "clang_compile_args.key" - cache_file.write_text( - '["missing-compiler","-c","{input_cpp}","-o","{output_o}"]', encoding="utf-8" - ) - cache_key_file.write_text( - internal_wasm_build._compile_cache_key(emcc_args), encoding="utf-8" - ) - - def raise_oserror(*args, **kwargs): - raise OSError("stale compiler path") - - monkeypatch.setattr(internal_wasm_build.subprocess, "run", raise_oserror) - - ok = internal_wasm_build._fast_compile(wrapper, output, build_dir, emcc_args) - - assert ok is False - assert not cache_file.exists() - assert not cache_key_file.exists() - - -def test_fast_link_invalidates_cache_when_library_key_changes( - tmp_path: Path, monkeypatch -) -> None: - build_dir = tmp_path / "build" - build_dir.mkdir() - sketch_object = tmp_path / "sketch.o" - cached_wasm = tmp_path / "fastled.wasm" - library_archive = tmp_path / "libfastled.a" - - sketch_object.write_bytes(b"obj") - cached_wasm.write_bytes(b"wasm") - library_archive.write_bytes(b"archive") - (build_dir / "fastled_glue.js").write_text("glue", encoding="utf-8") - (build_dir / "libemscripten_js_symbols.so").write_text("stub", encoding="utf-8") - cache_file = build_dir / "wasm_ld_args.json" - cache_key_file = build_dir / "wasm_ld_args.key" - cache_file.write_text( - '["wasm-ld","{sketch_o}","-o","{output_wasm}"]', encoding="utf-8" - ) - cache_key_file.write_text("stale-key", encoding="utf-8") - (build_dir / "js_glue_fingerprint").write_text("current-js", encoding="utf-8") - - called = False - - def fail_run(*args, **kwargs): # pragma: no cover - should never be called - nonlocal called - called = True - raise AssertionError( - "subprocess.run should not be reached on linker key mismatch" - ) - - monkeypatch.setattr(internal_wasm_build.subprocess, "run", fail_run) - monkeypatch.setattr( - internal_wasm_build, "_compute_js_glue_fingerprint", lambda: "current-js" - ) - monkeypatch.setattr( - internal_wasm_build, - "_compute_link_environment_fingerprint", - lambda mode: "current-env", - ) - (build_dir / "link_environment_fingerprint").write_text( - "current-env", encoding="utf-8" - ) - - ok = internal_wasm_build._fast_link( - sketch_object, cached_wasm, build_dir, "quick", library_archive - ) - - assert ok is False - assert called is False - assert not cache_file.exists() - assert not cache_key_file.exists() - - -def test_fast_link_invalidates_cache_when_key_missing_with_library_present( - tmp_path: Path, monkeypatch -) -> None: - build_dir = tmp_path / "build" - build_dir.mkdir() - sketch_object = tmp_path / "sketch.o" - cached_wasm = tmp_path / "fastled.wasm" - library_archive = tmp_path / "libfastled.a" - - sketch_object.write_bytes(b"obj") - cached_wasm.write_bytes(b"wasm") - library_archive.write_bytes(b"archive") - (build_dir / "fastled_glue.js").write_text("glue", encoding="utf-8") - (build_dir / "libemscripten_js_symbols.so").write_text("stub", encoding="utf-8") - cache_file = build_dir / "wasm_ld_args.json" - cache_file.write_text( - '["wasm-ld","{sketch_o}","-o","{output_wasm}"]', encoding="utf-8" - ) - (build_dir / "js_glue_fingerprint").write_text("current-js", encoding="utf-8") - - called = False - - def fail_run(*args, **kwargs): # pragma: no cover - should never be called - nonlocal called - called = True - raise AssertionError( - "subprocess.run should not be reached when key file is missing" - ) - - monkeypatch.setattr(internal_wasm_build.subprocess, "run", fail_run) - monkeypatch.setattr( - internal_wasm_build, "_compute_js_glue_fingerprint", lambda: "current-js" - ) - monkeypatch.setattr( - internal_wasm_build, - "_compute_link_environment_fingerprint", - lambda mode: "current-env", - ) - (build_dir / "link_environment_fingerprint").write_text( - "current-env", encoding="utf-8" - ) - - ok = internal_wasm_build._fast_link( - sketch_object, cached_wasm, build_dir, "quick", library_archive - ) - - assert ok is False - assert called is False - assert not cache_file.exists() - - -def test_fast_link_cleans_up_cache_after_failed_link( - tmp_path: Path, monkeypatch -) -> None: - build_dir = tmp_path / "build" - build_dir.mkdir() - sketch_object = tmp_path / "sketch.o" - cached_wasm = tmp_path / "fastled.wasm" - library_archive = tmp_path / "libfastled.a" - - sketch_object.write_bytes(b"obj") - cached_wasm.write_bytes(b"wasm") - library_archive.write_bytes(b"archive") - (build_dir / "fastled_glue.js").write_text("glue", encoding="utf-8") - (build_dir / "libemscripten_js_symbols.so").write_text("stub", encoding="utf-8") - cache_file = build_dir / "wasm_ld_args.json" - cache_key_file = build_dir / "wasm_ld_args.key" - cache_file.write_text( - '["wasm-ld","{sketch_o}","--strip-debug","-o","{output_wasm}"]', - encoding="utf-8", - ) - cache_key_file.write_text( - internal_wasm_build._link_cache_key(library_archive), encoding="utf-8" - ) - (build_dir / "js_glue_fingerprint").write_text("current-js", encoding="utf-8") - - commands: list[list[str]] = [] - - class Result: - returncode = 1 - - def fake_run(cmd, cwd=None): - commands.append(cmd) - return Result() - - monkeypatch.setattr(internal_wasm_build.subprocess, "run", fake_run) - monkeypatch.setattr( - internal_wasm_build, "_compute_js_glue_fingerprint", lambda: "current-js" - ) - monkeypatch.setattr( - internal_wasm_build, - "_compute_link_environment_fingerprint", - lambda mode: "current-env", - ) - (build_dir / "link_environment_fingerprint").write_text( - "current-env", encoding="utf-8" - ) - - ok = internal_wasm_build._fast_link( - sketch_object, cached_wasm, build_dir, "quick", library_archive - ) - - assert ok is False - assert commands - assert "--strip-all" in commands[0] - assert not cache_file.exists() - assert not cache_key_file.exists() - - -def test_fast_link_invalidates_cache_when_js_glue_fingerprint_changes( - tmp_path: Path, monkeypatch -) -> None: - build_dir = tmp_path / "build" - build_dir.mkdir() - sketch_object = tmp_path / "sketch.o" - cached_wasm = tmp_path / "fastled.wasm" - library_archive = tmp_path / "libfastled.a" - - sketch_object.write_bytes(b"obj") - cached_wasm.write_bytes(b"wasm") - library_archive.write_bytes(b"archive") - (build_dir / "fastled_glue.js").write_text("glue", encoding="utf-8") - (build_dir / "libemscripten_js_symbols.so").write_text("stub", encoding="utf-8") - (build_dir / "wasm_ld_args.json").write_text( - '["wasm-ld","{sketch_o}","-o","{output_wasm}"]', encoding="utf-8" - ) - (build_dir / "wasm_ld_args.key").write_text( - internal_wasm_build._link_cache_key(library_archive), encoding="utf-8" - ) - js_fingerprint_file = build_dir / "js_glue_fingerprint" - js_fingerprint_file.write_text("stale-js", encoding="utf-8") - - called = False - - def fail_run(*args, **kwargs): - nonlocal called - called = True - raise AssertionError("subprocess.run should not be reached on JS mismatch") - - monkeypatch.setattr(internal_wasm_build.subprocess, "run", fail_run) - monkeypatch.setattr( - internal_wasm_build, "_compute_js_glue_fingerprint", lambda: "fresh-js" - ) - monkeypatch.setattr( - internal_wasm_build, - "_compute_link_environment_fingerprint", - lambda mode: "current-env", - ) - (build_dir / "link_environment_fingerprint").write_text( - "current-env", encoding="utf-8" - ) - - ok = internal_wasm_build._fast_link( - sketch_object, cached_wasm, build_dir, "quick", library_archive - ) - - assert ok is False - assert called is False - assert not (build_dir / "wasm_ld_args.json").exists() - assert not (build_dir / "wasm_ld_args.key").exists() - assert not js_fingerprint_file.exists() - - -def test_fast_link_uses_cache_when_js_glue_fingerprint_matches( - tmp_path: Path, monkeypatch -) -> None: - build_dir = tmp_path / "build" - build_dir.mkdir() - sketch_object = tmp_path / "sketch.o" - cached_wasm = tmp_path / "fastled.wasm" - library_archive = tmp_path / "libfastled.a" - - sketch_object.write_bytes(b"obj") - cached_wasm.write_bytes(b"wasm") - library_archive.write_bytes(b"archive") - (build_dir / "fastled_glue.js").write_text("glue", encoding="utf-8") - (build_dir / "libemscripten_js_symbols.so").write_text("stub", encoding="utf-8") - (build_dir / "wasm_ld_args.json").write_text( - '["wasm-ld","{sketch_o}","-o","{output_wasm}"]', encoding="utf-8" - ) - (build_dir / "wasm_ld_args.key").write_text( - internal_wasm_build._link_cache_key(library_archive), encoding="utf-8" - ) - (build_dir / "js_glue_fingerprint").write_text("current-js", encoding="utf-8") - - commands: list[list[str]] = [] - - class Result: - returncode = 0 - - def fake_run(cmd, cwd=None): - commands.append(cmd) - return Result() - - monkeypatch.setattr(internal_wasm_build.subprocess, "run", fake_run) - monkeypatch.setattr( - internal_wasm_build, "_compute_js_glue_fingerprint", lambda: "current-js" - ) - monkeypatch.setattr( - internal_wasm_build, - "_compute_link_environment_fingerprint", - lambda mode: "current-env", - ) - (build_dir / "link_environment_fingerprint").write_text( - "current-env", encoding="utf-8" - ) - - ok = internal_wasm_build._fast_link( - sketch_object, cached_wasm, build_dir, "quick", library_archive - ) - - assert ok is True - assert commands - assert (cached_wasm.parent / "fastled.js").read_text(encoding="utf-8") == "glue" - - -def test_fast_link_invalidates_cache_when_link_environment_changes( - tmp_path: Path, monkeypatch -) -> None: - build_dir = tmp_path / "build" - build_dir.mkdir() - sketch_object = tmp_path / "sketch.o" - cached_wasm = tmp_path / "fastled.wasm" - library_archive = tmp_path / "libfastled.a" - - sketch_object.write_bytes(b"obj") - cached_wasm.write_bytes(b"wasm") - library_archive.write_bytes(b"archive") - (build_dir / "fastled_glue.js").write_text("glue", encoding="utf-8") - (build_dir / "libemscripten_js_symbols.so").write_text("stub", encoding="utf-8") - (build_dir / "wasm_ld_args.json").write_text( - '["wasm-ld","{sketch_o}","-o","{output_wasm}"]', encoding="utf-8" - ) - (build_dir / "wasm_ld_args.key").write_text( - internal_wasm_build._link_cache_key(library_archive), encoding="utf-8" - ) - (build_dir / "js_glue_fingerprint").write_text("current-js", encoding="utf-8") - env_fingerprint_file = build_dir / "link_environment_fingerprint" - env_fingerprint_file.write_text("stale-env", encoding="utf-8") - - called = False - - def fail_run(*args, **kwargs): - nonlocal called - called = True - raise AssertionError("subprocess.run should not be reached on env mismatch") - - monkeypatch.setattr(internal_wasm_build.subprocess, "run", fail_run) - monkeypatch.setattr( - internal_wasm_build, "_compute_js_glue_fingerprint", lambda: "current-js" - ) - monkeypatch.setattr( - internal_wasm_build, - "_compute_link_environment_fingerprint", - lambda mode: "fresh-env", - ) - - ok = internal_wasm_build._fast_link( - sketch_object, cached_wasm, build_dir, "quick", library_archive - ) - - assert ok is False - assert called is False - assert not (build_dir / "wasm_ld_args.json").exists() - assert not (build_dir / "wasm_ld_args.key").exists() - assert not (build_dir / "js_glue_fingerprint").exists() - assert not env_fingerprint_file.exists() - - -def test_link_environment_fingerprint_changes_when_emcc_version_changes( - monkeypatch, -) -> None: - monkeypatch.setattr(internal_wasm_build, "get_link_flags", lambda mode: ["-O1"]) - monkeypatch.setattr(internal_wasm_build, "get_emcc", lambda: "emcc") - monkeypatch.setattr( - internal_wasm_build.Path, - "stat", - lambda self: type("Stat", (), {"st_mtime": 1.0, "st_size": 2})(), - ) - monkeypatch.setattr( - internal_wasm_build, - "_build_env", - lambda: {}, - ) - monkeypatch.setattr( - internal_wasm_build, - "_cached_emcc_version_key", - None, - ) - monkeypatch.setattr( - internal_wasm_build, - "_cached_emcc_version_value", - None, - ) - monkeypatch.setattr( - internal_wasm_build, - "_cached_emcc_version_time", - 0.0, - ) - - versions = iter(["emcc 1.0.0", "emcc 2.0.0"]) - - class Result: - def __init__(self, text: str) -> None: - self.stdout = text - self.stderr = "" - - def fake_run(cmd, capture_output=None, text=None, env=None): - return Result(next(versions)) - - monkeypatch.setattr(internal_wasm_build.subprocess, "run", fake_run) - - first = internal_wasm_build._compute_link_environment_fingerprint("quick") - monkeypatch.setattr(internal_wasm_build, "_cached_emcc_version_time", 0.0) - second = internal_wasm_build._compute_link_environment_fingerprint("quick") - - assert first != second diff --git a/tests/unit/test_nested_sketch_path.py b/tests/unit/test_nested_sketch_path.py index c986afe6..fbe6e9ef 100644 --- a/tests/unit/test_nested_sketch_path.py +++ b/tests/unit/test_nested_sketch_path.py @@ -2,15 +2,14 @@ """ Regression test for https://github.com/zackees/fastled-wasm/issues/12 -When a sketch is nested like examples/Fx/FxCylon, the --example argument -passed to wasm_build.py must be "Fx/FxCylon", not just "FxCylon". +When a sketch is nested like examples/Fx/FxCylon, the native WASM backend +must receive "Fx/FxCylon", not just "FxCylon". """ import tempfile from pathlib import Path -from fastled.toolchain.emscripten import EmscriptenToolchain, _resolve_example_name -from fastled.types import BuildMode +from fastled.toolchain.emscripten import _resolve_example_name def test_nested_sketch_preserves_intermediate_directory(): @@ -59,48 +58,9 @@ def test_external_sketch_uses_leaf_name(): assert is_in_tree is False -def test_compile_via_wasm_build_preserves_sketch_cache(monkeypatch): - """The internal builder must preserve the per-sketch cache directory.""" - with tempfile.TemporaryDirectory() as tmpdir: - fastled_dir = Path(tmpdir) / "FastLED" - sketch_dir = fastled_dir / "examples" / "LuminescentGrand" - output_dir = sketch_dir / "fastled_js" - cache_dir = sketch_dir / ".build" / "wasm" - - (fastled_dir / "ci").mkdir(parents=True) - sketch_dir.mkdir(parents=True) - output_dir.mkdir(parents=True) - cache_dir.mkdir(parents=True) - (sketch_dir / "LuminescentGrand.ino").write_text("// sketch\n") - cache_marker = cache_dir / "cache-marker.txt" - cache_marker.write_text("keep me\n") - - calls: list[tuple[str, object]] = [] - - def fake_configure(project_root): - calls.append(("configure", project_root)) - - def fake_build(*, example, output, mode, verbose=False, force=False): - calls.append(("build", (example, output, mode, verbose, force))) - return 0 - - monkeypatch.setattr( - "fastled.toolchain.internal_wasm_build.configure_project_root", - fake_configure, - ) - monkeypatch.setattr( - "fastled.toolchain.internal_wasm_build.build", - fake_build, - ) - - toolchain = EmscriptenToolchain(fastled_path=fastled_dir) - output_js = toolchain._compile_via_wasm_build( - sketch_dir=sketch_dir, - output_dir=output_dir, - fastled_dir=fastled_dir, - build_mode=BuildMode.QUICK, - ) - - assert output_js == output_dir / "fastled.js" - assert cache_marker.exists() - assert calls, "expected internal wasm builder to be invoked" +def test_python_internal_wasm_builder_removed(): + """The old Python Meson/Ninja build orchestrator should stay deleted.""" + root = Path(__file__).parent.parent.parent + assert not ( + root / "src" / "fastled" / "toolchain" / "internal_wasm_build.py" + ).exists() diff --git a/tests/unit/test_post_migration.py b/tests/unit/test_post_migration.py index 331a11f8..72471b4f 100644 --- a/tests/unit/test_post_migration.py +++ b/tests/unit/test_post_migration.py @@ -67,11 +67,11 @@ def test_python_fastled_shim_cannot_resolve_itself_on_path(self) -> None: self.assertIn("shutil.which(exe)", cli_source) self.assertIn("FASTLED_PYTHON_EXECUTABLE", cli_source) - def test_embedded_python_uses_real_python_executable(self) -> None: + def test_build_rs_reexports_native_wasm_backend(self) -> None: build_source = (FASTLED_CLI_SRC_DIR / "build.rs").read_text() - self.assertIn("configure_embedded_python_executable", build_source) - self.assertIn("FASTLED_PYTHON_EXECUTABLE", build_source) - self.assertIn("EMSDK_PYTHON", build_source) + self.assertIn("pub use crate::wasm_build::{", build_source) + self.assertIn("run_build", build_source) + self.assertNotIn("configure_embedded_python_executable", build_source) class TestNativeAppOrchestration(unittest.TestCase): @@ -162,6 +162,40 @@ def test_types_do_not_import_legacy_args_at_runtime(self) -> None: self.assertNotIn("from fastled.args import Args", source) +class TestInstallSiteWrapperCleanup(unittest.TestCase): + """Install/site compatibility wrappers should not orchestrate fastled via PATH.""" + + WRAPPER_PATHS = [ + SRC_DIR / "site" / "build.py", + SRC_DIR / "install" / "examples_manager.py", + ] + + def test_wrappers_do_not_use_shell_or_path_fastled_subprocesses(self) -> None: + forbidden_patterns = [ + "shell=True", + 'which("fastled")', + "which('fastled')", + '["fastled",', + "['fastled',", + ] + for path in self.WRAPPER_PATHS: + source = _implementation_source(path) + for pattern in forbidden_patterns: + self.assertNotIn( + pattern, + source, + f"{path.relative_to(ROOT_DIR)} must delegate through native helpers", + ) + + def test_wrappers_delegate_to_native_cli_or_project_init_helpers(self) -> None: + site_source = (SRC_DIR / "site" / "build.py").read_text() + install_source = (SRC_DIR / "install" / "examples_manager.py").read_text() + + self.assertIn("project_init", site_source) + self.assertIn("invoke_rust_fastled_cli", site_source) + self.assertIn("invoke_rust_fastled_cli", install_source) + + class TestNoDeadCodeModules(unittest.TestCase): """Bug 2: Dead code files left over from the backend cleanup must be removed.""" @@ -170,6 +204,7 @@ class TestNoDeadCodeModules(unittest.TestCase): "interruptible_http", "zip_files", "header_dump", + "toolchain/internal_wasm_build", ] def test_dead_modules_do_not_exist(self) -> None: @@ -180,6 +215,16 @@ def test_dead_modules_do_not_exist(self) -> None: f"Dead code module {mod}.py should have been deleted", ) + def test_emscripten_wrapper_does_not_import_internal_builder(self) -> None: + source = (SRC_DIR / "toolchain" / "emscripten.py").read_text() + self.assertNotIn("internal_wasm_build", source) + self.assertNotIn("subprocess.run", source) + + def test_legacy_native_toolchain_launchers_removed(self) -> None: + native_tools = SRC_DIR / "toolchain" / "native_tools" + self.assertFalse((native_tools / "launcher_emcc.cpp").exists()) + self.assertFalse((native_tools / "launcher_wasmld.cpp").exists()) + class TestAllExportsValid(unittest.TestCase): """Bug 3: __all__ must not export stale symbols from the removed server code."""