xlab / c-for-go

Automatic C-Go Bindings Generator for Go Programming Language
https://c.for-go.com
MIT License
1.5k stars 119 forks source link

A few changes needed to support go-libvirt #164

Closed mpontillo closed 9 months ago

mpontillo commented 9 months ago

At DigitalOcean, we are using c-for-go for our go-libvirt project.

Two breaking changes have occurred recently, which are preventing us from using c-for-go to generate the necessary constants:

(1) In libvirt itself, header files were switched from using double-quotes (#include "foo.h") to using brackets (#include <foo.h>). Because these were self-referencing headers (intended for eventual placement on the system include path on the target system), this resulted in the inability of c-for-go to find libvirt's own headers. We resolved this by adding the ability to configure the system include path, such (in our case) by specifying SysIncludePaths: [./lv_source/include, ./lv_source/build/include] in the configuration.

(2) In c-for-go, an update of the cc dependency to v4 caused a change in the way enums were handled. For context, although c-for-go correctly treats C enumerations as being of type int, the libvirt code contains two enum usages where their values are used for bit fields, and explicitly declared as unsigned 32-bit values. This causes c-for-go to generate incorrect code (because the integer value 2147483648, which comes from 1u << 31, could not fit into a signed 32-bit field). This was resolved by adding the concept of enum type tips and unsigned type tips (TipTypeUnsigned), such as (in our case) by declaring the following in the configuration:

    TypeTips:
        enum:
            - { target: "ConnectGetAllDomainStatsFlags", tips: [ unsigned ] }
            - { target: "ConnectListAllNodeDeviceFlags", tips: [ unsigned ] }

We have been testing these changes successfully in our digitalocean-labs/c-for-go fork, and hope you will consider applying them to the official version.

xlab commented 9 months ago

@mpontillo thank you very much for coming up with these battlefield tested changes and actually publishing this as a PR to upstream! I'm glad that c-for-go helps and community makes it better in return :)

I'll merge these changes, at some point when having time will update the Wiki to have those tips.