Commit f07a99e3 authored by Jay Conrod's avatar Jay Conrod

cmd/go: recognize android suffix when constructing build list

cmd/go/internal/imports.ScanDir extracts a list of imports from a
directory. It's used instead of go/build.ImportDir when constructing
the build list. GOOS and GOARCH may be used to filter files.

With this change, imports.MatchFile understands that when the
"android" tag is set, the "linux" tag is implied.

Fixes #30888

Change-Id: Ia29bd1590b69c9183ab14a879d5fc1b639f8eaef
Reviewed-on: https://go-review.googlesource.com/c/go/+/168378
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent 5ee1d5d3
......@@ -184,13 +184,13 @@ func MatchFile(name string, tags map[string]bool) bool {
}
n := len(l)
if n >= 2 && KnownOS[l[n-2]] && KnownArch[l[n-1]] {
return tags[l[n-2]] && tags[l[n-1]]
return matchTag(l[n-2], tags, true) && matchTag(l[n-1], tags, true)
}
if n >= 1 && KnownOS[l[n-1]] {
return tags[l[n-1]]
return matchTag(l[n-1], tags, true)
}
if n >= 1 && KnownArch[l[n-1]] {
return tags[l[n-1]]
return matchTag(l[n-1], tags, true)
}
return true
}
......
......@@ -5,10 +5,13 @@
package imports
import (
"bytes"
"internal/testenv"
"io/ioutil"
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
)
......@@ -51,17 +54,41 @@ func TestScan(t *testing.T) {
t.Errorf("json missing test import net/http (%q)", testImports)
}
}
func TestScanStar(t *testing.T) {
func TestScanDir(t *testing.T) {
testenv.MustHaveGoBuild(t)
imports, _, err := ScanDir("testdata/import1", map[string]bool{"*": true})
dirs, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
for _, dir := range dirs {
if !dir.IsDir() || strings.HasPrefix(dir.Name(), ".") {
continue
}
t.Run(dir.Name(), func(t *testing.T) {
tagsData, err := ioutil.ReadFile(filepath.Join("testdata", dir.Name(), "tags.txt"))
if err != nil {
t.Fatalf("error reading tags: %v", err)
}
tags := make(map[string]bool)
for _, t := range strings.Fields(string(tagsData)) {
tags[t] = true
}
wantData, err := ioutil.ReadFile(filepath.Join("testdata", dir.Name(), "want.txt"))
if err != nil {
t.Fatalf("error reading want: %v", err)
}
want := string(bytes.TrimSpace(wantData))
want := []string{"import1", "import2", "import3", "import4"}
if !reflect.DeepEqual(imports, want) {
t.Errorf("ScanDir testdata/import1:\nhave %v\nwant %v", imports, want)
imports, _, err := ScanDir(path.Join("testdata", dir.Name()), tags)
if err != nil {
t.Fatal(err)
}
got := strings.Join(imports, "\n")
if got != want {
t.Errorf("ScanDir: got imports:\n%s\n\nwant:\n%s", got, want)
}
})
}
}
// +build android
package android
import _ "e"
// +build linux
package android
import _ "f"
// +build !android
package android
import _ "g"
android arm64
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment