config.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. package soongconfig
  15. import (
  16. "fmt"
  17. "strings"
  18. )
  19. type SoongConfig interface {
  20. // Bool interprets the variable named `name` as a boolean, returning true if, after
  21. // lowercasing, it matches one of "1", "y", "yes", "on", or "true". Unset, or any other
  22. // value will return false.
  23. Bool(name string) bool
  24. // String returns the string value of `name`. If the variable was not set, it will
  25. // return the empty string.
  26. String(name string) string
  27. // IsSet returns whether the variable `name` was set by Make.
  28. IsSet(name string) bool
  29. }
  30. func Config(vars map[string]string) SoongConfig {
  31. configVars := make(map[string]string)
  32. if len(vars) > 0 {
  33. for k, v := range vars {
  34. configVars[k] = v
  35. }
  36. if _, exists := configVars[conditionsDefault]; exists {
  37. panic(fmt.Sprintf("%q is a reserved soong config variable name", conditionsDefault))
  38. }
  39. }
  40. return soongConfig(configVars)
  41. }
  42. type soongConfig map[string]string
  43. func (c soongConfig) Bool(name string) bool {
  44. v := strings.ToLower(c[name])
  45. return v == "1" || v == "y" || v == "yes" || v == "on" || v == "true"
  46. }
  47. func (c soongConfig) String(name string) string {
  48. return c[name]
  49. }
  50. func (c soongConfig) IsSet(name string) bool {
  51. _, ok := c[name]
  52. return ok
  53. }