...
1# Issue 52863.
2
3# We manually create a .syso and a .a file in package a,
4# such that the .syso file only works when linked against the .a file.
5# Package a has #cgo LDFLAGS to make this happen.
6#
7# Package c imports package a, and uses cgo itself.
8# The generation of the _cgo_import.go for package c will fail,
9# because it won't know that it has to link against a/libb.a
10# (because we don't gather the #cgo LDFLAGS from all transitively
11# imported packages).
12#
13# The _cgo_import.go file is only needed for internal linking.
14# When generating _cgo_import.go for package c fails, an ordinary
15# external link should still work. But an internal link is expected
16# to fail, because the failure to create _cgo_import.go should cause
17# the linker to report an inability to internally link.
18
19[short] skip
20[!cgo] skip
21[!exec:ar] skip
22
23cc -c -o a/b.syso b/b.c
24cc -c -o b/lib.o b/lib.c
25exec ar rc a/libb.a b/lib.o
26go build
27! go build -ldflags=-linkmode=internal
28stderr 'some packages could not be built to support internal linking.*m/c|requires external linking|does not support internal cgo'
29
30-- go.mod --
31module m
32
33-- a/a.go --
34package a
35
36// #cgo LDFLAGS: -L. -lb
37// extern int CFn(int);
38import "C"
39
40func GoFn(v int) int { return int(C.CFn(C.int(v))) }
41
42-- b/b.c --
43extern int LibFn(int);
44int CFn(int i) { return LibFn(i); }
45
46-- b/lib.c --
47int LibFn(int i) { return i; }
48
49-- c/c.go --
50package c
51
52// static int D(int i) { return i; }
53import "C"
54
55import "m/a"
56
57func Fn(i int) (int, int) {
58 return a.GoFn(i), int(C.D(C.int(i)))
59}
60
61-- main.go --
62package main
63
64import "m/c"
65
66func main() {
67 println(c.Fn(0))
68}
View as plain text