| 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 |
|
| 23 |
cc -c -o a/b.syso b/b.c |
| 24 |
cc -c -o b/lib.o b/lib.c |
| 25 |
exec ar rc a/libb.a b/lib.o |
| 26 |
|
| 27 |
go build |
| 28 |
! stderr 'undefined reference' |
| 29 |
|
| 30 |
! go build -ldflags=-linkmode=internal |
| 31 |
stderr 'some packages could not be built to support internal linking.*m/c|requires external linking|does not support internal cgo' |
| 32 |
|
| 33 |
# Test for issue #68743. |
| 34 |
go build -x m/d |
| 35 |
! stderr 'undefined reference' |
| 36 |
stderr 'test for internal linking' |
| 37 |
|
| 38 |
-- go.mod -- |
| 39 |
module m |
| 40 |
|
| 41 |
-- a/a.go -- |
| 42 |
package a |
| 43 |
|
| 44 |
// #cgo LDFLAGS: -L. -lb |
| 45 |
// extern int CFn(int); |
| 46 |
import "C" |
| 47 |
|
| 48 |
func GoFn(v int) int { return int(C.CFn(C.int(v))) } |
| 49 |
|
| 50 |
-- b/b.c -- |
| 51 |
extern int LibFn(int); |
| 52 |
int CFn(int i) { return LibFn(i); } |
| 53 |
|
| 54 |
-- b/lib.c -- |
| 55 |
int LibFn(int i) { return i; } |
| 56 |
|
| 57 |
-- c/c.go -- |
| 58 |
package c |
| 59 |
|
| 60 |
// static int D(int i) { return i; } |
| 61 |
import "C" |
| 62 |
|
| 63 |
import "m/a" |
| 64 |
|
| 65 |
func Fn(i int) (int, int) { |
| 66 |
return a.GoFn(i), int(C.D(C.int(i))) |
| 67 |
} |
| 68 |
|
| 69 |
-- d/d.go -- |
| 70 |
// Package d is a copy of package c, to build with -x. |
| 71 |
package d |
| 72 |
|
| 73 |
// static int D(int i) { return i; } |
| 74 |
import "C" |
| 75 |
|
| 76 |
import "m/a" |
| 77 |
|
| 78 |
func Fn(i int) (int, int) { |
| 79 |
return a.GoFn(i), int(C.D(C.int(i))) |
| 80 |
} |
| 81 |
|
| 82 |
-- main.go -- |
| 83 |
package main |
| 84 |
|
| 85 |
import "m/c" |
| 86 |
|
| 87 |
func main() { |
| 88 |
println(c.Fn(0)) |
| 89 |
} |