Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/workflows/_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ jobs:

- name: Build fastled CLI binary (for wheel bundling)
run: |
# Compile the Rust `fastled` CLI and stage it under wheel-data/scripts/
# Compile the Rust `fastled-rs` CLI and stage it under wheel-data/scripts/
# so maturin packages it into <pkg>-<ver>.data/scripts/ in the wheel.
# See `build-wheel` and `[tool.maturin] data` in pyproject.toml.
if [ -n "${{ inputs.maturin-target }}" ]; then
soldr cargo build --release --bin fastled --target "${{ inputs.maturin-target }}"
soldr cargo build --release --bin fastled-rs --target "${{ inputs.maturin-target }}"
case "${{ inputs.maturin-target }}" in
*windows*) EXE_NAME="fastled.exe" ;;
*) EXE_NAME="fastled" ;;
*windows*) EXE_NAME="fastled-rs.exe" ;;
*) EXE_NAME="fastled-rs" ;;
esac
BIN_SRC="target/${{ inputs.maturin-target }}/release/${EXE_NAME}"
else
soldr cargo build --release --bin fastled
soldr cargo build --release --bin fastled-rs
case "$(uname -s)" in
MINGW*|MSYS*|CYGWIN*) EXE_NAME="fastled.exe" ;;
*) EXE_NAME="fastled" ;;
MINGW*|MSYS*|CYGWIN*) EXE_NAME="fastled-rs.exe" ;;
*) EXE_NAME="fastled-rs" ;;
esac
# On Windows the rustup default host triple is MSVC; cargo writes
# the binary under target/<triple>/release/ even without --target.
if [ -n "${CARGO_BUILD_TARGET:-}" ] || [ "$EXE_NAME" = "fastled.exe" ]; then
if [ -n "${CARGO_BUILD_TARGET:-}" ] || [ "$EXE_NAME" = "fastled-rs.exe" ]; then
ARCH="$(uname -m)"
if [ "$ARCH" = "x86_64" ]; then TRIPLE="x86_64-pc-windows-msvc"; else TRIPLE="aarch64-pc-windows-msvc"; fi
if [ -f "target/${TRIPLE}/release/${EXE_NAME}" ]; then
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ uv run pytest tests/unit --ignore=tests/unit/test_cli.py -v

## Common gotchas

- The installed `fastled` CLI on PATH (e.g. `/c/tools/python13/Scripts/fastled.exe`) may be stale; `find_rust_fastled_cli` in `src/fastled/_rust_cli.py` prefers `target/{release,debug}/fastled.exe` over PATH for exactly this reason. If a CLI test fails, check `uv run python -c "from fastled._rust_cli import find_rust_fastled_cli; print(find_rust_fastled_cli())"`.
- The installed `fastled` CLI on PATH (e.g. `/c/tools/python13/Scripts/fastled.exe`) may be a Python compatibility shim or stale script; the Rust CLI binary is named `fastled-rs[.exe]` during the migration. If a CLI test fails, check `uv run python -c "from fastled._rust_cli import find_rust_fastled_cli; print(find_rust_fastled_cli())"`.
- Error messages from the Rust CLI go to **stderr**. Tests asserting `result.stdout` for error text should use `result.stdout + result.stderr` (the parse_args.py removal in #72 exposed this).
- `tasklist` on Windows may report tens of thousands of `python.exe` zombies — most are stale tasklist artifacts, not real processes. `ps -ef | grep python` is more reliable for live ones.

Expand Down
8 changes: 4 additions & 4 deletions build-wheel
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"

# Platform-specific binary name and target directory.
case "$(uname -s)" in
MINGW*|MSYS*|CYGWIN*) EXE_NAME="fastled.exe" ;;
*) EXE_NAME="fastled" ;;
MINGW*|MSYS*|CYGWIN*) EXE_NAME="fastled-rs.exe" ;;
*) EXE_NAME="fastled-rs" ;;
esac

echo "Compiling fastled CLI (release)..."
if command -v soldr &> /dev/null; then
soldr cargo build --release --bin fastled
soldr cargo build --release --bin fastled-rs
else
echo "Warning: soldr not found; falling back to plain cargo (slow). Install with: uv tool install soldr"
cargo build --release --bin fastled
cargo build --release --bin fastled-rs
fi

# Resolve where cargo wrote the binary. cargo on Windows MSVC writes the
Expand Down
4 changes: 2 additions & 2 deletions crates/fastled-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ name = "fastled_cli"
path = "src/lib.rs"

[[bin]]
name = "fastled"
name = "fastled-rs"
path = "src/main.rs"

[dependencies]
Expand Down Expand Up @@ -41,7 +41,7 @@ tempfile = "3"

[package.metadata.binstall]
pkg-url = "{ repo }/releases/download/v{ version }/fastled-{ target }{ archive-suffix }"
bin-dir = "fastled{ binary-ext }"
bin-dir = "fastled-rs{ binary-ext }"
pkg-fmt = "zip"

[dev-dependencies]
166 changes: 70 additions & 96 deletions crates/fastled-cli/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Build orchestration for WASM compilation.
//!
//! The Rust CLI now drives the Python build service in-process through PyO3
//! instead of shelling out through `python -m fastled.app`.
//! The Rust CLI now drives the Python build service in-process through PyO3.

use std::fs;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -61,13 +60,6 @@ pub struct BuildRequest {
pub force_clean: bool,
}

impl BuildRequest {
/// Derive the conventional output directory for this request.
pub fn output_dir(&self) -> PathBuf {
self.sketch_dir.join("fastled_js")
}
}

/// Mirrors the useful fields returned from `build_types.BuildResult`.
#[derive(Debug)]
pub struct BuildResult {
Expand Down Expand Up @@ -106,6 +98,65 @@ 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<PathBuf> {
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<PathBuf> {
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<PathBuf> {
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
Expand Down Expand Up @@ -135,6 +186,8 @@ fn run_build_embedded(request: &BuildRequest) -> Result<BuildResult> {
Python::with_gil(|py| -> Result<BuildResult> {
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")?;
Expand Down Expand Up @@ -256,84 +309,19 @@ fn run_build_embedded(request: &BuildRequest) -> Result<BuildResult> {
})
}

fn run_build_subprocess(request: &BuildRequest, reason: &str) -> Result<BuildResult> {
use std::process::Command;

let python = if Command::new("python")
.args(["--version"])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
{
"python"
} else {
"python3"
};

let output_dir = request.output_dir();
let mut cmd = Command::new(python);
cmd.args(["-m", "fastled.app", "--just-compile"]);
cmd.arg(&request.sketch_dir);

match request.build_mode {
BuildMode::Quick => {}
BuildMode::Debug => {
cmd.arg("--debug");
}
BuildMode::Release => {
cmd.arg("--release");
}
}

if request.profile {
cmd.arg("--profile");
}
if let Some(fp) = &request.fastled_path {
cmd.arg("--fastled-path");
cmd.arg(fp);
}
if request.force_clean {
cmd.arg("--purge");
}

eprintln!("fastled: falling back to subprocess build path: {reason}");
let start = Instant::now();
let output = cmd
.output()
.with_context(|| format!("failed to launch `{python} -m fastled.app`"))?;
let duration_secs = start.elapsed().as_secs_f64();

let combined = {
let mut s = String::new();
s.push_str(&String::from_utf8_lossy(&output.stdout));
if !output.stderr.is_empty() {
s.push('\n');
s.push_str(&String::from_utf8_lossy(&output.stderr));
}
s
};

Ok(BuildResult {
success: output.status.success(),
output_dir,
duration_secs,
sketch_time_secs: duration_secs,
strategy: "unknown".to_string(),
output: combined,
})
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Run a WASM build through the Python build service without the legacy
/// `fastled.app` CLI trampoline.
/// Run a WASM build through the in-process Python build service.
pub fn run_build(request: &BuildRequest) -> Result<BuildResult> {
match run_build_embedded(request) {
Ok(result) => Ok(result),
Err(err) => run_build_subprocess(request, &format!("{err:#}")),
}
run_build_embedded(request).with_context(|| {
format!(
"native build failed for sketch `{}` in {} mode",
request.sketch_dir.display(),
request.build_mode
)
})
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -398,20 +386,6 @@ mod tests {
assert!(!req.force_clean);
}

#[test]
fn test_build_request_output_dir() {
let sketch = PathBuf::from("/tmp/my_sketch");
let req = BuildRequest {
sketch_dir: sketch.clone(),
build_mode: BuildMode::Debug,
profile: false,
fastled_path: None,
force_clean: false,
};

assert_eq!(req.output_dir(), sketch.join("fastled_js"));
}

#[test]
fn test_build_request_with_fastled_path() {
let sketch = PathBuf::from("/tmp/my_sketch");
Expand Down
29 changes: 14 additions & 15 deletions install
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ uv pip install "maturin>=1.7,<2"
# `[tool.maturin] data = "wheel-data"` config points here; maturin treats a
# missing path as an error during `pep517 build-wheel`. The cargo-build step
# later in this script will populate `wheel-data/scripts/` with the Rust
# fastled binary so subsequent `./build-wheel` invocations bundle it; an
# fastled-rs binary so subsequent `./build-wheel` invocations bundle it; an
# empty dir is fine for the editable-install path that pip is about to run.
mkdir -p wheel-data/scripts

Expand Down Expand Up @@ -104,39 +104,38 @@ if command -v cargo-binstall &> /dev/null; then
fi
fi

# Ensure the Rust `fastled` CLI binary is materialised somewhere our Python
# Ensure the Rust `fastled-rs` CLI binary is materialised somewhere our Python
# helpers (src/fastled/_rust_cli.py) can find it. The Python `[project.scripts]`
# entry-point shim collides with the Rust binary's name on PATH, so the helper
# explicitly looks in target/{release,debug}/ and $CARGO_HOME/bin. If
# entry-point shim owns the user-facing `fastled` command during the migration,
# while the Rust binary uses a distinct `fastled-rs` name. If
# cargo-binstall didn't supply a pre-built copy, build from source so unit
# tests and the deploy workflow can subprocess the binary for the internal
# auto-install plumbing flags (--internal-ensure-fastled-repo, etc.).
if [[ "$FASTLED_CLI_BINSTALLED" -eq 0 ]]; then
echo "Building fastled CLI from source..."
if command -v soldr &> /dev/null; then
soldr cargo build --release --bin fastled \
|| { echo "Error: soldr cargo build --bin fastled failed"; exit 1; }
soldr cargo build --release --bin fastled-rs \
|| { echo "Error: soldr cargo build --bin fastled-rs failed"; exit 1; }
else
echo "Warning: soldr not found; falling back to plain cargo (slow). Install with: uv tool install soldr"
cargo build --release --bin fastled \
|| { echo "Error: cargo build --bin fastled failed"; exit 1; }
cargo build --release --bin fastled-rs \
|| { echo "Error: cargo build --bin fastled-rs failed"; exit 1; }
fi
fi

# Locate the just-built (or binstalled) Rust binary and stage it where
# downstream tooling will find it. Editable installs (`uv pip install -e .`)
# don't extract the maturin `data` dir to the venv's Scripts/, so we mimic
# the wheel-install layout manually:
# * `.venv/Scripts/fastled[.exe]` (or `.venv/bin/fastled`) — so PATH lookup
# resolves the Rust binary for callers like `build_site.py` that shell
# out to plain `fastled`.
# * `wheel-data/scripts/fastled[.exe]` — so a subsequent `./build-wheel`
# * `.venv/Scripts/fastled-rs[.exe]` (or `.venv/bin/fastled-rs`) so the
# Python `fastled` compatibility shim can find the Rust binary.
# * `wheel-data/scripts/fastled-rs[.exe]` so a subsequent `./build-wheel`
# produces a wheel that bundles the binary.
if [[ "$OSTYPE" == "msys" ]]; then
EXE_NAME="fastled.exe"
EXE_NAME="fastled-rs.exe"
VENV_BIN_DIR=".venv/Scripts"
else
EXE_NAME="fastled"
EXE_NAME="fastled-rs"
VENV_BIN_DIR=".venv/bin"
fi

Expand All @@ -161,5 +160,5 @@ if [[ -n "$BIN_SRC" ]]; then
cp -f "$BIN_SRC" "wheel-data/scripts/$EXE_NAME"
echo "Staged $BIN_SRC -> $VENV_BIN_DIR/$EXE_NAME (and wheel-data/scripts/)"
else
echo "Warning: Rust fastled binary not found; PATH lookups for 'fastled' will miss" >&2
echo "Warning: Rust fastled-rs binary not found; Python fastled shims will fail" >&2
fi
15 changes: 7 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ features = ["pyo3/extension-module", "pyo3/abi3-py310"]
# Pre-built Rust binaries get dropped into wheel-data/scripts/ by the
# build-wheel helper before maturin runs. maturin packages this directory as
# the wheel's <pkg>-<ver>.data/ tree; pip then extracts scripts/ to the
# venv's Scripts/ (Windows) or bin/ (Unix) at install time, so end users get
# the Rust `fastled` binary directly — no Python entry-point shim, no
# cargo build at install time.
# venv's Scripts/ (Windows) or bin/ (Unix) at install time, so the Python
# compatibility shim can launch the Rust `fastled-rs` binary without a cargo
# build at runtime.
data = "wheel-data"

[tool.ruff]
Expand Down Expand Up @@ -71,10 +71,9 @@ ignore_missing_imports = true
disable_error_code = ["import-untyped"]

[project.scripts]
# `fastled` is intentionally NOT a Python entry-point; the Rust binary is
# bundled into the wheel via [tool.maturin] data (see above) and pip
# installs it directly to the venv's Scripts/bin directory. The Python
# aliases below are kept as thin shims for the rarely-used `python -m`
# invocation paths.
# `fastled` remains a Python compatibility entry point during the migration.
# It resolves and execs the Rust binary under the distinct `fastled-rs` name
# so the shim cannot recursively find itself on PATH.
fastled = "fastled.cli:main"
fastled-wasm = "fastled.cli:main"
fled = "fastled.cli:main"
Loading
Loading