blob: 4c2fb5e4a5c634b802cf97820d78ff6cba0f31d0 [file] [log] [blame]
Sam Delmerico7f889562022-03-25 14:55:40 +00001// 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
15package android
16
17import (
Sam Delmerico7f889562022-03-25 14:55:40 +000018 "strings"
19
Sam Delmerico7f889562022-03-25 14:55:40 +000020 "github.com/google/blueprint"
21)
22
Sam Delmerico7f889562022-03-25 14:55:40 +000023// ExportedVariables is a collection of interdependent configuration variables
24type 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
40func 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 Delmerico7f889562022-03-25 14:55:40 +000051// ExportStringStaticVariable declares a static string variable and exports it to
52// Bazel's toolchain.
53func (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.
60func (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.
68func (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.
75func (ev ExportedVariables) ExportSourcePathVariable(name string, value string) {
76 ev.pctx.SourcePathVariable(name, value)
77 ev.exportedStringVars.set(name, value)
78}
79
Sam Delmerico932c01c2022-03-25 16:33:26 +000080// ExportVariableFuncVariable declares a variable whose value is evaluated at
81// runtime via a function and exports it to Bazel's toolchain.
82func (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 Delmerico7f889562022-03-25 14:55:40 +000091// ExportString only exports a variable to Bazel, but does not declare it in Soong
92func (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
97func (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
102func (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
107func (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
114type ExportedConfigDependingVariables map[string]interface{}
115
116func (m ExportedConfigDependingVariables) set(k string, v interface{}) {
117 m[k] = v
118}
119
Sam Delmerico7f889562022-03-25 14:55:40 +0000120// ExportedStringVariables is a mapping of variable names to string values
121type ExportedStringVariables map[string]string
122
123func (m ExportedStringVariables) set(k string, v string) {
124 m[k] = v
125}
126
Sam Delmerico7f889562022-03-25 14:55:40 +0000127// ExportedStringListVariables is a mapping of variable names to a list of strings
128type ExportedStringListVariables map[string][]string
129
130func (m ExportedStringListVariables) set(k string, v []string) {
131 m[k] = v
132}
133
Sam Delmerico7f889562022-03-25 14:55:40 +0000134// ExportedStringListDictVariables is a mapping from variable names to a
135// dictionary which maps keys to lists of strings
136type ExportedStringListDictVariables map[string]map[string][]string
137
138func (m ExportedStringListDictVariables) set(k string, v map[string][]string) {
139 m[k] = v
140}
141
Sam Delmerico7f889562022-03-25 14:55:40 +0000142// 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 Crossd079e0b2022-08-16 10:27:33 -0700145//
146// string_var1 = "string1
147// var_ref_dict_var1 = {
148// "key1": string_var1
149// }
150//
Sam Delmerico7f889562022-03-25 14:55:40 +0000151// This type of variable collection must be expanded last so that it recognizes
152// previously defined variables.
153type ExportedVariableReferenceDictVariables map[string]map[string]string
154
155func (m ExportedVariableReferenceDictVariables) set(k string, v map[string]string) {
156 m[k] = v
157}