Source file src/cmd/go/internal/imports/tags.go

     1  // Copyright 2018 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 imports
     6  
     7  import (
     8  	"cmd/go/internal/cfg"
     9  	"sync"
    10  )
    11  
    12  // Tags returns a set of build tags that are true for the target platform.
    13  // It includes GOOS, GOARCH, the compiler, possibly "cgo",
    14  // release tags like "go1.13", and user-specified build tags.
    15  func Tags() map[string]bool {
    16  	return loadTagsOnce()
    17  }
    18  
    19  var loadTagsOnce = sync.OnceValue(loadTags)
    20  
    21  func loadTags() map[string]bool {
    22  	tags := map[string]bool{
    23  		cfg.BuildContext.GOOS:     true,
    24  		cfg.BuildContext.GOARCH:   true,
    25  		cfg.BuildContext.Compiler: true,
    26  	}
    27  	if cfg.BuildContext.CgoEnabled {
    28  		tags["cgo"] = true
    29  	}
    30  	for _, tag := range cfg.BuildContext.BuildTags {
    31  		tags[tag] = true
    32  	}
    33  	for _, tag := range cfg.BuildContext.ToolTags {
    34  		tags[tag] = true
    35  	}
    36  	for _, tag := range cfg.BuildContext.ReleaseTags {
    37  		tags[tag] = true
    38  	}
    39  	return tags
    40  }
    41  
    42  // AnyTags returns a special set of build tags that satisfy nearly all
    43  // build tag expressions. Only "ignore" and malformed build tag requirements
    44  // are considered false.
    45  func AnyTags() map[string]bool {
    46  	return anyTagsOnce()
    47  }
    48  
    49  var anyTagsOnce = sync.OnceValue(func() map[string]bool {
    50  	return map[string]bool{"*": true}
    51  })
    52  

View as plain text