Skip to content

Commit

Permalink
Feature: support legacy_symbols (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
comius authored Sep 13, 2024
1 parent 261ee16 commit 89f8bdc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions private/globals.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ GLOBALS = {
"DefaultInfo": "0.0.1",
"__TestingOnly_NeverAvailable": "1000000000.0.0",
}

# This one works in the reverse, put in the version when the global symbol is removed.
LEGACY_GLOBALS = {
"ProtoInfo": "8.0.0",
}
14 changes: 13 additions & 1 deletion private/globals_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ bzl_library(
value = global_ if bazel_version >= parse_version(version) else "None"
lines.append(" %s = %s," % (global_, value))

for global_, version in rctx.attr.legacy_globals.items():
if not _is_valid_identifier(global_):
fail("Invalid global name: %s" % global_)

value = global_ if bazel_version < parse_version(version) else "None"
# If the legacy_globals is available, we take the value from it.
# The value is populated by --incompatible_autoload_externally and may apply to older Bazel versions
lines.append(" %s = getattr(getattr(native, 'legacy_globals', None), '%s', %s)," % (global_, global_, value))

lines.append(")")

rctx.file("globals.bzl", "\n".join(lines))
Expand All @@ -41,7 +50,10 @@ globals_repo = repository_rule(
"globals": attr.string_dict(
mandatory = True,
),
},
"legacy_globals": attr.string_dict(
mandatory = True,
),
},
)

def _is_valid_identifier(s):
Expand Down
3 changes: 2 additions & 1 deletion private/repos.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Contains the macro bazel_features_repos to install internal repositories."""

load(":globals.bzl", "GLOBALS")
load(":globals.bzl", "GLOBALS", "LEGACY_GLOBALS")
load(":globals_repo.bzl", "globals_repo")
load(":version_repo.bzl", "version_repo")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
Expand All @@ -14,4 +14,5 @@ def bazel_features_repos():
globals_repo,
name = "bazel_features_globals",
globals = GLOBALS,
legacy_globals = LEGACY_GLOBALS,
)
6 changes: 5 additions & 1 deletion test/test.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Provides a macro to do some loading-time test assertions."""

load("//private:util.bzl", "BAZEL_VERSION", "ge")
load("//private:util.bzl", "BAZEL_VERSION", "ge", "lt")
load("//private:parse.bzl", "parse_version")
load("//:features.bzl", "bazel_features")

Expand Down Expand Up @@ -49,6 +49,10 @@ def run_test(name):
if not bazel_features.globals.DefaultInfo == DefaultInfo:
fail("bazel_features.globals.DefaultInfo != DefaultInfo")

# TODO: add tests with --incompatible_autoload_symbols
if lt("8.0.0") and not bazel_features.globals.ProtoInfo == ProtoInfo:
fail("bazel_features.globals.ProtoInfo != ProtoInfo")

if not bazel_features.globals.__TestingOnly_NeverAvailable == None:
fail("bazel_features.globals.__TestingOnly_NeverAvailable != None")

Expand Down

0 comments on commit 89f8bdc

Please sign in to comment.