Refactor product config files
This cl renames product_config/utils.bzl to android_product.bzl for
clarity, and moves platforms/product_variables/product_platform.bzl
to product_config/product_variables_providing_rule.bzl for consistency
because it's only ever used from android_product.bzl.
Also move a number of functions from
product_variables_providing_rule.bzl to android_product.bzl because
they're only used from android_product.bzl. In theory there's no need
for product_variables_providing_rule.bzl because that rule is also
only used from android_product.bzl, but I plan on adding more to this
rule to make it complicated enough to warrent its own file.
Bug: 269577299
Test: b build --config=android `bmod com.android.adbd`
Change-Id: I08a1eb672641f2e27ad8b9c20c2e3cab0146dfe1
diff --git a/platforms/product_variables/product_platform.bzl b/platforms/product_variables/product_platform.bzl
deleted file mode 100644
index 14d1153..0000000
--- a/platforms/product_variables/product_platform.bzl
+++ /dev/null
@@ -1,82 +0,0 @@
-"""Parallels variable.go to provide variables and create a platform based on converted config."""
-
-load("//build/bazel/platforms/arch/variants:constants.bzl", _arch_constants = "constants")
-
-def _product_variables_providing_rule_impl(ctx):
- return [
- platform_common.TemplateVariableInfo(ctx.attr.product_vars),
- ]
-
-# Provides product variables for templated string replacement.
-product_variables_providing_rule = rule(
- implementation = _product_variables_providing_rule_impl,
- attrs = {
- "product_vars": attr.string_dict(),
- },
-)
-
-def _is_variant_default(arch, variant):
- return variant == None or variant in (arch, "generic")
-
-def _soong_arch_config_to_struct(soong_arch_config):
- return struct(
- arch = soong_arch_config["arch"],
- arch_variant = soong_arch_config["arch_variant"],
- cpu_variant = soong_arch_config["cpu_variant"],
- )
-
-def _determine_target_arches_from_config(config):
- arches = []
-
- # ndk_abis and aml_abis explicitly get handled first as they override any setting
- # for DeviceArch, DeviceSecondaryArch in Soong:
- # https://cs.android.com/android/platform/superproject/+/master:build/soong/android/config.go;l=455-468;drc=b45a2ea782074944f79fc388df20b06e01f265f7
- if config.get("Ndk_abis"):
- for arch_config in _arch_constants.ndk_arches:
- arches.append(_soong_arch_config_to_struct(arch_config))
- return arches
- elif config.get("Aml_abis"):
- for arch_config in _arch_constants.aml_arches:
- arches.append(_soong_arch_config_to_struct(arch_config))
- return arches
-
- arch = config.get("DeviceArch")
- arch_variant = config.get("DeviceArchVariant")
- cpu_variant = config.get("DeviceCpuVariant")
-
- if _is_variant_default(arch, arch_variant):
- arch_variant = ""
- if _is_variant_default(arch, cpu_variant):
- cpu_variant = ""
-
- if not arch:
- # TODO(b/258839711): determine how to better id whether a config is actually host only or we're just missing the target config
- if "DeviceArch" in config:
- fail("No architecture was specified in the product config, expected one of Ndk_abis, Aml_abis, or DeviceArch to be set:\n%s" % config)
- else:
- return arches
-
- arches.append(struct(
- arch = arch,
- arch_variant = arch_variant,
- cpu_variant = cpu_variant,
- ))
-
- arch = config.get("DeviceSecondaryArch")
- arch_variant = config.get("DeviceSecondaryArchVariant")
- cpu_variant = config.get("DeviceSecondaryCpuVariant")
-
- if _is_variant_default(arch, arch_variant):
- arch_variant = ""
- if _is_variant_default(arch, cpu_variant):
- cpu_variant = ""
-
- if arch:
- arches.append(struct(
- arch = arch,
- arch_variant = arch_variant,
- cpu_variant = cpu_variant,
- ))
- return arches
-
-determine_target_arches_from_config = _determine_target_arches_from_config
diff --git a/product_config/utils.bzl b/product_config/android_product.bzl
similarity index 77%
rename from product_config/utils.bzl
rename to product_config/android_product.bzl
index 3aaf27c..23ada96 100644
--- a/product_config/utils.bzl
+++ b/product_config/android_product.bzl
@@ -1,5 +1,18 @@
-load("@//build/bazel/platforms:product_variables/product_platform.bzl", "determine_target_arches_from_config", "product_variables_providing_rule")
-load("@//build/bazel/platforms/arch/variants:constants.bzl", _variant_constants = "constants")
+# Copyright (C) 2023 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+load("//build/bazel/platforms/arch/variants:constants.bzl", _arch_constants = "constants")
load("//build/bazel/product_variables:constants.bzl", "constants")
load(
"//prebuilts/clang/host/linux-x86:cc_toolchain_constants.bzl",
@@ -7,8 +20,73 @@
"variant_constraints",
"variant_name",
)
+load(":product_variables_providing_rule.bzl", "product_variables_providing_rule")
-def product_variable_constraint_settings(variables):
+def _is_variant_default(arch, variant):
+ return variant == None or variant in (arch, "generic")
+
+def _soong_arch_config_to_struct(soong_arch_config):
+ return struct(
+ arch = soong_arch_config["arch"],
+ arch_variant = soong_arch_config["arch_variant"],
+ cpu_variant = soong_arch_config["cpu_variant"],
+ )
+
+def _determine_target_arches_from_config(config):
+ arches = []
+
+ # ndk_abis and aml_abis explicitly get handled first as they override any setting
+ # for DeviceArch, DeviceSecondaryArch in Soong:
+ # https://cs.android.com/android/platform/superproject/+/master:build/soong/android/config.go;l=455-468;drc=b45a2ea782074944f79fc388df20b06e01f265f7
+ if config.get("Ndk_abis"):
+ for arch_config in _arch_constants.ndk_arches:
+ arches.append(_soong_arch_config_to_struct(arch_config))
+ return arches
+ elif config.get("Aml_abis"):
+ for arch_config in _arch_constants.aml_arches:
+ arches.append(_soong_arch_config_to_struct(arch_config))
+ return arches
+
+ arch = config.get("DeviceArch")
+ arch_variant = config.get("DeviceArchVariant")
+ cpu_variant = config.get("DeviceCpuVariant")
+
+ if _is_variant_default(arch, arch_variant):
+ arch_variant = ""
+ if _is_variant_default(arch, cpu_variant):
+ cpu_variant = ""
+
+ if not arch:
+ # TODO(b/258839711): determine how to better id whether a config is actually host only or we're just missing the target config
+ if "DeviceArch" in config:
+ fail("No architecture was specified in the product config, expected one of Ndk_abis, Aml_abis, or DeviceArch to be set:\n%s" % config)
+ else:
+ return arches
+
+ arches.append(struct(
+ arch = arch,
+ arch_variant = arch_variant,
+ cpu_variant = cpu_variant,
+ ))
+
+ arch = config.get("DeviceSecondaryArch")
+ arch_variant = config.get("DeviceSecondaryArchVariant")
+ cpu_variant = config.get("DeviceSecondaryCpuVariant")
+
+ if _is_variant_default(arch, arch_variant):
+ arch_variant = ""
+ if _is_variant_default(arch, cpu_variant):
+ cpu_variant = ""
+
+ if arch:
+ arches.append(struct(
+ arch = arch,
+ arch_variant = arch_variant,
+ cpu_variant = cpu_variant,
+ ))
+ return arches
+
+def _product_variable_constraint_settings(variables):
constraints = []
local_vars = dict(variables)
@@ -102,7 +180,7 @@
"@//build/bazel/platforms/os:android",
] + ["@" + v for v in variant_constraints(
arch,
- _variant_constants.AndroidArchToVariantToFeatures[arch.arch],
+ _arch_constants.AndroidArchToVariantToFeatures[arch.arch],
)],
)
@@ -133,8 +211,8 @@
select statements in the BUILD files (ultimately) refer to, they're all
created here.
"""
- product_var_constraints, attribute_vars = product_variable_constraint_settings(soong_variables)
- arch_configs = determine_target_arches_from_config(soong_variables)
+ product_var_constraints, attribute_vars = _product_variable_constraint_settings(soong_variables)
+ arch_configs = _determine_target_arches_from_config(soong_variables)
product_variables_providing_rule(
name = name + "_product_vars",
@@ -169,7 +247,7 @@
"@//build/bazel/platforms/os:android",
] + ["@" + v for v in variant_constraints(
variant,
- _variant_constants.AndroidArchToVariantToFeatures[arch],
+ _arch_constants.AndroidArchToVariantToFeatures[arch],
)],
)
diff --git a/product_config/product_variables_providing_rule.bzl b/product_config/product_variables_providing_rule.bzl
new file mode 100644
index 0000000..5de34ec
--- /dev/null
+++ b/product_config/product_variables_providing_rule.bzl
@@ -0,0 +1,26 @@
+# Copyright (C) 2023 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+def _product_variables_providing_rule_impl(ctx):
+ return [
+ platform_common.TemplateVariableInfo(ctx.attr.product_vars),
+ ]
+
+# Provides product variables for templated string replacement.
+product_variables_providing_rule = rule(
+ implementation = _product_variables_providing_rule_impl,
+ attrs = {
+ "product_vars": attr.string_dict(),
+ },
+)