github.com/please-build/go-rules/tools/please_go@v0.0.0-20240319165128-ea27d6f5caba/generate/rules.go (about)

     1  package generate
     2  
     3  import "github.com/bazelbuild/buildtools/build"
     4  
     5  type Rule struct {
     6  	name          string
     7  	kind          string
     8  	module        string
     9  	subrepo       string
    10  	srcs          []string
    11  	cgoSrcs       []string
    12  	cSrcs         []string
    13  	compilerFlags []string
    14  	linkerFlags   []string
    15  	pkgConfigs    []string
    16  	asmFiles      []string
    17  	hdrs          []string
    18  	deps          []string
    19  	embedPatterns []string
    20  	isCMD         bool
    21  }
    22  
    23  func populateRule(r *build.Rule, targetState *Rule) {
    24  	if len(targetState.cgoSrcs) > 0 {
    25  		r.SetAttr("srcs", NewStringList(targetState.cgoSrcs))
    26  		r.SetAttr("go_srcs", NewStringList(targetState.srcs))
    27  	} else {
    28  		r.SetAttr("srcs", NewStringList(targetState.srcs))
    29  	}
    30  	if len(targetState.cSrcs) > 0 {
    31  		r.SetAttr("c_srcs", NewStringList(targetState.cSrcs))
    32  	}
    33  	if len(targetState.deps) > 0 {
    34  		r.SetAttr("deps", NewStringList(targetState.deps))
    35  	}
    36  	if len(targetState.pkgConfigs) > 0 {
    37  		r.SetAttr("pkg_config", NewStringList(targetState.pkgConfigs))
    38  	}
    39  	if len(targetState.compilerFlags) > 0 {
    40  		r.SetAttr("compiler_flags", NewStringList(targetState.compilerFlags))
    41  	}
    42  	if len(targetState.linkerFlags) > 0 {
    43  		r.SetAttr("linker_flags", NewStringList(targetState.linkerFlags))
    44  	}
    45  	if len(targetState.hdrs) > 0 {
    46  		r.SetAttr("hdrs", NewStringList(targetState.hdrs))
    47  	}
    48  	if len(targetState.asmFiles) > 0 {
    49  		r.SetAttr("asm_srcs", NewStringList(targetState.asmFiles))
    50  	}
    51  	if len(targetState.embedPatterns) > 0 {
    52  		r.SetAttr("resources", &build.CallExpr{
    53  			X: &build.Ident{Name: "glob"},
    54  			List: []build.Expr{
    55  				NewStringList(targetState.embedPatterns),
    56  			},
    57  		})
    58  	}
    59  	if !targetState.isCMD {
    60  		r.SetAttr("_module", NewStringExpr(targetState.module))
    61  		r.SetAttr("_subrepo", NewStringExpr(targetState.subrepo))
    62  	}
    63  }