libgo: Update to Go 1.3 release.
[official-gcc.git] / libgo / go / go / build / deps_test.go
blob3a795fdcc4bbed23494d5c4bc3a2a1d177f78da0
1 // Copyright 2012 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.
5 // This file exercises the import parser but also checks that
6 // some low-level packages do not have new dependencies added.
8 package build
10 import (
11 "runtime"
12 "sort"
13 "testing"
16 // pkgDeps defines the expected dependencies between packages in
17 // the Go source tree. It is a statement of policy.
18 // Changes should not be made to this map without prior discussion.
20 // The map contains two kinds of entries:
21 // 1) Lower-case keys are standard import paths and list the
22 // allowed imports in that package.
23 // 2) Upper-case keys define aliases for package sets, which can then
24 // be used as dependencies by other rules.
26 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
28 var pkgDeps = map[string][]string{
29 // L0 is the lowest level, core, nearly unavoidable packages.
30 "errors": {},
31 "io": {"errors", "sync"},
32 "runtime": {"unsafe"},
33 "sync": {"runtime", "sync/atomic", "unsafe"},
34 "sync/atomic": {"unsafe"},
35 "unsafe": {},
37 "L0": {
38 "errors",
39 "io",
40 "runtime",
41 "sync",
42 "sync/atomic",
43 "unsafe",
46 // L1 adds simple functions and strings processing,
47 // but not Unicode tables.
48 "math": {"unsafe"},
49 "math/cmplx": {"math"},
50 "math/rand": {"L0", "math"},
51 "sort": {},
52 "strconv": {"L0", "unicode/utf8", "math"},
53 "unicode/utf16": {},
54 "unicode/utf8": {},
56 "L1": {
57 "L0",
58 "math",
59 "math/cmplx",
60 "math/rand",
61 "sort",
62 "strconv",
63 "unicode/utf16",
64 "unicode/utf8",
67 // L2 adds Unicode and strings processing.
68 "bufio": {"L0", "unicode/utf8", "bytes"},
69 "bytes": {"L0", "unicode", "unicode/utf8"},
70 "path": {"L0", "unicode/utf8", "strings"},
71 "strings": {"L0", "unicode", "unicode/utf8"},
72 "unicode": {},
74 "L2": {
75 "L1",
76 "bufio",
77 "bytes",
78 "path",
79 "strings",
80 "unicode",
83 // L3 adds reflection and some basic utility packages
84 // and interface definitions, but nothing that makes
85 // system calls.
86 "crypto": {"L2", "hash"}, // interfaces
87 "crypto/cipher": {"L2", "crypto/subtle"}, // interfaces
88 "crypto/subtle": {},
89 "encoding/base32": {"L2"},
90 "encoding/base64": {"L2"},
91 "encoding/binary": {"L2", "reflect"},
92 "hash": {"L2"}, // interfaces
93 "hash/adler32": {"L2", "hash"},
94 "hash/crc32": {"L2", "hash"},
95 "hash/crc64": {"L2", "hash"},
96 "hash/fnv": {"L2", "hash"},
97 "image": {"L2", "image/color"}, // interfaces
98 "image/color": {"L2"}, // interfaces
99 "image/color/palette": {"L2", "image/color"},
100 "reflect": {"L2"},
102 "L3": {
103 "L2",
104 "crypto",
105 "crypto/cipher",
106 "crypto/subtle",
107 "encoding/base32",
108 "encoding/base64",
109 "encoding/binary",
110 "hash",
111 "hash/adler32",
112 "hash/crc32",
113 "hash/crc64",
114 "hash/fnv",
115 "image",
116 "image/color",
117 "image/color/palette",
118 "reflect",
121 // End of linear dependency definitions.
123 // Operating system access.
124 "syscall": {"L0", "unicode/utf16"},
125 "time": {"L0", "syscall"},
126 "os": {"L1", "os", "syscall", "time"},
127 "path/filepath": {"L2", "os", "syscall"},
128 "io/ioutil": {"L2", "os", "path/filepath", "time"},
129 "os/exec": {"L2", "os", "path/filepath", "syscall"},
130 "os/signal": {"L2", "os", "syscall"},
132 // OS enables basic operating system functionality,
133 // but not direct use of package syscall, nor os/signal.
134 "OS": {
135 "io/ioutil",
136 "os",
137 "os/exec",
138 "path/filepath",
139 "time",
142 // Formatted I/O: few dependencies (L1) but we must add reflect.
143 "fmt": {"L1", "os", "reflect"},
144 "log": {"L1", "os", "fmt", "time"},
146 // Packages used by testing must be low-level (L2+fmt).
147 "regexp": {"L2", "regexp/syntax"},
148 "regexp/syntax": {"L2"},
149 "runtime/debug": {"L2", "fmt", "io/ioutil", "os", "time"},
150 "runtime/pprof": {"L2", "fmt", "text/tabwriter"},
151 "text/tabwriter": {"L2"},
153 "testing": {"L2", "flag", "fmt", "os", "runtime/pprof", "time"},
154 "testing/iotest": {"L2", "log"},
155 "testing/quick": {"L2", "flag", "fmt", "reflect"},
157 // L4 is defined as L3+fmt+log+time, because in general once
158 // you're using L3 packages, use of fmt, log, or time is not a big deal.
159 "L4": {
160 "L3",
161 "fmt",
162 "log",
163 "time",
166 // Go parser.
167 "go/ast": {"L4", "OS", "go/scanner", "go/token"},
168 "go/doc": {"L4", "go/ast", "go/token", "regexp", "text/template"},
169 "go/parser": {"L4", "OS", "go/ast", "go/scanner", "go/token"},
170 "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
171 "go/scanner": {"L4", "OS", "go/token"},
172 "go/token": {"L4"},
174 "GOPARSER": {
175 "go/ast",
176 "go/doc",
177 "go/parser",
178 "go/printer",
179 "go/scanner",
180 "go/token",
183 // One of a kind.
184 "archive/tar": {"L4", "OS", "syscall"},
185 "archive/zip": {"L4", "OS", "compress/flate"},
186 "compress/bzip2": {"L4"},
187 "compress/flate": {"L4"},
188 "compress/gzip": {"L4", "compress/flate"},
189 "compress/lzw": {"L4"},
190 "compress/zlib": {"L4", "compress/flate"},
191 "database/sql": {"L4", "container/list", "database/sql/driver"},
192 "database/sql/driver": {"L4", "time"},
193 "debug/dwarf": {"L4"},
194 "debug/elf": {"L4", "OS", "debug/dwarf"},
195 "debug/gosym": {"L4"},
196 "debug/macho": {"L4", "OS", "debug/dwarf"},
197 "debug/pe": {"L4", "OS", "debug/dwarf"},
198 "encoding": {"L4"},
199 "encoding/ascii85": {"L4"},
200 "encoding/asn1": {"L4", "math/big"},
201 "encoding/csv": {"L4"},
202 "encoding/gob": {"L4", "OS", "encoding"},
203 "encoding/hex": {"L4"},
204 "encoding/json": {"L4", "encoding"},
205 "encoding/pem": {"L4"},
206 "encoding/xml": {"L4", "encoding"},
207 "flag": {"L4", "OS"},
208 "go/build": {"L4", "OS", "GOPARSER"},
209 "html": {"L4"},
210 "image/draw": {"L4"},
211 "image/gif": {"L4", "compress/lzw", "image/color/palette", "image/draw"},
212 "image/jpeg": {"L4"},
213 "image/png": {"L4", "compress/zlib"},
214 "index/suffixarray": {"L4", "regexp"},
215 "math/big": {"L4"},
216 "mime": {"L4", "OS", "syscall"},
217 "net/url": {"L4"},
218 "text/scanner": {"L4", "OS"},
219 "text/template/parse": {"L4"},
221 "html/template": {
222 "L4", "OS", "encoding/json", "html", "text/template",
223 "text/template/parse",
225 "text/template": {
226 "L4", "OS", "net/url", "text/template/parse",
229 // Cgo.
230 "runtime/cgo": {"L0", "C"},
231 "CGO": {"C", "runtime/cgo"},
233 // Fake entry to satisfy the pseudo-import "C"
234 // that shows up in programs that use cgo.
235 "C": {},
237 // Plan 9 alone needs io/ioutil and os.
238 "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"},
240 // Basic networking.
241 // Because net must be used by any package that wants to
242 // do networking portably, it must have a small dependency set: just L1+basic os.
243 "net": {"L1", "CGO", "os", "syscall", "time"},
245 // NET enables use of basic network-related packages.
246 "NET": {
247 "net",
248 "mime",
249 "net/textproto",
250 "net/url",
253 // Uses of networking.
254 "log/syslog": {"L4", "OS", "net"},
255 "net/mail": {"L4", "NET", "OS"},
256 "net/textproto": {"L4", "OS", "net"},
258 // Core crypto.
259 "crypto/aes": {"L3"},
260 "crypto/des": {"L3"},
261 "crypto/hmac": {"L3"},
262 "crypto/md5": {"L3"},
263 "crypto/rc4": {"L3"},
264 "crypto/sha1": {"L3"},
265 "crypto/sha256": {"L3"},
266 "crypto/sha512": {"L3"},
268 "CRYPTO": {
269 "crypto/aes",
270 "crypto/des",
271 "crypto/hmac",
272 "crypto/md5",
273 "crypto/rc4",
274 "crypto/sha1",
275 "crypto/sha256",
276 "crypto/sha512",
279 // Random byte, number generation.
280 // This would be part of core crypto except that it imports
281 // math/big, which imports fmt.
282 "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall"},
284 // Mathematical crypto: dependencies on fmt (L4) and math/big.
285 // We could avoid some of the fmt, but math/big imports fmt anyway.
286 "crypto/dsa": {"L4", "CRYPTO", "math/big"},
287 "crypto/ecdsa": {"L4", "CRYPTO", "crypto/elliptic", "math/big"},
288 "crypto/elliptic": {"L4", "CRYPTO", "math/big"},
289 "crypto/rsa": {"L4", "CRYPTO", "crypto/rand", "math/big"},
291 "CRYPTO-MATH": {
292 "CRYPTO",
293 "crypto/dsa",
294 "crypto/ecdsa",
295 "crypto/elliptic",
296 "crypto/rand",
297 "crypto/rsa",
298 "encoding/asn1",
299 "math/big",
302 // SSL/TLS.
303 "crypto/tls": {
304 "L4", "CRYPTO-MATH", "CGO", "OS",
305 "container/list", "crypto/x509", "encoding/pem", "net", "syscall",
307 "crypto/x509": {
308 "L4", "CRYPTO-MATH", "OS", "CGO",
309 "crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "syscall",
311 "crypto/x509/pkix": {"L4", "CRYPTO-MATH"},
313 // Simple net+crypto-aware packages.
314 "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto"},
315 "net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"},
317 // HTTP, kingpin of dependencies.
318 "net/http": {
319 "L4", "NET", "OS",
320 "compress/gzip", "crypto/tls", "mime/multipart", "runtime/debug",
323 // HTTP-using packages.
324 "expvar": {"L4", "OS", "encoding/json", "net/http"},
325 "net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
326 "net/http/fcgi": {"L4", "NET", "OS", "net/http", "net/http/cgi"},
327 "net/http/httptest": {"L4", "NET", "OS", "crypto/tls", "flag", "net/http"},
328 "net/http/httputil": {"L4", "NET", "OS", "net/http"},
329 "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof"},
330 "net/rpc": {"L4", "NET", "encoding/gob", "net/http", "text/template"},
331 "net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"},
334 // isMacro reports whether p is a package dependency macro
335 // (uppercase name).
336 func isMacro(p string) bool {
337 return 'A' <= p[0] && p[0] <= 'Z'
340 func allowed(pkg string) map[string]bool {
341 m := map[string]bool{}
342 var allow func(string)
343 allow = func(p string) {
344 if m[p] {
345 return
347 m[p] = true // set even for macros, to avoid loop on cycle
349 // Upper-case names are macro-expanded.
350 if isMacro(p) {
351 for _, pp := range pkgDeps[p] {
352 allow(pp)
356 for _, pp := range pkgDeps[pkg] {
357 allow(pp)
359 return m
362 var bools = []bool{false, true}
363 var geese = []string{"darwin", "dragonfly", "freebsd", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows"}
364 var goarches = []string{"386", "amd64", "arm", "arm64"}
366 type osPkg struct {
367 goos, pkg string
370 // allowedErrors are the operating systems and packages known to contain errors
371 // (currently just "no Go source files")
372 var allowedErrors = map[osPkg]bool{
373 osPkg{"windows", "log/syslog"}: true,
374 osPkg{"plan9", "log/syslog"}: true,
377 func TestDependencies(t *testing.T) {
378 if runtime.GOOS == "nacl" {
379 // NaCl tests run in a limited file system and we do not
380 // provide access to every source file.
381 t.Skip("skipping on NaCl")
383 var all []string
385 for k := range pkgDeps {
386 all = append(all, k)
388 sort.Strings(all)
390 ctxt := Default
391 test := func(mustImport bool) {
392 for _, pkg := range all {
393 if isMacro(pkg) {
394 continue
396 if pkg == "runtime/cgo" && !ctxt.CgoEnabled {
397 continue
399 p, err := ctxt.Import(pkg, "", 0)
400 if err != nil {
401 if allowedErrors[osPkg{ctxt.GOOS, pkg}] {
402 continue
404 if !ctxt.CgoEnabled && pkg == "runtime/cgo" {
405 continue
407 // Some of the combinations we try might not
408 // be reasonable (like arm,plan9,cgo), so ignore
409 // errors for the auto-generated combinations.
410 if !mustImport {
411 continue
413 t.Errorf("%s/%s/cgo=%v %v", ctxt.GOOS, ctxt.GOARCH, ctxt.CgoEnabled, err)
414 continue
416 ok := allowed(pkg)
417 var bad []string
418 for _, imp := range p.Imports {
419 if !ok[imp] {
420 bad = append(bad, imp)
423 if bad != nil {
424 t.Errorf("%s/%s/cgo=%v unexpected dependency: %s imports %v", ctxt.GOOS, ctxt.GOARCH, ctxt.CgoEnabled, pkg, bad)
428 test(true)
430 if testing.Short() {
431 t.Logf("skipping other systems")
432 return
435 for _, ctxt.GOOS = range geese {
436 for _, ctxt.GOARCH = range goarches {
437 for _, ctxt.CgoEnabled = range bools {
438 test(false)