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
13 changes: 11 additions & 2 deletions tools/please_go/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ type Generate struct {
replace map[string]string
knownImportTargets map[string]string // cache these so we don't end up looping over all the modules for every import
thirdPartyFolder string
definitions []string
install []string
labels []string
largePackages []string
licences []string
}

func New(srcRoot, thirdPartyFolder, hostModFile, module, version, subrepo string, buildFileNames, moduleDeps, install, buildTags, labels, largePackages, licences []string) *Generate {
func New(srcRoot, thirdPartyFolder, hostModFile, module, version, subrepo string, buildFileNames, moduleDeps, install, buildTags, definitions, labels, largePackages, licences []string) *Generate {
moduleArg := module
if version != "" {
moduleArg += "@" + version
Expand All @@ -54,6 +55,7 @@ func New(srcRoot, thirdPartyFolder, hostModFile, module, version, subrepo string
knownImportTargets: map[string]string{},
thirdPartyFolder: thirdPartyFolder,
install: install,
definitions: definitions,
moduleName: module,
moduleArg: moduleArg,
subrepo: subrepo,
Expand Down Expand Up @@ -454,21 +456,28 @@ func (g *Generate) ruleForPackage(pkg *build.Package, dir string) *Rule {
}

name := nameForLibInPkg(g.moduleName, trimPath(dir, g.srcRoot))
kind := packageKind(pkg)
deps := g.depTargets(pkg.Imports)
if len(pkg.IgnoredOtherFiles) != 0 {
deps = append(deps, ":a_files")
}

var definitions []string
if kind == "cgo_binary" || kind == "go_binary" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to future-proof this with strings.HasSuffix(kind, "_binary")?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think I'd prefer to match explicit strings - there's no guarantee that other targets with a _binary suffix will have a definitions parameter.

definitions = g.definitions
}

return &Rule{
name: name,
kind: packageKind(pkg),
kind: kind,
srcs: pkg.GoFiles,
module: g.moduleArg,
subrepo: g.subrepo,
cgoSrcs: pkg.CgoFiles,
cSrcs: pkg.CFiles,
compilerFlags: pkg.CgoCFLAGS,
linkerFlags: orderLinkerFlags(pkg.CgoLDFLAGS),
definitions: definitions,
pkgConfigs: pkg.CgoPkgConfig,
asmFiles: pkg.SFiles,
hdrs: pkg.HFiles,
Expand Down
4 changes: 4 additions & 0 deletions tools/please_go/generate/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Rule struct {
cgoSrcs []string
cSrcs []string
compilerFlags []string
definitions []string
linkerFlags []string
pkgConfigs []string
asmFiles []string
Expand Down Expand Up @@ -43,6 +44,9 @@ func populateRule(r *build.Rule, targetState *Rule) {
if len(targetState.linkerFlags) > 0 {
r.SetAttr("linker_flags", NewStringList(targetState.linkerFlags))
}
if len(targetState.definitions) > 0 {
r.SetAttr("definitions", NewStringList(targetState.definitions))
}
if len(targetState.hdrs) > 0 {
r.SetAttr("hdrs", NewStringList(targetState.hdrs))
}
Expand Down
18 changes: 17 additions & 1 deletion tools/please_go/please_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var opts = struct {
Version string `long:"version" description:"The version of the current module"`
Install []string `long:"install" description:"The packages to add to the :install alias"`
BuildTags []string `long:"build_tag" description:"Any build tags to apply to the build"`
Definitions []string `long:"definition" value-name:"[IMPORTPATH].[NAME]=[VALUE]" description:"Element to insert into \"definitions\" parameter when generating Go binary targets"`
Subrepo string `long:"subrepo" description:"The subrepo root to output into"`
Licences []string `long:"licence" description:"The licences under which the module is released"`
Labels []string `long:"label" description:"Additional labels to attach to subrepo targets"`
Expand Down Expand Up @@ -170,7 +171,22 @@ var subCommands = map[string]func() int{
},
"generate": func() int {
gen := opts.Generate
g := generate.New(gen.SrcRoot, gen.ThirdPartyFolder, gen.ModFile, gen.Module, gen.Version, gen.Subrepo, []string{"BUILD", "BUILD.plz"}, gen.Args.Requirements, gen.Install, gen.BuildTags, gen.Labels, gen.LargePackages, gen.Licences)
g := generate.New(
gen.SrcRoot,
gen.ThirdPartyFolder,
gen.ModFile,
gen.Module,
gen.Version,
gen.Subrepo,
[]string{"BUILD", "BUILD.plz"},
gen.Args.Requirements,
gen.Install,
gen.BuildTags,
gen.Definitions,
gen.Labels,
gen.LargePackages,
gen.Licences,
)
if err := g.Generate(); err != nil {
log.Fatalf("failed to generate go rules: %v", err)
}
Expand Down
Loading