blob: c72da2f849688496dc517378187fbe20dd81e0de [file] [log] [blame]
Colin Cross9d34f352019-11-22 16:03:51 -08001// Copyright 2020 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 soongconfig
16
Liz Kammer432bd592020-12-16 12:42:02 -080017import (
18 "fmt"
19 "strings"
20)
Colin Cross9d34f352019-11-22 16:03:51 -080021
22type SoongConfig interface {
23 // Bool interprets the variable named `name` as a boolean, returning true if, after
24 // lowercasing, it matches one of "1", "y", "yes", "on", or "true". Unset, or any other
25 // value will return false.
26 Bool(name string) bool
27
28 // String returns the string value of `name`. If the variable was not set, it will
29 // return the empty string.
30 String(name string) string
31
32 // IsSet returns whether the variable `name` was set by Make.
33 IsSet(name string) bool
34}
35
36func Config(vars map[string]string) SoongConfig {
Liz Kammer432bd592020-12-16 12:42:02 -080037 configVars := make(map[string]string)
38 if len(vars) > 0 {
39 for k, v := range vars {
40 configVars[k] = v
41 }
42 if _, exists := configVars[conditionsDefault]; exists {
43 panic(fmt.Sprintf("%q is a reserved soong config variable name", conditionsDefault))
44 }
45 }
46 return soongConfig(configVars)
Colin Cross9d34f352019-11-22 16:03:51 -080047}
48
49type soongConfig map[string]string
50
51func (c soongConfig) Bool(name string) bool {
52 v := strings.ToLower(c[name])
53 return v == "1" || v == "y" || v == "yes" || v == "on" || v == "true"
54}
55
56func (c soongConfig) String(name string) string {
57 return c[name]
58}
59
60func (c soongConfig) IsSet(name string) bool {
61 _, ok := c[name]
62 return ok
63}