github.com/bazelbuild/rules_go@v0.47.2-0.20240515105122-e7ddb9ea474e/go/private/polyfill_bazel_features.bzl (about)

     1  # bazel_features when used from a WORKSPACE rather than bzlmod context requires a two-step set-up (loading bazel_features, then calling a function from inside it).
     2  # rules_go only has one-step set-up, and it would be a breaking change to require a second step.
     3  # Accordingly, we supply a polyfill implementation of bazel_features which is only used when using rules_go from a WORKSPACE file,
     4  # to avoid complicating code in rules_go itself.
     5  # We just implement the checks we've seen we actually need, and hope to delete this completely when we are in a pure-bzlmod world.
     6  
     7  _POLYFILL_BAZEL_FEATURES = """bazel_features = struct(
     8    cc = struct(
     9      find_cpp_toolchain_has_mandatory_param = {find_cpp_toolchain_has_mandatory_param},
    10    )
    11  )
    12  """
    13  
    14  def _polyfill_bazel_features_impl(rctx):
    15      # An empty string is treated as a "dev version", which is greater than anything.
    16      bazel_version = native.bazel_version or "999999.999999.999999"
    17      version_parts = bazel_version.split("-")[0].split(".")
    18      if len(version_parts) != 3:
    19          fail("invalid Bazel version '{}': got {} dot-separated segments, want 3".format(bazel_version, len(version_parts)))
    20      major_version_int = int(version_parts[0])
    21      minor_version_int = int(version_parts[1])
    22  
    23      find_cpp_toolchain_has_mandatory_param = major_version_int > 6 or (major_version_int == 6 and minor_version_int >= 1)
    24  
    25      rctx.file("BUILD.bazel", """
    26  load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
    27  bzl_library(
    28      name = "features",
    29      srcs = ["features.bzl"],
    30      visibility = ["//visibility:public"],
    31  )
    32  exports_files(["features.bzl"])
    33  """)
    34      rctx.file("features.bzl", _POLYFILL_BAZEL_FEATURES.format(
    35          find_cpp_toolchain_has_mandatory_param = repr(find_cpp_toolchain_has_mandatory_param),
    36      ))
    37  
    38  polyfill_bazel_features = repository_rule(
    39      implementation = _polyfill_bazel_features_impl,
    40      # Force reruns on server restarts to keep native.bazel_version up-to-date.
    41      local = True,
    42  )