From 3d15f768814e8b06ae159b4da8f34a0c702a4cf1 Mon Sep 17 00:00:00 2001 From: Zhongpeng Lin Date: Mon, 21 May 2018 19:04:00 +0000 Subject: [PATCH] go/build: call ctxt.match for checking file name constraints This makes the checking of build tags in file names consistent to that of the build tags in `// +build` line. Fixed #25461 Change-Id: Iba14d1050f8aba44e7539ab3b8711af1980ccfe4 GitHub-Last-Rev: 11b14e239dd85e11e669919aab45494aee7c59a3 GitHub-Pull-Request: golang/go#25480 Reviewed-on: https://go-review.googlesource.com/113818 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/go/build/build.go | 28 +++------------------------- src/go/build/build_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/go/build/build.go b/src/go/build/build.go index 38380d323d..b19df28a63 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -1565,32 +1565,10 @@ func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool { } n := len(l) if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] { - if allTags != nil { - allTags[l[n-2]] = true - allTags[l[n-1]] = true - } - if l[n-1] != ctxt.GOARCH { - return false - } - if ctxt.GOOS == "android" && l[n-2] == "linux" { - return true - } - return l[n-2] == ctxt.GOOS + return ctxt.match(l[n-1], allTags) && ctxt.match(l[n-2], allTags) } - if n >= 1 && knownOS[l[n-1]] { - if allTags != nil { - allTags[l[n-1]] = true - } - if ctxt.GOOS == "android" && l[n-1] == "linux" { - return true - } - return l[n-1] == ctxt.GOOS - } - if n >= 1 && knownArch[l[n-1]] { - if allTags != nil { - allTags[l[n-1]] = true - } - return l[n-1] == ctxt.GOARCH + if n >= 1 && (knownOS[l[n-1]] || knownArch[l[n-1]]) { + return ctxt.match(l[n-1], allTags) } return true } diff --git a/src/go/build/build_test.go b/src/go/build/build_test.go index cb2ae3c775..3d4c92dcec 100644 --- a/src/go/build/build_test.go +++ b/src/go/build/build_test.go @@ -176,6 +176,18 @@ func TestShouldBuild(t *testing.T) { } } +func TestGoodOSArchFile(t *testing.T) { + ctx := &Context{BuildTags: []string{"linux"}, GOOS:"darwin"} + m := map[string]bool{} + want := map[string]bool{"linux": true} + if !ctx.goodOSArchFile("hello_linux.go", m) { + t.Errorf("goodOSArchFile(hello_linux.go) = false, want true") + } + if !reflect.DeepEqual(m, want) { + t.Errorf("goodOSArchFile(hello_linux.go) tags = %v, want %v", m, want) + } +} + type readNopCloser struct { io.Reader } -- 2.50.0