sdkconfig.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #
  2. # Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. from pyparsing import Word, printables, Combine, Literal, hexnums, quotedString, Optional, nums, removeQuotes, oneOf, Group, infixNotation, opAssoc
  18. import sys
  19. try:
  20. import kconfiglib
  21. except ImportError:
  22. parent_dir_name = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  23. kconfig_new_dir = os.path.abspath(parent_dir_name + "/kconfig_new")
  24. sys.path.append(kconfig_new_dir)
  25. import kconfiglib
  26. class SDKConfig:
  27. """
  28. Encapsulates an sdkconfig file. Defines grammar of a configuration entry, and enables
  29. evaluation of logical expressions involving those entries.
  30. """
  31. # A configuration entry is in the form CONFIG=VALUE. Definitions of components of that grammar
  32. IDENTIFIER = Word(printables.upper())
  33. HEX = Combine("0x" + Word(hexnums)).setParseAction(lambda t:int(t[0], 16))
  34. DECIMAL = Combine(Optional(Literal("+") | Literal("-")) + Word(nums)).setParseAction(lambda t:int(t[0]))
  35. LITERAL = Word(printables)
  36. QUOTED_LITERAL = quotedString.setParseAction(removeQuotes)
  37. VALUE = HEX | DECIMAL | LITERAL | QUOTED_LITERAL
  38. # Operators supported by the expression evaluation
  39. OPERATOR = oneOf(["=", "!=", ">", "<", "<=", ">="])
  40. def __init__(self, kconfig_file, sdkconfig_file, env=[]):
  41. env = [(name, value) for (name,value) in (e.split("=",1) for e in env)]
  42. for name, value in env:
  43. value = " ".join(value.split())
  44. os.environ[name] = value
  45. self.config = kconfiglib.Kconfig(kconfig_file.name)
  46. self.config.load_config(sdkconfig_file.name)
  47. def evaluate_expression(self, expression):
  48. result = self.config.eval_string(expression)
  49. if result == 0: # n
  50. return False
  51. elif result == 2: # y
  52. return True
  53. else: # m
  54. raise Exception("Unsupported config expression result.")
  55. @staticmethod
  56. def get_expression_grammar():
  57. identifier = SDKConfig.IDENTIFIER.setResultsName("identifier")
  58. operator = SDKConfig.OPERATOR.setResultsName("operator")
  59. value = SDKConfig.VALUE.setResultsName("value")
  60. test_binary = identifier + operator + value
  61. test_single = identifier
  62. test = test_binary | test_single
  63. condition = Group(Optional("(").suppress() + test + Optional(")").suppress())
  64. grammar = infixNotation(condition, [
  65. ("!", 1, opAssoc.RIGHT),
  66. ("&&", 2, opAssoc.LEFT),
  67. ("||", 2, opAssoc.LEFT)])
  68. return grammar