Skip to content

Commit 492ed83

Browse files
Add support for large packages in go_repos (#320)
Co-authored-by: Chris Novakovic <[email protected]>
1 parent 01084e9 commit 492ed83

File tree

12 files changed

+136
-46
lines changed

12 files changed

+136
-46
lines changed

.plzconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[Please]
2-
Version = >=17.11.0
2+
Version = >=17.21.0
33

44
[build]
55
hashcheckers = sha256
@@ -20,7 +20,7 @@ Target = //plugins:cc
2020

2121
[Plugin "e2e"]
2222
Target = //plugins:e2e
23-
PleaseVersion = 17.10.1
23+
PleaseVersion = 17.21.0
2424

2525
[PluginDefinition]
2626
Name = go

build_defs/go.build_defs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ def go_library(name:str, srcs:list, resources:list=[], asm_srcs:list=None, hdrs:
240240
_needs_transitive_deps=False, _all_srcs=False, cover:bool=True,
241241
filter_srcs:bool=True, _link_private:bool=False, _link_extra:bool=True, _abi:str=None,
242242
_generate_import_config:bool=True, _generate_pkg_info:bool=CONFIG.GO.PKG_INFO,
243-
import_path:str='', labels:list=[], package:str=None, pgo_file:str=None, _module:str='', _subrepo:str=''):
243+
import_path:str='', labels:list=[], package:str=None, pgo_file:str=None, _module:str='',
244+
_subrepo:str='', _is_large_package:bool=False):
244245
"""Generates a Go library which can be reused by other rules.
245246

246247
Args:
@@ -464,6 +465,7 @@ def go_library(name:str, srcs:list, resources:list=[], asm_srcs:list=None, hdrs:
464465
package = package,
465466
test_only = test_only,
466467
visibility = visibility,
468+
large_package = _is_large_package,
467469
)
468470
deps += [pkg_info]
469471
provides["pkg_info"] = pkg_info
@@ -484,7 +486,18 @@ def go_library(name:str, srcs:list, resources:list=[], asm_srcs:list=None, hdrs:
484486
if pgo_file:
485487
srcs['pgo'] = [pgo_file]
486488

487-
cmds, tools = _go_library_cmds(name, import_path=package_path, complete=complete, all_srcs=_all_srcs, cover=cover, filter_srcs=filter_srcs, abi=_abi, embedcfg=embedcfg, pgo_file=pgo_file)
489+
cmds, tools = _go_library_cmds(
490+
name,
491+
import_path=package_path,
492+
complete=complete,
493+
all_srcs=_all_srcs,
494+
cover=cover,
495+
filter_srcs=filter_srcs,
496+
abi=_abi,
497+
embedcfg=embedcfg,
498+
pgo_file=pgo_file,
499+
large_package=_is_large_package,
500+
)
488501

489502
return build_rule(
490503
name = name,
@@ -502,6 +515,7 @@ def go_library(name:str, srcs:list, resources:list=[], asm_srcs:list=None, hdrs:
502515
tools = tools,
503516
needs_transitive_deps = _needs_transitive_deps,
504517
exported_deps = exported_deps,
518+
src_list_files = _is_large_package,
505519
)
506520

507521
def _generate_pkg_import_cfg_cmd(name, out, pkg_location, handle_basename_eq_dirname=False):
@@ -1176,7 +1190,7 @@ def _module_rule_name(module):
11761190

11771191
def go_repo(module: str, version:str='', download:str=None, name:str=None, install:list=[], requirements:list=[],
11781192
licences:list=None, patch:list=None, visibility:list=["PUBLIC"], deps:list=[], build_tags:list=CONFIG.GO.BUILD_TAGS,
1179-
third_party_path:str="third_party/go", strip:list=None, labels:list=[]):
1193+
third_party_path:str="third_party/go", strip:list=None, labels:list=[], large_packages:list=[]):
11801194
"""Adds a third party go module to the build graph as a subrepo. This is designed to be closer to how the `go.mod`
11811195
file works, requiring only the module name and version to be specified. Unlike go_module, each package is compiled
11821196
individually, and dependencies between packages are inferred by convention.
@@ -1214,6 +1228,9 @@ def go_repo(module: str, version:str='', download:str=None, name:str=None, insta
12141228
third_party_path (str): Optional path of third_party directory.
12151229
strip (list): A list of directories to strip from the repo
12161230
labels (list): Labels for this rule.
1231+
large_packages (list): List of relative package names which should be marked as large in the
1232+
generated go_library rules (i.e. packages which have large number of
1233+
source files)
12171234
"""
12181235
subrepo_name = _module_rule_name(module)
12191236

@@ -1251,6 +1268,7 @@ def go_repo(module: str, version:str='', download:str=None, name:str=None, insta
12511268

12521269
build_tag_args = " ".join([f"--build_tag {build_tag}" for build_tag in build_tags])
12531270
label_args = " ".join([f"--label '{label}'" for label in labels])
1271+
large_package_args = " ".join([f"--large_package '{pkg}'" for pkg in large_packages])
12541272

12551273
pkgRoot = f"pkg/{CONFIG.OS}_{CONFIG.ARCH}/{module}"
12561274

@@ -1259,7 +1277,7 @@ def go_repo(module: str, version:str='', download:str=None, name:str=None, insta
12591277
"find $SRCS_DOWNLOAD -name BUILD -delete",
12601278
f"mkdir -p $(dirname {pkgRoot})",
12611279
f"mv $SRCS_DOWNLOAD {pkgRoot}",
1262-
f"$TOOL generate {modFileArg} --module {module} --version '{version}' {build_tag_args} {label_args} --src_root={pkgRoot} --third_part_folder='{third_party_path}' --subrepo '{pkg_name}/{subrepo_name}' {install_args} {requirements}",
1280+
f"$TOOL generate {modFileArg} --module {module} --version '{version}' {build_tag_args} {label_args} {large_package_args} --src_root={pkgRoot} --third_part_folder='{third_party_path}' --subrepo '{pkg_name}/{subrepo_name}' {install_args} {requirements}",
12631281
f"mv {pkgRoot} $OUT",
12641282
]
12651283
cmd = " && ".join(cmds)
@@ -1559,7 +1577,7 @@ def _go_modinfo(name:str, test_only:bool=False, deps:list):
15591577

15601578

15611579
def _go_pkg_info(name:str, srcs:list, visibility:list, importconfig:str=None, import_path:str="", package:str="",
1562-
module:str="", subrepo:str="", test_only:bool=False, install:list=[]):
1580+
module:str="", subrepo:str="", test_only:bool=False, install:list=[], large_package:bool=False):
15631581
"""Internal-only function for generating the pkg_info files"""
15641582
install_flags = ""
15651583
for i in install:
@@ -1594,6 +1612,7 @@ def _go_pkg_info(name:str, srcs:list, visibility:list, importconfig:str=None, im
15941612
labels = ["go_pkg_info"],
15951613
test_only = test_only,
15961614
visibility=visibility,
1615+
src_list_files=large_package,
15971616
)
15981617

15991618

@@ -1620,11 +1639,12 @@ def _set_go_env():
16201639
return f"export CGO_ENABLED=1 && {cmd}" if CONFIG.GO.CGO_ENABLED else cmd
16211640

16221641

1623-
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):
1642+
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, large_package=False):
16241643
"""Returns the commands to run for building a Go library."""
16251644
complete_flag = '-complete ' if complete else ''
16261645
embed_flag = ' -embedcfg $SRCS_EMBED' if embedcfg else ''
16271646
out_cmd = ' -o "$OUTS_O" -symabis $SRCS_ABI -asmhdr "$OUTS_H"' if abi else ' -o "$OUT"'
1647+
srcs_var = '$(cat _plz/named_srcs/go)' if large_package else '$SRCS_GO'
16281648

16291649
if CONFIG.GO.GO_COMPILE_TOOL:
16301650
tool = '"$TOOLS_COMPILE"'
@@ -1635,10 +1655,10 @@ def _go_library_cmds(name, import_path:str="", complete=True, all_srcs=False, co
16351655

16361656
if filter_srcs:
16371657
build_tags = '-t ' + ' -t '.join(CONFIG.GO.BUILD_TAGS) if CONFIG.GO.BUILD_TAGS else ''
1638-
filter_cmd = f'export SRCS_GO="$(\"${TOOLS_PLEASE_GO}\" filter {build_tags} $SRCS_GO)"; '
1658+
filter_cmd = f'filtered_srcs="$(\"${TOOLS_PLEASE_GO}\" filter {build_tags} {srcs_var})"; '
16391659
tools['please_go'] = [CONFIG.GO.PLEASE_GO_TOOL]
16401660
else:
1641-
filter_cmd = ''
1661+
filter_cmd = f'filtered_srcs={srcs_var}; '
16421662

16431663
compile_cmd = f'{tool} -importcfg importconfig -trimpath "$TMP_DIR" {complete_flag}{embed_flag} -pack {out_cmd}'
16441664
package_flag = f" -p {import_path}" if import_path else ""
@@ -1654,11 +1674,11 @@ def _go_library_cmds(name, import_path:str="", complete=True, all_srcs=False, co
16541674
prefix = ('export SRCS_GO="$PKG_DIR/*.go"; ' + gen_import_cfg) if all_srcs else gen_import_cfg
16551675

16561676
cmds = {
1657-
'dbg': f'{prefix}; {filter_cmd}{compile_cmd}{package_flag} -N -l $SRCS_GO',
1658-
'opt': f'{prefix}; {filter_cmd}{compile_cmd}{package_flag} $SRCS_GO',
1677+
'dbg': f'{prefix}; {filter_cmd}{compile_cmd}{package_flag} -N -l $filtered_srcs',
1678+
'opt': f'{prefix}; {filter_cmd}{compile_cmd}{package_flag} $filtered_srcs',
16591679
}
16601680
if cover:
1661-
cmds['cover'] = f'{prefix}; {filter_cmd} $TOOLS_PLEASE_GO cover -c covcfg.json -o covfiles.txt {package_flag} $SRCS_GO && {compile_cmd} -coveragecfg covcfg.json {package_flag} `cat covfiles.txt`'
1681+
cmds['cover'] = f'{prefix}; {filter_cmd} $TOOLS_PLEASE_GO cover -c covcfg.json -o covfiles.txt {package_flag} $filtered_srcs && {compile_cmd} -coveragecfg covcfg.json {package_flag} `cat covfiles.txt`'
16621682
tools['please_go'] = [CONFIG.GO.PLEASE_GO_TOOL]
16631683
if CONFIG.GO.GO_COVER_TOOL:
16641684
tools['cover'] = CONFIG.GO.GO_COVER_TOOL

test/large_packages/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
subinclude("//test/build_defs:e2e")
2+
3+
please_repo_e2e_test(
4+
name = "large_package_test",
5+
# Package known to have ~1800 source files
6+
plz_command = "plz --noupdate build ///third_party/go/github.com_elastic_go-elasticsearch_v9//typedapi/types",
7+
repo = "test_repo",
8+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[Parse]
2+
BuildFileName = BUILD_FILE
3+
BuildFileName = BUILD
4+
5+
[Plugin "go"]
6+
Target = //plugins:go
7+
PleaseGoTool = <pleasegotool>
8+
GoTool = <gotool>
9+
FeatureFlags = go_get
10+
Stdlib = //third_party/go:std
11+
Modfile = //third_party/go:modfile
12+
13+
[FeatureFlags]
14+
ExcludeGoRules = true
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plugin_repo(
2+
name = "go",
3+
revision = "master",
4+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
subinclude("///go//build_defs:go")
2+
3+
filegroup(
4+
name = "modfile",
5+
srcs = ["go.mod"],
6+
)
7+
8+
go_stdlib(
9+
name = "std",
10+
)
11+
12+
go_repo(
13+
licences = ["Apache-2.0"],
14+
module = "github.com/elastic/go-elasticsearch/v9",
15+
version = "v9.0.0",
16+
large_packages = ["typedapi/types"],
17+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module gorepolargepackages
2+
3+
go 1.21
4+
5+
require (
6+
github.com/elastic/go-elasticsearch/v9 v9.0.0
7+
)

tools/please_go/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Version 1.19.0
2+
-------------
3+
* Enable `go_library` to compile packages containing large numbers of files (#320)
4+
15
Version 1.18.0
26
-------------
37
* Migrate to `go_repo` and upgrade golang.org/x packages (#316)

tools/please_go/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.18.0
1+
1.19.0

tools/please_go/generate/generate.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path"
1111
"path/filepath"
12+
"slices"
1213
"strings"
1314

1415
bazelbuild "github.com/bazelbuild/buildtools/build"
@@ -31,9 +32,10 @@ type Generate struct {
3132
thirdPartyFolder string
3233
install []string
3334
labels []string
35+
largePackages []string
3436
}
3537

36-
func New(srcRoot, thirdPartyFolder, hostModFile, module, version, subrepo string, buildFileNames, moduleDeps, install []string, buildTags []string, labels []string) *Generate {
38+
func New(srcRoot, thirdPartyFolder, hostModFile, module, version, subrepo string, buildFileNames, moduleDeps, install []string, buildTags []string, labels []string, largePackages []string) *Generate {
3739
moduleArg := module
3840
if version != "" {
3941
moduleArg += "@" + version
@@ -55,6 +57,7 @@ func New(srcRoot, thirdPartyFolder, hostModFile, module, version, subrepo string
5557
moduleArg: moduleArg,
5658
subrepo: subrepo,
5759
labels: labels,
60+
largePackages: largePackages,
5861
}
5962
}
6063

@@ -401,6 +404,13 @@ func NewStringExpr(s string) *bazelbuild.StringExpr {
401404
return &bazelbuild.StringExpr{Value: s}
402405
}
403406

407+
func NewBoolExpr(v bool) bazelbuild.Expr {
408+
if v {
409+
return &bazelbuild.LiteralExpr{Token: "True"}
410+
}
411+
return &bazelbuild.LiteralExpr{Token: "False"}
412+
}
413+
404414
func NewStringList(ss []string) *bazelbuild.ListExpr {
405415
l := new(bazelbuild.ListExpr)
406416
for _, s := range ss {
@@ -447,21 +457,22 @@ func (g *Generate) ruleForPackage(pkg *build.Package, dir string) *Rule {
447457
}
448458

449459
return &Rule{
450-
name: name,
451-
kind: packageKind(pkg),
452-
srcs: pkg.GoFiles,
453-
module: g.moduleArg,
454-
subrepo: g.subrepo,
455-
cgoSrcs: pkg.CgoFiles,
456-
cSrcs: pkg.CFiles,
457-
compilerFlags: pkg.CgoCFLAGS,
458-
linkerFlags: orderLinkerFlags(pkg.CgoLDFLAGS),
459-
pkgConfigs: pkg.CgoPkgConfig,
460-
asmFiles: pkg.SFiles,
461-
hdrs: pkg.HFiles,
462-
deps: deps,
463-
embedPatterns: pkg.EmbedPatterns,
464-
isCMD: pkg.IsCommand(),
460+
name: name,
461+
kind: packageKind(pkg),
462+
srcs: pkg.GoFiles,
463+
module: g.moduleArg,
464+
subrepo: g.subrepo,
465+
cgoSrcs: pkg.CgoFiles,
466+
cSrcs: pkg.CFiles,
467+
compilerFlags: pkg.CgoCFLAGS,
468+
linkerFlags: orderLinkerFlags(pkg.CgoLDFLAGS),
469+
pkgConfigs: pkg.CgoPkgConfig,
470+
asmFiles: pkg.SFiles,
471+
hdrs: pkg.HFiles,
472+
deps: deps,
473+
embedPatterns: pkg.EmbedPatterns,
474+
isCMD: pkg.IsCommand(),
475+
isLargePackage: slices.Contains(g.largePackages, dir),
465476
}
466477
}
467478

0 commit comments

Comments
 (0)