mirror of
https://github.com/mappu/miqt.git
synced 2025-02-01 19:10:21 +00:00
187c0a02ec
This change helps keep rules for each language separate by moving `go` rules to `emitgo` while the C bindings stay closer to the original Qt naming (that already is mostly C-safe). Although it doesn't practically matter for go, it makes it slightly easier to reuse the generated code in other languages which have different keywords and naming conventions. The cabi generator also gains a few helpers to help keep names consistent across files which hopefully aids reading the generator code - it did for me at least;) The rule that converts under_score to CamelCase is left for another day since moving it turns out to be more invasive due to name collision handling - when underscores are kept, there are fewer name conflicts which ends up causing name changes in the public go api when done naively.
genbindings
The genbindings
program regenerates the Qt bindings.
Architecture design
Bindings are generated in two passes:
Pass 1
- Scan input directory for header files.
- For each header file:
- Run
clang --ast-dump=json
to get a JSON ast.- This is somewhat slow, the results will be cached in
./cachedir
after the first run. - Strip all Clang AST nodes that were included from other files, to only consider the header's own definitions.
- This is somewhat slow, the results will be cached in
- Convert Clang AST to our own intermediate representation.
- Run some transformations on the intermediate representation.
- Cache and collect the global state of all known class names, enum names, and typedefs.
Pass 2
- For each intermediate-representation AST:
- Emit "CABI" cpp/h pair.
- The CABI is a projection of Qt into plain C. The translation unit itself is C++, but the header can be used as extern c.
- Emit Go binding file.
- The Go binding uses CGO to call into the CABI binding.
Configuration
It's tested to work on with Debian 12 with system packages (Qt 5.15 / Qt 6.4 / Clang 14 / GCC 12).
You should check the following configuration:
config-libraries.go
: Input directories containing Qt headersconfig-allowlist.go
: Check everything
Steps to add extra libraries to MIQT
- Git clone this repository
- In
docker/genbindings.Dockerfile
, add your library's headers and pkg-config file.- If your library does not include a pkg-config file, you must create one.
- Patch
cmd/genbindings/config-libraries.go
to add a newgenerate
block for your target library - Run
genbindings
to regenerate all bindings- The first run must populate clang ASTs into a cache directory and may be slower, but it is fast afterwards
- Add a
cflags.go
file to the generated binding directory- It should have a
#cgo pkg-config: LibraryName
stanza and any extra flags (e.g.--std=c++17
) that are required but not system-specific
- It should have a
- Try to use the new binding within the repo, by adding an example in the
examples/libraries
directory - Commit the generated bindings
- You can then use your forked MIQT repo with
replace
insidego.mod
- Or, open a Pull Request to add the library to MIQT
- You can then use your forked MIQT repo with