This tool generates a JSON structure that represents a subset of C declarations
from a provided C header file. It leverages clang's C Interface, which is
discussed in detail here.
🔍 Note: The generated JSON is not a complete representation of the parsed header file. It does not contain all the information needed for a full reconstruction of the header file, but it captures the elements essential to automatically generating bindings to a C library.
If this is useful to you and you would like to add to it, please submit a PR.
To use this tool, you need to have libclang-dev installed on your system. If
you're using Ubuntu, you can install it with:
sudo apt update
sudo apt install libclang-devTo build the project, run:
makeTo generate the JSON bindings from a C header file, use the something like:
./generate /path/to/header.h > bindings.jsonReplace /path/to/header.h with the path to your C header file, and
bindings.json with the file to which output should be redirected.
The repository includes a simple test script written in JavaScript. This script
reads the generated JSON file, parses it using JSON.parse, and then
stringifies it using JSON.stringify. This process serves as a basic test to
ensure the integrity of the JSON structure. You can run it with: bun check-json.ts bindings.json.
If you are not using llvm version 14, or if you encounter issues locating the
correct headers, you can search for them using the following command: find /usr -name Index.h 2>/dev/null | grep clang-c Once you locate the headers,
update the Makefile accordingly to ensure the build process can find the
necessary files.