From 3512e5976cf38889c4a94096fe95d4ac3a038cd7 Mon Sep 17 00:00:00 2001 From: Steeve Morin Date: Wed, 18 Aug 2021 18:49:53 +0200 Subject: [PATCH] Honor build tags when listing files This is done using a brand new build context that is configured using the regular environment variables. Signed-off-by: Steeve Morin --- go/tools/gopackagesdriver/BUILD.bazel | 1 + go/tools/gopackagesdriver/build_context.go | 38 ++++++++++++++++++++ go/tools/gopackagesdriver/flatpackage.go | 7 ++++ go/tools/gopackagesdriver/packageregistry.go | 4 +++ 4 files changed, 50 insertions(+) create mode 100644 go/tools/gopackagesdriver/build_context.go diff --git a/go/tools/gopackagesdriver/BUILD.bazel b/go/tools/gopackagesdriver/BUILD.bazel index 89f7f1408d..5025cf6379 100644 --- a/go/tools/gopackagesdriver/BUILD.bazel +++ b/go/tools/gopackagesdriver/BUILD.bazel @@ -5,6 +5,7 @@ go_library( srcs = [ "bazel.go", "bazel_json_builder.go", + "build_context.go", "driver_request.go", "flatpackage.go", "json_packages_driver.go", diff --git a/go/tools/gopackagesdriver/build_context.go b/go/tools/gopackagesdriver/build_context.go new file mode 100644 index 0000000000..8074746c9f --- /dev/null +++ b/go/tools/gopackagesdriver/build_context.go @@ -0,0 +1,38 @@ +package main + +import ( + "go/build" + "os" + "path/filepath" + "strings" +) + +var buildContext = makeBuildContext() + +func makeBuildContext() *build.Context { + bctx := &build.Context{ + GOOS: getenvDefault("GOOS", build.Default.GOOS), + GOARCH: getenvDefault("GOARCH", build.Default.GOARCH), + GOROOT: getenvDefault("GOROOT", build.Default.GOROOT), + GOPATH: getenvDefault("GOPATH", build.Default.GOPATH), + BuildTags: strings.Split(getenvDefault("GOTAGS", ""), ","), + ReleaseTags: build.Default.ReleaseTags[:], + } + if v, ok := os.LookupEnv("CGO_ENABLED"); ok { + bctx.CgoEnabled = v == "1" + } else { + bctx.CgoEnabled = build.Default.CgoEnabled + } + return bctx +} + +func filterSourceFilesForTags(files []string) []string { + ret := make([]string, 0, len(files)) + for _, f := range files { + dir, filename := filepath.Split(f) + if match, _ := buildContext.MatchFile(dir, filename); match { + ret = append(ret, f) + } + } + return ret +} diff --git a/go/tools/gopackagesdriver/flatpackage.go b/go/tools/gopackagesdriver/flatpackage.go index 61aaadcde0..885acfd42e 100644 --- a/go/tools/gopackagesdriver/flatpackage.go +++ b/go/tools/gopackagesdriver/flatpackage.go @@ -102,6 +102,13 @@ func (fp *FlatPackage) ResolvePaths(prf PathResolverFunc) error { return nil } +// FilterFilesForBuildTags filters the source files given the current build +// tags. +func (fp *FlatPackage) FilterFilesForBuildTags() { + fp.GoFiles = filterSourceFilesForTags(fp.GoFiles) + fp.CompiledGoFiles = filterSourceFilesForTags(fp.CompiledGoFiles) +} + func (fp *FlatPackage) IsStdlib() bool { return fp.Standard } diff --git a/go/tools/gopackagesdriver/packageregistry.go b/go/tools/gopackagesdriver/packageregistry.go index e5d430614c..8c783abbf6 100644 --- a/go/tools/gopackagesdriver/packageregistry.go +++ b/go/tools/gopackagesdriver/packageregistry.go @@ -56,6 +56,10 @@ func (pr *PackageRegistry) Remove(pkgs ...*FlatPackage) *PackageRegistry { func (pr *PackageRegistry) ResolvePaths(prf PathResolverFunc) error { for _, pkg := range pr.packagesByImportPath { pkg.ResolvePaths(prf) + pkg.FilterFilesForBuildTags() + for _, f := range pkg.CompiledGoFiles { + pr.packagesByFile[f] = pkg + } for _, f := range pkg.CompiledGoFiles { pr.packagesByFile[f] = pkg }