Add thin FFI bindings to libclang with Nix flake

Phase 1 implementation: C shim wrapping libclang's cursor-based AST API,
Lean 4 opaque types and extern declarations, and a Nix flake for hermetic
builds. Each Cursor holds a refcounted reference to its TranslationUnit
to prevent use-after-free from Lean's GC.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Greg ShuflinandClaude Opus 4.6 committed 2026-03-18 09:37:43 -07:00
1 parent 02cc1ce363
commit b5eab38a20
10 files changed
+735 -13

No files matched your search

+1
View File
@@ -0,0 +1 @@
use flake
+231 -1
View File
@@ -1 +1,231 @@
def hello := "world"
namespace Clang
-- ---------------------------------------------------------------------------
-- Opaque types
-- ---------------------------------------------------------------------------
/-- An opaque handle to a libclang index (a set of translation units). -/
opaque Index.Nonempty : NonemptyType
def Index : Type := Index.Nonempty.type
instance : Nonempty Index := Index.Nonempty.property
/-- An opaque handle to a parsed translation unit. -/
opaque TranslationUnit.Nonempty : NonemptyType
def TranslationUnit : Type := TranslationUnit.Nonempty.type
instance : Nonempty TranslationUnit := TranslationUnit.Nonempty.property
/-- An opaque handle to a cursor pointing into the AST. -/
opaque Cursor.Nonempty : NonemptyType
def Cursor : Type := Cursor.Nonempty.type
instance : Nonempty Cursor := Cursor.Nonempty.property
-- ---------------------------------------------------------------------------
-- CursorKind (subset of CXCursorKind + catch-all)
-- ---------------------------------------------------------------------------
/-- A subset of `CXCursorKind` covering the most common C declaration,
reference, expression, and statement kinds. -/
inductive CursorKind where
-- Declarations
| unexposedDecl -- 1
| structDecl -- 2
| unionDecl -- 3
| classDecl -- 4
| enumDecl -- 5
| fieldDecl -- 6
| enumConstantDecl -- 7
| functionDecl -- 8
| varDecl -- 9
| parmDecl -- 10
| typedefDecl -- 20
-- References
| typeRef -- 43
| cxxBaseSpecifier -- 44
| templateRef -- 45
| namespaceRef -- 46
| memberRef -- 47
| labelRef -- 48
| overloadedDeclRef -- 49
| variableRef -- 50
-- Expressions
| unexposedExpr -- 100
| declRefExpr -- 101
| memberRefExpr -- 102
| callExpr -- 103
| integerLiteral -- 106
| floatingLiteral -- 107
| stringLiteral -- 109
| charLiteral -- 110
| parenExpr -- 111
| unaryOperator -- 112
| arraySubscriptExpr -- 113
| binaryOperator -- 114
| compoundAssignOp -- 115
| conditionalOperator -- 116
| cStyleCastExpr -- 117
| initListExpr -- 119
-- Statements
| unexposedStmt -- 200
| compoundStmt -- 202
| caseStmt -- 203
| defaultStmt -- 204
| ifStmt -- 205
| switchStmt -- 206
| whileStmt -- 207
| doStmt -- 208
| forStmt -- 209
| gotoStmt -- 210
| continueStmt -- 212
| breakStmt -- 213
| returnStmt -- 214
| declStmt -- 231
-- Translation unit
| translationUnit -- 350 (CXCursor_TranslationUnit = 300)
-- Other
| other (raw : UInt32)
deriving Repr, BEq, Inhabited
namespace CursorKind
def ofRaw (n : UInt32) : CursorKind :=
match n with
| 1 => .unexposedDecl
| 2 => .structDecl
| 3 => .unionDecl
| 4 => .classDecl
| 5 => .enumDecl
| 6 => .fieldDecl
| 7 => .enumConstantDecl
| 8 => .functionDecl
| 9 => .varDecl
| 10 => .parmDecl
| 20 => .typedefDecl
| 43 => .typeRef
| 44 => .cxxBaseSpecifier
| 45 => .templateRef
| 46 => .namespaceRef
| 47 => .memberRef
| 48 => .labelRef
| 49 => .overloadedDeclRef
| 50 => .variableRef
| 100 => .unexposedExpr
| 101 => .declRefExpr
| 102 => .memberRefExpr
| 103 => .callExpr
| 106 => .integerLiteral
| 107 => .floatingLiteral
| 109 => .stringLiteral
| 110 => .charLiteral
| 111 => .parenExpr
| 112 => .unaryOperator
| 113 => .arraySubscriptExpr
| 114 => .binaryOperator
| 115 => .compoundAssignOp
| 116 => .conditionalOperator
| 117 => .cStyleCastExpr
| 119 => .initListExpr
| 200 => .unexposedStmt
| 202 => .compoundStmt
| 203 => .caseStmt
| 204 => .defaultStmt
| 205 => .ifStmt
| 206 => .switchStmt
| 207 => .whileStmt
| 208 => .doStmt
| 209 => .forStmt
| 210 => .gotoStmt
| 212 => .continueStmt
| 213 => .breakStmt
| 214 => .returnStmt
| 231 => .declStmt
| 350 => .translationUnit
| n => .other n
end CursorKind
-- ---------------------------------------------------------------------------
-- Source location
-- ---------------------------------------------------------------------------
/-- A decoded source location: file path, line, and column. -/
structure SourceLocation where
file : String
line : UInt32
column : UInt32
deriving Repr, BEq, Inhabited
-- ---------------------------------------------------------------------------
-- FFI declarations
-- ---------------------------------------------------------------------------
/-- Create a new index. `excludeDeclsFromPCH` and `displayDiagnostics`
correspond to the libclang parameters of the same name. -/
@[extern "lean_clang_createIndex"]
opaque createIndex (excludeDeclsFromPCH : Bool)
(displayDiagnostics : Bool) : IO Index
/-- Parse a source file into a translation unit.
`args` are extra command-line arguments passed to clang (e.g. `#["-std=c11"]`). -/
@[extern "lean_clang_parseTranslationUnit"]
opaque parseTranslationUnit (idx : @& Index)
(filename : @& String)
(args : @& Array String) : IO TranslationUnit
/-- Get the root cursor of a translation unit. -/
@[extern "lean_clang_getTranslationUnitCursor"]
opaque getTranslationUnitCursor (tu : @& TranslationUnit) : IO Cursor
/-- Get the kind of a cursor (as a raw UInt32 from CXCursorKind). -/
@[extern "lean_clang_getCursorKind"]
opaque getCursorKindRaw (cursor : @& Cursor) : IO UInt32
/-- Get the kind of a cursor. -/
def getCursorKind (cursor : Cursor) : IO CursorKind := do
let raw ← getCursorKindRaw cursor
return CursorKind.ofRaw raw
/-- Get the spelling (name) of a cursor. -/
@[extern "lean_clang_getCursorSpelling"]
opaque getCursorSpelling (cursor : @& Cursor) : IO String
/-- Get the spelling of the cursor's type (e.g. "int", "struct foo"). -/
@[extern "lean_clang_getCursorTypeSpelling"]
opaque getCursorTypeSpelling (cursor : @& Cursor) : IO String
/-- Get the raw CXTypeKind of the cursor's type. -/
@[extern "lean_clang_getCursorTypeKind"]
opaque getCursorTypeKindRaw (cursor : @& Cursor) : IO UInt32
@[extern "lean_clang_getCursorFile"]
private opaque getCursorFile (cursor : @& Cursor) : IO String
@[extern "lean_clang_getCursorLine"]
private opaque getCursorLine (cursor : @& Cursor) : IO UInt32
@[extern "lean_clang_getCursorColumn"]
private opaque getCursorColumn (cursor : @& Cursor) : IO UInt32
/-- Get the source location of a cursor. -/
def getCursorLocation (cursor : Cursor) : IO SourceLocation := do
let file ← getCursorFile cursor
let line ← getCursorLine cursor
let column ← getCursorColumn cursor
return { file, line, column }
/-- Get the immediate children of a cursor. -/
@[extern "lean_clang_getChildren"]
opaque getChildren (cursor : @& Cursor) : IO (Array Cursor)
/-- Check if a cursor is null. -/
@[extern "lean_clang_cursorIsNull"]
opaque cursorIsNull (cursor : @& Cursor) : IO Bool
/-- Dispose of a translation unit (can also just let GC handle it). -/
@[extern "lean_clang_disposeTranslationUnit"]
opaque disposeTranslationUnit (tu : TranslationUnit) : IO Unit
/-- Dispose of an index (can also just let GC handle it). -/
@[extern "lean_clang_disposeIndex"]
opaque disposeIndex (idx : Index) : IO Unit
end Clang
+22 -2
View File
@@ -1,4 +1,24 @@
import LeanLibclang
def main : IO Unit :=
IO.println s!"Hello, {hello}!"
/-- Print an indented tree of the AST rooted at `cursor`. -/
partial def printAST (cursor : Clang.Cursor) (indent : Nat := 0) : IO Unit := do
let kind ← Clang.getCursorKind cursor
let spelling ← Clang.getCursorSpelling cursor
let typeSpelling ← Clang.getCursorTypeSpelling cursor
let loc ← Clang.getCursorLocation cursor
let pad := "".pushn ' ' (indent * 2)
IO.println s!"{pad}{repr kind} \"{spelling}\" type=\"{typeSpelling}\" {loc.file}:{loc.line}:{loc.column}"
let children ← Clang.getChildren cursor
for child in children do
printAST child (indent + 1)
def main (args : List String) : IO Unit := do
match args with
| [] =>
IO.eprintln "Usage: lean-libclang <file.c> [-- <clang-args>...]"
| filename :: rest =>
let clangArgs := rest.toArray
let idx ← Clang.createIndex false true
let tu ← Clang.parseTranslationUnit idx filename clangArgs
let cursor ← Clang.getTranslationUnitCursor tu
printAST cursor
+291
View File
@@ -0,0 +1,291 @@
#include <lean/lean.h>
#include <clang-c/Index.h>
#include <string.h>
#include <stdlib.h>
// ---------------------------------------------------------------------------
// External object classes (so Lean's GC can call our finalizers)
// ---------------------------------------------------------------------------
static void cxindex_finalizer(void *ptr) {
clang_disposeIndex((CXIndex)ptr);
}
static void cxtu_finalizer(void *ptr) {
clang_disposeTranslationUnit((CXTranslationUnit)ptr);
}
// Cursor wrapper: holds the CXCursor value plus a Lean reference to the TU
// so that the TU stays alive as long as any cursor exists.
typedef struct {
CXCursor cursor;
lean_object *tu_ref; // borrowed Lean TU object, ref-counted
} CursorData;
static void cxcursor_finalizer(void *ptr) {
CursorData *cd = (CursorData *)ptr;
lean_dec(cd->tu_ref);
free(cd);
}
static lean_external_class *g_cxindex_class = NULL;
static lean_external_class *g_cxtu_class = NULL;
static lean_external_class *g_cxcursor_class = NULL;
static lean_external_class *get_cxindex_class(void) {
if (g_cxindex_class == NULL) {
g_cxindex_class = lean_register_external_class(cxindex_finalizer, NULL);
}
return g_cxindex_class;
}
static lean_external_class *get_cxtu_class(void) {
if (g_cxtu_class == NULL) {
g_cxtu_class = lean_register_external_class(cxtu_finalizer, NULL);
}
return g_cxtu_class;
}
static lean_external_class *get_cxcursor_class(void) {
if (g_cxcursor_class == NULL) {
g_cxcursor_class = lean_register_external_class(cxcursor_finalizer, NULL);
}
return g_cxcursor_class;
}
// ---------------------------------------------------------------------------
// Helpers: box/unbox CXCursor
// ---------------------------------------------------------------------------
// Create a Lean Cursor object that holds both the CXCursor value and
// a reference to the TU Lean object (to keep it alive).
static lean_obj_res cursor_to_lean(CXCursor cursor, lean_object *tu_ref) {
CursorData *cd = malloc(sizeof(CursorData));
cd->cursor = cursor;
cd->tu_ref = tu_ref;
lean_inc(tu_ref); // cursor now holds a reference
return lean_alloc_external(get_cxcursor_class(), cd);
}
static CXCursor lean_to_cursor(lean_obj_arg obj) {
CursorData *cd = (CursorData *)lean_get_external_data(obj);
return cd->cursor;
}
static lean_object *lean_cursor_tu_ref(lean_obj_arg obj) {
CursorData *cd = (CursorData *)lean_get_external_data(obj);
return cd->tu_ref;
}
// ---------------------------------------------------------------------------
// Helper: CXString -> Lean String (disposes the CXString)
// ---------------------------------------------------------------------------
static lean_obj_res cxstring_to_lean(CXString s) {
const char *cstr = clang_getCString(s);
lean_obj_res result = lean_mk_string(cstr ? cstr : "");
clang_disposeString(s);
return result;
}
// ---------------------------------------------------------------------------
// Index lifecycle
// ---------------------------------------------------------------------------
lean_obj_res lean_clang_createIndex(uint8_t excludeDeclsFromPCH,
uint8_t displayDiagnostics,
lean_obj_arg world) {
CXIndex idx = clang_createIndex(excludeDeclsFromPCH, displayDiagnostics);
if (!idx) {
return lean_io_result_mk_error(
lean_mk_io_user_error(lean_mk_string("clang_createIndex failed")));
}
lean_obj_res obj = lean_alloc_external(get_cxindex_class(), idx);
return lean_io_result_mk_ok(obj);
}
lean_obj_res lean_clang_disposeIndex(lean_obj_arg idx, lean_obj_arg world) {
lean_dec_ref(idx);
return lean_io_result_mk_ok(lean_box(0));
}
// ---------------------------------------------------------------------------
// Translation unit
// ---------------------------------------------------------------------------
lean_obj_res lean_clang_parseTranslationUnit(lean_obj_arg idx,
lean_obj_arg filename,
lean_obj_arg args,
lean_obj_arg world) {
CXIndex cxIdx = (CXIndex)lean_get_external_data(idx);
const char *fname = lean_string_cstr(filename);
// Convert Lean Array String to const char**
size_t nargs = lean_array_size(args);
const char **cargs = NULL;
if (nargs > 0) {
cargs = malloc(nargs * sizeof(const char *));
for (size_t i = 0; i < nargs; i++) {
cargs[i] = lean_string_cstr(lean_array_get_core(args, i));
}
}
CXTranslationUnit tu = clang_parseTranslationUnit(
cxIdx, fname, cargs, (int)nargs, NULL, 0,
CXTranslationUnit_None);
if (cargs) free(cargs);
if (!tu) {
return lean_io_result_mk_error(
lean_mk_io_user_error(
lean_mk_string("clang_parseTranslationUnit failed")));
}
lean_obj_res obj = lean_alloc_external(get_cxtu_class(), tu);
return lean_io_result_mk_ok(obj);
}
lean_obj_res lean_clang_disposeTranslationUnit(lean_obj_arg tu,
lean_obj_arg world) {
lean_dec_ref(tu);
return lean_io_result_mk_ok(lean_box(0));
}
// ---------------------------------------------------------------------------
// Cursor: get TU cursor
// ---------------------------------------------------------------------------
lean_obj_res lean_clang_getTranslationUnitCursor(lean_obj_arg tu,
lean_obj_arg world) {
CXTranslationUnit cxTU =
(CXTranslationUnit)lean_get_external_data(tu);
CXCursor cursor = clang_getTranslationUnitCursor(cxTU);
// tu is borrowed (@&), so cursor_to_lean will lean_inc it
return lean_io_result_mk_ok(cursor_to_lean(cursor, tu));
}
// ---------------------------------------------------------------------------
// Cursor queries
// ---------------------------------------------------------------------------
lean_obj_res lean_clang_getCursorKind(lean_obj_arg cursor, lean_obj_arg world) {
CXCursor c = lean_to_cursor(cursor);
enum CXCursorKind kind = clang_getCursorKind(c);
return lean_io_result_mk_ok(lean_box((unsigned)kind));
}
lean_obj_res lean_clang_getCursorSpelling(lean_obj_arg cursor,
lean_obj_arg world) {
CXCursor c = lean_to_cursor(cursor);
CXString spelling = clang_getCursorSpelling(c);
return lean_io_result_mk_ok(cxstring_to_lean(spelling));
}
lean_obj_res lean_clang_getCursorTypeSpelling(lean_obj_arg cursor,
lean_obj_arg world) {
CXCursor c = lean_to_cursor(cursor);
CXType ty = clang_getCursorType(c);
CXString spelling = clang_getTypeSpelling(ty);
return lean_io_result_mk_ok(cxstring_to_lean(spelling));
}
lean_obj_res lean_clang_getCursorTypeKind(lean_obj_arg cursor,
lean_obj_arg world) {
CXCursor c = lean_to_cursor(cursor);
CXType ty = clang_getCursorType(c);
return lean_io_result_mk_ok(lean_box((unsigned)ty.kind));
}
// ---------------------------------------------------------------------------
// Source location
// ---------------------------------------------------------------------------
lean_obj_res lean_clang_getCursorFile(lean_obj_arg cursor,
lean_obj_arg world) {
CXCursor c = lean_to_cursor(cursor);
CXSourceLocation loc = clang_getCursorLocation(c);
CXFile file;
clang_getSpellingLocation(loc, &file, NULL, NULL, NULL);
CXString fileName = clang_getFileName(file);
return lean_io_result_mk_ok(cxstring_to_lean(fileName));
}
lean_obj_res lean_clang_getCursorLine(lean_obj_arg cursor, lean_obj_arg world) {
CXCursor c = lean_to_cursor(cursor);
CXSourceLocation loc = clang_getCursorLocation(c);
unsigned line;
clang_getSpellingLocation(loc, NULL, &line, NULL, NULL);
return lean_io_result_mk_ok(lean_box((size_t)line));
}
lean_obj_res lean_clang_getCursorColumn(lean_obj_arg cursor, lean_obj_arg world) {
CXCursor c = lean_to_cursor(cursor);
CXSourceLocation loc = clang_getCursorLocation(c);
unsigned column;
clang_getSpellingLocation(loc, NULL, NULL, &column, NULL);
return lean_io_result_mk_ok(lean_box((size_t)column));
}
// ---------------------------------------------------------------------------
// Child visitor: collects immediate children into a Lean Array
// ---------------------------------------------------------------------------
typedef struct {
CXCursor *buf;
size_t len;
size_t cap;
} CursorBuf;
static enum CXChildVisitResult collect_children_visitor(CXCursor cursor,
CXCursor parent,
CXClientData data) {
CursorBuf *buf = (CursorBuf *)data;
if (buf->len == buf->cap) {
buf->cap = buf->cap ? buf->cap * 2 : 16;
buf->buf = realloc(buf->buf, buf->cap * sizeof(CXCursor));
}
buf->buf[buf->len++] = cursor;
return CXChildVisit_Continue;
}
lean_obj_res lean_clang_getChildren(lean_obj_arg cursor, lean_obj_arg world) {
CXCursor c = lean_to_cursor(cursor);
lean_object *tu_ref = lean_cursor_tu_ref(cursor);
CursorBuf buf = {NULL, 0, 0};
clang_visitChildren(c, collect_children_visitor, &buf);
lean_obj_res arr = lean_alloc_array(buf.len, buf.len);
lean_object **data = lean_array_cptr(arr);
for (size_t i = 0; i < buf.len; i++) {
data[i] = cursor_to_lean(buf.buf[i], tu_ref);
}
free(buf.buf);
return lean_io_result_mk_ok(arr);
}
// ---------------------------------------------------------------------------
// Cursor equality and hashing (libclang provides these)
// ---------------------------------------------------------------------------
uint8_t lean_clang_equalCursors(lean_obj_arg a, lean_obj_arg b) {
CXCursor ca = lean_to_cursor(a);
CXCursor cb = lean_to_cursor(b);
return clang_equalCursors(ca, cb) != 0;
}
uint32_t lean_clang_hashCursor(lean_obj_arg cursor) {
CXCursor c = lean_to_cursor(cursor);
return clang_hashCursor(c);
}
// ---------------------------------------------------------------------------
// Null cursor check
// ---------------------------------------------------------------------------
lean_obj_res lean_clang_cursorIsNull(lean_obj_arg cursor, lean_obj_arg world) {
CXCursor c = lean_to_cursor(cursor);
return lean_io_result_mk_ok(lean_box(clang_Cursor_isNull(c) != 0));
}
+39
View File
@@ -0,0 +1,39 @@
# lean-libclang: Implementation Plan
Lean 4 bindings to libclang for parsing C source files and inspecting their ASTs.
## Architecture
**Two-layer approach:**
1. **Thin FFI bindings** — 1:1 wrapping of libclang's C API. Opaque pointer types, extern functions, and a small C shim to handle callback-based traversal. This layer is useful on its own for imperative AST exploration.
2. **Pure Lean AST types** (future) — Inductive types (`CDecl`, `CExpr`, `CType`, etc.) built by traversing via the thin layer. Consumers work with native Lean data structures instead of opaque pointers.
## Phase 1: FFI Foundation
- C shim layer (`c/clang_shim.c`) to bridge libclang's visitor-callback pattern. Lean FFI cannot directly pass Lean closures as C function pointers, so the shim collects child cursors into a buffer that Lean can consume as an array.
- `lakefile.toml` configuration: `[[c_src]]` for the shim, link args for `-lclang`.
- Opaque Lean types wrapping `CXIndex`, `CXTranslationUnit`, `CXCursor`.
- Lifecycle functions: `createIndex`, `parseTranslationUnit`, `disposeTranslationUnit`, `disposeIndex`.
- Cursor traversal: `getTranslationUnitCursor`, `getChildren` (returns `Array Cursor`).
- Cursor queries: `getCursorKind`, `getCursorSpelling`, `getCursorType`, `getCursorLocation`.
- `CursorKind` enum: subset of `CXCursorKind` + `other` fallback.
## Phase 2: Broader API Coverage
- More cursor/type query functions as needed.
- Source range and location utilities.
- Diagnostic surfacing.
## Phase 3: Pure Lean AST
- Define inductive types for C declarations, expressions, types, statements.
- Traversal function that converts the libclang AST into these Lean types.
- This is the primary consumer-facing API.
## Phase 4: Polish
- Resource safety (bracket patterns for index/TU lifecycle).
- Error handling and diagnostics as Lean-native errors.
- Test suite: parse known C files, assert on AST structure.
Generated
+83
View File
@@ -0,0 +1,83 @@
{
"nodes": {
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1765835352,
"narHash": "sha256-XswHlK/Qtjasvhd1nOa1e8MgZ8GS//jBoTqWtrS1Giw=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "a34fae9c08a15ad73f295041fec82323541400a9",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"lean4-nix": {
"inputs": {
"flake-parts": "flake-parts",
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1771875971,
"narHash": "sha256-D3PN4o8RtyHEjlAtsLa6M9xRjIwtMUk4pIkfsNSMAvQ=",
"owner": "lenianiva",
"repo": "lean4-nix",
"rev": "faebfa2e0d7093fea3ffaa493b316bf3449c1dbf",
"type": "github"
},
"original": {
"owner": "lenianiva",
"repo": "lean4-nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1765779637,
"narHash": "sha256-KJ2wa/BLSrTqDjbfyNx70ov/HdgNBCBBSQP3BIzKnv4=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "1306659b587dc277866c7b69eb97e5f07864d8c4",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1765674936,
"narHash": "sha256-k00uTP4JNfmejrCLJOwdObYC9jHRrr/5M/a/8L2EIdo=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "2075416fcb47225d9b68ac469a5c4801a9c4dd85",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"root": {
"inputs": {
"lean4-nix": "lean4-nix",
"nixpkgs": [
"lean4-nix",
"nixpkgs"
]
}
}
},
"root": "root",
"version": 7
}
+37
View File
@@ -0,0 +1,37 @@
{
description = "Lean 4 bindings to libclang";
inputs = {
lean4-nix.url = "github:lenianiva/lean4-nix";
nixpkgs.follows = "lean4-nix/nixpkgs";
};
outputs = { self, nixpkgs, lean4-nix }:
let
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems f;
in {
devShells = forAllSystems (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ (lean4-nix.readToolchainFile ./lean-toolchain) ];
};
libclang = pkgs.llvmPackages.libclang;
in {
default = pkgs.mkShell {
nativeBuildInputs = [
pkgs.lean.lean-all
pkgs.llvmPackages.clang
];
buildInputs = [
libclang.dev
libclang.lib
];
env = {
LEAN_CC = "${pkgs.llvmPackages.clang}/bin/clang";
};
};
});
};
}
+5
View File
@@ -0,0 +1,5 @@
{"version": "1.1.0",
"packagesDir": ".lake/packages",
"packages": [],
"name": "«lean-libclang»",
"lakeDir": ".lake"}
+26
View File
@@ -0,0 +1,26 @@
import Lake
open System Lake DSL
package «lean-libclang»
-- Compile the C shim
target «clang_shim.o» pkg : FilePath := do
let oFile := pkg.buildDir / "c" / "clang_shim.o"
let srcJob ← inputTextFile <| pkg.dir / "c" / "clang_shim.c"
let weakArgs := #["-I", (← getLeanIncludeDir).toString]
buildO oFile srcJob weakArgs #["-fPIC"] "cc" getLeanTrace
-- Package the C shim into a static library
extern_lib libclangshim pkg := do
let shimO ← «clang_shim.o».fetch
let name := nameToStaticLib "clangshim"
buildStaticLib (pkg.staticLibDir / name) #[shimO]
@[default_target]
lean_lib LeanLibclang where
moreLinkArgs := #["-lclang"]
@[default_target]
lean_exe «lean-libclang» where
root := `Main
moreLinkArgs := #["-lclang"]
-10
View File
@@ -1,10 +0,0 @@
name = "lean-libclang"
version = "0.1.0"
defaultTargets = ["lean-libclang"]
[[lean_lib]]
name = "LeanLibclang"
[[lean_exe]]
name = "lean-libclang"
root = "Main"