Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/controller-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"strings"

"github.com/spf13/cobra"
"golang.org/x/tools/go/packages"

"sigs.k8s.io/controller-tools/pkg/crd"
"sigs.k8s.io/controller-tools/pkg/deepcopy"
"sigs.k8s.io/controller-tools/pkg/genall"
Expand Down Expand Up @@ -126,6 +128,7 @@ func main() {
helpLevel := 0
whichLevel := 0
showVersion := false
var buildTags []string

cmd := &cobra.Command{
Use: "controller-gen",
Expand Down Expand Up @@ -165,7 +168,8 @@ func main() {
}

// otherwise, set up the runtime for actually running the generators
rt, err := genall.FromOptions(optionsRegistry, rawOpts)
tagsFlag := fmt.Sprintf("-tags=%s", strings.Join(buildTags, ","))
rt, err := genall.FromOptionsWithConfig(&packages.Config{BuildFlags: []string{tagsFlag}}, optionsRegistry, rawOpts)
if err != nil {
return err
}
Expand All @@ -184,6 +188,7 @@ func main() {
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)")
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)")
cmd.Flags().BoolVar(&showVersion, "version", false, "show version")
cmd.Flags().StringSliceVar(&buildTags, "load-build-tags", []string{"ignore_autogenerated"}, "build tags to use when loading Go packages")
cmd.Flags().Bool("help", false, "print out usage and a summary of options")
oldUsage := cmd.UsageFunc()
cmd.SetUsageFunc(func(c *cobra.Command) error {
Expand Down
6 changes: 5 additions & 1 deletion pkg/genall/genall.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ func (g GenerationContext) ReadFile(path string) ([]byte, error) {
// ForRoots produces a Runtime to run the given generators against the
// given packages. It outputs to /dev/null by default.
func (g Generators) ForRoots(rootPaths ...string) (*Runtime, error) {
roots, err := loader.LoadRoots(rootPaths...)
return g.ForRootsWithConfig(&packages.Config{}, rootPaths...)
}

func (g Generators) ForRootsWithConfig(cfg *packages.Config, rootPaths ...string) (*Runtime, error) {
roots, err := loader.LoadRootsWithConfig(cfg, rootPaths...)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/genall/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"strings"

"golang.org/x/tools/go/packages"
"sigs.k8s.io/controller-tools/pkg/markers"
)

Expand Down Expand Up @@ -74,13 +75,17 @@ func RegistryFromOptions(optionsRegistry *markers.Registry, options []string) (*
// further modified. Not default generators are used if none are specified -- you can check
// the output and rerun for that.
func FromOptions(optionsRegistry *markers.Registry, options []string) (*Runtime, error) {
return FromOptionsWithConfig(&packages.Config{}, optionsRegistry, options)
}

func FromOptionsWithConfig(cfg *packages.Config, optionsRegistry *markers.Registry, options []string) (*Runtime, error) {
protoRt, err := protoFromOptions(optionsRegistry, options)
if err != nil {
return nil, err
}

// make the runtime
genRuntime, err := protoRt.Generators.ForRoots(protoRt.Paths...)
genRuntime, err := protoRt.Generators.ForRootsWithConfig(cfg, protoRt.Paths...)
if err != nil {
return nil, err
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,14 @@ func LoadRootsWithConfig(cfg *packages.Config, roots ...string) ([]*Package, err
if l.cfg.Fset == nil {
l.cfg.Fset = token.NewFileSet()
}
// put our build flags first so that callers can override them
l.cfg.BuildFlags = append([]string{"-tags", "ignore_autogenerated"}, l.cfg.BuildFlags...)

// put our build flags first so that callers can override them.
//
// NOTE: if callers provide their own `-tags` flag, then our hardcoded `ignore_autogenerated` tag
// will be ignored. Callers that provide custom tags MUST include `ignore_autogenerated` in their
// custom tag set if they want that tag to remain active. Users can explicitly pass custom build
// flags with `-tags=""` to disable use of the default `ignore_autogenerated` tag.
l.cfg.BuildFlags = append([]string{"-tags=ignore_autogenerated"}, l.cfg.BuildFlags...)

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