diff --git a/.gitignore b/.gitignore index 5537b3c0..ab07b511 100644 --- a/.gitignore +++ b/.gitignore @@ -153,3 +153,8 @@ dist/ logs vscode-plugin .loop + +# Rust build artifacts +/target/ +/.cargo/ +/.rustup/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 00000000..ad26e3a9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,322 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "clap" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b193af5b67834b676abd72466a96c1024e6a6ad978a1f484bd90b85c94041351" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1110bd8a634a1ab8cb04345d8d878267d57c3cf1b38d91b71af6686408bbca6a" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "fastled-cli" +version = "2.0.6" +dependencies = [ + "clap", +] + +[[package]] +name = "fastled-py" +version = "2.0.6" +dependencies = [ + "pyo3", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "indoc" +version = "2.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706" +dependencies = [ + "rustversion", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "libc" +version = "0.2.184" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48f5d2a454e16a5ea0f4ced81bd44e4cfc7bd3a507b61887c99fd3538b28e4af" + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "pyo3" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7778bffd85cf38175ac1f545509665d0b9b92a198ca7941f131f85f7a4f9a872" +dependencies = [ + "cfg-if", + "indoc", + "libc", + "memoffset", + "once_cell", + "portable-atomic", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94f6cbe86ef3bf18998d9df6e0f3fc1050a8c5efa409bf712e661a4366e010fb" +dependencies = [ + "once_cell", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9f1b4c431c0bb1c8fb0a338709859eed0d030ff6daa34368d3b152a63dfdd8d" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-macros" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbc2201328f63c4710f68abdf653c89d8dbc2858b88c5d88b0ff38a75288a9da" +dependencies = [ + "proc-macro2", + "pyo3-macros-backend", + "quote", + "syn", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fca6726ad0f3da9c9de093d6f116a93c1a38e417ed73bf138472cf4064f72028" +dependencies = [ + "heck", + "proc-macro2", + "pyo3-build-config", + "quote", + "syn", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unindent" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..8131fd90 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,24 @@ +[workspace] +resolver = "2" +members = [ + "crates/fastled-cli", + "crates/fastled-py", +] + +[workspace.package] +version = "2.0.6" +edition = "2021" +rust-version = "1.85" +license = "MIT" +repository = "https://github.com/zackees/fastled-wasm" +homepage = "https://github.com/zackees/fastled-wasm" + +[workspace.dependencies] +pyo3 = { version = "0.23", features = ["abi3-py310"] } +clap = { version = "4", features = ["derive"] } +thiserror = "2" + +[profile.release] +debug = "line-tables-only" +split-debuginfo = "packed" +strip = "none" diff --git a/_cargo b/_cargo new file mode 100755 index 00000000..d89acd99 --- /dev/null +++ b/_cargo @@ -0,0 +1,33 @@ +#!/bin/bash +# Trampoline: ensure the correct Rust toolchain is used. +# On Windows, Chocolatey may install a GNU-based Rust that conflicts with +# the MSVC toolchain required by this project. This script: +# 1. Puts the rustup-managed toolchain bin FIRST in PATH +# 2. Sets CARGO_BUILD_TARGET to force MSVC on Windows +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" + +# Find rustup +RUSTUP="$(command -v rustup 2>/dev/null || true)" +if [ -z "$RUSTUP" ]; then + exec cargo "$@" +fi + +# Get the toolchain's bin directory and prepend it to PATH +TOOLCHAIN_BIN="$($RUSTUP run "$(grep channel "$SCRIPT_DIR/rust-toolchain.toml" 2>/dev/null | head -1 | sed 's/.*"\(.*\)"/\1/')" rustc --print sysroot 2>/dev/null)/bin" +if [ -d "$TOOLCHAIN_BIN" ]; then + export PATH="$TOOLCHAIN_BIN:$PATH" +fi + +# On Windows, force MSVC target to prevent GNU toolchain contamination +case "$(uname -s)" in + MINGW*|MSYS*|CYGWIN*) + ARCH="$(uname -m)" + if [ "$ARCH" = "x86_64" ]; then + export CARGO_BUILD_TARGET="x86_64-pc-windows-msvc" + else + export CARGO_BUILD_TARGET="aarch64-pc-windows-msvc" + fi + ;; +esac + +exec cargo "$@" diff --git a/_rustc b/_rustc new file mode 100755 index 00000000..cebfea3d --- /dev/null +++ b/_rustc @@ -0,0 +1,16 @@ +#!/bin/bash +# Trampoline: prefers the shared rustup proxy in ~/.cargo/bin when present. +if [ -n "${CARGO_HOME:-}" ]; then + CARGO_BIN="$CARGO_HOME/bin" +elif [ -n "${HOME:-}" ]; then + CARGO_BIN="$HOME/.cargo/bin" +elif [ -n "${USERPROFILE:-}" ]; then + CARGO_BIN="$USERPROFILE/.cargo/bin" +else + CARGO_BIN="" +fi + +if [ -n "$CARGO_BIN" ] && [ -d "$CARGO_BIN" ]; then + export PATH="$CARGO_BIN:$PATH" +fi +exec rustc "$@" diff --git a/_rustfmt b/_rustfmt new file mode 100755 index 00000000..1d9efda3 --- /dev/null +++ b/_rustfmt @@ -0,0 +1,16 @@ +#!/bin/bash +# Trampoline: prefers the shared rustup proxy in ~/.cargo/bin when present. +if [ -n "${CARGO_HOME:-}" ]; then + CARGO_BIN="$CARGO_HOME/bin" +elif [ -n "${HOME:-}" ]; then + CARGO_BIN="$HOME/.cargo/bin" +elif [ -n "${USERPROFILE:-}" ]; then + CARGO_BIN="$USERPROFILE/.cargo/bin" +else + CARGO_BIN="" +fi + +if [ -n "$CARGO_BIN" ] && [ -d "$CARGO_BIN" ]; then + export PATH="$CARGO_BIN:$PATH" +fi +exec rustfmt "$@" diff --git a/ci/env.py b/ci/env.py new file mode 100644 index 00000000..dfa0d105 --- /dev/null +++ b/ci/env.py @@ -0,0 +1,193 @@ +from __future__ import annotations + +import os +import platform +import shutil +import subprocess +import tomllib +from pathlib import Path + + +def cargo_home() -> Path: + if os.environ.get("CARGO_HOME"): + return Path(os.environ["CARGO_HOME"]).expanduser() + return Path.home() / ".cargo" + + +def cargo_bin() -> Path: + return cargo_home() / "bin" + + +def rustup_home() -> Path: + if os.environ.get("RUSTUP_HOME"): + return Path(os.environ["RUSTUP_HOME"]).expanduser() + return Path.home() / ".rustup" + + +def repo_root() -> Path: + return Path(__file__).resolve().parent.parent + + +def toolchain_file() -> Path: + return repo_root() / "rust-toolchain.toml" + + +def load_toolchain_channel() -> str: + with toolchain_file().open("rb") as handle: + data = tomllib.load(handle) + toolchain = data.get("toolchain") + if not isinstance(toolchain, dict): + raise RuntimeError(f"missing [toolchain] in {toolchain_file()}") + channel = toolchain.get("channel") + if not isinstance(channel, str) or not channel: + raise RuntimeError(f"missing toolchain.channel in {toolchain_file()}") + return channel + + +def host_target_triple() -> str: + system = platform.system() + machine = platform.machine().lower() + arch = { + "amd64": "x86_64", + "x86_64": "x86_64", + "arm64": "aarch64", + "aarch64": "aarch64", + }.get(machine) + if arch is None: + raise RuntimeError(f"unsupported architecture: {machine}") + if system == "Windows": + return f"{arch}-pc-windows-msvc" + if system == "Linux": + return f"{arch}-unknown-linux-gnu" + if system == "Darwin": + return f"{arch}-apple-darwin" + raise RuntimeError(f"unsupported platform: {system}") + + +def toolchain_name() -> str: + return f"{load_toolchain_channel()}-{host_target_triple()}" + + +def toolchain_bin() -> Path: + return rustup_home() / "toolchains" / toolchain_name() / "bin" + + +def _find_vswhere() -> Path | None: + candidates = [ + Path(r"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"), + Path(r"C:\Program Files\Microsoft Visual Studio\Installer\vswhere.exe"), + ] + for candidate in candidates: + if candidate.is_file(): + return candidate + return None + + +def _find_vsdevcmd() -> Path | None: + vswhere = _find_vswhere() + if vswhere is None: + return None + result = subprocess.run( + [ + str(vswhere), + "-latest", + "-products", + "*", + "-requires", + "Microsoft.VisualStudio.Component.VC.Tools.x86.x64", + "-property", + "installationPath", + ], + check=False, + capture_output=True, + text=True, + ) + installation_path = result.stdout.strip() + if not installation_path: + return None + candidate = Path(installation_path) / "Common7" / "Tools" / "VsDevCmd.bat" + if candidate.is_file(): + return candidate + return None + + +def _windows_build_env() -> dict[str, str]: + env = os.environ.copy() + toolchain_bin_dir = toolchain_bin() + if toolchain_bin_dir.is_dir(): + env["PATH"] = str(toolchain_bin_dir) + os.pathsep + env.get("PATH", "") + cargo_exe = toolchain_bin_dir / "cargo.exe" + rustc_exe = toolchain_bin_dir / "rustc.exe" + if cargo_exe.is_file(): + env["CARGO"] = str(cargo_exe) + if rustc_exe.is_file(): + env["RUSTC"] = str(rustc_exe) + env["RUSTUP_TOOLCHAIN"] = toolchain_name() + env["CARGO_BUILD_TARGET"] = host_target_triple() + + vsdevcmd = _find_vsdevcmd() + if vsdevcmd is None: + return env + + command = f'"{vsdevcmd}" -arch=x64 -host_arch=x64 >nul && set' + result = subprocess.run( + ["cmd", "/d", "/s", "/c", command], + check=False, + capture_output=True, + text=True, + env=env, + ) + if result.returncode != 0: + return env + for line in result.stdout.splitlines(): + if "=" not in line: + continue + key, value = line.split("=", 1) + env[key] = value + return env + + +def activate() -> None: + bin_dir = cargo_bin() + if not bin_dir.is_dir(): + return + + current_path = os.environ.get("PATH", "") + path_parts = current_path.split(os.pathsep) if current_path else [] + normalized_cargo_bin = os.path.normcase(os.path.normpath(str(bin_dir))) + normalized_parts = { + os.path.normcase(os.path.normpath(part)) for part in path_parts if part + } + if normalized_cargo_bin in normalized_parts: + return + os.environ["PATH"] = ( + str(bin_dir) if not current_path else str(bin_dir) + os.pathsep + current_path + ) + + +def _apply_sccache(env: dict[str, str]) -> dict[str, str]: + """Set RUSTC_WRAPPER=sccache when sccache is available.""" + if env.get("RUSTC_WRAPPER"): + return env + sccache = shutil.which("sccache", path=env.get("PATH")) + if sccache: + env["RUSTC_WRAPPER"] = sccache + return env + + +def clean_env() -> dict[str, str]: + activate() + env = os.environ.copy() + env.pop("VIRTUAL_ENV", None) + env.setdefault("PYTHONUTF8", "1") + if platform.system() == "Windows": + env = env | _windows_build_env() + env.pop("VIRTUAL_ENV", None) + env.setdefault("PYTHONUTF8", "1") + env.setdefault("RUSTUP_TOOLCHAIN", toolchain_name()) + env = _apply_sccache(env) + return env + + +def build_env() -> dict[str, str]: + return clean_env() diff --git a/ci/lint_python/keyboard_interrupt_checker.py b/ci/lint_python/keyboard_interrupt_checker.py index 6894cd78..c56b6913 100644 --- a/ci/lint_python/keyboard_interrupt_checker.py +++ b/ci/lint_python/keyboard_interrupt_checker.py @@ -22,7 +22,6 @@ from dataclasses import dataclass from pathlib import Path - _BROAD_EXCEPT_RE = re.compile( r"^\s*except\s*" r"(?:" @@ -175,7 +174,9 @@ def visit_Try(self, node: ast.Try) -> None: # noqa: N802 self.generic_visit(node) -_INTERRUPT_HANDLER_NAMES = frozenset({"handle_keyboard_interrupt", "notify_main_thread"}) +_INTERRUPT_HANDLER_NAMES = frozenset( + {"handle_keyboard_interrupt", "notify_main_thread"} +) def _find_interrupt_handler_calls(stmts: list[ast.stmt]) -> list[ast.Call]: diff --git a/crates/fastled-cli/Cargo.toml b/crates/fastled-cli/Cargo.toml new file mode 100644 index 00000000..83236a56 --- /dev/null +++ b/crates/fastled-cli/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "fastled-cli" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +description = "FastLED WASM compilation CLI" + +[[bin]] +name = "fastled" +path = "src/main.rs" + +[dependencies] +clap = { workspace = true } diff --git a/crates/fastled-cli/src/main.rs b/crates/fastled-cli/src/main.rs new file mode 100644 index 00000000..da4e37c8 --- /dev/null +++ b/crates/fastled-cli/src/main.rs @@ -0,0 +1,14 @@ +use clap::Parser; + +#[derive(Parser)] +#[command(name = "fastled", version, about = "FastLED WASM compilation CLI")] +struct Cli { + /// Print version and exit + #[arg(long)] + version_info: bool, +} + +fn main() { + let _cli = Cli::parse(); + println!("fastled-cli {}", env!("CARGO_PKG_VERSION")); +} diff --git a/crates/fastled-py/Cargo.toml b/crates/fastled-py/Cargo.toml new file mode 100644 index 00000000..8939db8d --- /dev/null +++ b/crates/fastled-py/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "fastled-py" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +description = "PyO3 bridge for FastLED WASM CLI" + +[lib] +name = "_native" +crate-type = ["cdylib", "lib"] + +[dependencies] +pyo3 = { workspace = true } diff --git a/crates/fastled-py/src/lib.rs b/crates/fastled-py/src/lib.rs new file mode 100644 index 00000000..ad139798 --- /dev/null +++ b/crates/fastled-py/src/lib.rs @@ -0,0 +1,14 @@ +use pyo3::prelude::*; + +/// Return the native module version. +#[pyfunction] +fn version() -> &'static str { + env!("CARGO_PKG_VERSION") +} + +/// FastLED native extension module. +#[pymodule] +fn _native(m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_function(wrap_pyfunction!(version, m)?)?; + Ok(()) +} diff --git a/install b/install index d47759ad..3564c48e 100755 --- a/install +++ b/install @@ -19,7 +19,7 @@ fi uv venv --python 3.11 --seed uv pip install -e . -uv run pip install -r requirements.testing.txt +uv pip install -r requirements.testing.txt # If activate exists, delete it @@ -33,3 +33,25 @@ if [[ "$OSTYPE" == "msys" ]]; then else ln -s .venv/bin/activate ./activate fi + +# ── Rust toolchain bootstrap ──────────────────────────────────────────────── +# Install rustup if not present, then install the pinned toolchain. + +CARGO_BIN="${CARGO_HOME:-$HOME/.cargo}/bin" + +# Prefer rustup-managed cargo over system packages (e.g. Chocolatey GNU Rust) +export PATH="$CARGO_BIN:$PATH" + +if ! command -v rustup &> /dev/null; then + echo "Installing rustup..." + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --no-modify-path + export PATH="$CARGO_BIN:$PATH" +fi + +# Parse channel from rust-toolchain.toml +if [[ -f rust-toolchain.toml ]]; then + CHANNEL=$(grep 'channel' rust-toolchain.toml | head -1 | sed 's/.*= *"\(.*\)"/\1/') + echo "Installing Rust toolchain: $CHANNEL" + rustup toolchain install "$CHANNEL" --profile minimal -c clippy -c rustfmt --no-self-update + cargo check --workspace +fi diff --git a/lint b/lint index d790dddf..85b0658e 100755 --- a/lint +++ b/lint @@ -1,22 +1,26 @@ #!/bin/bash set -e -# if venv doesn't exist, invoke ./install -if [ ! -d ".venv" ]; then - echo "No venv present, so installing..." - ./install -fi +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" -. ./activate +# Rust linting (skip if cargo not available) +if command -v cargo &> /dev/null; then + echo Running cargo fmt check + "$SCRIPT_DIR/_cargo" fmt --all --check + echo Running cargo clippy + "$SCRIPT_DIR/_cargo" clippy --workspace --all-targets -- -D warnings +else + echo "WARNING: cargo not found, skipping Rust linting" +fi echo Running ruff src compiler -ruff check --fix src compiler +uv run ruff check --fix src compiler echo Running ruff tests compiler -ruff check --fix tests compiler +uv run ruff check --fix tests compiler echo Running black src tests compiler -black src tests compiler +uv run black src tests compiler echo Running isort src tests compiler -isort --profile black src tests compiler +uv run isort --profile black src tests compiler echo Running pyright src tests compiler uv run pyright src compiler tests echo Running keyboard interrupt checker src tests compiler diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 00000000..b1dd491c --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "1.94.1" +profile = "minimal" +components = ["clippy", "rustfmt"] diff --git a/test b/test index d28265a2..fdf0e786 100755 --- a/test +++ b/test @@ -2,11 +2,13 @@ set -euo pipefail -if [ ! -d ".venv" ]; then - echo "No venv present, so installing..." - ./install -fi +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" -. ./activate +# Rust tests (skip if cargo not available) +if command -v cargo &> /dev/null; then + "$SCRIPT_DIR/_cargo" test --workspace +else + echo "WARNING: cargo not found, skipping Rust tests" +fi -pytest -x -v -s --durations=0 tests/unit "$@" +uv run pytest -x -v -s --durations=0 tests/unit "$@"