# Test that cgo -gen-binding generates Go binding files from //cgo: pragmas # and C headers, and that the result builds without a C compiler. env GOEXPERIMENT=$GOEXPERIMENT,runtimecgopurego,cgobindings [!GOEXPERIMENT:cgobindings] skip [!GOOS:darwin] [!GOOS:linux] skip [!GOARCH:amd64] [!GOARCH:arm64] skip [!exec:cc] skip # Generate the binding file. Only the pragmas decide what is bound; # C.Unused, referenced in the input, is not. cd gen go tool cgo -gen-binding x.go stdout '^// Code generated by cmd/cgo -gen-binding; DO NOT EDIT\.$' stdout '^package main$' stdout '^// #cgo LDFLAGS: \./libx\.so$' stdout '^// #cgo noescape Add$' stdout '^// #cgo nocallback Add$' stdout '^// #include "c\.h"$' stdout '^import "C"$' stdout '^import "structs"$' stdout '^//cgo:type Small C\.SmallStruct$' stdout '^//cgo:func Add C\.Add "libx\.so"$' stdout '^//cgo:type _LargeStruct C\.LargeStruct$' stdout '^//cgo:type _DoubleFloat C\.DoubleFloat$' stdout '^type Small struct \{$' stdout '\t_ structs\.HostLayout' stdout '^type MyInt = int32$' stdout '^type FloatUnion \[2\]float32$' stdout '^type _DoubleFloat struct \{$' ! stdout '_ \[' stdout '^const Limit = 0x64$' stdout '^const Green = 0x5$' stdout '^var globalCount int32$' stdout '^var globalSmall Small$' stdout '^func Add\(p0 int32, p1 int32\) int32$' stdout '^func ProcessStruct\(p0 Small, p1 _LargeStruct\) int32$' stdout '^func Scale\(p0 _DoubleFloat\) float64$' stdout '^func SumFloatUnion\(p0 FloatUnion\) float32$' ! stdout 'Unused' ! stdout 'func main' # A declaration in the input is kept as written. stdout '^// AddFloat is kept as written\.\nfunc AddFloat\(a, b float32\) float32$' cp stdout ../p/binding.go # Running -gen-binding on its output reproduces it. cp stdout regen.go go tool cgo -gen-binding regen.go cmp stdout ../p/binding.go # -gen-binding accepts all the Go files of a package. It keeps the # declarations in the files that import "C", with their doc comments. z.go, # which does not import "C", stays in the package next to the output, so its # declarations (here, the //cgo:dynload type) are left alone, and the names # -gen-binding invents avoid them. Anonymous C structs and unions are written # inline or by their typedef name, never by cgo's invented names. go tool cgo -gen-binding x.go y.go z.go stdout '^// #include "c\.h"$' stdout '^// #include "y\.h"$' ! stdout 'runtime/cgo' stdout '^//cgo:func Add C\.Add "libx\.so"$' stdout '^//cgo:dynload Calc$' stdout '^//cgo:func Calc\.Mul Mul$' stdout '^//cgo:var Calc\.Count GlobalCount$' stdout '^//cgo:type _Handle C\.Handle$' stdout '^//cgo:type _U C\.U$' stdout '^//cgo:type _LargeStruct_ C\.LargeStruct$' ! stdout 'struct___|union___|class___' ! stdout 'type Calc|name string' stdout '^// Bump is kept as written\.\nfunc \(\*Calc\) Bump\(\)$' stdout '^func \(\*Calc\) Mul\(p0 int32, p1 int32\) int32$' stdout '^func \(\*Calc\) Count\(\) \*int32$' stdout '^type _Handle \*struct \{$' stdout '^type _U \[4\]byte$' stdout '^func AnonParam\(p0 \*struct \{$' stdout '^\tinner struct \{$' stdout '^// AddFloat is kept as written\.\nfunc AddFloat\(a, b float32\) float32$' ! stdout 'helper' cp stdout ../p/binding.go cp stdout regen.go go tool cgo -gen-binding regen.go z.go cmp stdout ../p/binding.go # A generated //cgo:dynload type. go tool cgo -gen-binding gendyn.go stdout '^type Gen struct \{\n\tcgo\.Library\n\}$' stdout '^func \(\*Gen\) Mul\(p0 int32, p1 int32\) int32$' stdout '^import "runtime/cgo"$' # -trimpath and no -srcdir work too. go tool cgo -gen-binding -trimpath=$WORK=>/trimmed/path x.go stdout '^func Add\(p0 int32, p1 int32\) int32$' # Pragmas must name C declarations of the right kind. ! go tool cgo -gen-binding bad.go stderr 'bad\.go:5:1: could not determine what C\.NoSuchFunc refers to' ! go tool cgo -gen-binding kind.go stderr 'kind\.go:5:1: //cgo:type T: C\.Add is a func, not a type' # Method pragmas must name //cgo:dynload types, and declarations in the # input must be bodiless and declared once. ! go tool cgo -gen-binding dynload.go stderr 'dynload\.go:6:1: //cgo:func Nope\.Add: Nope is not declared with //cgo:dynload' stderr 'dynload\.go:10:1: -gen-binding: Calc\.Add must be declared on its own' stderr 'dynload\.go:12:1: -gen-binding: //cgo:func method Unused must have a pointer receiver \(\*Calc\)' ! go tool cgo -gen-binding x.go zdup.go stderr 'zdup\.go:6:1: -gen-binding: AddFloat is declared more than once' ! go tool cgo -gen-binding x.go otherpkg.go stderr 'otherpkg\.go:1:9: -gen-binding: package q; expected package main' # The generated file builds and runs without a C compiler. cd ../p cp ../gen/z.go z.go cc -fPIC -shared -o libx.so ../gen/c.c ../gen/y.c env CGO_ENABLED=bindings env CC=/bin/false [GOOS:linux] env LD_LIBRARY_PATH=. go run . stdout '^add=3 addf=3.5 count=7 small=2 process=46 scale=7.5 union=4.0 limit=100 green=5$' stdout '^anon: handle=9 param=9 union=1 s=9$' stdout '^dynload: mul=42 count=7$' -- gen/c.h -- typedef struct { int a; short b; char c; } SmallStruct; typedef struct { double d; float f; } DoubleFloat; typedef struct { int a, b, c, d, e, f, g, h; } LargeStruct; typedef union { float f[2]; struct { float x, y; } s; } FloatUnion; typedef int MyInt; enum color { RED, GREEN = 5 }; #define LIMIT 100 extern int GlobalCount; extern SmallStruct GlobalSmall; extern int Add(int a, int b); extern float AddFloat(float a, float b); extern int ProcessStruct(SmallStruct s, LargeStruct l); extern double Scale(DoubleFloat x); extern float SumFloatUnion(FloatUnion u); extern void Unused(void); -- gen/c.c -- #include "c.h" int GlobalCount = 7; SmallStruct GlobalSmall = {1, 2, 3}; int Add(int a, int b) { return a + b; } float AddFloat(float a, float b) { return a + b; } int ProcessStruct(SmallStruct s, LargeStruct l) { return s.a + s.b + s.c + l.a + l.h; } double Scale(DoubleFloat x) { return x.d * x.f; } float SumFloatUnion(FloatUnion u) { return u.f[0] + u.f[1]; } void Unused(void) {} -- gen/y.h -- typedef struct { int x; } *Handle; typedef union { int a; float b; } U; struct S { union { int a; float b; } u; struct { short p, q; } inner; }; extern int HandleX(Handle h); extern int AnonParam(struct { int x; } *p); extern int UnionA(U *u); extern int SumS(struct S *s); extern int Mul(int a, int b); extern void Bump(void); extern int GlobalCount; -- gen/y.c -- // Not including y.h: the untagged struct types here are compatible with the // ones there only across translation units. typedef struct { int x; } *Handle; typedef struct { int x; } P; typedef union { int a; float b; } U; struct S { union { int a; float b; } u; struct { short p, q; } inner; }; extern int GlobalCount; int HandleX(Handle h) { return h->x; } int AnonParam(P *p) { return p->x; } int UnionA(U *u) { return u->a; } int SumS(struct S *s) { return s->u.a + s->inner.p + s->inner.q; } int Mul(int a, int b) { return a * b; } void Bump(void) { GlobalCount++; } -- gen/y.go -- package main // #include "y.h" import "C" //cgo:type S C.struct_S //cgo:func HandleX HandleX "libx.so" //cgo:func AnonParam AnonParam "libx.so" //cgo:func UnionA UnionA "libx.so" //cgo:func SumS SumS "libx.so" //cgo:dynload Calc //cgo:func Calc.Mul Mul //cgo:var Calc.Count GlobalCount // Bump is kept as written. //cgo:func Calc.Bump Bump func (*Calc) Bump() -- gen/z.go -- package main import "runtime/cgo" // This file does not import "C". type Calc struct { cgo.Library name string } type _LargeStruct int func helper() {} -- gen/zdup.go -- package main // #include "c.h" import "C" func AddFloat(a, b float32) float32 -- gen/otherpkg.go -- package q -- gen/gendyn.go -- package main // #include "y.h" import "C" //cgo:dynload Gen //cgo:func Gen.Mul Mul -- gen/x.go -- package main // #cgo LDFLAGS: ./libx.so // #cgo noescape Add // #cgo nocallback Add // #include "c.h" import "C" //cgo:type Small C.SmallStruct //cgo:type MyInt C.MyInt //cgo:type FloatUnion C.FloatUnion //cgo:const Limit C.LIMIT //cgo:const Green GREEN //cgo:var globalCount C.GlobalCount "libx.so" //cgo:var globalSmall GlobalSmall "libx.so" //cgo:func Add C.Add "libx.so" //cgo:func AddFloat AddFloat "libx.so" //cgo:func ProcessStruct C.ProcessStruct "libx.so" //cgo:func Scale Scale "libx.so" //cgo:func SumFloatUnion SumFloatUnion "libx.so" // AddFloat is kept as written. func AddFloat(a, b float32) float32 func main() { C.Unused() } -- gen/bad.go -- package main // #include "c.h" import "C" //cgo:func F C.NoSuchFunc libc -- gen/kind.go -- package main // #include "c.h" import "C" //cgo:type T C.Add -- gen/dynload.go -- package main // #include "c.h" import "C" //cgo:dynload Calc //cgo:func Nope.Add Add //cgo:func Calc.Add Add //cgo:func Calc.Unused Unused func (*Calc) Add(a, b int32) int32 { return a + b } func (Calc) Unused() -- p/go.mod -- module p go 1.28 -- p/main.go -- package main import "C" import ( "fmt" "runtime/cgo" "structs" ) type handle = struct { _ structs.HostLayout x int32 } func main() { l := C.LargeStruct{a: 10, h: 30} fmt.Printf("add=%d addf=%.1f count=%d small=%d process=%d scale=%.1f union=%.1f limit=%d green=%d\n", C.Add(1, 2), AddFloat(1.5, 2), C.GlobalCount, globalSmall.b, C.ProcessStruct(Small{a: 1, b: 2, c: 3}, l), Scale(_DoubleFloat{d: 2.5, f: 3}), SumFloatUnion(FloatUnion{1.5, 2.5}), Limit, Green) var h _Handle = &handle{x: 9} u := _U{1} s := S{u: [4]byte{4}} s.inner.p, s.inner.q = 2, 3 fmt.Printf("anon: handle=%d param=%d union=%d s=%d\n", HandleX(h), AnonParam(&handle{x: 9}), UnionA(&u), SumS(&s)) calc, err := cgo.Open[Calc]("./libx.so") if err != nil { panic(err) } calc.name = "x" fmt.Printf("dynload: mul=%d count=%d\n", calc.Mul(6, 7), *calc.Count()) }