Gazelle select (7/x): platform constraints (#494) Add a table of platforms Gazelle should generate BUILD files for. Each platform has a label of a predefined config_setting rule and a list of tags that are true on that platform. At this time, platforms are not configurable, but they may be in the future if we find a compelling use case. Related #409
diff --git a/go/platform/BUILD b/go/platform/BUILD new file mode 100644 index 0000000..9eeee04 --- /dev/null +++ b/go/platform/BUILD
@@ -0,0 +1,29 @@ +package(default_visibility = ["//visibility:public"]) + +# This file declares a config_setting for each supported platform. +# These can be used in calls to select to choose platform-specific sources +# and dependencies. +# +# Eventually, we hope to be able to use platform or constraint_value instead, +# but Bazel doesn't support this yet. + +config_setting( + name = "darwin_amd64", + values = { + "cpu": "darwin", + }, +) + +config_setting( + name = "linux_amd64", + values = { + "cpu": "k8", + }, +) + +config_setting( + name = "windows_amd64", + values = { + "cpu": "x64_windows_msvc", + }, +)
diff --git a/go/tools/gazelle/generator/generator.go b/go/tools/gazelle/generator/generator.go index a2df4ca..cd9a4e4 100644 --- a/go/tools/gazelle/generator/generator.go +++ b/go/tools/gazelle/generator/generator.go
@@ -40,6 +40,7 @@ goPrefix string buildFileName string bctx build.Context + platforms packages.PlatformConstraints g rules.Generator } @@ -77,6 +78,7 @@ goPrefix: goPrefix, buildFileName: buildFileName, bctx: bctx, + platforms: packages.DefaultPlatformConstraints, g: rules.NewGenerator(repoRoot, goPrefix, external), }, nil }
diff --git a/go/tools/gazelle/packages/package.go b/go/tools/gazelle/packages/package.go index 0e77744..f55103b 100644 --- a/go/tools/gazelle/packages/package.go +++ b/go/tools/gazelle/packages/package.go
@@ -15,7 +15,29 @@ package packages -import "strings" +import ( + "fmt" + "strings" +) + +// PlatformConstraints is a map from config_setting labels (for example, +// "@io_bazel_rules_go//go/platform:linux_amd64") to a sets of build tags +// that are true on each platform (for example, "linux,amd64"). +type PlatformConstraints map[string]map[string]bool + +// DefaultPlatformConstraints is the default set of platforms that Gazelle +// will generate files for. These are the platforms that both Go and Bazel +// support. +var DefaultPlatformConstraints PlatformConstraints + +func init() { + DefaultPlatformConstraints = make(PlatformConstraints) + arch := "amd64" + for _, os := range []string{"darwin", "linux", "windows"} { + label := fmt.Sprintf("@io_bazel_rules_go//go/platform:%s_%s", os, arch) + DefaultPlatformConstraints[label] = map[string]bool{arch: true, os: true} + } +} // Package contains metadata about a Go package extracted from a directory. // It fills a similar role to go/build.Package, but it separates files by