Skip to content

Commit

Permalink
Merge GoLibrary and GoSource providers
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Aug 11, 2024
1 parent 0f88f8c commit 3da0b45
Show file tree
Hide file tree
Showing 27 changed files with 211 additions and 259 deletions.
2 changes: 0 additions & 2 deletions docs/go/core/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[Bourne shell tokenization]: https://docs.bazel.build/versions/master/be/common-definitions.html#sh-tokenization
[Gazelle]: https://github.com/bazelbuild/bazel-gazelle
[GoArchive]: /go/providers.rst#GoArchive
[GoLibrary]: /go/providers.rst#GoLibrary
[GoPath]: /go/providers.rst#GoPath
[GoSource]: /go/providers.rst#GoSource
[build constraints]: https://golang.org/pkg/go/build/#hdr-Build_Constraints
Expand Down Expand Up @@ -50,7 +49,6 @@ sufficient to match the capabilities of the normal go tools.
- [Bourne shell tokenization]
- [Gazelle]
- [GoArchive]
- [GoLibrary]
- [GoPath]
- [GoSource]
- [build constraints]:
Expand Down
22 changes: 8 additions & 14 deletions docs/go/core/rules.md

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions extras/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("//go/private:common.bzl", "GO_TOOLCHAIN", "GO_TOOLCHAIN_LABEL")
load("//go/private:context.bzl", "go_context")
load("//go/private:providers.bzl", "GoLibrary")
load("//go/private:providers.bzl", "GoSource")
load("//go/private/rules:wrappers.bzl", go_binary = "go_binary_macro")

_MOCKGEN_TOOL = Label("//extras/gomock:mockgen")
Expand All @@ -36,10 +36,10 @@ def _gomock_source_impl(ctx):

# In Source mode, it's not necessary to pass through a library, as the only thing we use it for is setting up
# the relative file locations. Forcing users to pass a library makes it difficult in the case where a mock should
# be included as part of that same library, as it results in a dependency loop (GoMock -> GoLibrary -> GoMock).
# be included as part of that same library, as it results in a dependency loop (GoMock -> GoSource -> GoMock).
# Allowing users to pass an importpath directly bypasses this issue.
# See the test case in //tests/extras/gomock/source_with_importpath for an example.
importpath = ctx.attr.source_importpath if ctx.attr.source_importpath else ctx.attr.library[GoLibrary].importmap
importpath = ctx.attr.source_importpath if ctx.attr.source_importpath else ctx.attr.library[GoSource].importmap

# create GOPATH and copy source into GOPATH
go_path_prefix = "gopath"
Expand Down Expand Up @@ -111,7 +111,7 @@ _gomock_source = rule(
attrs = {
"library": attr.label(
doc = "The target the Go library where this source file belongs",
providers = [GoLibrary],
providers = [GoSource],
mandatory = False,
),
"source_importpath": attr.string(
Expand Down Expand Up @@ -248,7 +248,7 @@ def _gomock_reflect(name, library, out, mockgen_tool, **kwargs):

def _gomock_prog_gen_impl(ctx):
args = ["-prog_only"]
args.append(ctx.attr.library[GoLibrary].importpath)
args.append(ctx.attr.library[GoSource].importpath)
args.append(",".join(ctx.attr.interfaces))

cmd = ctx.file.mockgen_tool
Expand All @@ -271,7 +271,7 @@ _gomock_prog_gen = rule(
attrs = {
"library": attr.label(
doc = "The target the Go library is at to look for the interfaces in. When this is set and source is not set, mockgen will use its reflect code to generate the mocks. If source is set, its dependencies will be included in the GOPATH that mockgen will be run in.",
providers = [GoLibrary],
providers = [GoSource],
mandatory = True,
),
"out": attr.output(
Expand Down Expand Up @@ -304,7 +304,7 @@ def _gomock_prog_exec_impl(ctx):

# annoyingly, the interfaces join has to go after the importpath so we can't
# share those.
args.append(ctx.attr.library[GoLibrary].importpath)
args.append(ctx.attr.library[GoSource].importpath)
args.append(",".join(ctx.attr.interfaces))

ctx.actions.run_shell(
Expand All @@ -328,7 +328,7 @@ _gomock_prog_exec = rule(
attrs = {
"library": attr.label(
doc = "The target the Go library is at to look for the interfaces in. When this is set and source is not set, mockgen will use its reflect code to generate the mocks. If source is set, its dependencies will be included in the GOPATH that mockgen will be run in.",
providers = [GoLibrary],
providers = [GoSource],
mandatory = True,
),
"out": attr.output(
Expand Down
1 change: 0 additions & 1 deletion go/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Core Go rules
.. _Bourne shell tokenization: https://docs.bazel.build/versions/master/be/common-definitions.html#sh-tokenization
.. _Gazelle: https://github.com/bazelbuild/bazel-gazelle
.. _GoArchive: providers.rst#GoArchive
.. _GoLibrary: providers.rst#GoLibrary
.. _GoPath: providers.rst#GoPath
.. _GoSource: providers.rst#GoSource
.. _build constraints: https://golang.org/pkg/go/build/#hdr-Build_Constraints
Expand Down
4 changes: 0 additions & 4 deletions go/def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ load(
"//go/private:providers.bzl",
_GoArchive = "GoArchive",
_GoArchiveData = "GoArchiveData",
_GoLibrary = "GoLibrary",
_GoPath = "GoPath",
_GoSDK = "GoSDK",
_GoSource = "GoSource",
Expand Down Expand Up @@ -130,9 +129,6 @@ go_tool_library = _go_tool_library
go_toolchain = _go_toolchain
nogo = _nogo

# See go/providers.rst#GoLibrary for full documentation.
GoLibrary = _GoLibrary

# See go/providers.rst#GoSource for full documentation.
GoSource = _GoSource

Expand Down
1 change: 0 additions & 1 deletion go/nogo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
.. _go_library: /docs/go/core/rules.md#go_library
.. _analysis: https://godoc.org/golang.org/x/tools/go/analysis
.. _Analyzer: https://godoc.org/golang.org/x/tools/go/analysis#Analyzer
.. _GoLibrary: providers.rst#GoLibrary
.. _GoSource: providers.rst#GoSource
.. _GoArchive: providers.rst#GoArchive
.. _vet: https://golang.org/cmd/vet/
Expand Down
30 changes: 14 additions & 16 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
if source == None:
fail("source is a required parameter")

testfilter = getattr(source.library, "testfilter", None)
testfilter = getattr(source, "testfilter", None)
pre_ext = ""
if go.mode.link == LINKMODE_C_ARCHIVE:
pre_ext = "_" # avoid collision with go_binary output file with .a extension
Expand All @@ -58,17 +58,17 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
pre_ext = ".external"
if _recompile_suffix:
pre_ext += _recompile_suffix
out_lib = go.declare_file(go, name = source.library.name, ext = pre_ext + ".a")
out_lib = go.declare_file(go, name = source.name, ext = pre_ext + ".a")

# store export information for compiling dependent packages separately
out_export = go.declare_file(go, name = source.library.name, ext = pre_ext + ".x")
out_export = go.declare_file(go, name = source.name, ext = pre_ext + ".x")
out_cgo_export_h = None # set if cgo used in c-shared or c-archive mode

nogo = get_nogo(go)
if nogo:
out_facts = go.declare_file(go, name = source.library.name, ext = pre_ext + ".facts")
out_nogo_log = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo.log")
out_nogo_validation = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo")
out_facts = go.declare_file(go, name = source.name, ext = pre_ext + ".facts")
out_nogo_log = go.declare_file(go, name = source.name, ext = pre_ext + ".nogo.log")
out_nogo_validation = go.declare_file(go, name = source.name, ext = pre_ext + ".nogo")
else:
out_facts = None
out_nogo_log = None
Expand All @@ -83,8 +83,8 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
fail("Archive mode does not match {} is {} expected {}".format(a.data.label, mode_string(a.source.mode), mode_string(go.mode)))
runfiles = source.runfiles.merge_all(files)

importmap = "main" if source.library.is_main else source.library.importmap
importpath, _ = effective_importpath_pkgpath(source.library)
importmap = "main" if source.is_main else source.importmap
importpath, _ = effective_importpath_pkgpath(source)

if source.cgo and not go.mode.pure:
# TODO(jayconrod): do we need to do full Bourne tokenization here?
Expand Down Expand Up @@ -162,15 +162,13 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
# may be mutable. For now, new copied fields are private (named with
# a leading underscore) since they may change in the future.

# GoLibrary fields
name = source.library.name,
label = source.library.label,
importpath = source.library.importpath,
importmap = source.library.importmap,
importpath_aliases = source.library.importpath_aliases,
pathtype = source.library.pathtype,

# GoSource fields
name = source.name,
label = source.label,
importpath = source.importpath,
importmap = source.importmap,
importpath_aliases = source.importpath_aliases,
pathtype = source.pathtype,
srcs = as_tuple(source.srcs),
orig_srcs = as_tuple(source.orig_srcs),
_cover = as_tuple(source.cover),
Expand Down
11 changes: 7 additions & 4 deletions go/private/actions/stdlib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ load(
"GO_TOOLCHAIN_LABEL",
"SUPPORTS_PATH_MAPPING_REQUIREMENT",
)
load(
"//go/private:context.bzl",
"new_source",
)
load(
"//go/private:mode.bzl",
"LINKMODE_NORMAL",
Expand All @@ -38,12 +42,11 @@ def emit_stdlib(go):
Otherwise, the standard library will be compiled for the target.
Returns:
A list of providers containing GoLibrary, GoSource and GoStdLib.
A list of providers containing GoSource and GoStdLib.
"""
library = go.new_library(go)
source = go.library_to_source(go, {}, library, False)
source = new_source(go, {}, coverage_instrumented = False)
stdlib = _sdk_stdlib(go) if _should_use_sdk_stdlib(go) else _build_stdlib(go)
return [source, library, stdlib]
return [source, stdlib]

def _should_use_sdk_stdlib(go):
version = parse_version(go.sdk.version)
Expand Down
82 changes: 41 additions & 41 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ load(
"GoArchive",
"GoConfigInfo",
"GoContextInfo",
"GoLibrary",
"GoSource",
"GoStdLib",
"INFERRED_PATH",
Expand Down Expand Up @@ -195,29 +194,6 @@ def _tool_args(go):
args.set_param_file_format("shell")
return args

def _new_library(go, name = None, importpath = None, resolver = None, importable = True, testfilter = None, is_main = False, **kwargs):
if not importpath:
importpath = go.importpath
importmap = go.importmap
else:
importmap = importpath
pathtype = go.pathtype
if not importable and pathtype == EXPLICIT_PATH:
pathtype = EXPORT_PATH

return GoLibrary(
name = go.label.name if not name else name,
label = go.label,
importpath = importpath,
importmap = importmap,
importpath_aliases = go.importpath_aliases,
pathtype = pathtype,
resolve = resolver,
testfilter = testfilter,
is_main = is_main,
**kwargs
)

def _merge_embed(source, embed):
s = get_source(embed)
source["srcs"] = s.srcs + source["srcs"]
Expand Down Expand Up @@ -255,24 +231,53 @@ def _dedup_deps(deps):
if hasattr(dep, "data") and hasattr(dep.data, "importpath"):
importpath = dep.data.importpath
else:
importpath = dep[GoLibrary].importpath
importpath = dep[GoSource].importpath
if importpath in importpaths:
continue
importpaths[importpath] = None
deduped_deps.append(dep)
return deduped_deps

def _library_to_source(go, attr, library, coverage_instrumented):
def new_source(
go,
attr,
name = None,
importpath = None,
resolver = None,
importable = True,
testfilter = None,
is_main = False,
coverage_instrumented = None,
generated_srcs = [],
pathtype = None):
if not importpath:
importpath = go.importpath
importmap = go.importmap
else:
importmap = importpath
if not pathtype:
pathtype = go.pathtype
if not importable and pathtype == EXPLICIT_PATH:
pathtype = EXPORT_PATH

if coverage_instrumented == None:
coverage_instrumented = go.coverage_instrumented

#TODO: stop collapsing a depset in this line...
attr_srcs = [f for t in getattr(attr, "srcs", []) for f in as_iterable(t.files)]
generated_srcs = getattr(library, "srcs", [])
srcs = attr_srcs + generated_srcs
embedsrcs = [f for t in getattr(attr, "embedsrcs", []) for f in as_iterable(t.files)]
attr_deps = getattr(attr, "deps", [])
generated_deps = getattr(library, "deps", [])
deps = attr_deps + generated_deps
deps = getattr(attr, "deps", [])

source = {
"library": library,
"name": go.label.name if not name else name,
"label": go.label,
"importpath": importpath,
"importmap": importmap,
"importpath_aliases": tuple(getattr(attr, "importpath_aliases", ())),
"pathtype": pathtype,
"testfilter": testfilter,
"is_main": is_main,
"mode": go.mode,
"srcs": srcs,
"orig_srcs": srcs,
Expand All @@ -291,7 +296,6 @@ def _library_to_source(go, attr, library, coverage_instrumented):
"cgo_deps": [],
"cgo_exports": [],
"cc_info": None,
"pgoprofile": getattr(attr, "pgoprofile", None),
}
if coverage_instrumented:
source["cover"] = attr_srcs
Expand All @@ -305,7 +309,7 @@ def _library_to_source(go, attr, library, coverage_instrumented):
for k, v in getattr(attr, "x_defs", {}).items():
v = _expand_location(go, attr, v)
if "." not in k:
k = "{}.{}".format(library.importmap, k)
k = "{}.{}".format(source["importmap"], k)
x_defs[k] = v
source["x_defs"] = x_defs
if not source["cgo"]:
Expand All @@ -317,8 +321,8 @@ def _library_to_source(go, attr, library, coverage_instrumented):
# sources. compilepkg will catch these instead.
if f.extension in ("c", "cc", "cxx", "cpp", "hh", "hpp", "hxx"):
fail("source {} has C/C++ extension, but cgo was not enabled (set 'cgo = True')".format(f.path))
if library.resolve:
library.resolve(go, attr, source, _merge_embed)
if resolver:
resolver(go, attr, source, _merge_embed)
source["cc_info"] = _collect_cc_infos(source["deps"], source["cdeps"])
return GoSource(**source)

Expand Down Expand Up @@ -390,9 +394,9 @@ def _infer_importpath(ctx, attr):
embed_importpath = ""
embed_importmap = ""
for embed in getattr(attr, "embed", []):
if GoLibrary not in embed:
if GoSource not in embed:
continue
lib = embed[GoLibrary]
lib = embed[GoSource]
if lib.pathtype == EXPLICIT_PATH:
embed_importpath = lib.importpath
embed_importmap = lib.importmap
Expand Down Expand Up @@ -558,7 +562,6 @@ def go_context(ctx, attr = None):

_check_importpaths(ctx)
importpath, importmap, pathtype = _infer_importpath(ctx, attr)
importpath_aliases = tuple(getattr(attr, "importpath_aliases", ()))

return struct(
# Fields
Expand All @@ -577,7 +580,6 @@ def go_context(ctx, attr = None):
package_list = toolchain.sdk.package_list,
importpath = importpath,
importmap = importmap,
importpath_aliases = importpath_aliases,
pathtype = pathtype,
cgo_tools = cgo_tools,
nogo = nogo,
Expand All @@ -600,8 +602,6 @@ def go_context(ctx, attr = None):
args = _new_args, # deprecated
builder_args = _builder_args,
tool_args = _tool_args,
new_library = _new_library,
library_to_source = _library_to_source,
declare_file = _declare_file,
declare_directory = _declare_directory,

Expand Down
Loading

0 comments on commit 3da0b45

Please sign in to comment.