• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Protocol Buffers - Google's data interchange format
2# Copyright 2024 Google Inc.  All rights reserved.
3#
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
7#
8"""Vendored version of bazel_features for protobuf, to keep a one-step setup"""
9
10_PROTO_BAZEL_FEATURES = """bazel_features = struct(
11  cc = struct(
12    protobuf_on_allowlist = {protobuf_on_allowlist},
13  ),
14  proto = struct(
15    starlark_proto_info = {starlark_proto_info},
16  ),
17  globals = struct(
18    PackageSpecificationInfo = {PackageSpecificationInfo},
19    ProtoInfo = getattr(getattr(native, 'legacy_globals', None), 'ProtoInfo', {ProtoInfo})
20  ),
21)
22"""
23
24def _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])
32
33    starlark_proto_info = major_version_int >= 7
34    PackageSpecificationInfo = major_version_int > 6 or (major_version_int == 6 and minor_version_int >= 4)
35
36    protobuf_on_allowlist = major_version_int > 7
37    ProtoInfo = "ProtoInfo" if major_version_int < 8 else "None"
38
39    rctx.file("BUILD.bazel", """
40load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
41bzl_library(
42    name = "features",
43    srcs = ["features.bzl"],
44    visibility = ["//visibility:public"],
45)
46exports_files(["features.bzl"])
47""")
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,
53    ))
54
55proto_bazel_features = repository_rule(
56    implementation = _proto_bazel_features_impl,
57    # Force reruns on server restarts to keep native.bazel_version up-to-date.
58    local = True,
59)
60