mirror of https://github.com/golang/go.git
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: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
parent
5ee1d5d39f
commit
f07a99e30a
|
|
@ -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
|
||||
}
|
||||
|
||||
want := []string{"import1", "import2", "import3", "import4"}
|
||||
if !reflect.DeepEqual(imports, want) {
|
||||
t.Errorf("ScanDir testdata/import1:\nhave %v\nwant %v", imports, want)
|
||||
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))
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
package android
|
||||
|
||||
import _ "a"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package android
|
||||
|
||||
import _ "b"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package android
|
||||
|
||||
import _ "c"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package android
|
||||
|
||||
import _ "d"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// +build android
|
||||
|
||||
package android
|
||||
|
||||
import _ "e"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// +build linux
|
||||
|
||||
package android
|
||||
|
||||
import _ "f"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// +build !android
|
||||
|
||||
package android
|
||||
|
||||
import _ "g"
|
||||
|
|
@ -0,0 +1 @@
|
|||
android arm64
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
a
|
||||
b
|
||||
c
|
||||
d
|
||||
e
|
||||
f
|
||||
|
|
@ -0,0 +1 @@
|
|||
*
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import1
|
||||
import2
|
||||
import3
|
||||
import4
|
||||
Loading…
Reference in New Issue