1 # Protocol Buffers - Google's data interchange format
2 # Copyright 2024 Google Inc. All rights reserved.
4 # Use of this source code is governed by a BSD-style
5 # license that can be found in the LICENSE file or at
6 # https://developers.google.com/open-source/licenses/bsd
8 """Vendored version of bazel_features for protobuf, to keep a one-step setup"""
10 _PROTO_BAZEL_FEATURES = """bazel_features = struct(
12 protobuf_on_allowlist = {protobuf_on_allowlist},
15 starlark_proto_info = {starlark_proto_info},
18 PackageSpecificationInfo = {PackageSpecificationInfo},
19 ProtoInfo = getattr(getattr(native, 'legacy_globals', None), 'ProtoInfo', {ProtoInfo})
24 def _proto_bazel_features_impl(rctx):
25 # An empty string is treated as a "dev version", which is greater than anything.
26 bazel_version = native.bazel_version or "999999.999999.999999"
27 version_parts = bazel_version.split("-")[0].split(".")
28 if len(version_parts) != 3:
29 fail("invalid Bazel version '{}': got {} dot-separated segments, want 3".format(bazel_version, len(version_parts)))
30 major_version_int = int(version_parts[0])
31 minor_version_int = int(version_parts[1])
33 starlark_proto_info = major_version_int >= 7
34 PackageSpecificationInfo = major_version_int > 6 or (major_version_int == 6 and minor_version_int >= 4)
36 protobuf_on_allowlist = major_version_int > 7
37 ProtoInfo = "ProtoInfo" if major_version_int < 8 else "None"
39 rctx.file("BUILD.bazel", """
40 load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
43 srcs = ["features.bzl"],
44 visibility = ["//visibility:public"],
46 exports_files(["features.bzl"])
48 rctx.file("features.bzl", _PROTO_BAZEL_FEATURES.format(
49 starlark_proto_info = repr(starlark_proto_info),
50 PackageSpecificationInfo = "PackageSpecificationInfo" if PackageSpecificationInfo else "None",
51 protobuf_on_allowlist = repr(protobuf_on_allowlist),
52 ProtoInfo = ProtoInfo,
55 proto_bazel_features = repository_rule(
56 implementation = _proto_bazel_features_impl,
57 # Force reruns on server restarts to keep native.bazel_version up-to-date.