Text file src/cmd/go/testdata/script/cgo_binding_gen.txt

     1  # Test that cgo -gen-binding generates Go binding files from //cgo: pragmas
     2  # and C headers, and that the result builds without a C compiler.
     3  
     4  env GOEXPERIMENT=$GOEXPERIMENT,runtimecgopurego,cgobindings
     5  [!GOEXPERIMENT:cgobindings] skip
     6  [!GOOS:darwin] [!GOOS:linux] skip
     7  [!GOARCH:amd64] [!GOARCH:arm64] skip
     8  [!exec:cc] skip
     9  
    10  # Generate the binding file. Only the pragmas decide what is bound;
    11  # C.Unused, referenced in the input, is not.
    12  cd gen
    13  go tool cgo -gen-binding x.go
    14  stdout '^// Code generated by cmd/cgo -gen-binding; DO NOT EDIT\.$'
    15  stdout '^package main$'
    16  stdout '^// #cgo LDFLAGS: \./libx\.so$'
    17  stdout '^// #cgo noescape Add$'
    18  stdout '^// #cgo nocallback Add$'
    19  stdout '^// #include "c\.h"$'
    20  stdout '^import "C"$'
    21  stdout '^import "structs"$'
    22  stdout '^//cgo:type Small C\.SmallStruct$'
    23  stdout '^//cgo:func Add C\.Add "libx\.so"$'
    24  stdout '^//cgo:type _LargeStruct C\.LargeStruct$'
    25  stdout '^//cgo:type _DoubleFloat C\.DoubleFloat$'
    26  stdout '^type Small struct \{$'
    27  stdout '\t_ structs\.HostLayout'
    28  stdout '^type MyInt = int32$'
    29  stdout '^type FloatUnion \[2\]float32$'
    30  stdout '^type _DoubleFloat struct \{$'
    31  ! stdout '_ \['
    32  stdout '^const Limit = 0x64$'
    33  stdout '^const Green = 0x5$'
    34  stdout '^var globalCount int32$'
    35  stdout '^var globalSmall Small$'
    36  stdout '^func Add\(p0 int32, p1 int32\) int32$'
    37  stdout '^func ProcessStruct\(p0 Small, p1 _LargeStruct\) int32$'
    38  stdout '^func Scale\(p0 _DoubleFloat\) float64$'
    39  stdout '^func SumFloatUnion\(p0 FloatUnion\) float32$'
    40  ! stdout 'Unused'
    41  ! stdout 'func main'
    42  # A declaration in the input is kept as written.
    43  stdout '^// AddFloat is kept as written\.\nfunc AddFloat\(a, b float32\) float32$'
    44  cp stdout ../p/binding.go
    45  
    46  # Running -gen-binding on its output reproduces it.
    47  cp stdout regen.go
    48  go tool cgo -gen-binding regen.go
    49  cmp stdout ../p/binding.go
    50  
    51  # -gen-binding accepts all the Go files of a package. It keeps the
    52  # declarations in the files that import "C", with their doc comments. z.go,
    53  # which does not import "C", stays in the package next to the output, so its
    54  # declarations (here, the //cgo:dynload type) are left alone, and the names
    55  # -gen-binding invents avoid them. Anonymous C structs and unions are written
    56  # inline or by their typedef name, never by cgo's invented names.
    57  go tool cgo -gen-binding x.go y.go z.go
    58  stdout '^// #include "c\.h"$'
    59  stdout '^// #include "y\.h"$'
    60  ! stdout 'runtime/cgo'
    61  stdout '^//cgo:func Add C\.Add "libx\.so"$'
    62  stdout '^//cgo:dynload Calc$'
    63  stdout '^//cgo:func Calc\.Mul Mul$'
    64  stdout '^//cgo:var Calc\.Count GlobalCount$'
    65  stdout '^//cgo:type _Handle C\.Handle$'
    66  stdout '^//cgo:type _U C\.U$'
    67  stdout '^//cgo:type _LargeStruct_ C\.LargeStruct$'
    68  ! stdout 'struct___|union___|class___'
    69  ! stdout 'type Calc|name string'
    70  stdout '^// Bump is kept as written\.\nfunc \(\*Calc\) Bump\(\)$'
    71  stdout '^func \(\*Calc\) Mul\(p0 int32, p1 int32\) int32$'
    72  stdout '^func \(\*Calc\) Count\(\) \*int32$'
    73  stdout '^type _Handle \*struct \{$'
    74  stdout '^type _U \[4\]byte$'
    75  stdout '^func AnonParam\(p0 \*struct \{$'
    76  stdout '^\tinner struct \{$'
    77  stdout '^// AddFloat is kept as written\.\nfunc AddFloat\(a, b float32\) float32$'
    78  ! stdout 'helper'
    79  cp stdout ../p/binding.go
    80  cp stdout regen.go
    81  go tool cgo -gen-binding regen.go z.go
    82  cmp stdout ../p/binding.go
    83  
    84  # A generated //cgo:dynload type.
    85  go tool cgo -gen-binding gendyn.go
    86  stdout '^type Gen struct \{\n\tcgo\.Library\n\}$'
    87  stdout '^func \(\*Gen\) Mul\(p0 int32, p1 int32\) int32$'
    88  stdout '^import "runtime/cgo"$'
    89  
    90  # -trimpath and no -srcdir work too.
    91  go tool cgo -gen-binding -trimpath=$WORK=>/trimmed/path x.go
    92  stdout '^func Add\(p0 int32, p1 int32\) int32$'
    93  
    94  # Pragmas must name C declarations of the right kind.
    95  ! go tool cgo -gen-binding bad.go
    96  stderr 'bad\.go:5:1: could not determine what C\.NoSuchFunc refers to'
    97  ! go tool cgo -gen-binding kind.go
    98  stderr 'kind\.go:5:1: //cgo:type T: C\.Add is a func, not a type'
    99  
   100  # Method pragmas must name //cgo:dynload types, and declarations in the
   101  # input must be bodiless and declared once.
   102  ! go tool cgo -gen-binding dynload.go
   103  stderr 'dynload\.go:6:1: //cgo:func Nope\.Add: Nope is not declared with //cgo:dynload'
   104  stderr 'dynload\.go:10:1: -gen-binding: Calc\.Add must be declared on its own'
   105  stderr 'dynload\.go:12:1: -gen-binding: //cgo:func method Unused must have a pointer receiver \(\*Calc\)'
   106  ! go tool cgo -gen-binding x.go zdup.go
   107  stderr 'zdup\.go:6:1: -gen-binding: AddFloat is declared more than once'
   108  ! go tool cgo -gen-binding x.go otherpkg.go
   109  stderr 'otherpkg\.go:1:9: -gen-binding: package q; expected package main'
   110  
   111  # The generated file builds and runs without a C compiler.
   112  cd ../p
   113  cp ../gen/z.go z.go
   114  cc -fPIC -shared -o libx.so ../gen/c.c ../gen/y.c
   115  env CGO_ENABLED=bindings
   116  env CC=/bin/false
   117  [GOOS:linux] env LD_LIBRARY_PATH=.
   118  go run .
   119  stdout '^add=3 addf=3.5 count=7 small=2 process=46 scale=7.5 union=4.0 limit=100 green=5$'
   120  stdout '^anon: handle=9 param=9 union=1 s=9$'
   121  stdout '^dynload: mul=42 count=7$'
   122  
   123  -- gen/c.h --
   124  typedef struct {
   125  	int a;
   126  	short b;
   127  	char c;
   128  } SmallStruct;
   129  
   130  typedef struct {
   131  	double d;
   132  	float f;
   133  } DoubleFloat;
   134  
   135  typedef struct {
   136  	int a, b, c, d, e, f, g, h;
   137  } LargeStruct;
   138  
   139  typedef union {
   140  	float f[2];
   141  	struct { float x, y; } s;
   142  } FloatUnion;
   143  
   144  typedef int MyInt;
   145  enum color { RED, GREEN = 5 };
   146  #define LIMIT 100
   147  
   148  extern int GlobalCount;
   149  extern SmallStruct GlobalSmall;
   150  
   151  extern int Add(int a, int b);
   152  extern float AddFloat(float a, float b);
   153  extern int ProcessStruct(SmallStruct s, LargeStruct l);
   154  extern double Scale(DoubleFloat x);
   155  extern float SumFloatUnion(FloatUnion u);
   156  extern void Unused(void);
   157  
   158  -- gen/c.c --
   159  #include "c.h"
   160  
   161  int GlobalCount = 7;
   162  SmallStruct GlobalSmall = {1, 2, 3};
   163  
   164  int Add(int a, int b) { return a + b; }
   165  float AddFloat(float a, float b) { return a + b; }
   166  int ProcessStruct(SmallStruct s, LargeStruct l) { return s.a + s.b + s.c + l.a + l.h; }
   167  double Scale(DoubleFloat x) { return x.d * x.f; }
   168  float SumFloatUnion(FloatUnion u) { return u.f[0] + u.f[1]; }
   169  void Unused(void) {}
   170  
   171  -- gen/y.h --
   172  typedef struct { int x; } *Handle;
   173  typedef union { int a; float b; } U;
   174  struct S {
   175  	union { int a; float b; } u;
   176  	struct { short p, q; } inner;
   177  };
   178  
   179  extern int HandleX(Handle h);
   180  extern int AnonParam(struct { int x; } *p);
   181  extern int UnionA(U *u);
   182  extern int SumS(struct S *s);
   183  extern int Mul(int a, int b);
   184  extern void Bump(void);
   185  extern int GlobalCount;
   186  
   187  -- gen/y.c --
   188  // Not including y.h: the untagged struct types here are compatible with the
   189  // ones there only across translation units.
   190  typedef struct { int x; } *Handle;
   191  typedef struct { int x; } P;
   192  typedef union { int a; float b; } U;
   193  struct S {
   194  	union { int a; float b; } u;
   195  	struct { short p, q; } inner;
   196  };
   197  extern int GlobalCount;
   198  
   199  int HandleX(Handle h) { return h->x; }
   200  int AnonParam(P *p) { return p->x; }
   201  int UnionA(U *u) { return u->a; }
   202  int SumS(struct S *s) { return s->u.a + s->inner.p + s->inner.q; }
   203  int Mul(int a, int b) { return a * b; }
   204  void Bump(void) { GlobalCount++; }
   205  
   206  -- gen/y.go --
   207  package main
   208  
   209  // #include "y.h"
   210  import "C"
   211  
   212  //cgo:type S C.struct_S
   213  //cgo:func HandleX HandleX "libx.so"
   214  //cgo:func AnonParam AnonParam "libx.so"
   215  //cgo:func UnionA UnionA "libx.so"
   216  //cgo:func SumS SumS "libx.so"
   217  //cgo:dynload Calc
   218  //cgo:func Calc.Mul Mul
   219  //cgo:var Calc.Count GlobalCount
   220  
   221  // Bump is kept as written.
   222  //cgo:func Calc.Bump Bump
   223  func (*Calc) Bump()
   224  
   225  -- gen/z.go --
   226  package main
   227  
   228  import "runtime/cgo"
   229  
   230  // This file does not import "C".
   231  
   232  type Calc struct {
   233  	cgo.Library
   234  	name string
   235  }
   236  
   237  type _LargeStruct int
   238  
   239  func helper() {}
   240  
   241  -- gen/zdup.go --
   242  package main
   243  
   244  // #include "c.h"
   245  import "C"
   246  
   247  func AddFloat(a, b float32) float32
   248  
   249  
   250  -- gen/otherpkg.go --
   251  package q
   252  
   253  -- gen/gendyn.go --
   254  package main
   255  
   256  // #include "y.h"
   257  import "C"
   258  
   259  //cgo:dynload Gen
   260  //cgo:func Gen.Mul Mul
   261  
   262  -- gen/x.go --
   263  package main
   264  
   265  // #cgo LDFLAGS: ./libx.so
   266  // #cgo noescape Add
   267  // #cgo nocallback Add
   268  // #include "c.h"
   269  import "C"
   270  
   271  //cgo:type Small C.SmallStruct
   272  //cgo:type MyInt C.MyInt
   273  //cgo:type FloatUnion C.FloatUnion
   274  //cgo:const Limit C.LIMIT
   275  //cgo:const Green GREEN
   276  //cgo:var globalCount C.GlobalCount "libx.so"
   277  //cgo:var globalSmall GlobalSmall "libx.so"
   278  //cgo:func Add C.Add "libx.so"
   279  //cgo:func AddFloat AddFloat "libx.so"
   280  //cgo:func ProcessStruct C.ProcessStruct "libx.so"
   281  //cgo:func Scale Scale "libx.so"
   282  //cgo:func SumFloatUnion SumFloatUnion "libx.so"
   283  
   284  // AddFloat is kept as written.
   285  func AddFloat(a, b float32) float32
   286  
   287  func main() { C.Unused() }
   288  
   289  -- gen/bad.go --
   290  package main
   291  
   292  // #include "c.h"
   293  import "C"
   294  //cgo:func F C.NoSuchFunc libc
   295  
   296  -- gen/kind.go --
   297  package main
   298  
   299  // #include "c.h"
   300  import "C"
   301  //cgo:type T C.Add
   302  
   303  -- gen/dynload.go --
   304  package main
   305  
   306  // #include "c.h"
   307  import "C"
   308  //cgo:dynload Calc
   309  //cgo:func Nope.Add Add
   310  //cgo:func Calc.Add Add
   311  //cgo:func Calc.Unused Unused
   312  
   313  func (*Calc) Add(a, b int32) int32 { return a + b }
   314  
   315  func (Calc) Unused()
   316  
   317  -- p/go.mod --
   318  module p
   319  
   320  go 1.28
   321  
   322  -- p/main.go --
   323  package main
   324  
   325  import "C"
   326  import (
   327  	"fmt"
   328  	"runtime/cgo"
   329  	"structs"
   330  )
   331  
   332  type handle = struct {
   333  	_ structs.HostLayout
   334  	x int32
   335  }
   336  
   337  func main() {
   338  	l := C.LargeStruct{a: 10, h: 30}
   339  	fmt.Printf("add=%d addf=%.1f count=%d small=%d process=%d scale=%.1f union=%.1f limit=%d green=%d\n",
   340  		C.Add(1, 2), AddFloat(1.5, 2), C.GlobalCount, globalSmall.b,
   341  		C.ProcessStruct(Small{a: 1, b: 2, c: 3}, l), Scale(_DoubleFloat{d: 2.5, f: 3}),
   342  		SumFloatUnion(FloatUnion{1.5, 2.5}), Limit, Green)
   343  
   344  	var h _Handle = &handle{x: 9}
   345  	u := _U{1}
   346  	s := S{u: [4]byte{4}}
   347  	s.inner.p, s.inner.q = 2, 3
   348  	fmt.Printf("anon: handle=%d param=%d union=%d s=%d\n", HandleX(h), AnonParam(&handle{x: 9}), UnionA(&u), SumS(&s))
   349  
   350  	calc, err := cgo.Open[Calc]("./libx.so")
   351  	if err != nil {
   352  		panic(err)
   353  	}
   354  	calc.name = "x"
   355  	fmt.Printf("dynload: mul=%d count=%d\n", calc.Mul(6, 7), *calc.Count())
   356  }
   357  

View as plain text