semver version range match
diff --git a/v2/BUILD.bazel b/v2/BUILD.bazel index e789ff2..ebc8cde 100644 --- a/v2/BUILD.bazel +++ b/v2/BUILD.bazel
@@ -1,15 +1,60 @@ load(":cc_proto_library.bzl", "cc_proto_library") +load("@bazel_skylib//lib:selects.bzl", "selects") cc_proto_library( name = "proto", ) config_setting( - name = "is_3_21_12", - flag_values = { - "//version:protoc": "3.21.12", - }, + name = "is_major_3", + flag_values = {"//version:protoc_major": "3"}, ) +config_setting( + name = "is_minor_21", + flag_values = {"//version:protoc_minor": "21"}, +) +config_setting( + name = "is_patch_12", + flag_values = {"//version:protoc_patch": "12"}, +) + + +selects.config_setting_group( + name = "is_3", + match_all = [":is_major_3"], +) + +selects.config_setting_group( + name = "is_3_21", + match_all = [ + ":is_major_3", + ":is_minor_21", + ], +) + +selects.config_setting_group( + name = "is_3_21_12", + match_all = [ + ":is_major_3", + ":is_minor_21", + ":is_patch_12", + ], +) + +# This config setting will satify the following configurations +# +# A transition that sets major version to 3 +# A transition that sets major.minor version to 3.21 +# A transition that sets major.minor.patch version to 2.21.12 +selects.config_setting_group( + name = "semver_3_21_12", + match_any = [ + ":is_3", + ":is_3_21", + ":is_3_21_12", + ], +) + toolchain_type( name = "demo_toolchain_type", @@ -26,7 +71,7 @@ "@platforms//cpu:arm64", ], target_settings = [ - ":is_3_21_12", + ":semver_3_21_12" ], toolchain = "@protobuf//:cc_toolchain", toolchain_type = ":demo_toolchain_type",
diff --git a/v2/cc_proto_library.bzl b/v2/cc_proto_library.bzl index 7ea779d..768ce08 100644 --- a/v2/cc_proto_library.bzl +++ b/v2/cc_proto_library.bzl
@@ -1,27 +1,47 @@ def _pb_version_transition_impl(settings, attr): - is_bcr_release = False # there's some way to detect - if is_bcr_release: - # This will loaded from a version.bzl file which - # protobuf releases will create with every release. - return { - "//version:protoc": "3.21.12", - } + is_bcr_release = True # there's some way to detect + if not is_bcr_release: + # Transition is not needed + return {} - # Transition is not needed - return {} + # Version information about the current protobuf release will be loaded + # from a version.bzl file which protobuf releases will create with every release. + + # Here some ways to match the version + # + # Make an exact match + return { + "//version:protoc_major": "3", + "//version:protoc_minor": "21", + "//version:protoc_patch": "12" + } + # + # Use to make a major version match + return { + "//version:protoc_major": "3" + } + # + # Use to make a major.minor match + return { + "//version:protoc_major": "3", + "//version:protoc_minor": "21" + } + _pb_version_transition = transition( implementation = _pb_version_transition_impl, inputs = [], - outputs = ["//version:protoc"], + outputs = [ + "//version:protoc_major", + "//version:protoc_minor", + "//version:protoc_patch", + ], ) def _cc_proto_library_impl(ctx): - toolchain = ctx.toolchains["//:demo_toolchain_type"].proto + toolchain = ctx.toolchains["//:demo_toolchain_type"] - print(toolchain) - - return DefaultInfo(files = depset([toolchain.proto_compiler.executable])) + return DefaultInfo(files = depset([toolchain.proto.proto_compiler.executable])) cc_proto_library = rule( implementation = _cc_proto_library_impl,
diff --git a/v2/version/BUILD.bazel b/v2/version/BUILD.bazel index 247ebc0..a577603 100644 --- a/v2/version/BUILD.bazel +++ b/v2/version/BUILD.bazel
@@ -3,27 +3,28 @@ package(default_visibility = ["//visibility:public"]) # https://github.com/bazelbuild/bazel/issues/16976#issuecomment-1363325329 - - # Modeling the Cross-Version Runtime Guarantee using Bazel toolchains is doable # but not trivial to get right due to lack of version range selection when using # build-setting combined with `target_settings` or constraint_setting combined with # exec_compatible_with and target_compatible_with. - -# # protoc constraint that represents the "Generated Code Version" requirement -# # in the cross version runtime guarantee matrix. -# constraint_setting(name = "protoc") - -# # similarly runtime constraint represents the "Runtime Version" in the compatibility -# # matrix. -# constraint_setting(name = "runtime") - - -# This will loaded from a version.bzl file which -# protobuf releases will create with every release. +# Representing the major version of the protoc compiler. string_setting( - name = "protoc", - build_setting_default = "3.21.12", + name = "protoc_major", + build_setting_default = "", + visibility = ["//visibility:public"], +) + +# Representing the minor version of the protoc compiler. +string_setting( + name = "protoc_minor", + build_setting_default = "", + visibility = ["//visibility:public"], +) + +# Representing the patch version of the protoc compiler. +string_setting( + name = "protoc_patch", + build_setting_default = "", visibility = ["//visibility:public"], )
diff --git a/v2/version/protoc/BUILD.bazel b/v2/version/protoc/BUILD.bazel deleted file mode 100644 index 4a68c6d..0000000 --- a/v2/version/protoc/BUILD.bazel +++ /dev/null
@@ -1,4 +0,0 @@ -constraint_value( - name = "3.21.12", - constraint_setting = "//version:protoc", -)
diff --git a/v2/version/runtime/BUILD.bazel b/v2/version/runtime/BUILD.bazel deleted file mode 100644 index 708ec68..0000000 --- a/v2/version/runtime/BUILD.bazel +++ /dev/null
@@ -1,4 +0,0 @@ -constraint_value( - name = "3.21.12", - constraint_setting = "//version:runtime", -)