From 0c869edcd1888d6e397e4b499dac64b784c35389 Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Thu, 7 Sep 2023 12:29:21 -0700 Subject: [PATCH 1/2] Make release_config.bzl match internal To avoid merge conflicts. These are buildifier errors that will be fixed in a followup cl. Test: presubmits Change-Id: I22c91c796aee16ef16f05471c2ff5221fa41d7b3 --- core/release_config.bzl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/core/release_config.bzl b/core/release_config.bzl index 805106f4e7..ca7927e52f 100644 --- a/core/release_config.bzl +++ b/core/release_config.bzl @@ -41,16 +41,17 @@ def flag(name, partitions, default): if len(partitions) > 1: fail("\"all\" can't be combined with other partitions: " + str(partitions)) elif partition not in _flag_partitions: - fail("Invalid partition: " + partition + ", allowed partitions: " + - str(_flag_partitions)) + fail("Invalid partition: " + partition + ", allowed partitions: " + + str(_flag_partitions)) if type(default) not in _valid_types: fail("Invalid type of default for flag \"" + name + "\" (" + type(default) + ")") return { "name": name, "partitions": partitions, - "default": default, + "default": default } + def value(name, value): "Define the flag value for a particular configuration." return { @@ -58,6 +59,7 @@ def value(name, value): "value": value, } + def _format_value(val): "Format the starlark type correctly for make" if type(val) == "NoneType": @@ -67,9 +69,9 @@ def _format_value(val): else: return val + def release_config(all_flags, all_values): "Return the make variables that should be set for this release config." - # Validate flags flag_names = [] for flag in all_flags: @@ -119,3 +121,4 @@ def release_config(all_flags, all_values): result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = set_in return result + From 8a7efafe50667ab9b69642c1ed36833080260b15 Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Tue, 15 Aug 2023 17:12:01 -0700 Subject: [PATCH 2/2] Validate release config with a schema To more thouroughly check that it's valid. Test: m nothing Change-Id: I1cfdc6f4d20e826eb870f5e1e71d0386c589bc9a --- core/release_config.bzl | 61 +++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/core/release_config.bzl b/core/release_config.bzl index ca7927e52f..a2f59e63a8 100644 --- a/core/release_config.bzl +++ b/core/release_config.bzl @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("//build/bazel/utils:schema_validation.bzl", "validate") + # Partitions that get build system flag summaries _flag_partitions = [ "product", @@ -28,6 +30,48 @@ VENDOR = ["vendor"] _valid_types = ["NoneType", "bool", "list", "string", "int"] +_all_flags_schema = { + "type": "list", + "of": { + "type": "dict", + "required_keys": { + "name": {"type": "string"}, + "partitions": { + "type": "list", + "of": { + "type": "string", + "choices": _flag_partitions + ["all"], + }, + "unique": True, + }, + "default": { + "or": [ + {"type": t} + for t in _valid_types + ], + }, + "declared_in": {"type": "string"}, + }, + }, +} + +_all_values_schema = { + "type": "list", + "of": { + "type": "dict", + "required_keys": { + "name": {"type": "string"}, + "value": { + "or": [ + {"type": t} + for t in _valid_types + ], + }, + "set_in": {"type": "string"}, + }, + }, +} + def flag(name, partitions, default): "Declare a flag." if not partitions: @@ -41,17 +85,16 @@ def flag(name, partitions, default): if len(partitions) > 1: fail("\"all\" can't be combined with other partitions: " + str(partitions)) elif partition not in _flag_partitions: - fail("Invalid partition: " + partition + ", allowed partitions: " - + str(_flag_partitions)) + fail("Invalid partition: " + partition + ", allowed partitions: " + + str(_flag_partitions)) if type(default) not in _valid_types: fail("Invalid type of default for flag \"" + name + "\" (" + type(default) + ")") return { "name": name, "partitions": partitions, - "default": default + "default": default, } - def value(name, value): "Define the flag value for a particular configuration." return { @@ -59,7 +102,6 @@ def value(name, value): "value": value, } - def _format_value(val): "Format the starlark type correctly for make" if type(val) == "NoneType": @@ -69,9 +111,11 @@ def _format_value(val): else: return val - def release_config(all_flags, all_values): "Return the make variables that should be set for this release config." + validate(all_flags, _all_flags_schema) + validate(all_values, _all_values_schema) + # Validate flags flag_names = [] for flag in all_flags: @@ -84,6 +128,8 @@ def release_config(all_flags, all_values): for flag in all_flags: for partition in flag["partitions"]: if partition == "all": + if len(flag["partitions"]) > 1: + fail("\"all\" can't be combined with other partitions: " + str(flag["partitions"])) for partition in _flag_partitions: partitions.setdefault(partition, []).append(flag["name"]) else: @@ -107,8 +153,6 @@ def release_config(all_flags, all_values): if flag["name"] in values: val = values[flag["name"]]["value"] set_in = values[flag["name"]]["set_in"] - if type(val) not in _valid_types: - fail("Invalid type of value for flag \"" + flag["name"] + "\" (" + type(val) + ")") else: val = flag["default"] set_in = flag["declared_in"] @@ -121,4 +165,3 @@ def release_config(all_flags, all_values): result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = set_in return result -