]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/go/internal/load/pkg.go
[dev.boringcrypto] all: merge master into dev.boringcrypto
[gostls13.git] / src / cmd / go / internal / load / pkg.go
index f9051cce3dd0bc902f9178bc42a71c093fc68e96..aba1dfd1c163bafe722229a84c8fb4a5ba2ff9b1 100644 (file)
@@ -504,7 +504,7 @@ type importError struct {
        err        error // created with fmt.Errorf
 }
 
-func ImportErrorf(path, format string, args ...interface{}) ImportPathError {
+func ImportErrorf(path, format string, args ...any) ImportPathError {
        err := &importError{importPath: path, err: fmt.Errorf(format, args...)}
        if errStr := err.Error(); !strings.Contains(errStr, path) {
                panic(fmt.Sprintf("path %q not in error %q", path, errStr))
@@ -595,10 +595,10 @@ func ClearPackageCachePartial(args []string) {
                        delete(packageCache, arg)
                }
        }
-       resolvedImportCache.DeleteIf(func(key interface{}) bool {
+       resolvedImportCache.DeleteIf(func(key any) bool {
                return shouldDelete[key.(importSpec).path]
        })
-       packageDataCache.DeleteIf(func(key interface{}) bool {
+       packageDataCache.DeleteIf(func(key any) bool {
                return shouldDelete[key.(string)]
        })
 }
@@ -611,7 +611,7 @@ func ReloadPackageNoFlags(arg string, stk *ImportStack) *Package {
        p := packageCache[arg]
        if p != nil {
                delete(packageCache, arg)
-               resolvedImportCache.DeleteIf(func(key interface{}) bool {
+               resolvedImportCache.DeleteIf(func(key any) bool {
                        return key.(importSpec).path == p.ImportPath
                })
                packageDataCache.Delete(p.ImportPath)
@@ -823,7 +823,7 @@ func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoo
                parentIsStd: parentIsStd,
                mode:        mode,
        }
-       r := resolvedImportCache.Do(importKey, func() interface{} {
+       r := resolvedImportCache.Do(importKey, func() any {
                var r resolvedImport
                if build.IsLocalImport(path) {
                        r.dir = filepath.Join(parentDir, path)
@@ -850,7 +850,7 @@ func loadPackageData(ctx context.Context, path, parentPath, parentDir, parentRoo
 
        // Load the package from its directory. If we already found the package's
        // directory when resolving its import path, use that.
-       data := packageDataCache.Do(r.path, func() interface{} {
+       data := packageDataCache.Do(r.path, func() any {
                loaded = true
                var data packageData
                if r.dir != "" {
@@ -1069,7 +1069,7 @@ func cleanImport(path string) string {
 var isDirCache par.Cache
 
 func isDir(path string) bool {
-       return isDirCache.Do(path, func() interface{} {
+       return isDirCache.Do(path, func() any {
                fi, err := fsys.Stat(path)
                return err == nil && fi.IsDir()
        }).(bool)
@@ -1197,7 +1197,7 @@ var (
 
 // goModPath returns the module path in the go.mod in dir, if any.
 func goModPath(dir string) (path string) {
-       return goModPathCache.Do(dir, func() interface{} {
+       return goModPathCache.Do(dir, func() any {
                data, err := os.ReadFile(filepath.Join(dir, "go.mod"))
                if err != nil {
                        return ""
@@ -2023,13 +2023,18 @@ func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[st
        for _, pattern = range patterns {
                pid++
 
+               glob := pattern
+               all := strings.HasPrefix(pattern, "all:")
+               if all {
+                       glob = pattern[len("all:"):]
+               }
                // Check pattern is valid for //go:embed.
-               if _, err := path.Match(pattern, ""); err != nil || !validEmbedPattern(pattern) {
+               if _, err := path.Match(glob, ""); err != nil || !validEmbedPattern(glob) {
                        return nil, nil, fmt.Errorf("invalid pattern syntax")
                }
 
                // Glob to find matches.
-               match, err := fsys.Glob(pkgdir + string(filepath.Separator) + filepath.FromSlash(pattern))
+               match, err := fsys.Glob(pkgdir + string(filepath.Separator) + filepath.FromSlash(glob))
                if err != nil {
                        return nil, nil, err
                }
@@ -2092,7 +2097,7 @@ func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[st
                                        }
                                        rel := filepath.ToSlash(path[len(pkgdir)+1:])
                                        name := info.Name()
-                                       if path != file && (isBadEmbedName(name) || name[0] == '.' || name[0] == '_') {
+                                       if path != file && (isBadEmbedName(name) || ((name[0] == '.' || name[0] == '_') && !all)) {
                                                // Ignore bad names, assuming they won't go into modules.
                                                // Also avoid hidden files that user may not know about.
                                                // See golang.org/issue/42328.
@@ -2204,6 +2209,10 @@ func (p *Package) collectDeps() {
        }
 }
 
+// vcsStatusCache maps repository directories (string)
+// to their VCS information (vcsStatusError).
+var vcsStatusCache par.Cache
+
 // setBuildInfo gathers build information, formats it as a string to be
 // embedded in the binary, then sets p.Internal.BuildInfo to that string.
 // setBuildInfo should only be called on a main package with no errors.
@@ -2218,7 +2227,7 @@ func (p *Package) setBuildInfo() {
        // executables always appear stale unless the user sets the same flags.
        // Perhaps it's safe to omit those flags when GO_GCFLAGS and GO_LDFLAGS
        // are not set?
-       setPkgErrorf := func(format string, args ...interface{}) {
+       setPkgErrorf := func(format string, args ...any) {
                if p.Error == nil {
                        p.Error = &PackageError{Err: fmt.Errorf(format, args...)}
                }
@@ -2282,33 +2291,57 @@ func (p *Package) setBuildInfo() {
                Deps: deps,
        }
        appendSetting := func(key, value string) {
+               value = strings.ReplaceAll(value, "\n", " ") // make value safe
                info.Settings = append(info.Settings, debug.BuildSetting{Key: key, Value: value})
        }
 
        // Add command-line flags relevant to the build.
        // This is informational, not an exhaustive list.
-       if cfg.BuildBuildinfo && !p.Standard {
-               appendSetting("compiler", cfg.BuildContext.Compiler)
-               if BuildAsmflags.present {
-                       appendSetting("asmflags", BuildAsmflags.String())
+       // Please keep the list sorted.
+       if !p.Standard {
+               if cfg.BuildASan {
+                       appendSetting("-asan", "true")
                }
-               if BuildGcflags.present && cfg.BuildContext.Compiler == "gc" {
-                       appendSetting("gcflags", BuildGcflags.String())
+               if BuildAsmflags.present {
+                       appendSetting("-asmflags", BuildAsmflags.String())
                }
+               appendSetting("-compiler", cfg.BuildContext.Compiler)
                if BuildGccgoflags.present && cfg.BuildContext.Compiler == "gccgo" {
-                       appendSetting("gccgoflags", BuildGccgoflags.String())
+                       appendSetting("-gccgoflags", BuildGccgoflags.String())
+               }
+               if BuildGcflags.present && cfg.BuildContext.Compiler == "gc" {
+                       appendSetting("-gcflags", BuildGcflags.String())
                }
                if BuildLdflags.present {
-                       appendSetting("ldflags", BuildLdflags.String())
+                       appendSetting("-ldflags", BuildLdflags.String())
+               }
+               if cfg.BuildMSan {
+                       appendSetting("-msan", "true")
                }
-               tags := append(cfg.BuildContext.BuildTags, cfg.BuildContext.ToolTags...)
-               appendSetting("tags", strings.Join(tags, ","))
-               appendSetting("CGO_ENABLED", strconv.FormatBool(cfg.BuildContext.CgoEnabled))
+               if cfg.BuildRace {
+                       appendSetting("-race", "true")
+               }
+               if tags := cfg.BuildContext.BuildTags; len(tags) > 0 {
+                       appendSetting("-tags", strings.Join(tags, ","))
+               }
+               cgo := "0"
                if cfg.BuildContext.CgoEnabled {
-                       for _, name := range []string{"CGO_CPPFLAGS", "CGO_CFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} {
+                       cgo = "1"
+               }
+               appendSetting("CGO_ENABLED", cgo)
+               if cfg.BuildContext.CgoEnabled {
+                       for _, name := range []string{"CGO_CFLAGS", "CGO_CPPFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} {
                                appendSetting(name, cfg.Getenv(name))
                        }
                }
+               appendSetting("GOARCH", cfg.BuildContext.GOARCH)
+               if cfg.GOEXPERIMENT != "" {
+                       appendSetting("GOEXPERIMENT", cfg.GOEXPERIMENT)
+               }
+               appendSetting("GOOS", cfg.BuildContext.GOOS)
+               if key, val := cfg.GetArchEnv(); key != "" && val != "" {
+                       appendSetting(key, val)
+               }
        }
 
        // Add VCS status if all conditions are true:
@@ -2366,19 +2399,29 @@ func (p *Package) setBuildInfo() {
                        return
                }
 
-               st, err := vcsCmd.Status(vcsCmd, repoDir)
-               if err != nil {
+               type vcsStatusError struct {
+                       Status vcs.Status
+                       Err    error
+               }
+               cached := vcsStatusCache.Do(repoDir, func() any {
+                       st, err := vcsCmd.Status(vcsCmd, repoDir)
+                       return vcsStatusError{st, err}
+               }).(vcsStatusError)
+               if err := cached.Err; err != nil {
                        setVCSError(err)
                        return
                }
+               st := cached.Status
+
+               appendSetting("vcs", vcsCmd.Cmd)
                if st.Revision != "" {
-                       appendSetting(vcsCmd.Cmd+"revision", st.Revision)
+                       appendSetting("vcs.revision", st.Revision)
                }
                if !st.CommitTime.IsZero() {
                        stamp := st.CommitTime.UTC().Format(time.RFC3339Nano)
-                       appendSetting(vcsCmd.Cmd+"committime", stamp)
+                       appendSetting("vcs.time", stamp)
                }
-               appendSetting(vcsCmd.Cmd+"uncommitted", strconv.FormatBool(st.Uncommitted))
+               appendSetting("vcs.modified", strconv.FormatBool(st.Uncommitted))
        }
 
        text, err := info.MarshalText()