plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


plum-cli/build.rs
//! Precompiles the `tree-sitter-plum` grammar into a native shared library at
//! `plum`-build time (using whatever C compiler is already on the machine —
//! the same one Cargo itself needs to build `tree-sitter-plum`'s own
//! `scanner.c`/`parser.c` for in-process parsing), so `plum editor helix` can
//! just copy the finished `.so` into Helix's `runtime/grammars/` directory
//! instead of shelling out to `hx --grammar build` (which requires `hx` on
//! PATH, a working C compiler on the *installing* machine again, and a
//! `languages.toml` grammar `source.path` that still resolves).
//!
//! If the compiler invocation fails (no C compiler found), this falls back to
//! writing an empty placeholder rather than failing the whole `plum-cli`
//! build — `editor::installHelix` detects the empty payload and falls back to
//! the old `hx --grammar build` codepath at install time instead.

use std::env;
use std::path::PathBuf;
use std::process::Command;

fn main() {
    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    let src_dir = manifest_dir.join("../plum-tooling/tree-sitter-plum/src");
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    let so_path = out_dir.join("plum-grammar.so");

    println!("cargo:rerun-if-changed={}", src_dir.join("parser.c").display());
    println!("cargo:rerun-if-changed={}", src_dir.join("scanner.c").display());

    let cc = env::var("CC").unwrap_or_else(|_| "cc".to_string());
    let status = Command::new(&cc)
        .arg("-shared")
        .arg("-fPIC")
        .arg("-O2")
        .arg("-I")
        .arg(&src_dir)
        .arg("-o")
        .arg(&so_path)
        .arg(src_dir.join("parser.c"))
        .arg(src_dir.join("scanner.c"))
        .status();

    match status {
        Ok(s) if s.success() => {
            println!("cargo:rustc-env=PLUM_GRAMMAR_SO={}", so_path.display());
        }
        other => {
            std::fs::write(&so_path, []).expect("failed to write placeholder grammar .so");
            println!("cargo:rustc-env=PLUM_GRAMMAR_SO={}", so_path.display());
            match other {
                Ok(s) => println!(
                    "cargo:warning=`{cc}` exited with {s} while precompiling the Plum tree-sitter grammar; `plum editor helix` will fall back to `hx --grammar build`"
                ),
                Err(e) => println!(
                    "cargo:warning=could not run `{cc}` to precompile the Plum tree-sitter grammar ({e}); `plum editor helix` will fall back to `hx --grammar build`"
                ),
            }
        }
    }
}