Files
GregandClaude Sonnet 4.6 15d0dfddfc Implement Phases 1–3: IR, parser, Lean + C codegen
Adds the full musubu pipeline from C header to generated .lean and .c
glue files, validated against SDL3/SDL_init.h.

- IR types (CType, Decl, BindingIR) in Musubu/IR.lean
- C type-string resolver in Musubu/TypeMap.lean (handles SDL3 typedefs)
- 4-pass libclang AST walker in Musubu/Parser.lean
- Lean declaration codegen in Musubu/Codegen/Lean.lean
- C glue codegen in Musubu/Codegen/C.lean (with heuristic finalizer
  detection and smart include-path stripping)
- lean-libclang updated to add getCursorNumArguments, getCursorArgument,
  getCursorResultTypeSpelling, and getCursorEnumConstantValue APIs
- CLI flags: --dump-ir, --out-lean, --out-c, --prefix, -I
- justfile recipes: build, run, fetch-sdl3, dump-ir, gen, compile-glue
- docs/architecture.md documents the full pipeline and phased roadmap

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 14:48:49 -07:00

2.9 KiB
Raw Permalink Blame History

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

Project Overview

Musubu is a Lean 4 library for generating bindings to C code, currently in early development (v0.1.0). It is heavily inspired by the Rust https://github.com/rust-lang/rust-bindgen library. The lean-libclang library (https://github.com/neunenak/lean-libclang) is used for C parsing in Lean. The intended use case is that musubu + associated configuration + C header files is a build-time dependency to another Lean project.

Plan of Attack

https://github.com/neunenak/lean-sdl3 is an incomplete and manually-implemented project of some of the SDL3 C bindings to Lean. Musubu should be able to create something comparable to these bindings automatically as part of the build step of a lean project.

Build Commands

This project uses Lake, Lean's build system:

lake build          # Build all default targets (library + executable)
lake exe musubu     # Build and run the executable
lake clean          # Remove all build artifacts
lake serve          # Start the Lean language server (LSP)
lake lean <file>    # Elaborate a single Lean file
lake update         # Update dependencies

No test or lint drivers are configured yet (lake check-test and lake check-lint will report this).

This project uses a justfile, and prefers to encode any commonly-used build command in the justfile, with documentation comments explaining how it is used.

Architecture

The pipeline is: C header → libclang AST (lean-libclang) → musubu IR → .lean + .c output files

Key modules:

  • Musubu/IR.lean — CType, Decl, BindingIR internal representation
  • Musubu/TypeMap.lean — C type spelling strings → CType mapping
  • Musubu/Parser.lean — lean-libclang cursor walker that builds a BindingIR
  • Musubu/Config.lean — CLI configuration structure
  • Main.lean — CLI argument parsing + calls parser

The executable currently implements --dump-ir (Phase 1). Code generation (Phases 2–3) is not yet implemented.

SDL3 headers are fetched into vendor/SDL/ via just fetch-sdl3 (gitignored).

lean-libclang lives at /home/greg/unsynced-code/lean-libclang and is referenced as a local path dependency. Changes go there first, then musubu is rebuilt.

Runtime Note

The musubu binary requires libclang at runtime. On this system the Lean/Nix toolchain doesn't resolve /usr/lib via rpath automatically; use LD_LIBRARY_PATH=/usr/lib or the justfile commands which set it.

Key Files

  • lakefile.toml — build config; includes moreLinkArgs for -lclang
  • lean-toolchain — pins Lean version (currently v4.29.1)
  • docs/architecture.md — full pipeline design and phased roadmap
  • justfile — preferred entry point for common commands (build, run, fetch-sdl3, dump-ir)

Version Control

The repo uses both Git and Jujutsu (jj). The .lake/ build directory and vendor/ are gitignored.