Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 430fcf64 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Drop support for release_config_map.mk files" into main

parents c107a44a 25589962
Loading
Loading
Loading
Loading
+34 −270
Original line number Diff line number Diff line
@@ -49,19 +49,7 @@ endif

# If this is a google source tree, restrict it to only the one file
# which has OWNERS control.  If it isn't let others define their own.
config_map_files := $(wildcard build/release/release_config_map.mk) \
    $(wildcard vendor/google_shared/build/release/release_config_map.mk) \
    $(if $(wildcard vendor/google/release/release_config_map.mk), \
        vendor/google/release/release_config_map.mk, \
        $(sort \
            $(wildcard device/*/release/release_config_map.mk) \
            $(wildcard device/*/*/release/release_config_map.mk) \
            $(wildcard vendor/*/release/release_config_map.mk) \
            $(wildcard vendor/*/*/release/release_config_map.mk) \
        ) \
    )

protobuf_map_files := build/release/release_config_map.textproto \
_protobuf_map_files := build/release/release_config_map.textproto \
    $(wildcard vendor/google_shared/build/release/release_config_map.textproto) \
    $(if $(wildcard vendor/google/release/release_config_map.textproto), \
        vendor/google/release/release_config_map.textproto, \
@@ -73,63 +61,19 @@ protobuf_map_files := build/release/release_config_map.textproto \
        ) \
    )

# Remove support for the legacy approach.
_must_protobuf := true

# PRODUCT_RELEASE_CONFIG_MAPS is set by Soong using an initial run of product
# config to capture only the list of config maps needed by the build.
# Keep them in the order provided, but remove duplicates.
# Treat .mk and .textproto as equal for duplicate elimination, but force
# protobuf if any PRODUCT_RELEASE_CONFIG_MAPS specify .textproto.
# Treat any .mk file as an error, since those have not worked since ap3a.
$(foreach map,$(PRODUCT_RELEASE_CONFIG_MAPS), \
    $(if $(filter $(basename $(map)),$(basename $(config_map_files))),, \
        $(eval config_map_files += $(map))) \
    $(if $(filter $(basename $(map)).textproto,$(map)),$(eval _must_protobuf := true)) \
)


# If we are missing the textproto version of any of $(config_map_files), we cannot use protobuf.
_can_protobuf := true
$(foreach map,$(config_map_files), \
    $(if $(wildcard $(basename $(map)).textproto),,$(eval _can_protobuf :=)) \
)
# If we are missing the mk version of any of $(protobuf_map_files), we must use protobuf.
$(foreach map,$(protobuf_map_files), \
    $(if $(wildcard $(basename $(map)).mk),,$(eval _must_protobuf := true)) \
    $(if $(filter $(basename $(map)).mk,$(map)),\
        $(error $(map): use of release_config_map.mk files is not supported))\
    $(if $(filter $(basename $(map)),$(basename $(_protobuf_map_files))),, \
        $(eval _protobuf_map_files += $(map))) \
)

ifneq (,$(_must_protobuf))
    ifeq (,$(_can_protobuf))
        # We must use protobuf, but we cannot use protobuf.
        $(error release config is a mixture of .scl and .textproto)
    endif
endif

_use_protobuf :=
ifneq (,$(_must_protobuf))
    _use_protobuf := true
else
    ifneq ($(_can_protobuf),)
        # Determine the default
        $(foreach map,$(config_map_files), \
            $(if $(wildcard $(dir $(map))/build_config/DEFAULT=proto),$(eval _use_protobuf := true)) \
            $(if $(wildcard $(dir $(map))/build_config/DEFAULT=make),$(eval _use_protobuf := )) \
        )
        # Update for this specific release config only (no inheritance).
        $(foreach map,$(config_map_files), \
            $(if $(wildcard $(dir $(map))/build_config/$(TARGET_RELEASE)=proto),$(eval _use_protobuf := true)) \
            $(if $(wildcard $(dir $(map))/build_config/$(TARGET_RELEASE)=make),$(eval _use_protobuf := )) \
        )
    endif
endif

ifneq (,$(_use_protobuf))
# The .textproto files are the canonical source of truth.
    _args := $(foreach map,$(config_map_files), --map $(map) )
    ifneq (,$(_must_protobuf))
        # Disable the build flag in release-config.
        _args += --guard=false
    endif
_args := --guard=false $(foreach map,$(_protobuf_map_files), --map $(map) )
_args += --allow-missing=true
ifneq (,$(TARGET_PRODUCT))
    _args += --product $(TARGET_PRODUCT)
@@ -145,7 +89,7 @@ ifneq (,$(_use_protobuf))
    $(shell if ! cmp --quiet $(_flags_varmk) $(_flags_file); then cp $(_flags_varmk) $(_flags_file); fi)
    # This will also set ALL_RELEASE_CONFIGS_FOR_PRODUCT and _used_files for us.
    $(eval include $(_flags_file))
        $(KATI_extra_file_deps $(OUT_DIR)/release-config $(protobuf_map_files) $(_flags_file))
    $(KATI_extra_file_deps $(OUT_DIR)/release-config $(_protobuf_map_files) $(_flags_file))
    ifneq (,$(_disallow_lunch_use))
        $(error Release config ${TARGET_RELEASE} is disallowed for build.  Please use one of: $(ALL_RELEASE_CONFIGS_FOR_PRODUCT))
    endif
@@ -153,104 +97,11 @@ ifneq (,$(_use_protobuf))
    # This is the first pass of product config.
    $(eval include $(_flags_varmk))
endif
_args:=
_used_files:=
    ifeq (,$(_must_protobuf)$(RELEASE_BUILD_FLAGS_IN_PROTOBUF))
        _use_protobuf :=
    endif
_flags_dir:=
_flags_file:=
_flags_varmk:=
endif
ifeq (,$(_use_protobuf))
    # The .mk files are the canonical source of truth.


# Declare an alias release-config
#
# This should be used to declare a release as an alias of another, meaning no
# release config files should be present.
#
# $1 config name
# $2 release config for which it is an alias
define alias-release-config
    $(call _declare-release-config,$(1),,$(2),true)
endef

# Declare or extend a release-config.
#
# The order of processing is:
# 1. Recursively apply any overridden release configs.  Only apply each config
#    the first time we reach it.
# 2. Apply any files for this release config, in the order they were added to
#    the declaration.
#
# Example:
#   With these declarations:
#     $(declare-release-config foo, foo.scl)
#     $(declare-release-config bar, bar.scl, foo)
#     $(declare-release-config baz, baz.scl, bar)
#     $(declare-release-config bif, bif.scl, foo baz)
#     $(declare-release-config bop, bop.scl, bar baz)
#
#   TARGET_RELEASE:
#     - bar will use: foo.scl bar.scl
#     - baz will use: foo.scl bar.scl baz.scl
#     - bif will use: foo.scl bar.scl baz.scl bif.scl
#     - bop will use: foo.scl bar.scl baz.scl bop.scl
#
# $1 config name
# $2 release config files
# $3 overridden release config
define declare-release-config
    $(call _declare-release-config,$(1),$(2),$(3),)
endef

define _declare-release-config
    $(if $(strip $(2)$(3)),,  \
        $(error declare-release-config: config $(strip $(1)) must have release config files, override another release config, or both) \
    )
    $(if $(strip $(4)),$(eval _all_release_configs.$(strip $(1)).ALIAS := true))
    $(eval ALL_RELEASE_CONFIGS_FOR_PRODUCT := $(sort $(ALL_RELEASE_CONFIGS_FOR_PRODUCT) $(strip $(1))))
    $(if $(strip $(3)), \
      $(if $(filter $(ALL_RELEASE_CONFIGS_FOR_PRODUCT), $(strip $(3))),
        $(if $(filter $(_all_release_configs.$(strip $(1)).OVERRIDES),$(strip $(3))),,
          $(eval _all_release_configs.$(strip $(1)).OVERRIDES := $(_all_release_configs.$(strip $(1)).OVERRIDES) $(strip $(3)))), \
        $(error No release config $(strip $(3))) \
      ) \
    )
    $(eval _all_release_configs.$(strip $(1)).DECLARED_IN := $(_included) $(_all_release_configs.$(strip $(1)).DECLARED_IN))
    $(eval _all_release_configs.$(strip $(1)).FILES := $(_all_release_configs.$(strip $(1)).FILES) $(strip $(2)))
endef

# Include the config map files and populate _flag_declaration_files.
# If the file is found more than once, only include it the first time.
_flag_declaration_files :=
_included_config_map_files :=
$(foreach f, $(config_map_files), \
    $(eval FLAG_DECLARATION_FILES:= ) \
    $(if $(filter $(_included_config_map_files),$(f)),,\
        $(eval _included := $(f)) \
        $(eval include $(f)) \
        $(eval _flag_declaration_files += $(FLAG_DECLARATION_FILES)) \
        $(eval _included_config_map_files += $(f)) \
    ) \
)
FLAG_DECLARATION_FILES :=

# Verify that all inherited/overridden release configs are declared.
$(foreach config,$(ALL_RELEASE_CONFIGS_FOR_PRODUCT),\
  $(foreach r,$(all_release_configs.$(r).OVERRIDES),\
    $(if $(strip $(_all_release_configs.$(r).FILES)$(_all_release_configs.$(r).OVERRIDES)),,\
    $(error Release config $(config) [declared in: $(_all_release_configs.$(r).DECLARED_IN)] inherits from non-existent $(r).)\
)))
# Verify that alias configs do not have config files.
$(foreach r,$(ALL_RELEASE_CONFIGS_FOR_PRODUCT),\
  $(if $(_all_release_configs.$(r).ALIAS),$(if $(_all_release_configs.$(r).FILES),\
    $(error Alias release config "$(r)" may not specify release config files $(_all_release_configs.$(r).FILES))\
)))

# Use makefiles
endif

ifeq ($(TARGET_RELEASE),)
    # We allow some internal paths to explicitly set TARGET_RELEASE to the
@@ -275,39 +126,6 @@ ifneq (,$(_final_product_config_pass))
    endif
endif

ifeq (,$(_use_protobuf))
# Choose flag files
# Don't sort this, use it in the order they gave us.
# Do allow duplicate entries, retaining only the first usage.
flag_value_files :=

# Apply overrides recursively
#
# $1 release config that we override
applied_releases :=
define _apply-release-config-overrides
$(foreach r,$(1), \
  $(if $(filter $(r),$(applied_releases)),, \
    $(foreach o,$(_all_release_configs.$(r).OVERRIDES),$(call _apply-release-config-overrides,$(o)))\
    $(eval applied_releases += $(r))\
    $(foreach f,$(_all_release_configs.$(r).FILES), \
      $(if $(filter $(f),$(flag_value_files)),,$(eval flag_value_files += $(f)))\
    )\
  )\
)
endef
$(call _apply-release-config-overrides,$(TARGET_RELEASE))
# Unset variables so they can't use them
define declare-release-config
$(error declare-release-config can only be called from inside release_config_map.mk files)
endef
define _apply-release-config-overrides
$(error invalid use of apply-release-config-overrides)
endef

# use makefiles
endif

# TODO: Remove this check after enough people have sourced lunch that we don't
# need to worry about it trying to do get_build_vars TARGET_RELEASE. Maybe after ~9/2023
ifneq ($(CALLED_FROM_SETUP),true)
@@ -319,58 +137,4 @@ TARGET_RELEASE:=
endif
.KATI_READONLY := TARGET_RELEASE

ifeq (,$(_use_protobuf))
$(foreach config, $(ALL_RELEASE_CONFIGS_FOR_PRODUCT), \
    $(eval _all_release_configs.$(config).DECLARED_IN:= ) \
    $(eval _all_release_configs.$(config).FILES:= ) \
)
applied_releases:=
# use makefiles
endif
config_map_files:=
protobuf_map_files:=


ifeq (,$(_use_protobuf))
# -----------------------------------------------------------------
# Flag declarations and values
# -----------------------------------------------------------------
# This part is in starlark.  We generate a root starlark file that loads
# all of the flags declaration files that we found, and the flag_value_files
# that we chose from the config map above.  Then we run that, and load the
# results of that into the make environment.

# _flag_declaration_files is the combined list of FLAG_DECLARATION_FILES set by
# release_config_map.mk files above.

# Because starlark can't find files with $(wildcard), write an entrypoint starlark script that
# contains the result of the above wildcards for the starlark code to use.
filename_to_starlark=$(subst /,_,$(subst .,_,$(1)))
_c:=load("//build/make/core/release_config.scl", "release_config")
_c+=$(newline)def add(d, k, v):
_c+=$(newline)$(space)d = dict(d)
_c+=$(newline)$(space)d[k] = v
_c+=$(newline)$(space)return d
_c+=$(foreach f,$(_flag_declaration_files),$(newline)load("$(f)", flags_$(call filename_to_starlark,$(f)) = "flags"))
_c+=$(newline)all_flags = [] $(foreach f,$(_flag_declaration_files),+ [add(x, "declared_in", "$(f)") for x in flags_$(call filename_to_starlark,$(f))])
_c+=$(foreach f,$(flag_value_files),$(newline)load("//$(f)", values_$(call filename_to_starlark,$(f)) = "values"))
_c+=$(newline)all_values = [] $(foreach f,$(flag_value_files),+ [add(x, "set_in", "$(f)") for x in values_$(call filename_to_starlark,$(f))])
_c+=$(newline)variables_to_export_to_make = release_config(all_flags, all_values)
$(file >$(OUT_DIR)/release_config_entrypoint.scl,$(_c))
_c:=
filename_to_starlark:=

# Exclude the entrypoint file as a dependency (by passing it as the 2nd argument) so that we don't
# rerun kati every build. Kati will replay the $(file) command that generates it every build,
# updating its timestamp.
#
# We also need to pass --allow_external_entrypoint to rbcrun in case the OUT_DIR is set to something
# outside of the source tree.
$(call run-starlark,$(OUT_DIR)/release_config_entrypoint.scl,$(OUT_DIR)/release_config_entrypoint.scl,--allow_external_entrypoint)

# use makefiles
endif
_can_protobuf :=
_must_protobuf :=
_use_protobuf :=
_protobuf_map_files:=

core/release_config.scl

deleted100644 → 0
+0 −243
Original line number Diff line number Diff line
# 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.
"""
Export build flags (with values) to make.
"""

load("//build/bazel/utils:schema_validation.scl", "validate")

# Partitions that get build system flag summaries
_flag_partitions = [
    "product",
    "system",
    "system_ext",
    "vendor",
]

ALL = ["all"]
PRODUCT = ["product"]
SYSTEM = ["system"]
SYSTEM_EXT = ["system_ext"]
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
                ],
            },
            "origin": {"type": "string"},
            "declared_in": {"type": "string"},
        },
        "optional_keys": {
            "appends": {
                "type": "bool",
            },
        },
    },
}

_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, *, origin = "Unknown", appends = False):
    """Declare a flag.

    Args:
      name: name of the flag
      partitions: the partitions where this should be recorded.
      default: the default value of the flag.
      origin: The origin of this flag.
      appends: Whether new values should be append (not replace) the old.

    Returns:
      A dictionary containing the flag declaration.
    """
    if not partitions:
        fail("At least 1 partition is required")
    if not name.startswith("RELEASE_"):
        fail("Release flag names must start with RELEASE_")
    if " " in name or "\t" in name or "\n" in name:
        fail("Flag names must not contain whitespace: \"" + name + "\"")
    for partition in partitions:
        if partition == "all":
            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))
    if type(default) not in _valid_types:
        fail("Invalid type of default for flag \"" + name + "\" (" + type(default) + ")")
    return {
        "name": name,
        "partitions": partitions,
        "default": default,
        "appends": appends,
        "origin": origin,
    }

def value(name, value):
    """Define the flag value for a particular configuration.

    Args:
      name: The name of the flag.
      value: The value for the flag.

    Returns:
      A dictionary containing the name and value to be used.
    """
    return {
        "name": name,
        "value": value,
    }

def _format_value(val):
    """Format the starlark type correctly for make.

    Args:
      val: The value to format

    Returns:
      The value, formatted correctly for make.
    """
    if type(val) == "NoneType":
        return ""
    elif type(val) == "bool":
        return "true" if val else ""
    else:
        return val

def equal_flag_declaration(flag, other):
    """Return true if the flag declarations are equal.

    Args:
      flag: This flag declaration.
      other: Another flag declaration.

    Returns:
      Whether the declarations are the same.
    """
    for key in "name", "partitions", "default", "appends":
        if flag[key] != other[key]:
            return False
    # For now, allow Unknown to match any other origin.
    if flag["origin"] == "Unknown" or other["origin"] == "Unknown":
        return True
    return flag["origin"] == other["origin"]

def release_config(all_flags, all_values):
    """Return the make variables that should be set for this release config.

    Args:
      all_flags: A list of flag objects (from flag() calls).
      all_values: A list of value objects (from value() calls).

    Returns:
      A dictionary of {name: value} variables for make.
    """
    validate(all_flags, _all_flags_schema)
    validate(all_values, _all_values_schema)

    # Final values.
    values = {}
    # Validate flags
    flag_names = []
    flags_dict = {}
    for flag in all_flags:
        name = flag["name"]
        if name in flag_names:
            if equal_flag_declaration(flag, flags_dict[name]):
                continue
            else:
                fail(flag["declared_in"] + ": Duplicate declaration of flag " + name +
                     " (declared first in " + flags_dict[name]["declared_in"] + ")")
        flag_names.append(name)
        flags_dict[name] = flag
        # Set the flag value to the default value.
        values[name] = {"name": name, "value": _format_value(flag["default"]), "set_in": flag["declared_in"]}

    # Record which flags go on which partition
    partitions = {}
    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:
                partitions.setdefault(partition, []).append(flag["name"])

    # Generate final values.
    # Only declared flags may have a value.
    for value in all_values:
        name = value["name"]
        if name not in flag_names:
            fail(value["set_in"] + ": Value set for undeclared build flag: " + name)
        if flags_dict[name]["appends"]:
            if name in values:
                values[name]["value"] += " " + value["value"]
                values[name]["set_in"] += " " + value["set_in"]
            else:
                values[name] = value
        else:
            values[name] = value

    # Collect values
    result = {
        "_ALL_RELEASE_FLAGS": sorted(flag_names),
    }
    for partition, names in partitions.items():
        result["_ALL_RELEASE_FLAGS.PARTITIONS." + partition] = names
    for flag in all_flags:
        val = _format_value(values[flag["name"]]["value"])
        result[flag["name"]] = val
        result["_ALL_RELEASE_FLAGS." + flag["name"] + ".PARTITIONS"] = flag["partitions"]
        result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DEFAULT"] = _format_value(flag["default"])
        result["_ALL_RELEASE_FLAGS." + flag["name"] + ".VALUE"] = val
        result["_ALL_RELEASE_FLAGS." + flag["name"] + ".DECLARED_IN"] = flag["declared_in"]
        result["_ALL_RELEASE_FLAGS." + flag["name"] + ".SET_IN"] = values[flag["name"]]["set_in"]
        result["_ALL_RELEASE_FLAGS." + flag["name"] + ".ORIGIN"] = flag["origin"]

    return result