1 # With CGO_ENABLED=bindings, the module index makes the same decisions
2 # about which packages are cgo binding packages as loading packages
3 # without it.
4
5 env GOEXPERIMENT=$GOEXPERIMENT,runtimecgopurego,cgobindings
6 [!GOEXPERIMENT:cgobindings] skip
7
8 env CGO_ENABLED=bindings
9 [!GOOS:windows] [!cgolinkext] env CC=/bin/false
10 [GOOS:windows] env CC=nonexistent-cc
11 env GOFLAGS=-modcacherw
12
13 go get example.com/cgobindings@v1.0.0
14
15 # Binding packages from the module cache, which is indexed, build without
16 # a C compiler. withc has no binding pragmas, so it is built as with
17 # CGO_ENABLED=0, using its fallback.
18 go run .
19 stdout '^withc=1 fallback=4$'
20
21 # A package with binding pragmas that also contains C files is an error.
22 ! go build example.com/cgobindings/withcfiles
23 stderr 'source files requiring a C compiler not allowed in a package using cgo binding pragmas: cfile.c'
24
25 # The module index and direct loading agree, for the module's packages
26 # and for the standard library.
27 go list -e -f '{{.ImportPath}} {{.CgoFiles}} {{.GoFiles}} {{.IgnoredGoFiles}} {{.CFiles}}' example.com/cgobindings/...
28 cp stdout index.txt
29 stdout '^example.com/cgobindings/withcfiles \[bind.go\] \[\] \[\] \[cfile.c\]$'
30 stdout '^example.com/cgobindings/fallback \[bind.go\] \[\] \[nocgo.go\] \[\]$'
31 stdout '^example.com/cgobindings/withc \[\] \[fallback.go\] \[withc.go\] \[\]$'
32 go list -f '{{.ImportPath}} {{.CgoFiles}} {{.GoFiles}} {{.IgnoredGoFiles}}' std
33 cp stdout std-index.txt
34 stdout '^runtime/cgo \[\] \[.+\]'
35
36 env GODEBUG=goindex=0
37 ! go build example.com/cgobindings/withcfiles
38 stderr 'source files requiring a C compiler not allowed in a package using cgo binding pragmas: cfile.c'
39 go list -e -f '{{.ImportPath}} {{.CgoFiles}} {{.GoFiles}} {{.IgnoredGoFiles}} {{.CFiles}}' example.com/cgobindings/...
40 cmp stdout index.txt
41 go list -f '{{.ImportPath}} {{.CgoFiles}} {{.GoFiles}} {{.IgnoredGoFiles}}' std
42 cmp stdout std-index.txt
43
44 -- go.mod --
45 module m
46
47 go 1.28
48 -- main.go --
49 package main
50
51 import (
52 "fmt"
53
54 "example.com/cgobindings/fallback"
55 "example.com/cgobindings/withc"
56 )
57
58 func main() {
59 fmt.Printf("withc=%d fallback=%d\n", withc.One(), fallback.Abs(-4))
60 }
61
View as plain text