Skip to content

Commit e0a5fc2

Browse files
authored
Filter target build-tags if user specified an overriding option (#3357)
compileopts: Filter target build-tags if user specified an overriding option
1 parent cecb80b commit e0a5fc2

File tree

2 files changed

+158
-1
lines changed

2 files changed

+158
-1
lines changed

compileopts/config.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ func (c *Config) GOARM() string {
7575

7676
// BuildTags returns the complete list of build tags used during this build.
7777
func (c *Config) BuildTags() []string {
78-
tags := append(c.Target.BuildTags, []string{"tinygo", "math_big_pure_go", "gc." + c.GC(), "scheduler." + c.Scheduler(), "serial." + c.Serial()}...)
78+
targetTags := filterTags(c.Target.BuildTags, c.Options.Tags)
79+
tags := append(targetTags, []string{"tinygo", "math_big_pure_go", "gc." + c.GC(), "scheduler." + c.Scheduler(), "serial." + c.Serial()}...)
7980
for i := 1; i <= c.GoMinorVersion; i++ {
8081
tags = append(tags, fmt.Sprintf("go1.%d", i))
8182
}
@@ -550,3 +551,27 @@ type TestConfig struct {
550551
CompileTestBinary bool
551552
// TODO: Filter the test functions to run, include verbose flag, etc
552553
}
554+
555+
// filterTags removes predefined build tags for a target if a conflicting option
556+
// is provided by the user.
557+
func filterTags(targetTags []string, userTags []string) []string {
558+
var filtered []string
559+
for _, t := range targetTags {
560+
switch {
561+
case strings.HasPrefix(t, "runtime_memhash_"):
562+
overridden := false
563+
for _, ut := range userTags {
564+
if strings.HasPrefix(ut, "runtime_memhash_") {
565+
overridden = true
566+
break
567+
}
568+
}
569+
if !overridden {
570+
filtered = append(filtered, t)
571+
}
572+
default:
573+
filtered = append(filtered, t)
574+
}
575+
}
576+
return filtered
577+
}

compileopts/config_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package compileopts
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestBuildTags(t *testing.T) {
10+
tests := []struct {
11+
targetTags []string
12+
userTags []string
13+
result []string
14+
}{
15+
{
16+
targetTags: []string{},
17+
userTags: []string{},
18+
result: []string{
19+
"tinygo",
20+
"math_big_pure_go",
21+
"gc.conservative",
22+
"scheduler.none",
23+
"serial.none",
24+
},
25+
},
26+
{
27+
targetTags: []string{"bear"},
28+
userTags: []string{},
29+
result: []string{
30+
"bear",
31+
"tinygo",
32+
"math_big_pure_go",
33+
"gc.conservative",
34+
"scheduler.none",
35+
"serial.none",
36+
},
37+
},
38+
{
39+
targetTags: []string{},
40+
userTags: []string{"cat"},
41+
result: []string{
42+
"tinygo",
43+
"math_big_pure_go",
44+
"gc.conservative",
45+
"scheduler.none",
46+
"serial.none",
47+
"cat",
48+
},
49+
},
50+
{
51+
targetTags: []string{"bear"},
52+
userTags: []string{"cat"},
53+
result: []string{
54+
"bear",
55+
"tinygo",
56+
"math_big_pure_go",
57+
"gc.conservative",
58+
"scheduler.none",
59+
"serial.none",
60+
"cat",
61+
},
62+
},
63+
{
64+
targetTags: []string{"bear", "runtime_memhash_leveldb"},
65+
userTags: []string{"cat"},
66+
result: []string{
67+
"bear",
68+
"runtime_memhash_leveldb",
69+
"tinygo",
70+
"math_big_pure_go",
71+
"gc.conservative",
72+
"scheduler.none",
73+
"serial.none",
74+
"cat",
75+
},
76+
},
77+
{
78+
targetTags: []string{"bear", "runtime_memhash_leveldb"},
79+
userTags: []string{"cat", "runtime_memhash_leveldb"},
80+
result: []string{
81+
"bear",
82+
"tinygo",
83+
"math_big_pure_go",
84+
"gc.conservative",
85+
"scheduler.none",
86+
"serial.none",
87+
"cat",
88+
"runtime_memhash_leveldb",
89+
},
90+
},
91+
{
92+
targetTags: []string{"bear", "runtime_memhash_leveldb"},
93+
userTags: []string{"cat", "runtime_memhash_sip"},
94+
result: []string{
95+
"bear",
96+
"tinygo",
97+
"math_big_pure_go",
98+
"gc.conservative",
99+
"scheduler.none",
100+
"serial.none",
101+
"cat",
102+
"runtime_memhash_sip",
103+
},
104+
},
105+
}
106+
107+
for _, tc := range tests {
108+
tt := tc
109+
t.Run(fmt.Sprintf("%s+%s", strings.Join(tt.targetTags, ","), strings.Join(tt.userTags, ",")), func(t *testing.T) {
110+
c := &Config{
111+
Target: &TargetSpec{
112+
BuildTags: tt.targetTags,
113+
},
114+
Options: &Options{
115+
Tags: tt.userTags,
116+
},
117+
}
118+
119+
res := c.BuildTags()
120+
121+
if len(res) != len(tt.result) {
122+
t.Errorf("expected %d tags, got %d", len(tt.result), len(res))
123+
}
124+
125+
for i, tag := range tt.result {
126+
if tag != res[i] {
127+
t.Errorf("tag %d: expected %s, got %s", i, tt.result[i], tag)
128+
}
129+
}
130+
})
131+
}
132+
}

0 commit comments

Comments
 (0)