Skip to content

Commit 57caa1e

Browse files
authored
Merge pull request #1181 from joelanford/0.17-build-tags
[release-0.17] ✨ Add `--load-build-tags` flag
2 parents 8526e19 + 004583e commit 57caa1e

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

Diff for: cmd/controller-gen/main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"strings"
2525

2626
"github.com/spf13/cobra"
27+
"golang.org/x/tools/go/packages"
28+
2729
"sigs.k8s.io/controller-tools/pkg/crd"
2830
"sigs.k8s.io/controller-tools/pkg/deepcopy"
2931
"sigs.k8s.io/controller-tools/pkg/genall"
@@ -126,6 +128,7 @@ func main() {
126128
helpLevel := 0
127129
whichLevel := 0
128130
showVersion := false
131+
var buildTags []string
129132

130133
cmd := &cobra.Command{
131134
Use: "controller-gen",
@@ -165,7 +168,8 @@ func main() {
165168
}
166169

167170
// otherwise, set up the runtime for actually running the generators
168-
rt, err := genall.FromOptions(optionsRegistry, rawOpts)
171+
tagsFlag := fmt.Sprintf("-tags=%s", strings.Join(buildTags, ","))
172+
rt, err := genall.FromOptionsWithConfig(&packages.Config{BuildFlags: []string{tagsFlag}}, optionsRegistry, rawOpts)
169173
if err != nil {
170174
return err
171175
}
@@ -184,6 +188,7 @@ func main() {
184188
cmd.Flags().CountVarP(&whichLevel, "which-markers", "w", "print out all markers available with the requested generators\n(up to -www for the most detailed output, or -wwww for json output)")
185189
cmd.Flags().CountVarP(&helpLevel, "detailed-help", "h", "print out more detailed help\n(up to -hhh for the most detailed output, or -hhhh for json output)")
186190
cmd.Flags().BoolVar(&showVersion, "version", false, "show version")
191+
cmd.Flags().StringSliceVar(&buildTags, "load-build-tags", []string{"ignore_autogenerated"}, "build tags to use when loading Go packages")
187192
cmd.Flags().Bool("help", false, "print out usage and a summary of options")
188193
oldUsage := cmd.UsageFunc()
189194
cmd.SetUsageFunc(func(c *cobra.Command) error {

Diff for: pkg/genall/genall.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ func (g GenerationContext) ReadFile(path string) ([]byte, error) {
218218
// ForRoots produces a Runtime to run the given generators against the
219219
// given packages. It outputs to /dev/null by default.
220220
func (g Generators) ForRoots(rootPaths ...string) (*Runtime, error) {
221-
roots, err := loader.LoadRoots(rootPaths...)
221+
return g.ForRootsWithConfig(&packages.Config{}, rootPaths...)
222+
}
223+
224+
func (g Generators) ForRootsWithConfig(cfg *packages.Config, rootPaths ...string) (*Runtime, error) {
225+
roots, err := loader.LoadRootsWithConfig(cfg, rootPaths...)
222226
if err != nil {
223227
return nil, err
224228
}

Diff for: pkg/genall/options.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"strings"
2222

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

@@ -74,13 +75,17 @@ func RegistryFromOptions(optionsRegistry *markers.Registry, options []string) (*
7475
// further modified. Not default generators are used if none are specified -- you can check
7576
// the output and rerun for that.
7677
func FromOptions(optionsRegistry *markers.Registry, options []string) (*Runtime, error) {
78+
return FromOptionsWithConfig(&packages.Config{}, optionsRegistry, options)
79+
}
80+
81+
func FromOptionsWithConfig(cfg *packages.Config, optionsRegistry *markers.Registry, options []string) (*Runtime, error) {
7782
protoRt, err := protoFromOptions(optionsRegistry, options)
7883
if err != nil {
7984
return nil, err
8085
}
8186

8287
// make the runtime
83-
genRuntime, err := protoRt.Generators.ForRoots(protoRt.Paths...)
88+
genRuntime, err := protoRt.Generators.ForRootsWithConfig(cfg, protoRt.Paths...)
8489
if err != nil {
8590
return nil, err
8691
}

Diff for: pkg/loader/loader.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,14 @@ func LoadRootsWithConfig(cfg *packages.Config, roots ...string) ([]*Package, err
374374
if l.cfg.Fset == nil {
375375
l.cfg.Fset = token.NewFileSet()
376376
}
377-
// put our build flags first so that callers can override them
378-
l.cfg.BuildFlags = append([]string{"-tags", "ignore_autogenerated"}, l.cfg.BuildFlags...)
377+
378+
// put our build flags first so that callers can override them.
379+
//
380+
// NOTE: if callers provide their own `-tags` flag, then our hardcoded `ignore_autogenerated` tag
381+
// will be ignored. Callers that provide custom tags MUST include `ignore_autogenerated` in their
382+
// custom tag set if they want that tag to remain active. Users can explicitly pass custom build
383+
// flags with `-tags=""` to disable use of the default `ignore_autogenerated` tag.
384+
l.cfg.BuildFlags = append([]string{"-tags=ignore_autogenerated"}, l.cfg.BuildFlags...)
379385

380386
// Visit the import graphs of the loaded, root packages. If an imported
381387
// package refers to another loaded, root package, then replace the

0 commit comments

Comments
 (0)