1
2
3
4
5 package cache
6
7 import (
8 "fmt"
9 "os"
10 "path/filepath"
11 "sync"
12
13 "cmd/go/internal/base"
14 "cmd/go/internal/cfg"
15 )
16
17
18
19 func Default() Cache {
20 return initDefaultCacheOnce()
21 }
22
23 var initDefaultCacheOnce = sync.OnceValue(initDefaultCache)
24
25
26
27
28 const cacheREADME = `This directory holds cached build artifacts from the Go build system.
29 Run "go clean -cache" if the directory is getting too large.
30 Run "go clean -fuzzcache" to delete the fuzz cache.
31 See golang.org to learn more about Go.
32 `
33
34
35
36 func initDefaultCache() Cache {
37 dir, _ := DefaultDir()
38 if dir == "off" {
39 if defaultDirErr != nil {
40 base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
41 }
42 base.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12")
43 }
44 if err := os.MkdirAll(dir, 0777); err != nil {
45 base.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
46 }
47 if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
48
49 os.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666)
50 }
51
52 diskCache, err := Open(dir)
53 if err != nil {
54 base.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
55 }
56
57 if v := cfg.Getenv("GOCACHEPROG"); v != "" {
58 return startCacheProg(v, diskCache)
59 }
60
61 return diskCache
62 }
63
64 var (
65 defaultDirOnce sync.Once
66 defaultDir string
67 defaultDirChanged bool
68 defaultDirErr error
69 )
70
71
72
73
74 func DefaultDir() (string, bool) {
75
76
77
78
79
80 defaultDirOnce.Do(func() {
81 defaultDir = cfg.Getenv("GOCACHE")
82 if defaultDir != "" {
83 defaultDirChanged = true
84 if filepath.IsAbs(defaultDir) || defaultDir == "off" {
85 return
86 }
87 defaultDir = "off"
88 defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path")
89 return
90 }
91
92
93 dir, err := os.UserCacheDir()
94 if err != nil {
95 defaultDir = "off"
96 defaultDirChanged = true
97 defaultDirErr = fmt.Errorf("GOCACHE is not defined and %v", err)
98 return
99 }
100 defaultDir = filepath.Join(dir, "go-build")
101 })
102
103 return defaultDir, defaultDirChanged
104 }
105
View as plain text