1
2
3
4
5 package work
6
7 import (
8 "os"
9 "strings"
10 "testing"
11 )
12
13 var goodCompilerFlags = [][]string{
14 {"-DFOO"},
15 {"-Dfoo=bar"},
16 {"-Ufoo"},
17 {"-Ufoo1"},
18 {"-F/Qt"},
19 {"-F", "/Qt"},
20 {"-I/"},
21 {"-I/etc/passwd"},
22 {"-I."},
23 {"-O"},
24 {"-O2"},
25 {"-Osmall"},
26 {"-W"},
27 {"-Wall"},
28 {"-Wp,-Dfoo=bar"},
29 {"-Wp,-Ufoo"},
30 {"-Wp,-Dfoo1"},
31 {"-Wp,-Ufoo1"},
32 {"-flto"},
33 {"-fobjc-arc"},
34 {"-fno-objc-arc"},
35 {"-fomit-frame-pointer"},
36 {"-fno-omit-frame-pointer"},
37 {"-fpic"},
38 {"-fno-pic"},
39 {"-fPIC"},
40 {"-fno-PIC"},
41 {"-fpie"},
42 {"-fno-pie"},
43 {"-fPIE"},
44 {"-fno-PIE"},
45 {"-fsplit-stack"},
46 {"-fno-split-stack"},
47 {"-fstack-xxx"},
48 {"-fno-stack-xxx"},
49 {"-fsanitize=hands"},
50 {"-g"},
51 {"-ggdb"},
52 {"-march=souza"},
53 {"-mcpu=123"},
54 {"-mfpu=123"},
55 {"-mtune=happybirthday"},
56 {"-mstack-overflow"},
57 {"-mno-stack-overflow"},
58 {"-mmacosx-version"},
59 {"-mnop-fun-dllimport"},
60 {"-pthread"},
61 {"-std=c99"},
62 {"-xc"},
63 {"-D", "FOO"},
64 {"-D", "foo=bar"},
65 {"-I", "."},
66 {"-I", "/etc/passwd"},
67 {"-I", "世界"},
68 {"-I", "=/usr/include/libxml2"},
69 {"-I", "dir"},
70 {"-I", "$SYSROOT/dir"},
71 {"-isystem", "/usr/include/mozjs-68"},
72 {"-include", "/usr/include/mozjs-68/RequiredDefines.h"},
73 {"-framework", "Chocolate"},
74 {"-x", "c"},
75 {"-v"},
76 }
77
78 var badCompilerFlags = [][]string{
79 {"-D@X"},
80 {"-D-X"},
81 {"-Ufoo=bar"},
82 {"-F@dir"},
83 {"-F-dir"},
84 {"-I@dir"},
85 {"-I-dir"},
86 {"-O@1"},
87 {"-Wa,-foo"},
88 {"-W@foo"},
89 {"-Wp,-DX,-D@X"},
90 {"-Wp,-UX,-U@X"},
91 {"-g@gdb"},
92 {"-g-gdb"},
93 {"-march=@dawn"},
94 {"-march=-dawn"},
95 {"-std=@c99"},
96 {"-std=-c99"},
97 {"-x@c"},
98 {"-x-c"},
99 {"-D", "@foo"},
100 {"-D", "-foo"},
101 {"-I", "@foo"},
102 {"-I", "-foo"},
103 {"-I", "=@obj"},
104 {"-include", "@foo"},
105 {"-framework", "-Caffeine"},
106 {"-framework", "@Home"},
107 {"-x", "--c"},
108 {"-x", "@obj"},
109 }
110
111 func TestCheckCompilerFlags(t *testing.T) {
112 for _, f := range goodCompilerFlags {
113 if err := checkCompilerFlags("test", "test", f); err != nil {
114 t.Errorf("unexpected error for %q: %v", f, err)
115 }
116 }
117 for _, f := range badCompilerFlags {
118 if err := checkCompilerFlags("test", "test", f); err == nil {
119 t.Errorf("missing error for %q", f)
120 }
121 }
122 }
123
124 var goodLinkerFlags = [][]string{
125 {"-Fbar"},
126 {"-lbar"},
127 {"-Lbar"},
128 {"-fpic"},
129 {"-fno-pic"},
130 {"-fPIC"},
131 {"-fno-PIC"},
132 {"-fpie"},
133 {"-fno-pie"},
134 {"-fPIE"},
135 {"-fno-PIE"},
136 {"-fsanitize=hands"},
137 {"-g"},
138 {"-ggdb"},
139 {"-march=souza"},
140 {"-mcpu=123"},
141 {"-mfpu=123"},
142 {"-mtune=happybirthday"},
143 {"-pic"},
144 {"-pthread"},
145 {"-Wl,--hash-style=both"},
146 {"-Wl,-rpath,foo"},
147 {"-Wl,-rpath,$ORIGIN/foo"},
148 {"-Wl,-R", "/foo"},
149 {"-Wl,-R", "foo"},
150 {"-Wl,-R,foo"},
151 {"-Wl,--just-symbols=foo"},
152 {"-Wl,--just-symbols,foo"},
153 {"-Wl,--warn-error"},
154 {"-Wl,--no-warn-error"},
155 {"foo.so"},
156 {"_世界.dll"},
157 {"./x.o"},
158 {"libcgosotest.dylib"},
159 {"-F", "framework"},
160 {"-l", "."},
161 {"-l", "/etc/passwd"},
162 {"-l", "世界"},
163 {"-L", "framework"},
164 {"-framework", "Chocolate"},
165 {"-v"},
166 {"-Wl,-sectcreate,__TEXT,__info_plist,${SRCDIR}/Info.plist"},
167 {"-Wl,-framework", "-Wl,Chocolate"},
168 {"-Wl,-framework,Chocolate"},
169 {"-Wl,-unresolved-symbols=ignore-all"},
170 {"-Wl,-z,relro"},
171 {"-Wl,-z,relro,-z,now"},
172 {"-Wl,-z,now"},
173 {"-Wl,-z,noexecstack"},
174 {"libcgotbdtest.tbd"},
175 {"./libcgotbdtest.tbd"},
176 }
177
178 var badLinkerFlags = [][]string{
179 {"-DFOO"},
180 {"-Dfoo=bar"},
181 {"-W"},
182 {"-Wall"},
183 {"-fobjc-arc"},
184 {"-fno-objc-arc"},
185 {"-fomit-frame-pointer"},
186 {"-fno-omit-frame-pointer"},
187 {"-fsplit-stack"},
188 {"-fno-split-stack"},
189 {"-fstack-xxx"},
190 {"-fno-stack-xxx"},
191 {"-mstack-overflow"},
192 {"-mno-stack-overflow"},
193 {"-mnop-fun-dllimport"},
194 {"-std=c99"},
195 {"-xc"},
196 {"-D", "FOO"},
197 {"-D", "foo=bar"},
198 {"-I", "FOO"},
199 {"-L", "@foo"},
200 {"-L", "-foo"},
201 {"-x", "c"},
202 {"-D@X"},
203 {"-D-X"},
204 {"-I@dir"},
205 {"-I-dir"},
206 {"-O@1"},
207 {"-Wa,-foo"},
208 {"-W@foo"},
209 {"-g@gdb"},
210 {"-g-gdb"},
211 {"-march=@dawn"},
212 {"-march=-dawn"},
213 {"-std=@c99"},
214 {"-std=-c99"},
215 {"-x@c"},
216 {"-x-c"},
217 {"-D", "@foo"},
218 {"-D", "-foo"},
219 {"-I", "@foo"},
220 {"-I", "-foo"},
221 {"-l", "@foo"},
222 {"-l", "-foo"},
223 {"-framework", "-Caffeine"},
224 {"-framework", "@Home"},
225 {"-Wl,-framework,-Caffeine"},
226 {"-Wl,-framework", "-Wl,@Home"},
227 {"-Wl,-framework", "@Home"},
228 {"-Wl,-framework,Chocolate,@Home"},
229 {"-Wl,--hash-style=foo"},
230 {"-x", "--c"},
231 {"-x", "@obj"},
232 {"-Wl,-rpath,@foo"},
233 {"-Wl,-R,foo,bar"},
234 {"-Wl,-R,@foo"},
235 {"-Wl,--just-symbols,@foo"},
236 {"../x.o"},
237 {"-Wl,-R,"},
238 {"-Wl,-O"},
239 {"-Wl,-e="},
240 {"-Wl,-e,"},
241 {"-Wl,-R,-flag"},
242 }
243
244 func TestCheckLinkerFlags(t *testing.T) {
245 for _, f := range goodLinkerFlags {
246 if err := checkLinkerFlags("test", "test", f); err != nil {
247 t.Errorf("unexpected error for %q: %v", f, err)
248 }
249 }
250 for _, f := range badLinkerFlags {
251 if err := checkLinkerFlags("test", "test", f); err == nil {
252 t.Errorf("missing error for %q", f)
253 }
254 }
255 }
256
257 func TestCheckFlagAllowDisallow(t *testing.T) {
258 if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err == nil {
259 t.Fatalf("missing error for -disallow")
260 }
261 os.Setenv("CGO_TEST_ALLOW", "-disallo")
262 if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err == nil {
263 t.Fatalf("missing error for -disallow with CGO_TEST_ALLOW=-disallo")
264 }
265 os.Setenv("CGO_TEST_ALLOW", "-disallow")
266 if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err != nil {
267 t.Fatalf("unexpected error for -disallow with CGO_TEST_ALLOW=-disallow: %v", err)
268 }
269 os.Unsetenv("CGO_TEST_ALLOW")
270
271 if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err != nil {
272 t.Fatalf("unexpected error for -Wall: %v", err)
273 }
274 os.Setenv("CGO_TEST_DISALLOW", "-Wall")
275 if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err == nil {
276 t.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall")
277 }
278 os.Setenv("CGO_TEST_ALLOW", "-Wall")
279 if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err == nil {
280 t.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall and CGO_TEST_ALLOW=-Wall")
281 }
282
283 os.Setenv("CGO_TEST_ALLOW", "-fplugin.*")
284 os.Setenv("CGO_TEST_DISALLOW", "-fplugin=lint.so")
285 if err := checkCompilerFlags("TEST", "test", []string{"-fplugin=faster.so"}); err != nil {
286 t.Fatalf("unexpected error for -fplugin=faster.so: %v", err)
287 }
288 if err := checkCompilerFlags("TEST", "test", []string{"-fplugin=lint.so"}); err == nil {
289 t.Fatalf("missing error for -fplugin=lint.so: %v", err)
290 }
291 }
292
293 func TestCheckCompilerFlagsForInternalLink(t *testing.T) {
294
295 for _, f := range badCompilerFlags {
296 if err := checkCompilerFlagsForInternalLink("test", "test", f); err == nil {
297 t.Errorf("missing error for %q", f)
298 }
299 }
300
301
302
303 for _, f := range goodCompilerFlags {
304 foundLTO := false
305 for _, s := range f {
306 if strings.Contains(s, "-flto") {
307 foundLTO = true
308 }
309 }
310 if err := checkCompilerFlagsForInternalLink("test", "test", f); err != nil {
311
312 if !foundLTO {
313 t.Errorf("unexpected error for %q: %v", f, err)
314 }
315 } else {
316
317 if foundLTO {
318 t.Errorf("missing error for %q: %v", f, err)
319 }
320 }
321 }
322 }
323
View as plain text