DMD 2.111.0 - Now with more ImportC!
Adam Wilson
flyboynw at gmail.com
Tue Feb 25 09:04:49 UTC 2025
During BeerConf this month I was finally able to deliver on a
promise I made back at DConf 2023. The new ODBC 4.0 ImportC
generated bindings have finally landed in Phobos!
Why is this such a big deal? Because for the first time D will
ship bindings for a major library that are generated using
ImportC. This represents a major milestone on our long-term
mission of being able to use ImportC to import bindings for all
of our major Operating System targets. There will be more bugs to
crush along the way, but ImportC has finally achieved
production-grade quality usability.
The final result is that out of nearly 2000 symbols, only two had
to be hand-ported. One was a complex enum with multiple bitwise
OR's. The other is a multi-line string enum. It is unlikely that
either of these will be supported by ImportC in the near future.
The process to reach this milestone involved crushing a number of
bugs. And there are still a couple of bugs that need to be fixed
to reach optimal conversion, one of which was an arcane lexer bug
that Walter has already put out a PR for.
Specifically they are:
https://github.com/dlang/dmd/pull/20914 (Lexer skips to EoF when
reading a 0.)
https://github.com/dlang/dmd/issues/20911 (Forward Declared
Structs are emitted with the struct and a "forward declaration"
that conflicts.)
There remains an outstanding issue with C union types not being
able to be emitted into DI files, but this one is rather arcane
and likely to require a complex solution, if possible at all.
These bindings are generated using ImportC in conjunction with a
small tool I developed (here:
https://github.com/LightBender/odbc-d). Initially, I wrote the
tool to work around bugs in ImportC, but as we've closed out
those bugs that need has reduced.
However, one use of the tool that does not appear likely to
change is removing unwanted symbols. For example, in the
canonical ODBC headers from Microsoft, there are symbols for
optional Windows-specific and Visual Studio-specific capabilities
that are unavailable across platforms. I was able to use the tool
to remove these and it creates a much cleaner binding.
Additionally, because ImportC modules were never intended to be
written to files, the DI emitter for ImportC bindings emits a
bunch of garbage that the importer generates. Using this tool I
was able to circumvent those symbols.
I would stress that the tool approach will not be useful for all
cases as there are C header constructs that are not expressible
in D. But in practice this appears to be a highly infrequent
situation.
If you have any questions about this process and the future of
ImportC bindings (which will play a critical role in the future
of DRT and Phobos) please ask away!
In the mean time, my congratulations to Walter and the rest of
the D compiler hackers who made the ImportC dream a practical
reality!
More information about the Digitalmars-d
mailing list