Support passing input variables to the product configuration

Since rblf_env / rblf_cli are not typed properly, accept
input variables via a file so that they can be converted
with the correct types.

Bug: 201700692
Test: go test
Change-Id: I9b56067cfe396d1bcd8d62c353ff222dd61a6c9f
This commit is contained in:
Cole Faust 2021-12-01 13:43:17 -08:00
parent cd4335ae45
commit 864028a71b
6 changed files with 12 additions and 199 deletions

View file

@ -38,7 +38,6 @@ bootstrap_go_package {
"soong_variables.go",
"types.go",
"variable.go",
"version_defaults.go",
],
deps: ["androidmk-parser"],
}

View file

@ -80,7 +80,6 @@ var backupSuffix string
var tracedVariables []string
var errorLogger = errorSink{data: make(map[string]datum)}
var makefileFinder = &LinuxMakefileFinder{}
var versionDefaultsMk = filepath.Join("build", "make", "core", "version_defaults.mk")
func main() {
flag.Usage = func() {
@ -168,18 +167,14 @@ func main() {
if len(files) != 1 {
quit(fmt.Errorf("a launcher can be generated only for a single product"))
}
versionDefaults, err := generateVersionDefaults()
if err != nil {
quit(err)
if *inputVariables == "" {
quit(fmt.Errorf("the product launcher requires an input variables file"))
}
versionDefaultsPath := outputFilePath(versionDefaultsMk)
err = writeGenerated(versionDefaultsPath, versionDefaults)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s", files[0], err)
ok = false
if !convertOne(*inputVariables) {
quit(fmt.Errorf("the product launcher input variables file failed to convert"))
}
err = writeGenerated(*launcher, mk2rbc.Launcher(outputFilePath(files[0]), versionDefaultsPath,
err := writeGenerated(*launcher, mk2rbc.Launcher(outputFilePath(files[0]), outputFilePath(*inputVariables),
mk2rbc.MakePath2ModuleName(files[0])))
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s", files[0], err)
@ -213,15 +208,6 @@ func main() {
}
}
func generateVersionDefaults() (string, error) {
versionSettings, err := mk2rbc.ParseVersionDefaults(filepath.Join(*rootDir, versionDefaultsMk))
if err != nil {
return "", err
}
return mk2rbc.VersionDefaults(versionSettings), nil
}
func quit(s interface{}) {
fmt.Fprintln(os.Stderr, s)
os.Exit(2)

View file

@ -1695,12 +1695,12 @@ func Convert(req Request) (*StarlarkScript, error) {
return starScript, nil
}
func Launcher(mainModuleUri, versionDefaultsUri, mainModuleName string) string {
func Launcher(mainModuleUri, inputVariablesUri, mainModuleName string) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "load(%q, %q)\n", baseUri, baseName)
fmt.Fprintf(&buf, "load(%q, \"version_defaults\")\n", versionDefaultsUri)
fmt.Fprintf(&buf, "load(%q, input_variables_init = \"init\")\n", inputVariablesUri)
fmt.Fprintf(&buf, "load(%q, \"init\")\n", mainModuleUri)
fmt.Fprintf(&buf, "%s(%s(%q, init, version_defaults))\n", cfnPrintVars, cfnMain, mainModuleName)
fmt.Fprintf(&buf, "%s(%s(%q, init, input_variables_init))\n", cfnPrintVars, cfnMain, mainModuleName)
return buf.String()
}

View file

@ -1,113 +0,0 @@
// Copyright 2021 Google LLC
//
// 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 mk2rbc
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
mkparser "android/soong/androidmk/parser"
)
const codenamePrefix = "PLATFORM_VERSION_CODENAME."
// ParseVersionDefaults extracts version settings from the given file
// and returns the map.
func ParseVersionDefaults(path string) (map[string]string, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
parser := mkparser.NewParser(path, bytes.NewBuffer(contents))
nodes, errs := parser.Parse()
if len(errs) > 0 {
for _, e := range errs {
fmt.Fprintln(os.Stderr, "ERROR:", e)
}
return nil, fmt.Errorf("cannot parse %s", path)
}
result := map[string]string{
"DEFAULT_PLATFORM_VERSION": "",
"MAX_PLATFORM_VERSION": "",
"MIN_PLATFORM_VERSION": "A",
"PLATFORM_BASE_SDK_EXTENSION_VERSION": "",
"PLATFORM_SDK_EXTENSION_VERSION": "",
"PLATFORM_SDK_VERSION": "",
"PLATFORM_SECURITY_PATCH": "",
"PLATFORM_VERSION_LAST_STABLE": "",
}
for _, node := range nodes {
asgn, ok := node.(*mkparser.Assignment)
if !(ok && asgn.Name.Const()) {
continue
}
s := asgn.Name.Strings[0]
_, ok = result[s]
if !ok {
ok = strings.HasPrefix(s, codenamePrefix)
}
if !ok {
continue
}
v := asgn.Value
if !v.Const() {
return nil, fmt.Errorf("the value of %s should be constant", s)
}
result[s] = strings.TrimSpace(v.Strings[0])
}
return result, nil
}
func genericValue(s string) interface{} {
if ival, err := strconv.ParseInt(s, 0, 0); err == nil {
return ival
}
return s
}
// VersionDefaults generates the contents of the version_defaults.rbc file
func VersionDefaults(values map[string]string) string {
var sink bytes.Buffer
var lines []string
var codenames []string
for name, value := range values {
if strings.HasPrefix(name, codenamePrefix) {
codenames = append(codenames,
fmt.Sprintf("%q: %q", strings.TrimPrefix(name, codenamePrefix), value))
} else {
// Print numbers as such
lines = append(lines, fmt.Sprintf(" %s = %#v,\n",
strings.ToLower(name), genericValue(value)))
}
}
sort.Strings(lines)
sort.Strings(codenames)
sink.WriteString("version_defaults = struct(\n")
for _, l := range lines {
sink.WriteString(l)
}
sink.WriteString(" codenames = { ")
sink.WriteString(strings.Join(codenames, ", "))
sink.WriteString(" }\n)\n")
return sink.String()
}

View file

@ -1,60 +0,0 @@
package mk2rbc
import (
"path/filepath"
"reflect"
"strings"
"testing"
)
func TestParseVersionDefaults(t *testing.T) {
testDir := getTestDirectory()
abspath := func(relPath string) string { return filepath.Join(testDir, relPath) }
actualProducts, err := ParseVersionDefaults(abspath("version_defaults.mk.test"))
if err != nil {
t.Fatal(err)
}
expectedProducts := map[string]string{
"DEFAULT_PLATFORM_VERSION": "TP1A",
"MAX_PLATFORM_VERSION": "TP1A",
"MIN_PLATFORM_VERSION": "TP1A",
"PLATFORM_BASE_SDK_EXTENSION_VERSION": "0",
"PLATFORM_SDK_EXTENSION_VERSION": "1",
"PLATFORM_SDK_VERSION": "31",
"PLATFORM_SECURITY_PATCH": "2021-10-05",
"PLATFORM_VERSION_LAST_STABLE": "12",
"PLATFORM_VERSION_CODENAME.SP2A": "Sv2",
"PLATFORM_VERSION_CODENAME.TP1A": "Tiramisu",
}
if !reflect.DeepEqual(actualProducts, expectedProducts) {
t.Errorf("\nExpected: %v\n Actual: %v", expectedProducts, actualProducts)
}
}
func TestVersionDefaults(t *testing.T) {
testDir := getTestDirectory()
abspath := func(relPath string) string { return filepath.Join(testDir, relPath) }
actualProducts, err := ParseVersionDefaults(abspath("version_defaults.mk.test"))
if err != nil {
t.Fatal(err)
}
expectedString := `version_defaults = struct(
default_platform_version = "TP1A",
max_platform_version = "TP1A",
min_platform_version = "TP1A",
platform_base_sdk_extension_version = 0,
platform_sdk_extension_version = 1,
platform_sdk_version = 31,
platform_security_patch = "2021-10-05",
platform_version_last_stable = 12,
codenames = { "SP2A": "Sv2", "TP1A": "Tiramisu" }
)
`
actualString := VersionDefaults(actualProducts)
if !reflect.DeepEqual(actualString, expectedString) {
t.Errorf("\nExpected: %v\nActual:\n%v",
strings.ReplaceAll(expectedString, "\n", "␤\n"),
strings.ReplaceAll(actualString, "\n", "␤\n"))
}
}

View file

@ -2,7 +2,7 @@
# Convert and run one configuration
# Args: a product/board makefile optionally followed by additional arguments
# that will be passed to rbcrun.
[[ $# -gt 0 && -f "$1" ]] || { echo "Usage: ${0##*/} product.mk [Additional rbcrun arguments]" >&2; exit 1; }
[[ $# -gt 1 && -f "$1" && -f "$2" ]] || { echo "Usage: ${0##*/} product.mk input_variables.mk [Additional rbcrun arguments]" >&2; exit 1; }
set -eu
declare -r output_root="${OUT_DIR:-out}"
@ -10,7 +10,8 @@ declare -r runner="${output_root}/soong/rbcrun"
declare -r converter="${output_root}/soong/mk2rbc"
declare -r launcher="${output_root}/rbc/launcher.rbc"
declare -r makefile="$1"
shift
"${converter}" -mode=write -r --outdir "${output_root}/rbc" --launcher="${launcher}" "${makefile}"
declare -r input_variables="$2"
shift 2
"${converter}" -mode=write -r --outdir "${output_root}/rbc" --input_variables "${input_variables}" --launcher="${launcher}" "${makefile}"
"${runner}" RBC_OUT="make,global" RBC_DEBUG="${RBC_DEBUG:-}" $@ "${launcher}"