-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ts
57 lines (43 loc) · 1.49 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { ClangTypeInfoCache, clangGetAstJson, CodeGen, parseClangAst, clangClean, addIncludeDir } from "./src";
import { LogLevel, setLogLevel } from "./src/log";
import path from "path";
const HEADER_PATH = "./test.h";
const TYPE_CACHE_PATH = "./test-bindings_cache";
setLogLevel(LogLevel.verbose);
// add include dirs for clang
addIncludeDir(path.resolve("my_include_dir"));
// get header ast from clang
const wgpuAst = await clangGetAstJson(HEADER_PATH);
// create clang types cache (for sizeof / offsetof)
const clangTypeInfoCache = await ClangTypeInfoCache.create(TYPE_CACHE_PATH);
// parse ast
const result = parseClangAst(wgpuAst, HEADER_PATH, clangTypeInfoCache);
// update clang cache
await clangTypeInfoCache.save();
// prepare code generation
const codeGen = new CodeGen({
// see more options below
funcSymbolsImportLibPathCode(out) {
out.push(`
let _LIB_PATH: string = "";
if (process.platform == "darwin") {
_LIB_PATH =
import.meta.dir +
"/../wgpu/libwgpu_native.dylib";
} else {
throw new Error("not supported wgpu bindings platform");
}
`);
return "_LIB_PATH";
},
});
codeGen.generateAll(result);
if (codeGen.failedSymbols.size) {
console.log("ffi failed for:");
console.log(Array.from(codeGen.failedSymbols));
}
// write output
codeGen.writeToFile("./test_out.ts");
// cleanup
await clangTypeInfoCache.save();
await clangClean();