2018-04-18 02:57:45 +00:00
|
|
|
#
|
2021-01-27 08:03:24 +00:00
|
|
|
# Copyright 2021 Espressif Systems (Shanghai) CO LTD
|
2018-04-18 02:57:45 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2020-09-21 09:37:23 +00:00
|
|
|
import kconfiglib
|
2021-01-26 02:49:01 +00:00
|
|
|
from pyparsing import (Combine, Group, Literal, Optional, Word, alphanums, hexnums, infixNotation, nums, oneOf,
|
|
|
|
opAssoc, printables, quotedString, removeQuotes)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SDKConfig:
|
2018-12-04 12:46:48 +00:00
|
|
|
"""
|
2021-03-05 09:49:10 +00:00
|
|
|
Evaluates conditional expressions based on the build's sdkconfig and Kconfig files.
|
|
|
|
This also defines the grammar of conditional expressions.
|
2018-12-04 12:46:48 +00:00
|
|
|
"""
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
# A configuration entry is in the form CONFIG=VALUE. Definitions of components of that grammar
|
2021-01-26 02:49:01 +00:00
|
|
|
IDENTIFIER = Word(alphanums.upper() + '_')
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
HEX = Combine('0x' + Word(hexnums)).setParseAction(lambda t:int(t[0], 16))
|
|
|
|
DECIMAL = Combine(Optional(Literal('+') | Literal('-')) + Word(nums)).setParseAction(lambda t:int(t[0]))
|
|
|
|
LITERAL = Word(printables.replace(':', ''))
|
2018-04-18 02:57:45 +00:00
|
|
|
QUOTED_LITERAL = quotedString.setParseAction(removeQuotes)
|
|
|
|
|
|
|
|
VALUE = HEX | DECIMAL | LITERAL | QUOTED_LITERAL
|
|
|
|
|
|
|
|
# Operators supported by the expression evaluation
|
2021-01-26 02:49:01 +00:00
|
|
|
OPERATOR = oneOf(['=', '!=', '>', '<', '<=', '>='])
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2019-06-27 10:30:42 +00:00
|
|
|
def __init__(self, kconfig_file, sdkconfig_file):
|
2019-03-19 06:35:47 +00:00
|
|
|
self.config = kconfiglib.Kconfig(kconfig_file)
|
|
|
|
self.config.load_config(sdkconfig_file)
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
def evaluate_expression(self, expression):
|
|
|
|
result = self.config.eval_string(expression)
|
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
if result == 0: # n
|
2018-04-18 02:57:45 +00:00
|
|
|
return False
|
2018-12-04 12:46:48 +00:00
|
|
|
elif result == 2: # y
|
2018-04-18 02:57:45 +00:00
|
|
|
return True
|
2018-12-04 12:46:48 +00:00
|
|
|
else: # m
|
2021-01-26 02:49:01 +00:00
|
|
|
raise Exception('unsupported config expression result')
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_expression_grammar():
|
2021-01-26 02:49:01 +00:00
|
|
|
identifier = SDKConfig.IDENTIFIER.setResultsName('identifier')
|
|
|
|
operator = SDKConfig.OPERATOR.setResultsName('operator')
|
|
|
|
value = SDKConfig.VALUE.setResultsName('value')
|
2018-04-18 02:57:45 +00:00
|
|
|
|
|
|
|
test_binary = identifier + operator + value
|
|
|
|
test_single = identifier
|
|
|
|
|
|
|
|
test = test_binary | test_single
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
condition = Group(Optional('(').suppress() + test + Optional(')').suppress())
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
grammar = infixNotation(condition, [
|
2021-01-26 02:49:01 +00:00
|
|
|
('!', 1, opAssoc.RIGHT),
|
|
|
|
('&&', 2, opAssoc.LEFT),
|
|
|
|
('||', 2, opAssoc.LEFT)])
|
2018-04-18 02:57:45 +00:00
|
|
|
|
2018-11-29 00:46:54 +00:00
|
|
|
return grammar
|