Skip to content

Commit 9a05ae2

Browse files
committed
Add buildTag support
Adds a new `buildTag` marker, to allow the definition of multiple go build tags, to be passed via the build config. Signed-off-by: Todd Short <todd.short@me.com>
1 parent fe6de35 commit 9a05ae2

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

pkg/genall/genall.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,11 @@ func (g GenerationContext) ReadFile(path string) ([]byte, error) {
215215
return io.ReadAll(file)
216216
}
217217

218-
// ForRoots produces a Runtime to run the given generators against the
219-
// given packages. It outputs to /dev/null by default.
220-
func (g Generators) ForRoots(rootPaths ...string) (*Runtime, error) {
221-
roots, err := loader.LoadRoots(rootPaths...)
218+
// ForRootsWithConfig produces a Runtime to run the given generators against the
219+
// given packages, but with the specified configuration. It outputs to /dev/null
220+
// by default.
221+
func (g Generators) ForRootsWithConfig(cfg *packages.Config, rootPaths ...string) (*Runtime, error) {
222+
roots, err := loader.LoadRootsWithConfig(cfg, rootPaths...)
222223
if err != nil {
223224
return nil, err
224225
}
@@ -242,6 +243,12 @@ func (g Generators) ForRoots(rootPaths ...string) (*Runtime, error) {
242243
return rt, nil
243244
}
244245

246+
// ForRoots produces a Runtime to run the given generators against the
247+
// given packages. It outputs to /dev/null by default.
248+
func (g Generators) ForRoots(rootPaths ...string) (*Runtime, error) {
249+
return g.ForRootsWithConfig(&packages.Config{}, rootPaths...)
250+
}
251+
245252
// Run runs the Generators in this Runtime against its packages, printing
246253
// errors (except type errors, which common result from using TypeChecker with
247254
// filters), returning true if errors were found.

pkg/genall/options.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ import (
2020
"fmt"
2121
"strings"
2222

23+
"golang.org/x/tools/go/packages"
2324
"sigs.k8s.io/controller-tools/pkg/markers"
2425
)
2526

2627
var (
2728
InputPathsMarker = markers.Must(markers.MakeDefinition("paths", markers.DescribesPackage, InputPaths(nil)))
29+
BuildTagsMarker = markers.MustOptional(markers.MakeDefinition("buildTag", markers.DescribesPackage, InputBuildTags(nil)))
2830
)
2931

3032
// +controllertools:marker:generateHelp:category=""
@@ -34,6 +36,13 @@ var (
3436
// Multiple paths can be specified using "{path1, path2, path3}".
3537
type InputPaths []string
3638

39+
// +controllertools:marker:generateHelp:category=""
40+
41+
// InputBuildTags represents go build tags used when including or excluding code during builds.
42+
//
43+
// Multiple tags can be specified by using this marker multiple times: buildTag=tag1 buildTag=tag2
44+
type InputBuildTags []string
45+
3746
// RegisterOptionsMarkers registers "mandatory" options markers for FromOptions into the given registry.
3847
// At this point, that's just InputPaths.
3948
func RegisterOptionsMarkers(into *markers.Registry) error {
@@ -44,6 +53,14 @@ func RegisterOptionsMarkers(into *markers.Registry) error {
4453
if helpGiver, hasHelp := ((interface{})(InputPaths(nil))).(HasHelp); hasHelp {
4554
into.AddHelp(InputPathsMarker, helpGiver.Help())
4655
}
56+
57+
if err := into.Register(BuildTagsMarker); err != nil {
58+
return err
59+
}
60+
if helpGiver, hasHelp := ((interface{})(InputBuildTags(nil))).(HasHelp); hasHelp {
61+
into.AddHelp(BuildTagsMarker, helpGiver.Help())
62+
}
63+
4764
return nil
4865
}
4966

@@ -78,9 +95,8 @@ func FromOptions(optionsRegistry *markers.Registry, options []string) (*Runtime,
7895
if err != nil {
7996
return nil, err
8097
}
81-
8298
// make the runtime
83-
genRuntime, err := protoRt.Generators.ForRoots(protoRt.Paths...)
99+
genRuntime, err := protoRt.Generators.ForRootsWithConfig(protoRt.getConfig(), protoRt.Paths...)
84100
if err != nil {
85101
return nil, err
86102
}
@@ -112,6 +128,7 @@ func protoFromOptions(optionsRegistry *markers.Registry, options []string) (prot
112128
ByGenerator: make(map[*Generator]OutputRule),
113129
}
114130
var paths []string
131+
var tags []string
115132

116133
// collect the generators first, so that we can key the output on the actual
117134
// generator, which matters if there's settings in the gen object and it's not a pointer.
@@ -151,6 +168,8 @@ func protoFromOptions(optionsRegistry *markers.Registry, options []string) (prot
151168
continue
152169
case InputPaths:
153170
paths = append(paths, val...)
171+
case InputBuildTags:
172+
tags = append(tags, val...)
154173
default:
155174
return protoRuntime{}, fmt.Errorf("unknown option marker %q", defn.Name)
156175
}
@@ -171,6 +190,7 @@ func protoFromOptions(optionsRegistry *markers.Registry, options []string) (prot
171190
Generators: gens,
172191
OutputRules: rules,
173192
GeneratorsByName: gensByName,
193+
Tags: tags,
174194
}, nil
175195
}
176196

@@ -181,6 +201,7 @@ type protoRuntime struct {
181201
Generators Generators
182202
OutputRules OutputRules
183203
GeneratorsByName map[string]*Generator
204+
Tags []string
184205
}
185206

186207
// splitOutputRuleOption splits a marker name of "output:rule:gen" or "output:rule"
@@ -194,3 +215,13 @@ func splitOutputRuleOption(name string) (ruleName string, genName string) {
194215
// output:<rule>
195216
return parts[1], ""
196217
}
218+
219+
// getConfig generates the intiial config based on the the optional arguments,
220+
// which are currently only tags
221+
func (p protoRuntime) getConfig() *packages.Config {
222+
cfg := &packages.Config{}
223+
for _, tag := range p.Tags {
224+
cfg.BuildFlags = append(cfg.BuildFlags, "-tags", tag)
225+
}
226+
return cfg
227+
}

pkg/genall/zz_generated.markerhelp.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/markers/regutil.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
package markers
1818

19+
import "fmt"
20+
1921
// Must panics on errors creating definitions.
2022
func Must(def *Definition, err error) *Definition {
2123
if err != nil {
@@ -24,6 +26,17 @@ func Must(def *Definition, err error) *Definition {
2426
return def
2527
}
2628

29+
func MustOptional(def *Definition, err error) *Definition {
30+
def = Must(def, err)
31+
if !def.AnonymousField() {
32+
def = Must(def, fmt.Errorf("not an anonymous field: %v", def))
33+
}
34+
field := def.Fields[""]
35+
field.Optional = true
36+
def.Fields[""] = field
37+
return def
38+
}
39+
2740
// RegisterAll attempts to register all definitions against the given registry,
2841
// stopping and returning if an error occurs.
2942
func RegisterAll(reg *Registry, defs ...*Definition) error {

0 commit comments

Comments
 (0)