@@ -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
2627var  (
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="" 
3436// Multiple paths can be specified using "{path1, path2, path3}". 
3537type  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. 
3948func  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+ }
0 commit comments