sdkconfig.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, alphanums, printables, Combine, Literal, hexnums, quotedString, Optional, nums, removeQuotes, oneOf, Group, infixNotation, opAssoc
  18. import sys
  19. try:
  20. from . import kconfiglib
  21. except Exception:
  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.insert(0, 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(alphanums.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.replace(":", ""))
  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):
  41. self.config = kconfiglib.Kconfig(kconfig_file)
  42. self.config.load_config(sdkconfig_file)
  43. def evaluate_expression(self, expression):
  44. result = self.config.eval_string(expression)
  45. if result == 0: # n
  46. return False
  47. elif result == 2: # y
  48. return True
  49. else: # m
  50. raise Exception("unsupported config expression result")
  51. @staticmethod
  52. def get_expression_grammar():
  53. identifier = SDKConfig.IDENTIFIER.setResultsName("identifier")
  54. operator = SDKConfig.OPERATOR.setResultsName("operator")
  55. value = SDKConfig.VALUE.setResultsName("value")
  56. test_binary = identifier + operator + value
  57. test_single = identifier
  58. test = test_binary | test_single
  59. condition = Group(Optional("(").suppress() + test + Optional(")").suppress())
  60. grammar = infixNotation(condition, [
  61. ("!", 1, opAssoc.RIGHT),
  62. ("&&", 2, opAssoc.LEFT),
  63. ("||", 2, opAssoc.LEFT)])
  64. return grammar