Skip to content

Commit

Permalink
Honor build tags when listing files
Browse files Browse the repository at this point in the history
This is done using a brand new build context that is configured using
the regular environment variables.

Signed-off-by: Steeve Morin <[email protected]>
  • Loading branch information
steeve committed Aug 18, 2021
1 parent 651024f commit 3512e59
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions go/tools/gopackagesdriver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 38 additions & 0 deletions go/tools/gopackagesdriver/build_context.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions go/tools/gopackagesdriver/flatpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions go/tools/gopackagesdriver/packageregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 3512e59

Please sign in to comment.