Dart FFI binding generator for any LLVM Clang JSON AST dump
- build_runner
- code_gen
- LLVM Clang >10.* to produce the AST-dump in JSON
brew install llvm
- A great post on installing llvm.
- How to get your framework dependency to run with LLVM you've just installed
# dump json
clang-10 -Isomewhere/include -c main.c -o build/target.o -Xclang -ast-dump=json -fsyntax-only > project_os_arch.json
# dump record memory layout
clang-10 -Isomewhere/include -c main.c -o build/target.o -Xclang -fdump-record-layouts > project_os_arch.layout
# check for padding optimization
clang-10 -Isomewhere/include -c main.c -o build/target.o -Wpadded
Also useful: -fsave-optimization-record
- Working on examples, tests and workarounds
- Waiting for the dart team to provide a offset mechanism to solve alignment/padding issues.
Grouping commonalities between generated ARCH and OSs is still a manual task.
- Nested structs/unions/etc.
- Pass by value e.g. Struct/Union/Enum
extern C
required for C++ code due to name mangling- Packed structs
- No bitfields, DIY bitmasking
- No union
- No support for non scalar struct fields, e.g.
int meh[2];
- And more...
- Each struct depends on the largest member (e.g. another struct), to determine its alignment and padding, use
-Wpadded
in clang - Just don't, especially for big padded structs, don't overflow the stack.
- Just do it.
- Clang has a
-Wpadded
that could provide clues on how to spatially optimize (instead of packing) - Don't. Instead use a machine word sized integer, and provide masking where necessary; explicit is better.
- Use Struct from dart's FFI, but use a single memory location, e.g. a
Pointer<Void>
as the only field. - The workaround is to repeatedly state the field
@Int32 int meh_0, meh_1; // etc.
to determine the proper alignment and padding. Yuck.
- Add a
build.yaml
with the necessary configuration, see the examples - Compile your C/C++ code for your ARCH and OS, with clang, and enable
-dump-ast=json
- Capture the JSON output in a file,
<project>_<os>_arch.json
, e.g.miniaudio_android_arm64.py
. Place the file generated by adding-ast-dump=json
file in yourlib/src
. Then run:
pub run build_runner build
Sort out types in the AST JSON:
grep desugaredQualType web/miniaudio.json | grep -v __attr | sed -e 's/^[ \t]*//' | sort | uniq | less