Skip to content

Commit

Permalink
Set build tags with config option (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
samwestmoreland authored Jan 11, 2024
1 parent 1dc76f2 commit 5149d99
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .plzconfig
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,10 @@ Type = bool
DefaultValue = true
Help = Generate pkg info for Go targets

[PluginConfig "build_tags"]
Repeatable = true
Optional = true
Help = Build tags to pass to the Go compiler

[Plugin "shell"]
Target = //plugins:shell
12 changes: 10 additions & 2 deletions build_defs/go.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,11 +1218,14 @@ def go_repo(module: str, version:str='', download:str=None, name:str=None, insta
if version:
labels += [f"go_module:{module}@{version}"]
requirements = " ".join(requirements)

build_tags = '--build_tag ' + ' --build_tag '.join(CONFIG.GO.BUILD_TAGS) if CONFIG.GO.BUILD_TAGS else ''

repo = build_rule(
name = name,
tag = "repo" if install else None,
srcs = srcs,
cmd = f"rm -rf $SRCS_DOWNLOAD/.plzconfig && find $SRCS_DOWNLOAD -name BUILD -delete && $TOOL generate {modFileArg} --module {module} --version '{version}' --src_root=$SRCS_DOWNLOAD --third_part_folder='{third_party_path}' {install_args} {requirements} && mv $SRCS_DOWNLOAD $OUT",
cmd = f"rm -rf $SRCS_DOWNLOAD/.plzconfig && find $SRCS_DOWNLOAD -name BUILD -delete && $TOOL generate {modFileArg} --module {module} --version '{version}' {build_tags} --src_root=$SRCS_DOWNLOAD --third_part_folder='{third_party_path}' {install_args} {requirements} && mv $SRCS_DOWNLOAD $OUT",
outs = [subrepo_name],
tools = [CONFIG.GO.PLEASE_GO_TOOL],
env= {
Expand Down Expand Up @@ -1576,7 +1579,12 @@ def _set_go_env():

def _go_library_cmds(name, import_path:str="", complete=True, all_srcs=False, cover=True, filter_srcs=True, abi=False, embedcfg=None, pgo_file=None):
"""Returns the commands to run for building a Go library."""
filter_cmd = 'export SRCS_GO="$(\"${TOOLS_PLEASE_GO}\" filter ${SRCS_GO})"; ' if filter_srcs else ''
if filter_srcs:
build_tags = '-t ' + ' -t '.join(CONFIG.GO.BUILD_TAGS) if CONFIG.GO.BUILD_TAGS else ''
filter_cmd = f'export SRCS_GO="$(\"${TOOLS_PLEASE_GO}\" filter {build_tags} $SRCS_GO)"; '
else:
filter_cmd = ''

# Invokes the Go compiler.
complete_flag = '-complete ' if complete else ''
embed_flag = ' -embedcfg $SRCS_EMBED' if embedcfg else ''
Expand Down
5 changes: 5 additions & 0 deletions tools/please_go/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version 1.8.0
-------------
* Add --build_tag flag to `generate` subcommand to allow specifying a build tag to use when
generating the BUILD files (#199)

Version 1.7.0
-------------
* Allow selecting the pkg-config tool via PKG_CONFIG_TOOL env var
Expand Down
2 changes: 1 addition & 1 deletion tools/please_go/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.0
1.8.0
30 changes: 24 additions & 6 deletions tools/please_go/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ type Generate struct {
install []string
}

func New(srcRoot, thirdPartyFolder, modFile, module, version string, buildFileNames, moduleDeps, install []string) *Generate {
func New(srcRoot, thirdPartyFolder, modFile, module, version string, buildFileNames, moduleDeps, install []string, buildTags []string) *Generate {
moduleArg := module
if version != "" {
moduleArg += "@" + version
}

ctxt := build.Default
ctxt.BuildTags = buildTags

return &Generate{
srcRoot: srcRoot,
buildContext: build.Default,
buildContext: ctxt,
buildFileNames: buildFileNames,
moduleDeps: moduleDeps,
modFile: modFile,
Expand All @@ -54,17 +58,17 @@ func New(srcRoot, thirdPartyFolder, modFile, module, version string, buildFileNa
// files. This is primarily intended to generate a please subrepo for third party code.
func (g *Generate) Generate() error {
if err := g.readGoMod(g.modFile); err != nil {
return err
return fmt.Errorf("failed to read go.mod: %w", err)
}
if err := g.writeConfig(); err != nil {
return err
return fmt.Errorf("failed to write config: %w", err)
}
if err := g.parseImportConfigs(); err != nil {
return err
return fmt.Errorf("failed to parse import configs: %w", err)
}

if err := g.generateAll(g.srcRoot); err != nil {
return err
return fmt.Errorf("failed to generate BUILD files: %w", err)
}
return g.writeInstallFilegroup()
}
Expand Down Expand Up @@ -274,6 +278,20 @@ func (g *Generate) generate(dir string) error {
return err
}

// filter out pkg.GoFiles based on build tags
var goFiles []string
for _, f := range pkg.GoFiles {
match, err := g.buildContext.MatchFile(pkg.Dir, f)
if err != nil {
return err
}
if match {
goFiles = append(goFiles, f)
}
}

pkg.GoFiles = goFiles

lib := g.ruleForPackage(pkg, dir)
if lib == nil {
return nil
Expand Down
3 changes: 2 additions & 1 deletion tools/please_go/please_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ var opts = struct {
Module string `long:"module" description:"The name of the current module"`
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"`
Args struct {
Requirements []string `positional-arg-name:"requirements" description:"Any module requirements not included in the go.mod"`
} `positional-args:"true"`
Expand Down Expand Up @@ -179,7 +180,7 @@ var subCommands = map[string]func() int{
return 0
},
"generate": func() int {
g := generate.New(opts.Generate.SrcRoot, opts.Generate.ThirdPartyFolder, opts.Generate.ModFile, opts.Generate.Module, opts.Generate.Version, []string{"BUILD", "BUILD.plz"}, opts.Generate.Args.Requirements, opts.Generate.Install)
g := generate.New(opts.Generate.SrcRoot, opts.Generate.ThirdPartyFolder, opts.Generate.ModFile, opts.Generate.Module, opts.Generate.Version, []string{"BUILD", "BUILD.plz"}, opts.Generate.Args.Requirements, opts.Generate.Install, opts.Generate.BuildTags)
if err := g.Generate(); err != nil {
log.Fatalf("failed to generate go rules: %v", err)
}
Expand Down

0 comments on commit 5149d99

Please sign in to comment.