• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 Google LLC
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 mk2rbc
16
17import (
18	"bytes"
19	"fmt"
20	"io/ioutil"
21	"os"
22	"strings"
23
24	mkparser "android/soong/androidmk/parser"
25)
26
27// Extracts the list of product config variables from a file, calling
28// given registrar for each variable.
29func FindConfigVariables(mkFile string, vr variableRegistrar) error {
30	mkContents, err := ioutil.ReadFile(mkFile)
31	if err != nil {
32		return err
33	}
34	parser := mkparser.NewParser(mkFile, bytes.NewBuffer(mkContents))
35	nodes, errs := parser.Parse()
36	if len(errs) > 0 {
37		for _, e := range errs {
38			fmt.Fprintln(os.Stderr, "ERROR:", e)
39		}
40		return fmt.Errorf("cannot parse %s", mkFile)
41	}
42	for _, node := range nodes {
43		asgn, ok := node.(*mkparser.Assignment)
44		if !ok {
45			continue
46		}
47		// We are looking for a variable called '_product_list_vars'
48		// or '_product_single_value_vars'.
49		if !asgn.Name.Const() {
50			continue
51		}
52		varName := asgn.Name.Strings[0]
53		var starType starlarkType
54		if varName == "_product_list_vars" {
55			starType = starlarkTypeList
56		} else if varName == "_product_single_value_vars" {
57			starType = starlarkTypeUnknown
58		} else {
59			continue
60		}
61		for _, name := range strings.Fields(asgn.Value.Dump()) {
62			vr.NewVariable(name, VarClassConfig, starType)
63		}
64
65	}
66	return nil
67}
68