Skip to content

Commit 15cd7ff

Browse files
authored
Merge pull request #1161 from kubernetes-sigs/build-tags
✨ Add `--load-build-tags` flag
2 parents 06ca07b + 71cec62 commit 15cd7ff

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

cmd/controller-gen/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"github.com/spf13/cobra"
27+
"golang.org/x/tools/go/packages"
2728

2829
"sigs.k8s.io/controller-tools/pkg/applyconfiguration"
2930
"sigs.k8s.io/controller-tools/pkg/crd"
@@ -129,6 +130,7 @@ func main() {
129130
helpLevel := 0
130131
whichLevel := 0
131132
showVersion := false
133+
var buildTags []string
132134

133135
cmd := &cobra.Command{
134136
Use: "controller-gen",
@@ -173,7 +175,8 @@ func main() {
173175
}
174176

175177
// otherwise, set up the runtime for actually running the generators
176-
rt, err := genall.FromOptions(optionsRegistry, rawOpts)
178+
tagsFlag := fmt.Sprintf("-tags=%s", strings.Join(buildTags, ","))
179+
rt, err := genall.FromOptionsWithConfig(&packages.Config{BuildFlags: []string{tagsFlag}}, optionsRegistry, rawOpts)
177180
if err != nil {
178181
return err
179182
}
@@ -192,6 +195,7 @@ func main() {
192195
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)")
193196
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)")
194197
cmd.Flags().BoolVar(&showVersion, "version", false, "show version")
198+
cmd.Flags().StringSliceVar(&buildTags, "load-build-tags", []string{"ignore_autogenerated"}, "build tags to use when loading Go packages")
195199
cmd.Flags().Bool("help", false, "print out usage and a summary of options")
196200
oldUsage := cmd.UsageFunc()
197201
cmd.SetUsageFunc(func(c *cobra.Command) error {

pkg/genall/genall.go

Lines changed: 5 additions & 1 deletion
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
}

pkg/genall/options.go

Lines changed: 6 additions & 1 deletion
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
}

pkg/loader/loader.go

Lines changed: 8 additions & 2 deletions
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)