Skip to content

Commit

Permalink
interp: fix the logic to skip source files based on OS or CPU arch
Browse files Browse the repository at this point in the history
For example, on architecture GOARCH=amd64, a file named `foobar_amd64.go` would be skipped instead of being read and parsed. The function `skipFile` is fixed and missing tests are added.
  • Loading branch information
mvertes authored Oct 24, 2022
1 parent a5242cb commit e4e3d11
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
18 changes: 15 additions & 3 deletions interp/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,22 @@ func skipFile(ctx *build.Context, p string, skipTest bool) bool {
}
a := strings.Split(p[i+1:], "_")
last := len(a) - 1
if last1 := last - 1; last1 >= 0 && a[last1] == ctx.GOOS && a[last] == ctx.GOARCH {
return false
if last-1 >= 0 {
switch x, y := a[last-1], a[last]; {
case x == ctx.GOOS:
if knownArch[y] {
return y != ctx.GOARCH
}
return false
case knownOs[x] && knownArch[y]:
return true
case knownArch[y] && y != ctx.GOARCH:
return true
default:
return false
}
}
if s := a[last]; s != ctx.GOOS && s != ctx.GOARCH && knownOs[s] || knownArch[s] {
if x := a[last]; knownOs[x] && x != ctx.GOOS || knownArch[x] && x != ctx.GOARCH {
return true
}
return false
Expand Down
10 changes: 9 additions & 1 deletion interp/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestBuildTag(t *testing.T) {
}
}

func TestBuildFile(t *testing.T) {
func TestSkipFile(t *testing.T) {
// Assume a specific OS, arch and go pattern no matter the real underlying system
ctx := build.Context{
GOARCH: "amd64",
Expand All @@ -65,10 +65,18 @@ func TestBuildFile(t *testing.T) {
{"bar_linux.go", false},
{"bar_maix.go", false},
{"bar_mlinux.go", false},

{"bar_aix_foo.go", false},
{"bar_linux_foo.go", false},
{"bar_foo_amd64.go", false},
{"bar_foo_arm.go", true},

{"bar_aix_s390x.go", true},
{"bar_aix_amd64.go", true},
{"bar_linux_arm.go", true},

{"bar_amd64.go", false},
{"bar_arm.go", true},
}

for _, test := range tests {
Expand Down

0 comments on commit e4e3d11

Please sign in to comment.