Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 1 | // Copyright 2021 Google Inc. All rights reserved. |
| 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 18 | "strings" |
| 19 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 20 | "github.com/google/blueprint" |
| 21 | ) |
| 22 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 23 | // ExportedVariables is a collection of interdependent configuration variables |
| 24 | type ExportedVariables struct { |
| 25 | // Maps containing toolchain variables that are independent of the |
| 26 | // environment variables of the build. |
| 27 | exportedStringVars ExportedStringVariables |
| 28 | exportedStringListVars ExportedStringListVariables |
| 29 | exportedStringListDictVars ExportedStringListDictVariables |
| 30 | |
| 31 | exportedVariableReferenceDictVars ExportedVariableReferenceDictVariables |
| 32 | |
| 33 | /// Maps containing variables that are dependent on the build config. |
| 34 | exportedConfigDependingVars ExportedConfigDependingVariables |
| 35 | |
| 36 | pctx PackageContext |
| 37 | } |
| 38 | |
| 39 | // NewExportedVariables creats an empty ExportedVariables struct with non-nil maps |
| 40 | func NewExportedVariables(pctx PackageContext) ExportedVariables { |
| 41 | return ExportedVariables{ |
| 42 | exportedStringVars: ExportedStringVariables{}, |
| 43 | exportedStringListVars: ExportedStringListVariables{}, |
| 44 | exportedStringListDictVars: ExportedStringListDictVariables{}, |
| 45 | exportedVariableReferenceDictVars: ExportedVariableReferenceDictVariables{}, |
| 46 | exportedConfigDependingVars: ExportedConfigDependingVariables{}, |
| 47 | pctx: pctx, |
| 48 | } |
| 49 | } |
| 50 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 51 | // ExportStringStaticVariable declares a static string variable and exports it to |
| 52 | // Bazel's toolchain. |
| 53 | func (ev ExportedVariables) ExportStringStaticVariable(name string, value string) { |
| 54 | ev.pctx.StaticVariable(name, value) |
| 55 | ev.exportedStringVars.set(name, value) |
| 56 | } |
| 57 | |
| 58 | // ExportStringListStaticVariable declares a static variable and exports it to |
| 59 | // Bazel's toolchain. |
| 60 | func (ev ExportedVariables) ExportStringListStaticVariable(name string, value []string) { |
| 61 | ev.pctx.StaticVariable(name, strings.Join(value, " ")) |
| 62 | ev.exportedStringListVars.set(name, value) |
| 63 | } |
| 64 | |
| 65 | // ExportVariableConfigMethod declares a variable whose value is evaluated at |
| 66 | // runtime via a function with access to the Config and exports it to Bazel's |
| 67 | // toolchain. |
| 68 | func (ev ExportedVariables) ExportVariableConfigMethod(name string, method interface{}) blueprint.Variable { |
| 69 | ev.exportedConfigDependingVars.set(name, method) |
| 70 | return ev.pctx.VariableConfigMethod(name, method) |
| 71 | } |
| 72 | |
| 73 | // ExportSourcePathVariable declares a static "source path" variable and exports |
| 74 | // it to Bazel's toolchain. |
| 75 | func (ev ExportedVariables) ExportSourcePathVariable(name string, value string) { |
| 76 | ev.pctx.SourcePathVariable(name, value) |
| 77 | ev.exportedStringVars.set(name, value) |
| 78 | } |
| 79 | |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 80 | // ExportVariableFuncVariable declares a variable whose value is evaluated at |
| 81 | // runtime via a function and exports it to Bazel's toolchain. |
| 82 | func (ev ExportedVariables) ExportVariableFuncVariable(name string, f func() string) { |
| 83 | ev.exportedConfigDependingVars.set(name, func(config Config) string { |
| 84 | return f() |
| 85 | }) |
| 86 | ev.pctx.VariableFunc(name, func(PackageVarContext) string { |
| 87 | return f() |
| 88 | }) |
| 89 | } |
| 90 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 91 | // ExportString only exports a variable to Bazel, but does not declare it in Soong |
| 92 | func (ev ExportedVariables) ExportString(name string, value string) { |
| 93 | ev.exportedStringVars.set(name, value) |
| 94 | } |
| 95 | |
| 96 | // ExportStringList only exports a variable to Bazel, but does not declare it in Soong |
| 97 | func (ev ExportedVariables) ExportStringList(name string, value []string) { |
| 98 | ev.exportedStringListVars.set(name, value) |
| 99 | } |
| 100 | |
| 101 | // ExportStringListDict only exports a variable to Bazel, but does not declare it in Soong |
| 102 | func (ev ExportedVariables) ExportStringListDict(name string, value map[string][]string) { |
| 103 | ev.exportedStringListDictVars.set(name, value) |
| 104 | } |
| 105 | |
| 106 | // ExportVariableReferenceDict only exports a variable to Bazel, but does not declare it in Soong |
| 107 | func (ev ExportedVariables) ExportVariableReferenceDict(name string, value map[string]string) { |
| 108 | ev.exportedVariableReferenceDictVars.set(name, value) |
| 109 | } |
| 110 | |
| 111 | // ExportedConfigDependingVariables is a mapping of variable names to functions |
| 112 | // of type func(config Config) string which return the runtime-evaluated string |
| 113 | // value of a particular variable |
| 114 | type ExportedConfigDependingVariables map[string]interface{} |
| 115 | |
| 116 | func (m ExportedConfigDependingVariables) set(k string, v interface{}) { |
| 117 | m[k] = v |
| 118 | } |
| 119 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 120 | // ExportedStringVariables is a mapping of variable names to string values |
| 121 | type ExportedStringVariables map[string]string |
| 122 | |
| 123 | func (m ExportedStringVariables) set(k string, v string) { |
| 124 | m[k] = v |
| 125 | } |
| 126 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 127 | // ExportedStringListVariables is a mapping of variable names to a list of strings |
| 128 | type ExportedStringListVariables map[string][]string |
| 129 | |
| 130 | func (m ExportedStringListVariables) set(k string, v []string) { |
| 131 | m[k] = v |
| 132 | } |
| 133 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 134 | // ExportedStringListDictVariables is a mapping from variable names to a |
| 135 | // dictionary which maps keys to lists of strings |
| 136 | type ExportedStringListDictVariables map[string]map[string][]string |
| 137 | |
| 138 | func (m ExportedStringListDictVariables) set(k string, v map[string][]string) { |
| 139 | m[k] = v |
| 140 | } |
| 141 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 142 | // ExportedVariableReferenceDictVariables is a mapping from variable names to a |
| 143 | // dictionary which references previously defined variables. This is used to |
| 144 | // create a Starlark output such as: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 145 | // |
| 146 | // string_var1 = "string1 |
| 147 | // var_ref_dict_var1 = { |
| 148 | // "key1": string_var1 |
| 149 | // } |
| 150 | // |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 151 | // This type of variable collection must be expanded last so that it recognizes |
| 152 | // previously defined variables. |
| 153 | type ExportedVariableReferenceDictVariables map[string]map[string]string |
| 154 | |
| 155 | func (m ExportedVariableReferenceDictVariables) set(k string, v map[string]string) { |
| 156 | m[k] = v |
| 157 | } |