Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package soongconfig |
| 16 | |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | ) |
Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 21 | |
| 22 | type 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 | |
| 36 | func Config(vars map[string]string) SoongConfig { |
Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 37 | 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 Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | type soongConfig map[string]string |
| 50 | |
| 51 | func (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 | |
| 56 | func (c soongConfig) String(name string) string { |
| 57 | return c[name] |
| 58 | } |
| 59 | |
| 60 | func (c soongConfig) IsSet(name string) bool { |
| 61 | _, ok := c[name] |
| 62 | return ok |
| 63 | } |