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
25 changes: 0 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ flate2 = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
anyhow = "1"
ctcb-manifest = "0.4.1"
dirs = "5"
strsim = "0.11"
crossterm = "0.28"
Expand Down
1 change: 0 additions & 1 deletion crates/fastled-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ tokio = { workspace = true }
axum = { workspace = true }
tower-http = { workspace = true }
tokio-stream = { workspace = true }
ctcb-manifest = { workspace = true }
dirs = { workspace = true }
tempfile = "3"

Expand Down
122 changes: 121 additions & 1 deletion crates/fastled-cli/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,92 @@ use std::io::{BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};
use ctcb_manifest::{PartRef, PlatformManifest};
use serde::Deserialize;
use std::collections::BTreeMap;

use crate::archive;

// ---------------------------------------------------------------------------
// Manifest schema
// ---------------------------------------------------------------------------
//
// The clang-tool-chain-bins emscripten manifest is served in two different
// shapes depending on platform. Linux/macOS nests version entries under a
// `versions` key:
//
// { "latest": "4.0.21",
// "versions": { "4.0.21": { "href": "...", "sha256": "...", "parts": [...] }, ... } }
//
// Windows inlines version entries at the top level alongside `latest`:
//
// { "latest": "4.0.19",
// "4.0.19": { "href": "...", "sha256": "...", "parts": [...] } }
//
// Our `PlatformManifest` accepts both via a custom Deserialize.

#[derive(Debug, Deserialize)]
struct PartRef {
href: String,
sha256: String,
}

#[derive(Debug, Deserialize)]
struct VersionInfo {
href: String,
sha256: String,
parts: Option<Vec<PartRef>>,
}

#[derive(Debug)]
struct PlatformManifest {
latest: String,
versions: BTreeMap<String, VersionInfo>,
}

impl<'de> Deserialize<'de> for PlatformManifest {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let raw: serde_json::Value = Deserialize::deserialize(deserializer)?;
let obj = raw
.as_object()
.ok_or_else(|| serde::de::Error::custom("expected JSON object at manifest root"))?;

let latest = obj
.get("latest")
.and_then(|v| v.as_str())
.ok_or_else(|| serde::de::Error::custom("missing 'latest' key"))?
.to_string();

let mut versions = BTreeMap::new();

// Prefer the nested-`versions` shape (Linux / macOS manifests) when
// present. Fall back to top-level inline entries (Windows).
if let Some(serde_json::Value::Object(versions_obj)) = obj.get("versions") {
for (key, value) in versions_obj {
let info: VersionInfo = serde_json::from_value(value.clone()).map_err(|e| {
serde::de::Error::custom(format!(
"bad version entry '{key}' (nested versions): {e}"
))
})?;
versions.insert(key.clone(), info);
}
} else {
for (key, value) in obj {
if key == "latest" {
continue;
}
let info: VersionInfo = serde_json::from_value(value.clone()).map_err(|e| {
serde::de::Error::custom(format!(
"bad version entry '{key}' (inline versions): {e}"
))
})?;
versions.insert(key.clone(), info);
}
}

Ok(PlatformManifest { latest, versions })
}
}

const EMSCRIPTEN_MANIFEST_BASE_URL: &str =
"https://raw.githubusercontent.com/zackees/clang-tool-chain-bins/main/assets/emscripten";

Expand Down Expand Up @@ -172,6 +254,21 @@ pub fn ensure_emscripten_installed() -> Result<PathBuf> {
fs::create_dir_all(&staging)?;
archive::extract_tar_zst(&archive_path, &staging)?;

// Restore execute bits on Unix — some tar implementations (and the
// umask of the extracting process) strip the +x bit from binaries
// under bin/, emscripten/, and libexec/. emcc.py then hits
// PermissionError when it tries to exec clang. Mirror the
// belt-and-suspenders chmod from the previous Python implementation.
#[cfg(unix)]
{
for subdir in ["bin", "emscripten", "libexec"] {
let dir = staging.join(subdir);
if dir.is_dir() {
add_executable_bits_recursive(&dir)?;
}
}
}

fs::write(staging.join("done.txt"), "ok\n")?;

if install_dir.exists() {
Expand All @@ -184,6 +281,29 @@ pub fn ensure_emscripten_installed() -> Result<PathBuf> {
Ok(install_dir)
}

/// Recursively chmod every file under `dir` to include +x for user / group /
/// other (no-op on Windows). Used right after emscripten extraction so the
/// shipped binaries (clang, wasm-ld, etc.) are actually executable.
#[cfg(unix)]
fn add_executable_bits_recursive(dir: &Path) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
for entry in fs::read_dir(dir).with_context(|| format!("read_dir {}", dir.display()))? {
let entry = entry?;
let path = entry.path();
let file_type = entry.file_type()?;
if file_type.is_dir() {
add_executable_bits_recursive(&path)?;
} else if file_type.is_file() {
let mode = entry.metadata()?.permissions().mode();
let new_mode = mode | 0o111;
if new_mode != mode {
fs::set_permissions(&path, fs::Permissions::from_mode(new_mode))?;
}
}
}
Ok(())
}

// ---------------------------------------------------------------------------
// esbuild
// ---------------------------------------------------------------------------
Expand Down
41 changes: 41 additions & 0 deletions crates/fastled-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ fn compile_and_serve(dir: &str, cli: &Cli, mode: ViewerMode) -> ExitCode {
match install::ensure_emscripten_installed() {
Ok(install_dir) => {
std::env::set_var("FASTLED_EMSCRIPTEN_DIR", &install_dir);
// FastLED's meson cross-file calls `clang-tool-chain-emcc` /
// `-em++` / `-emar`. Those wrappers (from the `clang-tool-chain`
// PyPI package) look up emscripten under
// `$CLANG_TOOL_CHAIN_DOWNLOAD_PATH/emscripten/<plat>/<arch>/<ver>`.
// install_dir here is `<root>/emscripten/<plat>/<arch>/<ver>`,
// so walk four parents up to the toolchains root and export it.
if let Some(root) = install_dir
.parent()
.and_then(|p| p.parent())
.and_then(|p| p.parent())
.and_then(|p| p.parent())
{
std::env::set_var("CLANG_TOOL_CHAIN_DOWNLOAD_PATH", root);
}
}
Err(e) => {
eprintln!("fastled: emscripten toolchain install failed: {e:#}");
Expand Down Expand Up @@ -683,6 +697,33 @@ pub fn run() -> ExitCode {
}
}

// If we're about to delegate a compile to Python (`--just-compile <dir>`)
// ensure the emscripten + esbuild toolchains are installed first and
// export the paths through env vars — matches what compile_and_serve()
// does for the non-just-compile path. Without this, Python's emscripten
// toolchain lookup falls through to "not found" on a fresh CI runner.
let needs_compile_toolchain = cli.directory.is_some() && cli.init.is_none() && !cli.install;
if needs_compile_toolchain {
match install::ensure_emscripten_installed() {
Ok(install_dir) => {
std::env::set_var("FASTLED_EMSCRIPTEN_DIR", &install_dir);
}
Err(e) => {
eprintln!("fastled: emscripten toolchain install failed: {e:#}");
return ExitCode::FAILURE;
}
}
match install::ensure_esbuild_installed() {
Ok(esbuild_path) => {
std::env::set_var("FASTLED_ESBUILD_PATH", &esbuild_path);
}
Err(e) => {
eprintln!("fastled: esbuild install failed: {e:#}");
return ExitCode::FAILURE;
}
}
}

// --- Delegate to Python for everything else ------------------------------
// (--just-compile, --init, --install, etc.)

Expand Down
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ keywords = ["template-python-cmd"]
license = { text = "BSD 3-Clause License" }
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`.
"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'",
]

Expand Down
Loading