c-for-go cgo 绑定自动生成工具
c-for-go 可以快速的生成cgo 绑定代码的工具,目前有不少golang 项目使用了此工具,比如cloudflare/ipvs 也使用了此工具
参考处理
参考使用
这个是libvpx 的一个项目
- yaml 定义文件
---
GENERATOR:
PackageName: vpx
PackageDescription: "Package vpx provides Go bindings for libvpx-1.3.0, the WebM Project VP8/VP9 codec implementation."
PackageLicense: "THE AUTOGENERATED LICENSE. ALL THE RIGHTS ARE RESERVED BY ROBOTS."
PkgConfigOpts: [vpx]
SysIncludes: ["vpx/vpx_encoder.h", "vpx/vpx_decoder.h", "vpx/vp8.h"]
PARSER:
# /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include is meant for stddef.h and
# may vary depending on platform/OS setup you are on.
# This path is taken from my Arch Linux OS
IncludePaths: [/usr/include, /usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include]
SourcesPaths: ["vpx/vpx_encoder.h", "vpx/vpx_decoder.h"]
Defines:
VP8_FOURCC: 0x30385056
VP9_FOURCC: 0x30395056
VP10_FOURCC: 0x303a5056
TRANSLATOR:
ConstRules:
decl: expand
enum: cgo
MemTips:
- {target: _codec_stream_info, self: bind}
- {target: _image, self: bind}
- {target: _svc_, self: raw}
- {target: _codec_ctx, self: raw}
PtrTips:
function:
- {target: "_codec_get_cx_data$", tips: [ref,ref]}
- {target: "_codec_decode$", tips: [ref,arr,size,ref,0]}
- {target: "(?i)^(vpx|vp8|vp9)", tips: [ref,ref,ref,ref,ref,ref]} # defaults
Rules:
global:
- {transform: lower}
- {action: accept, from: "(?i)^(vpx|vp8|vp9)"}
- {action: ignore, from: "vpx_codec_control_"} # variadic
- {action: replace, from: "^vpx_", to: _}
function:
- {action: replace, from: codec_error$, to: codec_get_error}
const:
- {action: replace, from: "VPX_SCALING_MODE", to: "VPX_SCALING_MODE_TYPE"}
type:
- {action: replace, from: "_t$"}
private:
- {transform: unexport}
post-global:
- {from: _abi, transform: upper}
- {from: "_id.?|$", transform: upper}
- {action: replace, from: _img, to: _image}
- {action: replace, from: _fmt, to: _format}
- {action: replace, from: _cs, to: _color_space}
- {action: replace, from: _$}
- {transform: export}
- {load: snakecase}
- 构建
generate 模式
//go:generate c-for-go -out ../ -nocgo ip_vs.yaml
cli
c-for-go -ccdefs app.yaml
说明
对于基于cgo 集成的,c-for-go 是一个很不错的工具
参考资料
https://github.com/xlab/c-for-go
https://pkg.go.dev/cmd/cgo
https://github.com/xlab/c-for-go/wiki
https://github.com/xlab/vorbis-go
https://github.com/cloudflare/ipvs
https://github.com/xlab/libvpx-go
https://github.com/xlab/c-for-go/wiki/Parser-config-section
https://github.com/xlab/c-for-go/wiki/Translator-config-section