Joe Onorato | 964f401 | 2023-05-06 12:29:01 -0700 | [diff] [blame] | 1 | # Copyright (C) 2023 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame^] | 15 | |
| 16 | # ----------------------------------------------------------------- |
| 17 | # Choose the flag files |
| 18 | # ----------------------------------------------------------------- |
| 19 | # Do this first, because we're going to unset TARGET_RELEASE before |
| 20 | # including anyone, so they don't start making conditionals based on it. |
| 21 | # This logic is in make because starlark doesn't understand optional |
| 22 | # vendor files. |
| 23 | |
| 24 | # If this is a google source tree, restrict it to only the one file |
| 25 | # which has OWNERS control. If it isn't let others define their own. |
| 26 | # TODO: Remove wildcard for build/release one when all branch manifests |
| 27 | # have updated. |
| 28 | config_map_files := $(wildcard build/release/release_config_map.mk) \ |
| 29 | $(if $(wildcard vendor/google/release/release_config_map.mk), \ |
| 30 | vendor/google/release/release_config_map.mk, \ |
| 31 | $(sort \ |
| 32 | $(wildcard device/*/release/release_config_map.mk) \ |
| 33 | $(wildcard device/*/*/release/release_config_map.mk) \ |
| 34 | $(wildcard vendor/*/release/release_config_map.mk) \ |
| 35 | $(wildcard vendor/*/*/release/release_config_map.mk) \ |
| 36 | ) \ |
| 37 | ) |
| 38 | |
| 39 | # $1 config name |
| 40 | # $2 release config files |
| 41 | define declare-release-config |
| 42 | $(eval # No duplicates) |
| 43 | $(if $(filter $(_all_release_configs), $(strip $(1))), \ |
| 44 | $(error declare-release-config: config $(strip $(1)) declared in: $(_included) Previously declared here: $(_all_release_configs.$(strip $(1)).DECLARED_IN)) \ |
| 45 | ) |
| 46 | $(eval # Must have release config files) |
| 47 | $(if $(strip $(2)),, \ |
| 48 | $(error declare-release-config: config $(strip $(1)) must have release config files) \ |
| 49 | ) |
| 50 | $(eval _all_release_configs := $(sort $(_all_release_configs) $(strip $(1)))) |
| 51 | $(eval _all_release_configs.$(strip $(1)).DECLARED_IN := $(_included)) |
| 52 | $(eval _all_release_configs.$(strip $(1)).FILES := $(strip $(2))) |
| 53 | endef |
| 54 | |
| 55 | # Include the config map files |
| 56 | $(foreach f, $(config_map_files), \ |
| 57 | $(eval _included := $(f)) \ |
| 58 | $(eval include $(f)) \ |
| 59 | ) |
| 60 | |
| 61 | # If TARGET_RELEASE is set, fail if there is no matching release config |
| 62 | # If it isn't set, no release config files will be included and all flags |
| 63 | # will get their default values. |
| 64 | ifneq ($(TARGET_RELEASE),) |
| 65 | ifeq ($(filter $(_all_release_configs), $(TARGET_RELEASE)),) |
| 66 | $(error No release config found for TARGET_RELEASE: $(TARGET_RELEASE). Available releases are: $(_all_release_configs)) |
| 67 | else |
| 68 | # Choose flag files |
| 69 | # Don't sort this, use it in the order they gave us. |
| 70 | flag_value_files := $(_all_release_configs.$(TARGET_RELEASE).FILES) |
| 71 | endif |
| 72 | else |
| 73 | # Useful for finding scripts etc that aren't passing or setting TARGET_RELEASE |
| 74 | ifneq ($(FAIL_IF_NO_RELEASE_CONFIG),) |
| 75 | $(error FAIL_IF_NO_RELEASE_CONFIG was set and TARGET_RELEASE was not) |
| 76 | endif |
| 77 | flag_value_files := |
| 78 | endif |
| 79 | |
| 80 | # Unset variables so they can't use them |
| 81 | define declare-release-config |
| 82 | $(error declare-release-config can only be called from inside release_config_map.mk files) |
| 83 | endef |
| 84 | |
| 85 | # TODO: Remove this check after enough people have sourced lunch that we don't |
| 86 | # need to worry about it trying to do get_build_vars TARGET_RELEASE. Maybe after ~9/2023 |
| 87 | ifneq ($(CALLED_FROM_SETUP),true) |
| 88 | define TARGET_RELEASE |
| 89 | $(error TARGET_RELEASE may not be accessed directly. Use individual flags.) |
| 90 | endef |
| 91 | else |
| 92 | TARGET_RELEASE:= |
| 93 | endif |
| 94 | .KATI_READONLY := TARGET_RELEASE |
| 95 | |
| 96 | |
| 97 | $(foreach config, $(_all_release_configs), \ |
| 98 | $(eval _all_release_configs.$(config).DECLARED_IN:= ) \ |
| 99 | $(eval _all_release_configs.$(config).FILES:= ) \ |
| 100 | ) |
| 101 | _all_release_configs:= |
| 102 | config_map_files:= |
| 103 | |
| 104 | |
| 105 | # ----------------------------------------------------------------- |
| 106 | # Flag declarations and values |
| 107 | # ----------------------------------------------------------------- |
| 108 | # This part is in starlark. We generate a root starlark file that loads |
| 109 | # all of the flags declaration files that we found, and the flag_value_files |
| 110 | # that we chose from the config map above. Then we run that, and load the |
| 111 | # results of that into the make environment. |
| 112 | |
Joe Onorato | 964f401 | 2023-05-06 12:29:01 -0700 | [diff] [blame] | 113 | # If this is a google source tree, restrict it to only the one file |
| 114 | # which has OWNERS control. If it isn't let others define their own. |
Joe Onorato | 0d1a981 | 2023-05-07 13:40:25 -0700 | [diff] [blame] | 115 | # TODO: Remove wildcard for build/release one when all branch manifests |
| 116 | # have updated. |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 117 | flag_declaration_files := $(wildcard build/release/build_flags.bzl) \ |
| 118 | $(if $(wildcard vendor/google/release/build_flags.bzl), \ |
| 119 | vendor/google/release/build_flags.bzl, \ |
Joe Onorato | 964f401 | 2023-05-06 12:29:01 -0700 | [diff] [blame] | 120 | $(sort \ |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 121 | $(wildcard device/*/release/build_flags.bzl) \ |
| 122 | $(wildcard device/*/*/release/build_flags.bzl) \ |
| 123 | $(wildcard vendor/*/release/build_flags.bzl) \ |
| 124 | $(wildcard vendor/*/*/release/build_flags.bzl) \ |
| 125 | ) \ |
| 126 | ) |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame^] | 127 | |
Joe Onorato | 964f401 | 2023-05-06 12:29:01 -0700 | [diff] [blame] | 128 | |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 129 | # Because starlark can't find files with $(wildcard), write an entrypoint starlark script that |
| 130 | # contains the result of the above wildcards for the starlark code to use. |
| 131 | filename_to_starlark=$(subst /,_,$(subst .,_,$(1))) |
| 132 | _c:=load("//build/make/core/release_config.bzl", "release_config") |
Joe Onorato | d6df20a | 2023-06-09 18:51:00 -0700 | [diff] [blame^] | 133 | _c+=$(newline)def add(d, k, v): |
| 134 | _c+=$(newline)$(space)d = dict(d) |
| 135 | _c+=$(newline)$(space)d[k] = v |
| 136 | _c+=$(newline)$(space)return d |
| 137 | _c+=$(foreach f,$(flag_declaration_files),$(newline)load("$(f)", flags_$(call filename_to_starlark,$(f)) = "flags")) |
| 138 | _c+=$(newline)all_flags = [] $(foreach f,$(flag_declaration_files),+ [add(x, "declared_in", "$(f)") for x in flags_$(call filename_to_starlark,$(f))]) |
| 139 | _c+=$(foreach f,$(flag_value_files),$(newline)load("//$(f)", values_$(call filename_to_starlark,$(f)) = "values")) |
| 140 | _c+=$(newline)all_values = [] $(foreach f,$(flag_value_files),+ [add(x, "set_in", "$(f)") for x in values_$(call filename_to_starlark,$(f))]) |
| 141 | _c+=$(newline)variables_to_export_to_make = release_config(all_flags, all_values) |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 142 | $(file >$(OUT_DIR)/release_config_entrypoint.bzl,$(_c)) |
| 143 | _c:= |
| 144 | filename_to_starlark:= |
Joe Onorato | 964f401 | 2023-05-06 12:29:01 -0700 | [diff] [blame] | 145 | |
Cole Faust | 386b374 | 2023-06-06 16:55:58 -0700 | [diff] [blame] | 146 | # Exclude the entrypoint file as a dependency (by passing it as the 2nd argument) so that we don't |
| 147 | # rerun kati every build. Kati will replay the $(file) command that generates it every build, |
| 148 | # updating its timestamp. |
| 149 | # |
| 150 | # We also need to pass --allow_external_entrypoint to rbcrun in case the OUT_DIR is set to something |
| 151 | # outside of the source tree. |
| 152 | $(call run-starlark,$(OUT_DIR)/release_config_entrypoint.bzl,$(OUT_DIR)/release_config_entrypoint.bzl,--allow_external_entrypoint) |
Joe Onorato | 7b578d3 | 2023-05-19 09:13:36 -0700 | [diff] [blame] | 153 | |