2021-05-06 15:31:18 +02:00
|
|
|
// Copyright 2021 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
package android
|
2021-05-06 15:31:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-10-14 20:08:38 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2021-05-06 15:31:18 +02:00
|
|
|
)
|
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
// ExportedVariables is a collection of interdependent configuration variables
|
|
|
|
type ExportedVariables struct {
|
2021-10-14 20:08:38 +02:00
|
|
|
// Maps containing toolchain variables that are independent of the
|
2021-05-06 15:31:18 +02:00
|
|
|
// environment variables of the build.
|
2022-03-25 15:55:40 +01:00
|
|
|
exportedStringVars ExportedStringVariables
|
|
|
|
exportedStringListVars ExportedStringListVariables
|
|
|
|
exportedStringListDictVars ExportedStringListDictVariables
|
|
|
|
|
|
|
|
exportedVariableReferenceDictVars ExportedVariableReferenceDictVariables
|
2021-10-14 20:08:38 +02:00
|
|
|
|
|
|
|
/// Maps containing variables that are dependent on the build config.
|
2022-03-25 15:55:40 +01:00
|
|
|
exportedConfigDependingVars ExportedConfigDependingVariables
|
|
|
|
|
|
|
|
pctx PackageContext
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewExportedVariables creats an empty ExportedVariables struct with non-nil maps
|
|
|
|
func NewExportedVariables(pctx PackageContext) ExportedVariables {
|
|
|
|
return ExportedVariables{
|
|
|
|
exportedStringVars: ExportedStringVariables{},
|
|
|
|
exportedStringListVars: ExportedStringListVariables{},
|
|
|
|
exportedStringListDictVars: ExportedStringListDictVariables{},
|
|
|
|
exportedVariableReferenceDictVars: ExportedVariableReferenceDictVariables{},
|
|
|
|
exportedConfigDependingVars: ExportedConfigDependingVariables{},
|
|
|
|
pctx: pctx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportStringStaticVariable declares a static string variable and exports it to
|
|
|
|
// Bazel's toolchain.
|
|
|
|
func (ev ExportedVariables) ExportStringStaticVariable(name string, value string) {
|
|
|
|
ev.pctx.StaticVariable(name, value)
|
|
|
|
ev.exportedStringVars.set(name, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportStringListStaticVariable declares a static variable and exports it to
|
|
|
|
// Bazel's toolchain.
|
|
|
|
func (ev ExportedVariables) ExportStringListStaticVariable(name string, value []string) {
|
|
|
|
ev.pctx.StaticVariable(name, strings.Join(value, " "))
|
|
|
|
ev.exportedStringListVars.set(name, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportVariableConfigMethod declares a variable whose value is evaluated at
|
|
|
|
// runtime via a function with access to the Config and exports it to Bazel's
|
|
|
|
// toolchain.
|
|
|
|
func (ev ExportedVariables) ExportVariableConfigMethod(name string, method interface{}) blueprint.Variable {
|
|
|
|
ev.exportedConfigDependingVars.set(name, method)
|
|
|
|
return ev.pctx.VariableConfigMethod(name, method)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportSourcePathVariable declares a static "source path" variable and exports
|
|
|
|
// it to Bazel's toolchain.
|
|
|
|
func (ev ExportedVariables) ExportSourcePathVariable(name string, value string) {
|
|
|
|
ev.pctx.SourcePathVariable(name, value)
|
|
|
|
ev.exportedStringVars.set(name, value)
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:33:26 +01:00
|
|
|
// ExportVariableFuncVariable declares a variable whose value is evaluated at
|
|
|
|
// runtime via a function and exports it to Bazel's toolchain.
|
|
|
|
func (ev ExportedVariables) ExportVariableFuncVariable(name string, f func() string) {
|
|
|
|
ev.exportedConfigDependingVars.set(name, func(config Config) string {
|
|
|
|
return f()
|
|
|
|
})
|
|
|
|
ev.pctx.VariableFunc(name, func(PackageVarContext) string {
|
|
|
|
return f()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
// ExportString only exports a variable to Bazel, but does not declare it in Soong
|
|
|
|
func (ev ExportedVariables) ExportString(name string, value string) {
|
|
|
|
ev.exportedStringVars.set(name, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportStringList only exports a variable to Bazel, but does not declare it in Soong
|
|
|
|
func (ev ExportedVariables) ExportStringList(name string, value []string) {
|
|
|
|
ev.exportedStringListVars.set(name, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportStringListDict only exports a variable to Bazel, but does not declare it in Soong
|
|
|
|
func (ev ExportedVariables) ExportStringListDict(name string, value map[string][]string) {
|
|
|
|
ev.exportedStringListDictVars.set(name, value)
|
|
|
|
}
|
2021-05-06 15:31:18 +02:00
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
// ExportVariableReferenceDict only exports a variable to Bazel, but does not declare it in Soong
|
|
|
|
func (ev ExportedVariables) ExportVariableReferenceDict(name string, value map[string]string) {
|
|
|
|
ev.exportedVariableReferenceDictVars.set(name, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportedConfigDependingVariables is a mapping of variable names to functions
|
|
|
|
// of type func(config Config) string which return the runtime-evaluated string
|
|
|
|
// value of a particular variable
|
|
|
|
type ExportedConfigDependingVariables map[string]interface{}
|
2021-10-14 20:08:38 +02:00
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
func (m ExportedConfigDependingVariables) set(k string, v interface{}) {
|
2021-10-14 20:08:38 +02:00
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
// ExportedStringVariables is a mapping of variable names to string values
|
|
|
|
type ExportedStringVariables map[string]string
|
2021-05-20 15:40:14 +02:00
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
func (m ExportedStringVariables) set(k string, v string) {
|
2021-05-20 15:40:14 +02:00
|
|
|
m[k] = v
|
|
|
|
}
|
2021-05-06 15:31:18 +02:00
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
// ExportedStringListVariables is a mapping of variable names to a list of strings
|
|
|
|
type ExportedStringListVariables map[string][]string
|
2021-05-06 15:31:18 +02:00
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
func (m ExportedStringListVariables) set(k string, v []string) {
|
2021-05-06 15:31:18 +02:00
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
// ExportedStringListDictVariables is a mapping from variable names to a
|
|
|
|
// dictionary which maps keys to lists of strings
|
|
|
|
type ExportedStringListDictVariables map[string]map[string][]string
|
2021-10-14 20:08:38 +02:00
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
func (m ExportedStringListDictVariables) set(k string, v map[string][]string) {
|
2021-08-02 16:41:48 +02:00
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
|
2022-03-25 15:55:40 +01:00
|
|
|
// ExportedVariableReferenceDictVariables is a mapping from variable names to a
|
|
|
|
// dictionary which references previously defined variables. This is used to
|
|
|
|
// create a Starlark output such as:
|
2022-08-16 19:27:33 +02:00
|
|
|
//
|
|
|
|
// string_var1 = "string1
|
|
|
|
// var_ref_dict_var1 = {
|
|
|
|
// "key1": string_var1
|
|
|
|
// }
|
|
|
|
//
|
2022-03-25 15:55:40 +01:00
|
|
|
// This type of variable collection must be expanded last so that it recognizes
|
|
|
|
// previously defined variables.
|
|
|
|
type ExportedVariableReferenceDictVariables map[string]map[string]string
|
|
|
|
|
|
|
|
func (m ExportedVariableReferenceDictVariables) set(k string, v map[string]string) {
|
2022-02-16 15:02:48 +01:00
|
|
|
m[k] = v
|
|
|
|
}
|