1# SPDX-License-Identifier: GPL-2.0 2# 3# Builds a .config from a kunitconfig. 4# 5# Copyright (C) 2019, Google LLC. 6# Author: Felix Guo <felixguoxiuping@gmail.com> 7# Author: Brendan Higgins <brendanhiggins@google.com> 8 9import collections 10import re 11from typing import List, Set 12 13CONFIG_IS_NOT_SET_PATTERN = r'^# CONFIG_(\w+) is not set$' 14CONFIG_PATTERN = r'^CONFIG_(\w+)=(\S+|".*")$' 15 16KconfigEntryBase = collections.namedtuple('KconfigEntryBase', ['name', 'value']) 17 18class KconfigEntry(KconfigEntryBase): 19 20 def __str__(self) -> str: 21 if self.value == 'n': 22 return r'# CONFIG_%s is not set' % (self.name) 23 else: 24 return r'CONFIG_%s=%s' % (self.name, self.value) 25 26 27class KconfigParseError(Exception): 28 """Error parsing Kconfig defconfig or .config.""" 29 30 31class Kconfig(object): 32 """Represents defconfig or .config specified using the Kconfig language.""" 33 34 def __init__(self) -> None: 35 self._entries = [] # type: List[KconfigEntry] 36 37 def entries(self) -> Set[KconfigEntry]: 38 return set(self._entries) 39 40 def add_entry(self, entry: KconfigEntry) -> None: 41 self._entries.append(entry) 42 43 def is_subset_of(self, other: 'Kconfig') -> bool: 44 other_dict = {e.name: e.value for e in other.entries()} 45 for a in self.entries(): 46 b = other_dict.get(a.name) 47 if b is None: 48 if a.value == 'n': 49 continue 50 return False 51 elif a.value != b: 52 return False 53 return True 54 55 def merge_in_entries(self, other: 'Kconfig') -> None: 56 if other.is_subset_of(self): 57 return 58 self._entries = list(self.entries().union(other.entries())) 59 60 def write_to_file(self, path: str) -> None: 61 with open(path, 'a+') as f: 62 for entry in self.entries(): 63 f.write(str(entry) + '\n') 64 65 def parse_from_string(self, blob: str) -> None: 66 """Parses a string containing KconfigEntrys and populates this Kconfig.""" 67 self._entries = [] 68 is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN) 69 config_matcher = re.compile(CONFIG_PATTERN) 70 for line in blob.split('\n'): 71 line = line.strip() 72 if not line: 73 continue 74 75 match = config_matcher.match(line) 76 if match: 77 entry = KconfigEntry(match.group(1), match.group(2)) 78 self.add_entry(entry) 79 continue 80 81 empty_match = is_not_set_matcher.match(line) 82 if empty_match: 83 entry = KconfigEntry(empty_match.group(1), 'n') 84 self.add_entry(entry) 85 continue 86 87 if line[0] == '#': 88 continue 89 else: 90 raise KconfigParseError('Failed to parse: ' + line) 91 92 def read_from_file(self, path: str) -> None: 93 with open(path, 'r') as f: 94 self.parse_from_string(f.read()) 95