49ca5a51923ac0fe920b7be053ce2959883ec735
[openwrt-packages.git] /
1 From 39f8e98946a0958a4f69ec28c78b8e5d46125e44 Mon Sep 17 00:00:00 2001
2 From: Ian Lance Taylor <iant@golang.org>
3 Date: Fri, 13 Nov 2020 11:05:37 -0800
4 Subject: [PATCH] [release-branch.go1.15] cmd/go: permit CGO_LDFLAGS to appear in //go:ldflag
5
6 For #42565
7 Fixes #42567
8
9 Change-Id: If7cf39905d124dbd54dfac6a53ee38270498efed
10 Reviewed-on: https://go-review.googlesource.com/c/go/+/269818
11 Trust: Ian Lance Taylor <iant@golang.org>
12 Run-TryBot: Ian Lance Taylor <iant@golang.org>
13 TryBot-Result: Go Bot <gobot@golang.org>
14 Reviewed-by: Jay Conrod <jayconrod@google.com>
15 (cherry picked from commit 782cf560db4c919790fdb476d1bbe18e5ddf5ffd)
16 ---
17
18 diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
19 index 13d4c8c..dc0c4fc 100644
20 --- a/src/cmd/go/internal/work/exec.go
21 +++ b/src/cmd/go/internal/work/exec.go
22 @@ -2766,6 +2766,21 @@
23                                 idx = bytes.Index(src, []byte(cgoLdflag))
24                         }
25                 }
26 +
27 +               // We expect to find the contents of cgoLDFLAGS in flags.
28 +               if len(cgoLDFLAGS) > 0 {
29 +               outer:
30 +                       for i := range flags {
31 +                               for j, f := range cgoLDFLAGS {
32 +                                       if f != flags[i+j] {
33 +                                               continue outer
34 +                                       }
35 +                               }
36 +                               flags = append(flags[:i], flags[i+len(cgoLDFLAGS):]...)
37 +                               break
38 +                       }
39 +               }
40 +
41                 if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil {
42                         return nil, nil, err
43                 }
44 diff --git a/src/cmd/go/testdata/script/ldflag.txt b/src/cmd/go/testdata/script/ldflag.txt
45 new file mode 100644
46 index 0000000..6ceb33b
47 --- /dev/null
48 +++ b/src/cmd/go/testdata/script/ldflag.txt
49 @@ -0,0 +1,44 @@
50 +# Issue #42565
51 +
52 +[!cgo] skip
53 +
54 +# We can't build package bad, which uses #cgo LDFLAGS.
55 +cd bad
56 +! go build
57 +stderr no-such-warning
58 +
59 +# We can build package ok with the same flags in CGO_LDFLAGS.
60 +env CGO_LDFLAGS=-Wno-such-warning -Wno-unknown-warning-option
61 +cd ../ok
62 +go build
63 +
64 +# Build a main program that actually uses LDFLAGS.
65 +cd ..
66 +go build -ldflags=-v
67 +
68 +# Because we passed -v the Go linker should print the external linker
69 +# command which should include the flag we passed in CGO_LDFLAGS.
70 +stderr no-such-warning
71 +
72 +-- go.mod --
73 +module ldflag
74 +
75 +-- bad/bad.go --
76 +package bad
77 +
78 +// #cgo LDFLAGS: -Wno-such-warning -Wno-unknown-warning
79 +import "C"
80 +
81 +func F() {}
82 +-- ok/ok.go --
83 +package ok
84 +
85 +import "C"
86 +
87 +func F() {}
88 +-- main.go --
89 +package main
90 +
91 +import _ "ldflag/ok"
92 +
93 +func main() {}
git clone https://git.99rst.org/PROJECT