Skip to content

Commit cc8621c

Browse files
committed
fix: Type exports, missing README.md, OS X support
1 parent 4388591 commit cc8621c

File tree

5 files changed

+97
-29
lines changed

5 files changed

+97
-29
lines changed

build/build.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ HEADER_FILES.forEach((fileName) => {
8383
name = name.substring("enum ".length);
8484
}
8585
if (!TYPE_MEMORY.has(name)) {
86-
const type = toAnyType(TYPE_MEMORY, cx.getType());
86+
const type = toAnyType(TYPE_MEMORY, cx.getType()!);
8787
TYPE_MEMORY.set(name, type);
8888
}
8989
break;
@@ -93,7 +93,7 @@ HEADER_FILES.forEach((fileName) => {
9393

9494
if (!TYPE_MEMORY.has(typedefName)) {
9595
const originalTypeDeclaration = cx
96-
.getTypedefDeclarationOfUnderlyingType();
96+
.getTypedefDeclarationOfUnderlyingType()!;
9797
let structDeclAnyType = toAnyType(
9898
TYPE_MEMORY,
9999
originalTypeDeclaration,
@@ -217,11 +217,11 @@ HEADER_FILES.forEach((fileName) => {
217217
}
218218
case CXCursorKind.CXCursor_FunctionDecl: {
219219
const parameters: FunctionParameter[] = [];
220-
const resultAnyType = toAnyType(TYPE_MEMORY, cx.getResultType());
220+
const resultAnyType = toAnyType(TYPE_MEMORY, cx.getResultType()!);
221221
const length = cx.getNumberOfArguments();
222222
for (let i = 0; i < length; i++) {
223-
const argument = cx.getArgument(i);
224-
const argumentAnyType = toAnyType(TYPE_MEMORY, argument.getType());
223+
const argument = cx.getArgument(i)!;
224+
const argumentAnyType = toAnyType(TYPE_MEMORY, argument.getType()!);
225225
parameters.push({
226226
comment: commentToJSDcoString(
227227
argument.getParsedComment(),

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"lock": true,
33
"tasks": {
4-
"build": "./deno run --unstable --allow-env=LIBCLANG_PATH --allow-run=deno --allow-ffi --allow-write=lib/include build/build.ts"
4+
"build": "deno run --unstable --allow-env=LIBCLANG_PATH --allow-run=deno --allow-ffi --allow-write=lib/include build/build.ts"
55
}
66
}

lib/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## libclang
2+
3+
Deno bindings for [Clang](https://clang.llvm.org/)'s C API `libclang`. Full
4+
project details are found on the
5+
[libclang-deno GitHub page](https://github.com/aapoalas/libclang-deno).
6+
7+
### Startup
8+
9+
To use `libclang` in your Deno program, import the `mod.ts` file into your
10+
program:
11+
12+
```ts
13+
import * as libclang from "https://deno.land/x/[email protected]/mod.ts";
14+
```
15+
16+
You must run your program with the `LIBCLANG_PATH` environment variable set to
17+
either the direct path to your `libclang.so` / `libclang.dll` / `libclang.dylib`
18+
shared library or to the folder in which the shared library resides. If the
19+
environment variable is not set, the import will fail.
20+
21+
### Basic usage
22+
23+
The following code will walk through all the cursors in a given header and log
24+
their kind and spelling:
25+
26+
```ts
27+
import * as libclang from "https://deno.land/x/[email protected]/mod.ts";
28+
29+
const index = new libclang.CXIndex();
30+
31+
const tu = index.parseTranslationUnit("/path/to/header.h", [
32+
"-I/path/to/include",
33+
]);
34+
35+
tu.getCursor().visitChildren((cursor) => {
36+
console.log(`${cursor.getKindSpelling()}: ${cursor.getSpelling()}`);
37+
return libclang.CXChildVisitResult.CXChildVisit_Recurse;
38+
});
39+
```

lib/ffi.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,53 @@ if (!libclangPath) {
3333

3434
let libclang: ReturnType<typeof Deno.dlopen<typeof IMPORTS>>;
3535

36-
if (libclangPath.includes(".so") || libclangPath.endsWith(".dll")) {
37-
// Explicit path, try without catching
38-
libclang = Deno.dlopen(libclangPath, IMPORTS);
39-
} else if (Deno.build.os === "windows") {
36+
if (Deno.build.os === "windows") {
37+
if (libclangPath.includes(".dll")) {
38+
try {
39+
libclang = Deno.dlopen(libclangPath, IMPORTS);
40+
} catch {
41+
// Ignore
42+
}
43+
}
4044
libclang = Deno.dlopen(
4145
join(libclangPath, "libclang.dll"),
4246
IMPORTS,
4347
);
48+
} else if (Deno.build.os === "darwin") {
49+
if (libclangPath.includes(".dylib")) {
50+
try {
51+
libclang = Deno.dlopen(libclangPath, IMPORTS);
52+
} catch {
53+
// Ignore
54+
}
55+
}
56+
libclang = Deno.dlopen(
57+
join(libclangPath, "libclang.dylib"),
58+
IMPORTS,
59+
);
4460
} else {
45-
// Try 14.0.6 first, then 14, then 13
61+
if (libclangPath.includes(".so")) {
62+
try {
63+
libclang = Deno.dlopen(libclangPath, IMPORTS);
64+
} catch {
65+
// Ignore
66+
}
67+
}
68+
// Try plain libclang first, then 14.0.6, then 14, and finally try 13.
4669
try {
47-
libclang = Deno.dlopen(
48-
join(libclangPath, "libclang.so.14.0.6"),
49-
IMPORTS,
50-
);
70+
libclang = Deno.dlopen(join(libclangPath, "libclang.so"), IMPORTS);
5171
} catch {
5272
try {
53-
libclang = Deno.dlopen(join(libclangPath, "libclang.so.14"), IMPORTS);
73+
libclang = Deno.dlopen(
74+
join(libclangPath, "libclang.so.14.0.6"),
75+
IMPORTS,
76+
);
5477
} catch {
55-
libclang = Deno.dlopen(join(libclangPath, "libclang.so.13"), IMPORTS);
78+
try {
79+
libclang = Deno.dlopen(join(libclangPath, "libclang.so.14"), IMPORTS);
80+
} catch {
81+
libclang = Deno.dlopen(join(libclangPath, "libclang.so.13"), IMPORTS);
82+
}
5683
}
5784
}
5885
}

lib/mod.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,57 +55,59 @@ import {
5555
} from "./utils.ts";
5656
export * from "./BuildSystem.ts";
5757
export * from "./CXCompilationDatabase.ts";
58-
export type {
58+
export {
5959
CX_CXXAccessSpecifier,
6060
CX_StorageClass,
6161
CXAvailabilityKind,
6262
CXCallingConv,
6363
CXChildVisitResult,
6464
CXCodeComplete_Flags,
65-
CXCodeCompleteResults,
6665
CXCommentInlineCommandRenderKind,
6766
CXCommentKind,
6867
CXCommentParamPassDirection,
6968
CXCompletionChunkKind,
7069
CXCompletionContext,
71-
CXCompletionString,
7270
CXCursor_ExceptionSpecificationKind,
7371
CXCursorKind,
7472
CXDiagnosticDisplayOptions,
7573
CXDiagnosticSeverity,
7674
CXErrorCode,
77-
CXEvalResult,
7875
CXEvalResultKind,
7976
CXGlobalOptFlags,
80-
CXIndexAction,
8177
CXLanguageKind,
8278
CXLinkageKind,
8379
CXLoadDiag_Error,
84-
CXModule,
8580
CXNameRefFlags,
8681
CXObjCDeclQualifierKind,
8782
CXObjCPropertyAttrKind,
88-
CXPrintingPolicy,
8983
CXPrintingPolicyProperty,
9084
CXRefQualifierKind,
9185
CXReparse_Flags,
9286
CXResult,
93-
CXRewriter,
9487
CXSaveError,
95-
CXSourceRangeList,
9688
CXTemplateArgumentKind,
9789
CXTLSKind,
98-
CXToken,
9990
CXTokenKind,
10091
CXTranslationUnit_Flags,
101-
CXTUResourceUsage,
102-
CXType,
10392
CXTypeKind,
10493
CXTypeLayoutError,
10594
CXTypeNullabilityKind,
10695
CXVisibilityKind,
10796
CXVisitorResult,
10897
};
98+
export type {
99+
CXCodeCompleteResults,
100+
CXCompletionString,
101+
CXEvalResult,
102+
CXIndexAction,
103+
CXModule,
104+
CXPrintingPolicy,
105+
CXRewriter,
106+
CXSourceRangeList,
107+
CXToken,
108+
CXTUResourceUsage,
109+
CXType,
110+
};
109111

110112
const CONSTRUCTOR = Symbol("[[constructor]]");
111113
const POINTER = Symbol("[[pointer]]");

0 commit comments

Comments
 (0)