refactor Bazel variable export
Most of the variable export code for cc modules can be re-used for
exporting variables for java modules. Refactor this code into a more
composable structure for reuse.
Test: build/bazel/bp2build.sh
Test: manual comparison of
out/soong/soong_injection/cc_toolchain/constants.bzl
with previous output
Change-Id: Ie5a6fee08cc888b7dc69c3e324e5c3f8aa269a8f
diff --git a/android/Android.bp b/android/Android.bp
index c072ac2..49d5b91 100644
--- a/android/Android.bp
+++ b/android/Android.bp
@@ -36,6 +36,7 @@
"bazel_handler.go",
"bazel_paths.go",
"config.go",
+ "config_bp2build.go",
"csuite_config.go",
"deapexer.go",
"defaults.go",
@@ -96,6 +97,7 @@
"bazel_handler_test.go",
"bazel_test.go",
"config_test.go",
+ "config_bp2build_test.go",
"csuite_config_test.go",
"defaults_test.go",
"depset_test.go",
diff --git a/android/api_levels.go b/android/api_levels.go
index 27a3b7f..8163894 100644
--- a/android/api_levels.go
+++ b/android/api_levels.go
@@ -19,6 +19,7 @@
"fmt"
"strconv"
+ "android/soong/bazel"
"android/soong/starlark_fmt"
)
@@ -393,10 +394,10 @@
}
func StarlarkApiLevelConfigs(config Config) string {
- return fmt.Sprintf(`# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
+ return fmt.Sprintf(bazel.GeneratedBazelFileWarning+`
_api_levels = %s
api_levels = _api_levels
`, printApiLevelsStarlarkDict(config),
)
-}
\ No newline at end of file
+}
diff --git a/android/config_bp2build.go b/android/config_bp2build.go
new file mode 100644
index 0000000..80b09fc
--- /dev/null
+++ b/android/config_bp2build.go
@@ -0,0 +1,435 @@
+// 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.
+
+package android
+
+import (
+ "fmt"
+ "reflect"
+ "regexp"
+ "sort"
+ "strings"
+
+ "android/soong/bazel"
+ "android/soong/starlark_fmt"
+
+ "github.com/google/blueprint"
+)
+
+// BazelVarExporter is a collection of configuration variables that can be exported for use in Bazel rules
+type BazelVarExporter interface {
+ // asBazel expands strings of configuration variables into their concrete values
+ asBazel(Config, ExportedStringVariables, ExportedStringListVariables, ExportedConfigDependingVariables) []bazelConstant
+}
+
+// ExportedVariables is a collection of interdependent configuration variables
+type ExportedVariables struct {
+ // Maps containing toolchain variables that are independent of the
+ // environment variables of the build.
+ exportedStringVars ExportedStringVariables
+ exportedStringListVars ExportedStringListVariables
+ exportedStringListDictVars ExportedStringListDictVariables
+
+ exportedVariableReferenceDictVars ExportedVariableReferenceDictVariables
+
+ /// Maps containing variables that are dependent on the build config.
+ 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,
+ }
+}
+
+func (ev ExportedVariables) asBazel(config Config,
+ stringVars ExportedStringVariables, stringListVars ExportedStringListVariables, cfgDepVars ExportedConfigDependingVariables) []bazelConstant {
+ ret := []bazelConstant{}
+ ret = append(ret, ev.exportedStringVars.asBazel(config, stringVars, stringListVars, cfgDepVars)...)
+ ret = append(ret, ev.exportedStringListVars.asBazel(config, stringVars, stringListVars, cfgDepVars)...)
+ ret = append(ret, ev.exportedStringListDictVars.asBazel(config, stringVars, stringListVars, cfgDepVars)...)
+ // Note: ExportedVariableReferenceDictVars collections can only contain references to other variables and must be printed last
+ ret = append(ret, ev.exportedVariableReferenceDictVars.asBazel(config, stringVars, stringListVars, cfgDepVars)...)
+ return ret
+}
+
+// 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)
+}
+
+// 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)
+}
+
+// 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{}
+
+func (m ExportedConfigDependingVariables) set(k string, v interface{}) {
+ m[k] = v
+}
+
+// Ensure that string s has no invalid characters to be generated into the bzl file.
+func validateCharacters(s string) string {
+ for _, c := range []string{`\n`, `"`, `\`} {
+ if strings.Contains(s, c) {
+ panic(fmt.Errorf("%s contains illegal character %s", s, c))
+ }
+ }
+ return s
+}
+
+type bazelConstant struct {
+ variableName string
+ internalDefinition string
+ sortLast bool
+}
+
+// ExportedStringVariables is a mapping of variable names to string values
+type ExportedStringVariables map[string]string
+
+func (m ExportedStringVariables) set(k string, v string) {
+ m[k] = v
+}
+
+func (m ExportedStringVariables) asBazel(config Config,
+ stringVars ExportedStringVariables, stringListVars ExportedStringListVariables, cfgDepVars ExportedConfigDependingVariables) []bazelConstant {
+ ret := make([]bazelConstant, 0, len(m))
+ for k, variableValue := range m {
+ expandedVar, err := expandVar(config, variableValue, stringVars, stringListVars, cfgDepVars)
+ if err != nil {
+ panic(fmt.Errorf("error expanding config variable %s: %s", k, err))
+ }
+ if len(expandedVar) > 1 {
+ panic(fmt.Errorf("%s expands to more than one string value: %s", variableValue, expandedVar))
+ }
+ ret = append(ret, bazelConstant{
+ variableName: k,
+ internalDefinition: fmt.Sprintf(`"%s"`, validateCharacters(expandedVar[0])),
+ })
+ }
+ return ret
+}
+
+// ExportedStringListVariables is a mapping of variable names to a list of strings
+type ExportedStringListVariables map[string][]string
+
+func (m ExportedStringListVariables) set(k string, v []string) {
+ m[k] = v
+}
+
+func (m ExportedStringListVariables) asBazel(config Config,
+ stringScope ExportedStringVariables, stringListScope ExportedStringListVariables,
+ exportedVars ExportedConfigDependingVariables) []bazelConstant {
+ ret := make([]bazelConstant, 0, len(m))
+ // For each exported variable, recursively expand elements in the variableValue
+ // list to ensure that interpolated variables are expanded according to their values
+ // in the variable scope.
+ for k, variableValue := range m {
+ var expandedVars []string
+ for _, v := range variableValue {
+ expandedVar, err := expandVar(config, v, stringScope, stringListScope, exportedVars)
+ if err != nil {
+ panic(fmt.Errorf("Error expanding config variable %s=%s: %s", k, v, err))
+ }
+ expandedVars = append(expandedVars, expandedVar...)
+ }
+ // Assign the list as a bzl-private variable; this variable will be exported
+ // out through a constants struct later.
+ ret = append(ret, bazelConstant{
+ variableName: k,
+ internalDefinition: starlark_fmt.PrintStringList(expandedVars, 0),
+ })
+ }
+ return ret
+}
+
+// ExportedStringListDictVariables is a mapping from variable names to a
+// dictionary which maps keys to lists of strings
+type ExportedStringListDictVariables map[string]map[string][]string
+
+func (m ExportedStringListDictVariables) set(k string, v map[string][]string) {
+ m[k] = v
+}
+
+// Since dictionaries are not supported in Ninja, we do not expand variables for dictionaries
+func (m ExportedStringListDictVariables) asBazel(_ Config, _ ExportedStringVariables,
+ _ ExportedStringListVariables, _ ExportedConfigDependingVariables) []bazelConstant {
+ ret := make([]bazelConstant, 0, len(m))
+ for k, dict := range m {
+ ret = append(ret, bazelConstant{
+ variableName: k,
+ internalDefinition: starlark_fmt.PrintStringListDict(dict, 0),
+ })
+ }
+ return ret
+}
+
+// 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:
+// string_var1 = "string1
+// var_ref_dict_var1 = {
+// "key1": string_var1
+// }
+// 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) {
+ m[k] = v
+}
+
+func (m ExportedVariableReferenceDictVariables) asBazel(_ Config, _ ExportedStringVariables,
+ _ ExportedStringListVariables, _ ExportedConfigDependingVariables) []bazelConstant {
+ ret := make([]bazelConstant, 0, len(m))
+ for n, dict := range m {
+ for k, v := range dict {
+ matches, err := variableReference(v)
+ if err != nil {
+ panic(err)
+ } else if !matches.matches {
+ panic(fmt.Errorf("Expected a variable reference, got %q", v))
+ } else if len(matches.fullVariableReference) != len(v) {
+ panic(fmt.Errorf("Expected only a variable reference, got %q", v))
+ }
+ dict[k] = "_" + matches.variable
+ }
+ ret = append(ret, bazelConstant{
+ variableName: n,
+ internalDefinition: starlark_fmt.PrintDict(dict, 0),
+ sortLast: true,
+ })
+ }
+ return ret
+}
+
+// BazelToolchainVars expands an ExportedVariables collection and returns a string
+// of formatted Starlark variable definitions
+func BazelToolchainVars(config Config, exportedVars ExportedVariables) string {
+ results := exportedVars.asBazel(
+ config,
+ exportedVars.exportedStringVars,
+ exportedVars.exportedStringListVars,
+ exportedVars.exportedConfigDependingVars,
+ )
+
+ sort.Slice(results, func(i, j int) bool {
+ if results[i].sortLast != results[j].sortLast {
+ return !results[i].sortLast
+ }
+ return results[i].variableName < results[j].variableName
+ })
+
+ definitions := make([]string, 0, len(results))
+ constants := make([]string, 0, len(results))
+ for _, b := range results {
+ definitions = append(definitions,
+ fmt.Sprintf("_%s = %s", b.variableName, b.internalDefinition))
+ constants = append(constants,
+ fmt.Sprintf("%[1]s%[2]s = _%[2]s,", starlark_fmt.Indention(1), b.variableName))
+ }
+
+ // Build the exported constants struct.
+ ret := bazel.GeneratedBazelFileWarning
+ ret += "\n\n"
+ ret += strings.Join(definitions, "\n\n")
+ ret += "\n\n"
+ ret += "constants = struct(\n"
+ ret += strings.Join(constants, "\n")
+ ret += "\n)"
+
+ return ret
+}
+
+type match struct {
+ matches bool
+ fullVariableReference string
+ variable string
+}
+
+func variableReference(input string) (match, error) {
+ // e.g. "${ExternalCflags}"
+ r := regexp.MustCompile(`\${(?:config\.)?([a-zA-Z0-9_]+)}`)
+
+ matches := r.FindStringSubmatch(input)
+ if len(matches) == 0 {
+ return match{}, nil
+ }
+ if len(matches) != 2 {
+ return match{}, fmt.Errorf("Expected to only match 1 subexpression in %s, got %d", input, len(matches)-1)
+ }
+ return match{
+ matches: true,
+ fullVariableReference: matches[0],
+ // Index 1 of FindStringSubmatch contains the subexpression match
+ // (variable name) of the capture group.
+ variable: matches[1],
+ }, nil
+}
+
+// expandVar recursively expand interpolated variables in the exportedVars scope.
+//
+// We're using a string slice to track the seen variables to avoid
+// stackoverflow errors with infinite recursion. it's simpler to use a
+// string slice than to handle a pass-by-referenced map, which would make it
+// quite complex to track depth-first interpolations. It's also unlikely the
+// interpolation stacks are deep (n > 1).
+func expandVar(config Config, toExpand string, stringScope ExportedStringVariables,
+ stringListScope ExportedStringListVariables, exportedVars ExportedConfigDependingVariables) ([]string, error) {
+
+ // Internal recursive function.
+ var expandVarInternal func(string, map[string]bool) (string, error)
+ expandVarInternal = func(toExpand string, seenVars map[string]bool) (string, error) {
+ var ret string
+ remainingString := toExpand
+ for len(remainingString) > 0 {
+ matches, err := variableReference(remainingString)
+ if err != nil {
+ panic(err)
+ }
+ if !matches.matches {
+ return ret + remainingString, nil
+ }
+ matchIndex := strings.Index(remainingString, matches.fullVariableReference)
+ ret += remainingString[:matchIndex]
+ remainingString = remainingString[matchIndex+len(matches.fullVariableReference):]
+
+ variable := matches.variable
+ // toExpand contains a variable.
+ if _, ok := seenVars[variable]; ok {
+ return ret, fmt.Errorf(
+ "Unbounded recursive interpolation of variable: %s", variable)
+ }
+ // A map is passed-by-reference. Create a new map for
+ // this scope to prevent variables seen in one depth-first expansion
+ // to be also treated as "seen" in other depth-first traversals.
+ newSeenVars := map[string]bool{}
+ for k := range seenVars {
+ newSeenVars[k] = true
+ }
+ newSeenVars[variable] = true
+ if unexpandedVars, ok := stringListScope[variable]; ok {
+ expandedVars := []string{}
+ for _, unexpandedVar := range unexpandedVars {
+ expandedVar, err := expandVarInternal(unexpandedVar, newSeenVars)
+ if err != nil {
+ return ret, err
+ }
+ expandedVars = append(expandedVars, expandedVar)
+ }
+ ret += strings.Join(expandedVars, " ")
+ } else if unexpandedVar, ok := stringScope[variable]; ok {
+ expandedVar, err := expandVarInternal(unexpandedVar, newSeenVars)
+ if err != nil {
+ return ret, err
+ }
+ ret += expandedVar
+ } else if unevaluatedVar, ok := exportedVars[variable]; ok {
+ evalFunc := reflect.ValueOf(unevaluatedVar)
+ validateVariableMethod(variable, evalFunc)
+ evaluatedResult := evalFunc.Call([]reflect.Value{reflect.ValueOf(config)})
+ evaluatedValue := evaluatedResult[0].Interface().(string)
+ expandedVar, err := expandVarInternal(evaluatedValue, newSeenVars)
+ if err != nil {
+ return ret, err
+ }
+ ret += expandedVar
+ } else {
+ return "", fmt.Errorf("Unbound config variable %s", variable)
+ }
+ }
+ return ret, nil
+ }
+ var ret []string
+ for _, v := range strings.Split(toExpand, " ") {
+ val, err := expandVarInternal(v, map[string]bool{})
+ if err != nil {
+ return ret, err
+ }
+ ret = append(ret, val)
+ }
+
+ return ret, nil
+}
+
+func validateVariableMethod(name string, methodValue reflect.Value) {
+ methodType := methodValue.Type()
+ if methodType.Kind() != reflect.Func {
+ panic(fmt.Errorf("method given for variable %s is not a function",
+ name))
+ }
+ if n := methodType.NumIn(); n != 1 {
+ panic(fmt.Errorf("method for variable %s has %d inputs (should be 1)",
+ name, n))
+ }
+ if n := methodType.NumOut(); n != 1 {
+ panic(fmt.Errorf("method for variable %s has %d outputs (should be 1)",
+ name, n))
+ }
+ if kind := methodType.Out(0).Kind(); kind != reflect.String {
+ panic(fmt.Errorf("method for variable %s does not return a string",
+ name))
+ }
+}
diff --git a/cc/config/bp2build_test.go b/android/config_bp2build_test.go
similarity index 68%
rename from cc/config/bp2build_test.go
rename to android/config_bp2build_test.go
index 9a8178a..05a1798 100644
--- a/cc/config/bp2build_test.go
+++ b/android/config_bp2build_test.go
@@ -12,25 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package config
+package android
import (
+ "android/soong/bazel"
"testing"
-
- "android/soong/android"
)
func TestExpandVars(t *testing.T) {
- android_arm64_config := android.TestConfig("out", nil, "", nil)
- android_arm64_config.BuildOS = android.Android
- android_arm64_config.BuildArch = android.Arm64
+ android_arm64_config := TestConfig("out", nil, "", nil)
+ android_arm64_config.BuildOS = Android
+ android_arm64_config.BuildArch = Arm64
testCases := []struct {
description string
- config android.Config
- stringScope exportedStringVariables
- stringListScope exportedStringListVariables
- configVars exportedConfigDependingVariables
+ config Config
+ stringScope ExportedStringVariables
+ stringListScope ExportedStringListVariables
+ configVars ExportedConfigDependingVariables
toExpand string
expectedValues []string
}{
@@ -41,7 +40,7 @@
},
{
description: "single level expansion for string var",
- stringScope: exportedStringVariables{
+ stringScope: ExportedStringVariables{
"foo": "bar",
},
toExpand: "${foo}",
@@ -49,7 +48,7 @@
},
{
description: "single level expansion with short-name for string var",
- stringScope: exportedStringVariables{
+ stringScope: ExportedStringVariables{
"foo": "bar",
},
toExpand: "${config.foo}",
@@ -57,7 +56,7 @@
},
{
description: "single level expansion string list var",
- stringListScope: exportedStringListVariables{
+ stringListScope: ExportedStringListVariables{
"foo": []string{"bar"},
},
toExpand: "${foo}",
@@ -65,11 +64,11 @@
},
{
description: "mixed level expansion for string list var",
- stringScope: exportedStringVariables{
+ stringScope: ExportedStringVariables{
"foo": "${bar}",
"qux": "hello",
},
- stringListScope: exportedStringListVariables{
+ stringListScope: ExportedStringListVariables{
"bar": []string{"baz", "${qux}"},
},
toExpand: "${foo}",
@@ -77,7 +76,7 @@
},
{
description: "double level expansion",
- stringListScope: exportedStringListVariables{
+ stringListScope: ExportedStringListVariables{
"foo": []string{"${bar}"},
"bar": []string{"baz"},
},
@@ -86,7 +85,7 @@
},
{
description: "double level expansion with a literal",
- stringListScope: exportedStringListVariables{
+ stringListScope: ExportedStringListVariables{
"a": []string{"${b}", "c"},
"b": []string{"d"},
},
@@ -95,7 +94,7 @@
},
{
description: "double level expansion, with two variables in a string",
- stringListScope: exportedStringListVariables{
+ stringListScope: ExportedStringListVariables{
"a": []string{"${b} ${c}"},
"b": []string{"d"},
"c": []string{"e"},
@@ -105,7 +104,7 @@
},
{
description: "triple level expansion with two variables in a string",
- stringListScope: exportedStringListVariables{
+ stringListScope: ExportedStringListVariables{
"a": []string{"${b} ${c}"},
"b": []string{"${c}", "${d}"},
"c": []string{"${d}"},
@@ -116,9 +115,9 @@
},
{
description: "expansion with config depending vars",
- configVars: exportedConfigDependingVariables{
- "a": func(c android.Config) string { return c.BuildOS.String() },
- "b": func(c android.Config) string { return c.BuildArch.String() },
+ configVars: ExportedConfigDependingVariables{
+ "a": func(c Config) string { return c.BuildOS.String() },
+ "b": func(c Config) string { return c.BuildArch.String() },
},
config: android_arm64_config,
toExpand: "${a}-${b}",
@@ -126,14 +125,14 @@
},
{
description: "double level multi type expansion",
- stringListScope: exportedStringListVariables{
+ stringListScope: ExportedStringListVariables{
"platform": []string{"${os}-${arch}"},
"const": []string{"const"},
},
- configVars: exportedConfigDependingVariables{
- "os": func(c android.Config) string { return c.BuildOS.String() },
- "arch": func(c android.Config) string { return c.BuildArch.String() },
- "foo": func(c android.Config) string { return "foo" },
+ configVars: ExportedConfigDependingVariables{
+ "os": func(c Config) string { return c.BuildOS.String() },
+ "arch": func(c Config) string { return c.BuildArch.String() },
+ "foo": func(c Config) string { return "foo" },
},
config: android_arm64_config,
toExpand: "${const}/${platform}/${foo}",
@@ -160,19 +159,19 @@
func TestBazelToolchainVars(t *testing.T) {
testCases := []struct {
name string
- config android.Config
- vars []bazelVarExporter
+ config Config
+ vars ExportedVariables
expectedOut string
}{
{
name: "exports strings",
- vars: []bazelVarExporter{
- exportedStringVariables{
+ vars: ExportedVariables{
+ exportedStringVars: ExportedStringVariables{
"a": "b",
"c": "d",
},
},
- expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
+ expectedOut: bazel.GeneratedBazelFileWarning + `
_a = "b"
@@ -185,13 +184,13 @@
},
{
name: "exports string lists",
- vars: []bazelVarExporter{
- exportedStringListVariables{
+ vars: ExportedVariables{
+ exportedStringListVars: ExportedStringListVariables{
"a": []string{"b1", "b2"},
"c": []string{"d1", "d2"},
},
},
- expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
+ expectedOut: bazel.GeneratedBazelFileWarning + `
_a = [
"b1",
@@ -210,13 +209,13 @@
},
{
name: "exports string lists dicts",
- vars: []bazelVarExporter{
- exportedStringListDictVariables{
- "a": map[string][]string{"b1": []string{"b2"}},
- "c": map[string][]string{"d1": []string{"d2"}},
+ vars: ExportedVariables{
+ exportedStringListDictVars: ExportedStringListDictVariables{
+ "a": map[string][]string{"b1": {"b2"}},
+ "c": map[string][]string{"d1": {"d2"}},
},
},
- expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
+ expectedOut: bazel.GeneratedBazelFileWarning + `
_a = {
"b1": ["b2"],
@@ -233,13 +232,13 @@
},
{
name: "exports dict with var refs",
- vars: []bazelVarExporter{
- exportedVariableReferenceDictVariables{
+ vars: ExportedVariables{
+ exportedVariableReferenceDictVars: ExportedVariableReferenceDictVariables{
"a": map[string]string{"b1": "${b2}"},
"c": map[string]string{"d1": "${config.d2}"},
},
},
- expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
+ expectedOut: bazel.GeneratedBazelFileWarning + `
_a = {
"b1": _b2,
@@ -256,25 +255,25 @@
},
{
name: "sorts across types with variable references last",
- vars: []bazelVarExporter{
- exportedStringVariables{
+ vars: ExportedVariables{
+ exportedStringVars: ExportedStringVariables{
"b": "b-val",
"d": "d-val",
},
- exportedStringListVariables{
+ exportedStringListVars: ExportedStringListVariables{
"c": []string{"c-val"},
"e": []string{"e-val"},
},
- exportedStringListDictVariables{
- "a": map[string][]string{"a1": []string{"a2"}},
- "f": map[string][]string{"f1": []string{"f2"}},
+ exportedStringListDictVars: ExportedStringListDictVariables{
+ "a": map[string][]string{"a1": {"a2"}},
+ "f": map[string][]string{"f1": {"f2"}},
},
- exportedVariableReferenceDictVariables{
+ exportedVariableReferenceDictVars: ExportedVariableReferenceDictVariables{
"aa": map[string]string{"b1": "${b}"},
"cc": map[string]string{"d1": "${config.d}"},
},
},
- expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
+ expectedOut: bazel.GeneratedBazelFileWarning + `
_a = {
"a1": ["a2"],
@@ -315,7 +314,7 @@
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
- out := bazelToolchainVars(tc.config, tc.vars...)
+ out := BazelToolchainVars(tc.config, tc.vars)
if out != tc.expectedOut {
t.Errorf("Expected \n%s, got \n%s", tc.expectedOut, out)
}
diff --git a/bazel/constants.go b/bazel/constants.go
index 6beb496..b10f256 100644
--- a/bazel/constants.go
+++ b/bazel/constants.go
@@ -21,7 +21,7 @@
SoongInjectionDirName = "soong_injection"
- GeneratedBazelFileWarning = "# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT"
+ GeneratedBazelFileWarning = "# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT."
)
// String returns the name of the run.
diff --git a/cc/config/Android.bp b/cc/config/Android.bp
index e1b0605..1a21c13 100644
--- a/cc/config/Android.bp
+++ b/cc/config/Android.bp
@@ -11,7 +11,6 @@
"soong-starlark-format",
],
srcs: [
- "bp2build.go",
"clang.go",
"global.go",
"tidy.go",
@@ -33,7 +32,6 @@
"arm64_linux_host.go",
],
testSrcs: [
- "bp2build_test.go",
"tidy_test.go",
],
}
diff --git a/cc/config/arm64_device.go b/cc/config/arm64_device.go
index 4d0ae1a..dfe143f 100644
--- a/cc/config/arm64_device.go
+++ b/cc/config/arm64_device.go
@@ -98,28 +98,28 @@
pctx.SourcePathVariable("Arm64GccRoot",
"prebuilts/gcc/${HostPrebuiltTag}/aarch64/aarch64-linux-android-${arm64GccVersion}")
- exportStringListStaticVariable("Arm64Ldflags", arm64Ldflags)
- exportStringListStaticVariable("Arm64Lldflags", arm64Lldflags)
+ exportedVars.ExportStringListStaticVariable("Arm64Ldflags", arm64Ldflags)
+ exportedVars.ExportStringListStaticVariable("Arm64Lldflags", arm64Lldflags)
- exportStringListStaticVariable("Arm64Cflags", arm64Cflags)
- exportStringListStaticVariable("Arm64Cppflags", arm64Cppflags)
+ exportedVars.ExportStringListStaticVariable("Arm64Cflags", arm64Cflags)
+ exportedVars.ExportStringListStaticVariable("Arm64Cppflags", arm64Cppflags)
- exportedVariableReferenceDictVars.Set("Arm64ArchVariantCflags", arm64ArchVariantCflagsVar)
- exportedVariableReferenceDictVars.Set("Arm64CpuVariantCflags", arm64CpuVariantCflagsVar)
- exportedVariableReferenceDictVars.Set("Arm64CpuVariantLdflags", arm64CpuVariantLdflags)
+ exportedVars.ExportVariableReferenceDict("Arm64ArchVariantCflags", arm64ArchVariantCflagsVar)
+ exportedVars.ExportVariableReferenceDict("Arm64CpuVariantCflags", arm64CpuVariantCflagsVar)
+ exportedVars.ExportVariableReferenceDict("Arm64CpuVariantLdflags", arm64CpuVariantLdflags)
- exportStringListStaticVariable("Arm64Armv8ACflags", arm64ArchVariantCflags["armv8-a"])
- exportStringListStaticVariable("Arm64Armv8ABranchProtCflags", arm64ArchVariantCflags["armv8-a-branchprot"])
- exportStringListStaticVariable("Arm64Armv82ACflags", arm64ArchVariantCflags["armv8-2a"])
- exportStringListStaticVariable("Arm64Armv82ADotprodCflags", arm64ArchVariantCflags["armv8-2a-dotprod"])
+ exportedVars.ExportStringListStaticVariable("Arm64Armv8ACflags", arm64ArchVariantCflags["armv8-a"])
+ exportedVars.ExportStringListStaticVariable("Arm64Armv8ABranchProtCflags", arm64ArchVariantCflags["armv8-a-branchprot"])
+ exportedVars.ExportStringListStaticVariable("Arm64Armv82ACflags", arm64ArchVariantCflags["armv8-2a"])
+ exportedVars.ExportStringListStaticVariable("Arm64Armv82ADotprodCflags", arm64ArchVariantCflags["armv8-2a-dotprod"])
- exportStringListStaticVariable("Arm64CortexA53Cflags", arm64CpuVariantCflags["cortex-a53"])
- exportStringListStaticVariable("Arm64CortexA55Cflags", arm64CpuVariantCflags["cortex-a55"])
- exportStringListStaticVariable("Arm64KryoCflags", arm64CpuVariantCflags["kryo"])
- exportStringListStaticVariable("Arm64ExynosM1Cflags", arm64CpuVariantCflags["exynos-m1"])
- exportStringListStaticVariable("Arm64ExynosM2Cflags", arm64CpuVariantCflags["exynos-m2"])
+ exportedVars.ExportStringListStaticVariable("Arm64CortexA53Cflags", arm64CpuVariantCflags["cortex-a53"])
+ exportedVars.ExportStringListStaticVariable("Arm64CortexA55Cflags", arm64CpuVariantCflags["cortex-a55"])
+ exportedVars.ExportStringListStaticVariable("Arm64KryoCflags", arm64CpuVariantCflags["kryo"])
+ exportedVars.ExportStringListStaticVariable("Arm64ExynosM1Cflags", arm64CpuVariantCflags["exynos-m1"])
+ exportedVars.ExportStringListStaticVariable("Arm64ExynosM2Cflags", arm64CpuVariantCflags["exynos-m2"])
- exportStringListStaticVariable("Arm64FixCortexA53Ldflags", []string{"-Wl,--fix-cortex-a53-843419"})
+ exportedVars.ExportStringListStaticVariable("Arm64FixCortexA53Ldflags", []string{"-Wl,--fix-cortex-a53-843419"})
}
var (
diff --git a/cc/config/arm_device.go b/cc/config/arm_device.go
index 4466632..d702c61 100644
--- a/cc/config/arm_device.go
+++ b/cc/config/arm_device.go
@@ -178,41 +178,41 @@
pctx.SourcePathVariable("ArmGccRoot", "prebuilts/gcc/${HostPrebuiltTag}/arm/arm-linux-androideabi-${armGccVersion}")
// Just exported. Not created as a Ninja static variable.
- exportedStringVars.Set("ArmClangTriple", clangTriple)
+ exportedVars.ExportString("ArmClangTriple", clangTriple)
- exportStringListStaticVariable("ArmLdflags", armLdflags)
- exportStringListStaticVariable("ArmLldflags", armLldflags)
+ exportedVars.ExportStringListStaticVariable("ArmLdflags", armLdflags)
+ exportedVars.ExportStringListStaticVariable("ArmLldflags", armLldflags)
- exportStringListStaticVariable("ArmFixCortexA8LdFlags", armFixCortexA8LdFlags)
- exportStringListStaticVariable("ArmNoFixCortexA8LdFlags", armNoFixCortexA8LdFlags)
+ exportedVars.ExportStringListStaticVariable("ArmFixCortexA8LdFlags", armFixCortexA8LdFlags)
+ exportedVars.ExportStringListStaticVariable("ArmNoFixCortexA8LdFlags", armNoFixCortexA8LdFlags)
// Clang cflags
- exportStringListStaticVariable("ArmToolchainCflags", armToolchainCflags)
- exportStringListStaticVariable("ArmCflags", armCflags)
- exportStringListStaticVariable("ArmCppflags", armCppflags)
+ exportedVars.ExportStringListStaticVariable("ArmToolchainCflags", armToolchainCflags)
+ exportedVars.ExportStringListStaticVariable("ArmCflags", armCflags)
+ exportedVars.ExportStringListStaticVariable("ArmCppflags", armCppflags)
// Clang ARM vs. Thumb instruction set cflags
- exportStringListStaticVariable("ArmArmCflags", armArmCflags)
- exportStringListStaticVariable("ArmThumbCflags", armThumbCflags)
+ exportedVars.ExportStringListStaticVariable("ArmArmCflags", armArmCflags)
+ exportedVars.ExportStringListStaticVariable("ArmThumbCflags", armThumbCflags)
- exportedVariableReferenceDictVars.Set("ArmArchVariantCflags", armArchVariantCflagsVar)
- exportedVariableReferenceDictVars.Set("ArmCpuVariantCflags", armCpuVariantCflagsVar)
+ exportedVars.ExportVariableReferenceDict("ArmArchVariantCflags", armArchVariantCflagsVar)
+ exportedVars.ExportVariableReferenceDict("ArmCpuVariantCflags", armCpuVariantCflagsVar)
// Clang arch variant cflags
- exportStringListStaticVariable("ArmArmv7ACflags", armArchVariantCflags["armv7-a"])
- exportStringListStaticVariable("ArmArmv7ANeonCflags", armArchVariantCflags["armv7-a-neon"])
- exportStringListStaticVariable("ArmArmv8ACflags", armArchVariantCflags["armv8-a"])
- exportStringListStaticVariable("ArmArmv82ACflags", armArchVariantCflags["armv8-2a"])
+ exportedVars.ExportStringListStaticVariable("ArmArmv7ACflags", armArchVariantCflags["armv7-a"])
+ exportedVars.ExportStringListStaticVariable("ArmArmv7ANeonCflags", armArchVariantCflags["armv7-a-neon"])
+ exportedVars.ExportStringListStaticVariable("ArmArmv8ACflags", armArchVariantCflags["armv8-a"])
+ exportedVars.ExportStringListStaticVariable("ArmArmv82ACflags", armArchVariantCflags["armv8-2a"])
// Clang cpu variant cflags
- exportStringListStaticVariable("ArmGenericCflags", armCpuVariantCflags[""])
- exportStringListStaticVariable("ArmCortexA7Cflags", armCpuVariantCflags["cortex-a7"])
- exportStringListStaticVariable("ArmCortexA8Cflags", armCpuVariantCflags["cortex-a8"])
- exportStringListStaticVariable("ArmCortexA15Cflags", armCpuVariantCflags["cortex-a15"])
- exportStringListStaticVariable("ArmCortexA53Cflags", armCpuVariantCflags["cortex-a53"])
- exportStringListStaticVariable("ArmCortexA55Cflags", armCpuVariantCflags["cortex-a55"])
- exportStringListStaticVariable("ArmKraitCflags", armCpuVariantCflags["krait"])
- exportStringListStaticVariable("ArmKryoCflags", armCpuVariantCflags["kryo"])
+ exportedVars.ExportStringListStaticVariable("ArmGenericCflags", armCpuVariantCflags[""])
+ exportedVars.ExportStringListStaticVariable("ArmCortexA7Cflags", armCpuVariantCflags["cortex-a7"])
+ exportedVars.ExportStringListStaticVariable("ArmCortexA8Cflags", armCpuVariantCflags["cortex-a8"])
+ exportedVars.ExportStringListStaticVariable("ArmCortexA15Cflags", armCpuVariantCflags["cortex-a15"])
+ exportedVars.ExportStringListStaticVariable("ArmCortexA53Cflags", armCpuVariantCflags["cortex-a53"])
+ exportedVars.ExportStringListStaticVariable("ArmCortexA55Cflags", armCpuVariantCflags["cortex-a55"])
+ exportedVars.ExportStringListStaticVariable("ArmKraitCflags", armCpuVariantCflags["krait"])
+ exportedVars.ExportStringListStaticVariable("ArmKryoCflags", armCpuVariantCflags["kryo"])
}
var (
diff --git a/cc/config/bp2build.go b/cc/config/bp2build.go
deleted file mode 100644
index 73f65f5..0000000
--- a/cc/config/bp2build.go
+++ /dev/null
@@ -1,378 +0,0 @@
-// 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.
-
-package config
-
-import (
- "fmt"
- "reflect"
- "regexp"
- "sort"
- "strings"
-
- "android/soong/android"
- "android/soong/starlark_fmt"
-
- "github.com/google/blueprint"
-)
-
-type bazelVarExporter interface {
- asBazel(android.Config, exportedStringVariables, exportedStringListVariables, exportedConfigDependingVariables) []bazelConstant
-}
-
-// Helpers for exporting cc configuration information to Bazel.
-var (
- // Maps containing toolchain variables that are independent of the
- // environment variables of the build.
- exportedStringListVars = exportedStringListVariables{}
- exportedStringVars = exportedStringVariables{}
- exportedStringListDictVars = exportedStringListDictVariables{}
- // Note: these can only contain references to other variables and must be printed last
- exportedVariableReferenceDictVars = exportedVariableReferenceDictVariables{}
-
- /// Maps containing variables that are dependent on the build config.
- exportedConfigDependingVars = exportedConfigDependingVariables{}
-)
-
-type exportedConfigDependingVariables map[string]interface{}
-
-func (m exportedConfigDependingVariables) Set(k string, v interface{}) {
- m[k] = v
-}
-
-// Ensure that string s has no invalid characters to be generated into the bzl file.
-func validateCharacters(s string) string {
- for _, c := range []string{`\n`, `"`, `\`} {
- if strings.Contains(s, c) {
- panic(fmt.Errorf("%s contains illegal character %s", s, c))
- }
- }
- return s
-}
-
-type bazelConstant struct {
- variableName string
- internalDefinition string
- sortLast bool
-}
-
-type exportedStringVariables map[string]string
-
-func (m exportedStringVariables) Set(k string, v string) {
- m[k] = v
-}
-
-func (m exportedStringVariables) asBazel(config android.Config,
- stringVars exportedStringVariables, stringListVars exportedStringListVariables, cfgDepVars exportedConfigDependingVariables) []bazelConstant {
- ret := make([]bazelConstant, 0, len(m))
- for k, variableValue := range m {
- expandedVar, err := expandVar(config, variableValue, stringVars, stringListVars, cfgDepVars)
- if err != nil {
- panic(fmt.Errorf("error expanding config variable %s: %s", k, err))
- }
- if len(expandedVar) > 1 {
- panic(fmt.Errorf("%s expands to more than one string value: %s", variableValue, expandedVar))
- }
- ret = append(ret, bazelConstant{
- variableName: k,
- internalDefinition: fmt.Sprintf(`"%s"`, validateCharacters(expandedVar[0])),
- })
- }
- return ret
-}
-
-// Convenience function to declare a static variable and export it to Bazel's cc_toolchain.
-func exportStringStaticVariable(name string, value string) {
- pctx.StaticVariable(name, value)
- exportedStringVars.Set(name, value)
-}
-
-type exportedStringListVariables map[string][]string
-
-func (m exportedStringListVariables) Set(k string, v []string) {
- m[k] = v
-}
-
-func (m exportedStringListVariables) asBazel(config android.Config,
- stringScope exportedStringVariables, stringListScope exportedStringListVariables,
- exportedVars exportedConfigDependingVariables) []bazelConstant {
- ret := make([]bazelConstant, 0, len(m))
- // For each exported variable, recursively expand elements in the variableValue
- // list to ensure that interpolated variables are expanded according to their values
- // in the variable scope.
- for k, variableValue := range m {
- var expandedVars []string
- for _, v := range variableValue {
- expandedVar, err := expandVar(config, v, stringScope, stringListScope, exportedVars)
- if err != nil {
- panic(fmt.Errorf("Error expanding config variable %s=%s: %s", k, v, err))
- }
- expandedVars = append(expandedVars, expandedVar...)
- }
- // Assign the list as a bzl-private variable; this variable will be exported
- // out through a constants struct later.
- ret = append(ret, bazelConstant{
- variableName: k,
- internalDefinition: starlark_fmt.PrintStringList(expandedVars, 0),
- })
- }
- return ret
-}
-
-// Convenience function to declare a static "source path" variable and export it to Bazel's cc_toolchain.
-func exportVariableConfigMethod(name string, method interface{}) blueprint.Variable {
- exportedConfigDependingVars.Set(name, method)
- return pctx.VariableConfigMethod(name, method)
-}
-
-// Convenience function to declare a static "source path" variable and export it to Bazel's cc_toolchain.
-func exportSourcePathVariable(name string, value string) {
- pctx.SourcePathVariable(name, value)
- exportedStringVars.Set(name, value)
-}
-
-// Convenience function to declare a static variable and export it to Bazel's cc_toolchain.
-func exportStringListStaticVariable(name string, value []string) {
- pctx.StaticVariable(name, strings.Join(value, " "))
- exportedStringListVars.Set(name, value)
-}
-
-func ExportStringList(name string, value []string) {
- exportedStringListVars.Set(name, value)
-}
-
-type exportedStringListDictVariables map[string]map[string][]string
-
-func (m exportedStringListDictVariables) Set(k string, v map[string][]string) {
- m[k] = v
-}
-
-// Since dictionaries are not supported in Ninja, we do not expand variables for dictionaries
-func (m exportedStringListDictVariables) asBazel(_ android.Config, _ exportedStringVariables,
- _ exportedStringListVariables, _ exportedConfigDependingVariables) []bazelConstant {
- ret := make([]bazelConstant, 0, len(m))
- for k, dict := range m {
- ret = append(ret, bazelConstant{
- variableName: k,
- internalDefinition: starlark_fmt.PrintStringListDict(dict, 0),
- })
- }
- return ret
-}
-
-type exportedVariableReferenceDictVariables map[string]map[string]string
-
-func (m exportedVariableReferenceDictVariables) Set(k string, v map[string]string) {
- m[k] = v
-}
-
-func (m exportedVariableReferenceDictVariables) asBazel(_ android.Config, _ exportedStringVariables,
- _ exportedStringListVariables, _ exportedConfigDependingVariables) []bazelConstant {
- ret := make([]bazelConstant, 0, len(m))
- for n, dict := range m {
- for k, v := range dict {
- matches, err := variableReference(v)
- if err != nil {
- panic(err)
- } else if !matches.matches {
- panic(fmt.Errorf("Expected a variable reference, got %q", v))
- } else if len(matches.fullVariableReference) != len(v) {
- panic(fmt.Errorf("Expected only a variable reference, got %q", v))
- }
- dict[k] = "_" + matches.variable
- }
- ret = append(ret, bazelConstant{
- variableName: n,
- internalDefinition: starlark_fmt.PrintDict(dict, 0),
- sortLast: true,
- })
- }
- return ret
-}
-
-// BazelCcToolchainVars generates bzl file content containing variables for
-// Bazel's cc_toolchain configuration.
-func BazelCcToolchainVars(config android.Config) string {
- return bazelToolchainVars(
- config,
- exportedStringListDictVars,
- exportedStringListVars,
- exportedStringVars,
- exportedVariableReferenceDictVars)
-}
-
-func bazelToolchainVars(config android.Config, vars ...bazelVarExporter) string {
- ret := "# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.\n\n"
-
- results := []bazelConstant{}
- for _, v := range vars {
- results = append(results, v.asBazel(config, exportedStringVars, exportedStringListVars, exportedConfigDependingVars)...)
- }
-
- sort.Slice(results, func(i, j int) bool {
- if results[i].sortLast != results[j].sortLast {
- return !results[i].sortLast
- }
- return results[i].variableName < results[j].variableName
- })
-
- definitions := make([]string, 0, len(results))
- constants := make([]string, 0, len(results))
- for _, b := range results {
- definitions = append(definitions,
- fmt.Sprintf("_%s = %s", b.variableName, b.internalDefinition))
- constants = append(constants,
- fmt.Sprintf("%[1]s%[2]s = _%[2]s,", starlark_fmt.Indention(1), b.variableName))
- }
-
- // Build the exported constants struct.
- ret += strings.Join(definitions, "\n\n")
- ret += "\n\n"
- ret += "constants = struct(\n"
- ret += strings.Join(constants, "\n")
- ret += "\n)"
-
- return ret
-}
-
-type match struct {
- matches bool
- fullVariableReference string
- variable string
-}
-
-func variableReference(input string) (match, error) {
- // e.g. "${ExternalCflags}"
- r := regexp.MustCompile(`\${(?:config\.)?([a-zA-Z0-9_]+)}`)
-
- matches := r.FindStringSubmatch(input)
- if len(matches) == 0 {
- return match{}, nil
- }
- if len(matches) != 2 {
- return match{}, fmt.Errorf("Expected to only match 1 subexpression in %s, got %d", input, len(matches)-1)
- }
- return match{
- matches: true,
- fullVariableReference: matches[0],
- // Index 1 of FindStringSubmatch contains the subexpression match
- // (variable name) of the capture group.
- variable: matches[1],
- }, nil
-}
-
-// expandVar recursively expand interpolated variables in the exportedVars scope.
-//
-// We're using a string slice to track the seen variables to avoid
-// stackoverflow errors with infinite recursion. it's simpler to use a
-// string slice than to handle a pass-by-referenced map, which would make it
-// quite complex to track depth-first interpolations. It's also unlikely the
-// interpolation stacks are deep (n > 1).
-func expandVar(config android.Config, toExpand string, stringScope exportedStringVariables,
- stringListScope exportedStringListVariables, exportedVars exportedConfigDependingVariables) ([]string, error) {
-
- // Internal recursive function.
- var expandVarInternal func(string, map[string]bool) (string, error)
- expandVarInternal = func(toExpand string, seenVars map[string]bool) (string, error) {
- var ret string
- remainingString := toExpand
- for len(remainingString) > 0 {
- matches, err := variableReference(remainingString)
- if err != nil {
- panic(err)
- }
- if !matches.matches {
- return ret + remainingString, nil
- }
- matchIndex := strings.Index(remainingString, matches.fullVariableReference)
- ret += remainingString[:matchIndex]
- remainingString = remainingString[matchIndex+len(matches.fullVariableReference):]
-
- variable := matches.variable
- // toExpand contains a variable.
- if _, ok := seenVars[variable]; ok {
- return ret, fmt.Errorf(
- "Unbounded recursive interpolation of variable: %s", variable)
- }
- // A map is passed-by-reference. Create a new map for
- // this scope to prevent variables seen in one depth-first expansion
- // to be also treated as "seen" in other depth-first traversals.
- newSeenVars := map[string]bool{}
- for k := range seenVars {
- newSeenVars[k] = true
- }
- newSeenVars[variable] = true
- if unexpandedVars, ok := stringListScope[variable]; ok {
- expandedVars := []string{}
- for _, unexpandedVar := range unexpandedVars {
- expandedVar, err := expandVarInternal(unexpandedVar, newSeenVars)
- if err != nil {
- return ret, err
- }
- expandedVars = append(expandedVars, expandedVar)
- }
- ret += strings.Join(expandedVars, " ")
- } else if unexpandedVar, ok := stringScope[variable]; ok {
- expandedVar, err := expandVarInternal(unexpandedVar, newSeenVars)
- if err != nil {
- return ret, err
- }
- ret += expandedVar
- } else if unevaluatedVar, ok := exportedVars[variable]; ok {
- evalFunc := reflect.ValueOf(unevaluatedVar)
- validateVariableMethod(variable, evalFunc)
- evaluatedResult := evalFunc.Call([]reflect.Value{reflect.ValueOf(config)})
- evaluatedValue := evaluatedResult[0].Interface().(string)
- expandedVar, err := expandVarInternal(evaluatedValue, newSeenVars)
- if err != nil {
- return ret, err
- }
- ret += expandedVar
- } else {
- return "", fmt.Errorf("Unbound config variable %s", variable)
- }
- }
- return ret, nil
- }
- var ret []string
- for _, v := range strings.Split(toExpand, " ") {
- val, err := expandVarInternal(v, map[string]bool{})
- if err != nil {
- return ret, err
- }
- ret = append(ret, val)
- }
-
- return ret, nil
-}
-
-func validateVariableMethod(name string, methodValue reflect.Value) {
- methodType := methodValue.Type()
- if methodType.Kind() != reflect.Func {
- panic(fmt.Errorf("method given for variable %s is not a function",
- name))
- }
- if n := methodType.NumIn(); n != 1 {
- panic(fmt.Errorf("method for variable %s has %d inputs (should be 1)",
- name, n))
- }
- if n := methodType.NumOut(); n != 1 {
- panic(fmt.Errorf("method for variable %s has %d outputs (should be 1)",
- name, n))
- }
- if kind := methodType.Out(0).Kind(); kind != reflect.String {
- panic(fmt.Errorf("method for variable %s does not return a string",
- name))
- }
-}
diff --git a/cc/config/global.go b/cc/config/global.go
index fad675a..f5b8f3c 100644
--- a/cc/config/global.go
+++ b/cc/config/global.go
@@ -23,6 +23,9 @@
)
var (
+ pctx = android.NewPackageContext("android/soong/cc/config")
+ exportedVars = android.NewExportedVariables(pctx)
+
// Flags used by lots of devices. Putting them in package static variables
// will save bytes in build.ninja so they aren't repeated for every file
commonGlobalCflags = []string{
@@ -299,20 +302,28 @@
WarningAllowedOldProjects = []string{}
)
-var pctx = android.NewPackageContext("android/soong/cc/config")
+// BazelCcToolchainVars generates bzl file content containing variables for
+// Bazel's cc_toolchain configuration.
+func BazelCcToolchainVars(config android.Config) string {
+ return android.BazelToolchainVars(config, exportedVars)
+}
+
+func ExportStringList(name string, value []string) {
+ exportedVars.ExportStringList(name, value)
+}
func init() {
if runtime.GOOS == "linux" {
commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
}
- exportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags)
- exportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags)
- exportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags)
- exportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags)
- exportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags)
- exportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags)
- exportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags)
+ exportedVars.ExportStringListStaticVariable("CommonGlobalConlyflags", commonGlobalConlyflags)
+ exportedVars.ExportStringListStaticVariable("DeviceGlobalCppflags", deviceGlobalCppflags)
+ exportedVars.ExportStringListStaticVariable("DeviceGlobalLdflags", deviceGlobalLdflags)
+ exportedVars.ExportStringListStaticVariable("DeviceGlobalLldflags", deviceGlobalLldflags)
+ exportedVars.ExportStringListStaticVariable("HostGlobalCppflags", hostGlobalCppflags)
+ exportedVars.ExportStringListStaticVariable("HostGlobalLdflags", hostGlobalLdflags)
+ exportedVars.ExportStringListStaticVariable("HostGlobalLldflags", hostGlobalLldflags)
// Export the static default CommonGlobalCflags to Bazel.
// TODO(187086342): handle cflags that are set in VariableFuncs.
@@ -323,7 +334,7 @@
"-ftrivial-auto-var-init=zero",
"-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang",
}...)
- exportedStringListVars.Set("CommonGlobalCflags", bazelCommonGlobalCflags)
+ exportedVars.ExportStringList("CommonGlobalCflags", bazelCommonGlobalCflags)
pctx.VariableFunc("CommonGlobalCflags", func(ctx android.PackageVarContext) string {
flags := commonGlobalCflags
@@ -352,17 +363,17 @@
// Export the static default DeviceGlobalCflags to Bazel.
// TODO(187086342): handle cflags that are set in VariableFuncs.
- exportedStringListVars.Set("DeviceGlobalCflags", deviceGlobalCflags)
+ exportedVars.ExportStringList("DeviceGlobalCflags", deviceGlobalCflags)
pctx.VariableFunc("DeviceGlobalCflags", func(ctx android.PackageVarContext) string {
return strings.Join(deviceGlobalCflags, " ")
})
- exportStringListStaticVariable("HostGlobalCflags", hostGlobalCflags)
- exportStringListStaticVariable("NoOverrideGlobalCflags", noOverrideGlobalCflags)
- exportStringListStaticVariable("NoOverrideExternalGlobalCflags", noOverrideExternalGlobalCflags)
- exportStringListStaticVariable("CommonGlobalCppflags", commonGlobalCppflags)
- exportStringListStaticVariable("ExternalCflags", extraExternalCflags)
+ exportedVars.ExportStringListStaticVariable("HostGlobalCflags", hostGlobalCflags)
+ exportedVars.ExportStringListStaticVariable("NoOverrideGlobalCflags", noOverrideGlobalCflags)
+ exportedVars.ExportStringListStaticVariable("NoOverrideExternalGlobalCflags", noOverrideExternalGlobalCflags)
+ exportedVars.ExportStringListStaticVariable("CommonGlobalCppflags", commonGlobalCppflags)
+ exportedVars.ExportStringListStaticVariable("ExternalCflags", extraExternalCflags)
// Everything in these lists is a crime against abstraction and dependency tracking.
// Do not add anything to this list.
@@ -377,11 +388,11 @@
"frameworks/native/opengl/include",
"frameworks/av/include",
}
- exportedStringListVars.Set("CommonGlobalIncludes", commonGlobalIncludes)
+ exportedVars.ExportStringList("CommonGlobalIncludes", commonGlobalIncludes)
pctx.PrefixedExistentPathsForSourcesVariable("CommonGlobalIncludes", "-I", commonGlobalIncludes)
- exportStringStaticVariable("CLANG_DEFAULT_VERSION", ClangDefaultVersion)
- exportStringStaticVariable("CLANG_DEFAULT_SHORT_VERSION", ClangDefaultShortVersion)
+ exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_VERSION", ClangDefaultVersion)
+ exportedVars.ExportStringStaticVariable("CLANG_DEFAULT_SHORT_VERSION", ClangDefaultShortVersion)
pctx.StaticVariableWithEnvOverride("ClangBase", "LLVM_PREBUILTS_BASE", ClangDefaultBase)
pctx.StaticVariableWithEnvOverride("ClangVersion", "LLVM_PREBUILTS_VERSION", ClangDefaultVersion)
@@ -421,7 +432,7 @@
pctx.StaticVariableWithEnvOverride("REAbiLinkerExecStrategy", "RBE_ABI_LINKER_EXEC_STRATEGY", remoteexec.LocalExecStrategy)
}
-var HostPrebuiltTag = exportVariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
+var HostPrebuiltTag = exportedVars.ExportVariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
func ClangPath(ctx android.PathContext, file string) android.SourcePath {
type clangToolKey string
diff --git a/cc/config/x86_64_device.go b/cc/config/x86_64_device.go
index 164e7a6..aebda0b 100644
--- a/cc/config/x86_64_device.go
+++ b/cc/config/x86_64_device.go
@@ -91,26 +91,26 @@
pctx.SourcePathVariable("X86_64GccRoot",
"prebuilts/gcc/${HostPrebuiltTag}/x86/x86_64-linux-android-${x86_64GccVersion}")
- exportStringListStaticVariable("X86_64ToolchainCflags", []string{"-m64"})
- exportStringListStaticVariable("X86_64ToolchainLdflags", []string{"-m64"})
+ exportedVars.ExportStringListStaticVariable("X86_64ToolchainCflags", []string{"-m64"})
+ exportedVars.ExportStringListStaticVariable("X86_64ToolchainLdflags", []string{"-m64"})
- exportStringListStaticVariable("X86_64Ldflags", x86_64Ldflags)
- exportStringListStaticVariable("X86_64Lldflags", x86_64Ldflags)
+ exportedVars.ExportStringListStaticVariable("X86_64Ldflags", x86_64Ldflags)
+ exportedVars.ExportStringListStaticVariable("X86_64Lldflags", x86_64Ldflags)
// Clang cflags
- exportStringListStaticVariable("X86_64Cflags", x86_64Cflags)
- exportStringListStaticVariable("X86_64Cppflags", x86_64Cppflags)
+ exportedVars.ExportStringListStaticVariable("X86_64Cflags", x86_64Cflags)
+ exportedVars.ExportStringListStaticVariable("X86_64Cppflags", x86_64Cppflags)
// Yasm flags
- exportStringListStaticVariable("X86_64YasmFlags", []string{
+ exportedVars.ExportStringListStaticVariable("X86_64YasmFlags", []string{
"-f elf64",
"-m amd64",
})
// Extended cflags
- exportedStringListDictVars.Set("X86_64ArchVariantCflags", x86_64ArchVariantCflags)
- exportedStringListDictVars.Set("X86_64ArchFeatureCflags", x86_64ArchFeatureCflags)
+ exportedVars.ExportStringListDict("X86_64ArchVariantCflags", x86_64ArchVariantCflags)
+ exportedVars.ExportStringListDict("X86_64ArchFeatureCflags", x86_64ArchFeatureCflags)
// Architecture variant cflags
for variant, cflags := range x86_64ArchVariantCflags {
diff --git a/cc/config/x86_device.go b/cc/config/x86_device.go
index e32e1bd..421b083 100644
--- a/cc/config/x86_device.go
+++ b/cc/config/x86_device.go
@@ -98,25 +98,25 @@
pctx.SourcePathVariable("X86GccRoot",
"prebuilts/gcc/${HostPrebuiltTag}/x86/x86_64-linux-android-${x86GccVersion}")
- exportStringListStaticVariable("X86ToolchainCflags", []string{"-m32"})
- exportStringListStaticVariable("X86ToolchainLdflags", []string{"-m32"})
+ exportedVars.ExportStringListStaticVariable("X86ToolchainCflags", []string{"-m32"})
+ exportedVars.ExportStringListStaticVariable("X86ToolchainLdflags", []string{"-m32"})
- exportStringListStaticVariable("X86Ldflags", x86Ldflags)
- exportStringListStaticVariable("X86Lldflags", x86Ldflags)
+ exportedVars.ExportStringListStaticVariable("X86Ldflags", x86Ldflags)
+ exportedVars.ExportStringListStaticVariable("X86Lldflags", x86Ldflags)
// Clang cflags
- exportStringListStaticVariable("X86Cflags", x86Cflags)
- exportStringListStaticVariable("X86Cppflags", x86Cppflags)
+ exportedVars.ExportStringListStaticVariable("X86Cflags", x86Cflags)
+ exportedVars.ExportStringListStaticVariable("X86Cppflags", x86Cppflags)
// Yasm flags
- exportStringListStaticVariable("X86YasmFlags", []string{
+ exportedVars.ExportStringListStaticVariable("X86YasmFlags", []string{
"-f elf32",
"-m x86",
})
// Extended cflags
- exportedStringListDictVars.Set("X86ArchVariantCflags", x86ArchVariantCflags)
- exportedStringListDictVars.Set("X86ArchFeatureCflags", x86ArchFeatureCflags)
+ exportedVars.ExportStringListDict("X86ArchVariantCflags", x86ArchVariantCflags)
+ exportedVars.ExportStringListDict("X86ArchFeatureCflags", x86ArchFeatureCflags)
// Architecture variant cflags
for variant, cflags := range x86ArchVariantCflags {
diff --git a/cc/config/x86_linux_host.go b/cc/config/x86_linux_host.go
index e1659d3..4e8fd77 100644
--- a/cc/config/x86_linux_host.go
+++ b/cc/config/x86_linux_host.go
@@ -121,40 +121,40 @@
)
func init() {
- exportStringStaticVariable("LinuxGccVersion", linuxGccVersion)
- exportStringStaticVariable("LinuxGlibcVersion", linuxGlibcVersion)
+ exportedVars.ExportStringStaticVariable("LinuxGccVersion", linuxGccVersion)
+ exportedVars.ExportStringStaticVariable("LinuxGlibcVersion", linuxGlibcVersion)
// Most places use the full GCC version. A few only use up to the first two numbers.
if p := strings.Split(linuxGccVersion, "."); len(p) > 2 {
- exportStringStaticVariable("ShortLinuxGccVersion", strings.Join(p[:2], "."))
+ exportedVars.ExportStringStaticVariable("ShortLinuxGccVersion", strings.Join(p[:2], "."))
} else {
- exportStringStaticVariable("ShortLinuxGccVersion", linuxGccVersion)
+ exportedVars.ExportStringStaticVariable("ShortLinuxGccVersion", linuxGccVersion)
}
- exportSourcePathVariable("LinuxGccRoot",
+ exportedVars.ExportSourcePathVariable("LinuxGccRoot",
"prebuilts/gcc/linux-x86/host/x86_64-linux-glibc${LinuxGlibcVersion}-${ShortLinuxGccVersion}")
- exportStringListStaticVariable("LinuxGccTriple", []string{"x86_64-linux"})
+ exportedVars.ExportStringListStaticVariable("LinuxGccTriple", []string{"x86_64-linux"})
- exportStringListStaticVariable("LinuxCflags", linuxCflags)
- exportStringListStaticVariable("LinuxLdflags", linuxLdflags)
- exportStringListStaticVariable("LinuxLldflags", linuxLdflags)
- exportStringListStaticVariable("LinuxGlibcCflags", linuxGlibcCflags)
- exportStringListStaticVariable("LinuxGlibcLdflags", linuxGlibcLdflags)
- exportStringListStaticVariable("LinuxGlibcLldflags", linuxGlibcLdflags)
- exportStringListStaticVariable("LinuxMuslCflags", linuxMuslCflags)
- exportStringListStaticVariable("LinuxMuslLdflags", linuxMuslLdflags)
- exportStringListStaticVariable("LinuxMuslLldflags", linuxMuslLdflags)
+ exportedVars.ExportStringListStaticVariable("LinuxCflags", linuxCflags)
+ exportedVars.ExportStringListStaticVariable("LinuxLdflags", linuxLdflags)
+ exportedVars.ExportStringListStaticVariable("LinuxLldflags", linuxLdflags)
+ exportedVars.ExportStringListStaticVariable("LinuxGlibcCflags", linuxGlibcCflags)
+ exportedVars.ExportStringListStaticVariable("LinuxGlibcLdflags", linuxGlibcLdflags)
+ exportedVars.ExportStringListStaticVariable("LinuxGlibcLldflags", linuxGlibcLdflags)
+ exportedVars.ExportStringListStaticVariable("LinuxMuslCflags", linuxMuslCflags)
+ exportedVars.ExportStringListStaticVariable("LinuxMuslLdflags", linuxMuslLdflags)
+ exportedVars.ExportStringListStaticVariable("LinuxMuslLldflags", linuxMuslLdflags)
- exportStringListStaticVariable("LinuxX86Cflags", linuxX86Cflags)
- exportStringListStaticVariable("LinuxX8664Cflags", linuxX8664Cflags)
- exportStringListStaticVariable("LinuxX86Ldflags", linuxX86Ldflags)
- exportStringListStaticVariable("LinuxX86Lldflags", linuxX86Ldflags)
- exportStringListStaticVariable("LinuxX8664Ldflags", linuxX8664Ldflags)
- exportStringListStaticVariable("LinuxX8664Lldflags", linuxX8664Ldflags)
+ exportedVars.ExportStringListStaticVariable("LinuxX86Cflags", linuxX86Cflags)
+ exportedVars.ExportStringListStaticVariable("LinuxX8664Cflags", linuxX8664Cflags)
+ exportedVars.ExportStringListStaticVariable("LinuxX86Ldflags", linuxX86Ldflags)
+ exportedVars.ExportStringListStaticVariable("LinuxX86Lldflags", linuxX86Ldflags)
+ exportedVars.ExportStringListStaticVariable("LinuxX8664Ldflags", linuxX8664Ldflags)
+ exportedVars.ExportStringListStaticVariable("LinuxX8664Lldflags", linuxX8664Ldflags)
// Yasm flags
- exportStringListStaticVariable("LinuxX86YasmFlags", []string{"-f elf32 -m x86"})
- exportStringListStaticVariable("LinuxX8664YasmFlags", []string{"-f elf64 -m amd64"})
+ exportedVars.ExportStringListStaticVariable("LinuxX86YasmFlags", []string{"-f elf32 -m x86"})
+ exportedVars.ExportStringListStaticVariable("LinuxX8664YasmFlags", []string{"-f elf64 -m amd64"})
}
type toolchainLinux struct {