...

Source file src/pkg/cmd/go/internal/cache/default.go

     1	// Copyright 2017 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.
     4	
     5	package cache
     6	
     7	import (
     8		"fmt"
     9		"io/ioutil"
    10		"os"
    11		"path/filepath"
    12		"sync"
    13	
    14		"cmd/go/internal/base"
    15		"cmd/go/internal/cfg"
    16	)
    17	
    18	// Default returns the default cache to use, or nil if no cache should be used.
    19	func Default() *Cache {
    20		defaultOnce.Do(initDefaultCache)
    21		return defaultCache
    22	}
    23	
    24	var (
    25		defaultOnce  sync.Once
    26		defaultCache *Cache
    27	)
    28	
    29	// cacheREADME is a message stored in a README in the cache directory.
    30	// Because the cache lives outside the normal Go trees, we leave the
    31	// README as a courtesy to explain where it came from.
    32	const cacheREADME = `This directory holds cached build artifacts from the Go build system.
    33	Run "go clean -cache" if the directory is getting too large.
    34	See golang.org to learn more about Go.
    35	`
    36	
    37	// initDefaultCache does the work of finding the default cache
    38	// the first time Default is called.
    39	func initDefaultCache() {
    40		dir := DefaultDir()
    41		if dir == "off" {
    42			if defaultDirErr != nil {
    43				base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
    44			}
    45			base.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12")
    46		}
    47		if err := os.MkdirAll(dir, 0777); err != nil {
    48			base.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
    49		}
    50		if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
    51			// Best effort.
    52			ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666)
    53		}
    54	
    55		c, err := Open(dir)
    56		if err != nil {
    57			base.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
    58		}
    59		defaultCache = c
    60	}
    61	
    62	var (
    63		defaultDirOnce sync.Once
    64		defaultDir     string
    65		defaultDirErr  error
    66	)
    67	
    68	// DefaultDir returns the effective GOCACHE setting.
    69	// It returns "off" if the cache is disabled.
    70	func DefaultDir() string {
    71		// Save the result of the first call to DefaultDir for later use in
    72		// initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that
    73		// subprocesses will inherit it, but that means initDefaultCache can't
    74		// otherwise distinguish between an explicit "off" and a UserCacheDir error.
    75	
    76		defaultDirOnce.Do(func() {
    77			defaultDir = cfg.Getenv("GOCACHE")
    78			if filepath.IsAbs(defaultDir) || defaultDir == "off" {
    79				return
    80			}
    81			if defaultDir != "" {
    82				defaultDir = "off"
    83				defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path")
    84				return
    85			}
    86	
    87			// Compute default location.
    88			dir, err := os.UserCacheDir()
    89			if err != nil {
    90				defaultDir = "off"
    91				defaultDirErr = fmt.Errorf("GOCACHE is not defined and %v", err)
    92				return
    93			}
    94			defaultDir = filepath.Join(dir, "go-build")
    95		})
    96	
    97		return defaultDir
    98	}
    99	

View as plain text