Skip to content
Open
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
8 changes: 7 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ bazel_dep(name = "rules_proto", version = "7.0.2")
bazel_dep(name = "protobuf", version = "29.0", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_shell", version = "0.3.0")
bazel_dep(name = "rules_cc", version = "0.1.5")
bazel_dep(name = "rules_license", version = "1.0.0")
single_version_override(
module_name = "rules_license",
patch_strip = 1,
patches = ["//third_party:rules_license-stardoc.patch"],
)

go_sdk = use_extension("//go:extensions.bzl", "go_sdk")

Expand All @@ -32,7 +38,7 @@ use_repo(

register_toolchains("@go_toolchains//:all")

bazel_dep(name = "gazelle", version = "0.36.0")
bazel_dep(name = "gazelle", version = "0.47.0")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
Expand Down
6 changes: 4 additions & 2 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ load("//docs:doc_helpers.bzl", "stardoc_with_diff_test", "update_docs")
# For each doc file, generate MD from bzl_library, then perform diff test
stardoc_with_diff_test(
bzl_library_target = "//docs/go/extras:extras",
extra_deps = [
"@rules_license//rules:providers.bzl",
"@rules_license//rules_gathering:gathering_providers.bzl",
],
out_label = "//docs/go/extras:extras.md",
)

stardoc_with_diff_test(
bzl_library_target = "//docs/go/core:rules",
extra_deps = [
"@rules_license//rules:providers.bzl",
"@rules_license//rules_gathering:gathering_providers.bzl",
],
out_label = "//docs/go/core:rules.md",
)

Expand Down
8 changes: 5 additions & 3 deletions docs/doc_helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@

load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@stardoc//stardoc:stardoc.bzl", "stardoc")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("@stardoc//stardoc:stardoc.bzl", "stardoc")

def stardoc_with_diff_test(
bzl_library_target,
out_label):
out_label,
extra_deps = []):
"""Creates a stardoc target coupled with a diff_test for a given bzl_library.

This is helpful for minimizing boilerplate when lots of stardoc targets are to be generated.

Args:
bzl_library_target: the label of the bzl_library target to generate documentation for
out_label: the label of the output MD file
extra_deps: additional Starlark files or bzl_library targets required for doc extraction
"""

out_file = out_label.replace("//", "").replace(":", "/")
Expand All @@ -36,7 +38,7 @@ def stardoc_with_diff_test(
name = out_file.replace("/", "_").replace(".md", "-docgen"),
out = out_file.replace(".md", "-docgen.md"),
input = bzl_library_target + ".bzl",
deps = [bzl_library_target],
deps = [bzl_library_target] + extra_deps,
)

# Ensure that the generated MD has been updated in the local source tree
Expand Down
4 changes: 4 additions & 0 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
_copts = tuple(source.copts),
_cxxopts = tuple(source.cxxopts),
_clinkopts = tuple(source.clinkopts),
_module_path = getattr(source, "_module_path", ""),
_module_version = getattr(source, "_module_version", ""),

# Information on dependencies
_dep_labels = tuple([d.data.label for d in direct]),
Expand Down Expand Up @@ -227,4 +229,6 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
cgo_exports = cgo_exports,
runfiles = runfiles,
_headers = headers,
_module_path = getattr(data, "_module_path", ""),
_module_version = getattr(data, "_module_version", ""),
)
10 changes: 9 additions & 1 deletion go/private/actions/link.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ load(
)

def _format_archive(d):
return "{}={}={}".format(d.label, d.importmap, d.file.path)
return "{}={}={}={}={}={}".format(
d.label,
d.importpath,
d.importmap,
d.file.path,
getattr(d, "_module_path", ""),
getattr(d, "_module_version", ""),
)

def emit_link(
go,
Expand Down Expand Up @@ -125,6 +132,7 @@ def emit_link(
arcs = depset(test_archives, transitive = [d.transitive for d in archive.direct])

builder_args.add_all(arcs, before_each = "-arc", map_each = _format_archive)
builder_args.add("-go_version", go.sdk.version)
builder_args.add("-package_list", go.sdk.package_list)

# Build a list of rpaths for dynamic libraries we need to find.
Expand Down
42 changes: 42 additions & 0 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ load(
NOGO_INCLUDES = "INCLUDES",
)
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_license//rules:providers.bzl", "PackageInfo")
load(
"//go/platform:apple.bzl",
"apple_ensure_options",
Expand Down Expand Up @@ -240,6 +241,35 @@ def _tool_args(go):
args.use_param_file("-param=%s")
return args

def normalize_module_version(version):
if not version:
return "(devel)"
if version == "(devel)":
return version
if version.startswith("v"):
return version
return "v" + version

def module_info_from_metadata(label, package_metadata = (), applicable_licenses = ()):
if not label.repo_name and not label.workspace_root:
return struct(path = "", version = "")

# Bazel may surface repo-level metadata through either spelling depending on
# the version and rule surface, so probe both.
for metadata_group in (package_metadata, applicable_licenses):
for metadata in metadata_group:
if PackageInfo not in metadata:
continue
info = metadata[PackageInfo]
if not info.package_name:
continue
return struct(
path = info.package_name,
version = normalize_module_version(info.package_version),
)

return struct(path = "", version = "")

def _merge_embed(source, embed):
s = get_source(embed)
source["srcs"] = s.srcs + source["srcs"]
Expand All @@ -249,6 +279,10 @@ def _merge_embed(source, embed):
source["x_defs"].update(s.x_defs)
source["gc_goopts"] = source["gc_goopts"] + s.gc_goopts
source["runfiles"] = source["runfiles"].merge(s.runfiles)
module_path = getattr(s, "_module_path", "")
if not source["_module_path"] and module_path:
source["_module_path"] = module_path
source["_module_version"] = getattr(s, "_module_version", "")

if s.cgo:
if source["cgo"]:
Expand Down Expand Up @@ -354,6 +388,12 @@ def new_go_info(
if deps == None:
deps = [get_archive(dep) for dep in getattr(attr, "deps", [])]

module_info = module_info_from_metadata(
go.label,
getattr(attr, "package_metadata", ()),
getattr(attr, "applicable_licenses", ()),
)

go_info = {
"name": go.label.name if not name else name,
"label": go.label,
Expand All @@ -378,6 +418,8 @@ def new_go_info(
"cxxopts": _expand_opts(go, "cxxopts", getattr(attr, "cxxopts", [])),
"clinkopts": _expand_opts(go, "clinkopts", getattr(attr, "clinkopts", [])),
"pgoprofile": getattr(attr, "pgoprofile", None),
"_module_path": module_info.path,
"_module_version": module_info.version,
}

for e in getattr(attr, "embed", []):
Expand Down
11 changes: 11 additions & 0 deletions go/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ def go_rules_dependencies(force = False):
url = "https://github.com/bazelbuild/rules_cc/releases/download/0.1.5/rules_cc-0.1.5.tar.gz",
)

# Required for reading Gazelle-generated PackageInfo metadata.
wrapper(
http_archive,
name = "rules_license",
sha256 = "26d4021f6898e23b82ef953078389dd49ac2b5618ac564ade4ef87cced147b38",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/1.0.0/rules_license-1.0.0.tar.gz",
"https://github.com/bazelbuild/rules_license/releases/download/1.0.0/rules_license-1.0.0.tar.gz",
],
)

def _go_host_compatible_sdk_label_impl(ctx):
ctx.file("BUILD.bazel")
ctx.file("defs.bzl", """HOST_COMPATIBLE_SDK = Label("@go_sdk//:ROOT")""")
Expand Down
4 changes: 4 additions & 0 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ def _recompile_external_deps(go, external_go_info, internal_archive, library_lab
copts = list(arc_data._copts),
cxxopts = list(arc_data._cxxopts),
clinkopts = list(arc_data._clinkopts),
_module_path = getattr(arc_data, "_module_path", ""),
_module_version = getattr(arc_data, "_module_version", ""),
)

# If this archive needs to be recompiled, use go.archive.
Expand All @@ -747,6 +749,8 @@ def _recompile_external_deps(go, external_go_info, internal_archive, library_lab
runfiles = go_info.runfiles,
mode = go.mode,
_headers = internal_archive._headers,
_module_path = getattr(arc_data, "_module_path", ""),
_module_version = getattr(arc_data, "_module_version", ""),
)
label_to_archive[label] = archive

Expand Down
10 changes: 10 additions & 0 deletions go/tools/builders/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ go_test(
],
)

go_test(
name = "buildinfo_test",
size = "small",
srcs = [
"buildinfo.go",
"buildinfo_test.go",
],
)

go_test(
name = "nogo_version_test",
size = "small",
Expand All @@ -101,6 +110,7 @@ filegroup(
"ar.go",
"asm.go",
"builder.go",
"buildinfo.go",
"cc.go",
"cgo2.go",
"compilepkg.go",
Expand Down
Loading