1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 var goodCompilerFlags
= [][]string{
26 {"-fomit-frame-pointer"},
27 {"-fno-omit-frame-pointer"},
46 {"-mtune=happybirthday"},
48 {"-mno-stack-overflow"},
50 {"-mnop-fun-dllimport"},
57 {"-I", "/etc/passwd"},
59 {"-framework", "Chocolate"},
63 var badCompilerFlags
= [][]string{
85 {"-framework", "-Caffeine"},
86 {"-framework", "@Home"},
91 func TestCheckCompilerFlags(t
*testing
.T
) {
92 for _
, f
:= range goodCompilerFlags
{
93 if err
:= checkCompilerFlags("test", "test", f
); err
!= nil {
94 t
.Errorf("unexpected error for %q: %v", f
, err
)
97 for _
, f
:= range badCompilerFlags
{
98 if err
:= checkCompilerFlags("test", "test", f
); err
== nil {
99 t
.Errorf("missing error for %q", f
)
104 var goodLinkerFlags
= [][]string{
116 {"-fsanitize=hands"},
122 {"-mtune=happybirthday"},
126 {"-Wl,-rpath,$ORIGIN/foo"},
127 {"-Wl,--warn-error"},
128 {"-Wl,--no-warn-error"},
132 {"libcgosotest.dylib"},
135 {"-l", "/etc/passwd"},
138 {"-framework", "Chocolate"},
139 {"-Wl,-framework", "-Wl,Chocolate"},
140 {"-Wl,-framework,Chocolate"},
141 {"-Wl,-unresolved-symbols=ignore-all"},
144 var badLinkerFlags
= [][]string{
151 {"-fomit-frame-pointer"},
152 {"-fno-omit-frame-pointer"},
154 {"-fno-split-stack"},
157 {"-mstack-overflow"},
158 {"-mno-stack-overflow"},
159 {"-mnop-fun-dllimport"},
189 {"-framework", "-Caffeine"},
190 {"-framework", "@Home"},
191 {"-Wl,-framework,-Caffeine"},
192 {"-Wl,-framework", "-Wl,@Home"},
193 {"-Wl,-framework", "@Home"},
194 {"-Wl,-framework,Chocolate,@Home"},
201 func TestCheckLinkerFlags(t
*testing
.T
) {
202 for _
, f
:= range goodLinkerFlags
{
203 if err
:= checkLinkerFlags("test", "test", f
); err
!= nil {
204 t
.Errorf("unexpected error for %q: %v", f
, err
)
207 for _
, f
:= range badLinkerFlags
{
208 if err
:= checkLinkerFlags("test", "test", f
); err
== nil {
209 t
.Errorf("missing error for %q", f
)
214 func TestCheckFlagAllowDisallow(t
*testing
.T
) {
215 if err
:= checkCompilerFlags("TEST", "test", []string{"-disallow"}); err
== nil {
216 t
.Fatalf("missing error for -disallow")
218 os
.Setenv("CGO_TEST_ALLOW", "-disallo")
219 if err
:= checkCompilerFlags("TEST", "test", []string{"-disallow"}); err
== nil {
220 t
.Fatalf("missing error for -disallow with CGO_TEST_ALLOW=-disallo")
222 os
.Setenv("CGO_TEST_ALLOW", "-disallow")
223 if err
:= checkCompilerFlags("TEST", "test", []string{"-disallow"}); err
!= nil {
224 t
.Fatalf("unexpected error for -disallow with CGO_TEST_ALLOW=-disallow: %v", err
)
226 os
.Unsetenv("CGO_TEST_ALLOW")
228 if err
:= checkCompilerFlags("TEST", "test", []string{"-Wall"}); err
!= nil {
229 t
.Fatalf("unexpected error for -Wall: %v", err
)
231 os
.Setenv("CGO_TEST_DISALLOW", "-Wall")
232 if err
:= checkCompilerFlags("TEST", "test", []string{"-Wall"}); err
== nil {
233 t
.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall")
235 os
.Setenv("CGO_TEST_ALLOW", "-Wall") // disallow wins
236 if err
:= checkCompilerFlags("TEST", "test", []string{"-Wall"}); err
== nil {
237 t
.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall and CGO_TEST_ALLOW=-Wall")
240 os
.Setenv("CGO_TEST_ALLOW", "-fplugin.*")
241 os
.Setenv("CGO_TEST_DISALLOW", "-fplugin=lint.so")
242 if err
:= checkCompilerFlags("TEST", "test", []string{"-fplugin=faster.so"}); err
!= nil {
243 t
.Fatalf("unexpected error for -fplugin=faster.so: %v", err
)
245 if err
:= checkCompilerFlags("TEST", "test", []string{"-fplugin=lint.so"}); err
== nil {
246 t
.Fatalf("missing error for -fplugin=lint.so: %v", err
)