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 9from dataclasses import dataclass 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 16@dataclass(frozen=True) 17class KconfigEntry: 18 name: str 19 value: str 20 21 def __str__(self) -> str: 22 if self.value == 'n': 23 return r'# CONFIG_%s is not set' % (self.name) 24 else: 25 return r'CONFIG_%s=%s' % (self.name, self.value) 26 27 28class KconfigParseError(Exception): 29 """Error parsing Kconfig defconfig or .config.""" 30 31 32class Kconfig(object): 33 """Represents defconfig or .config specified using the Kconfig language.""" 34 35 def __init__(self) -> None: 36 self._entries = [] # type: List[KconfigEntry] 37 38 def entries(self) -> Set[KconfigEntry]: 39 return set(self._entries) 40 41 def add_entry(self, entry: KconfigEntry) -> None: 42 self._entries.append(entry) 43 44 def is_subset_of(self, other: 'Kconfig') -> bool: 45 other_dict = {e.name: e.value for e in other.entries()} 46 for a in self.entries(): 47 b = other_dict.get(a.name) 48 if b is None: 49 if a.value == 'n': 50 continue 51 return False 52 elif a.value != b: 53 return False 54 return True 55 56 def merge_in_entries(self, other: 'Kconfig') -> None: 57 if other.is_subset_of(self): 58 return 59 self._entries = list(self.entries().union(other.entries())) 60 61 def write_to_file(self, path: str) -> None: 62 with open(path, 'a+') as f: 63 for entry in self.entries(): 64 f.write(str(entry) + '\n') 65 66def parse_file(path: str) -> Kconfig: 67 with open(path, 'r') as f: 68 return parse_from_string(f.read()) 69 70def parse_from_string(blob: str) -> Kconfig: 71 """Parses a string containing Kconfig entries.""" 72 kconfig = Kconfig() 73 is_not_set_matcher = re.compile(CONFIG_IS_NOT_SET_PATTERN) 74 config_matcher = re.compile(CONFIG_PATTERN) 75 for line in blob.split('\n'): 76 line = line.strip() 77 if not line: 78 continue 79 80 match = config_matcher.match(line) 81 if match: 82 entry = KconfigEntry(match.group(1), match.group(2)) 83 kconfig.add_entry(entry) 84 continue 85 86 empty_match = is_not_set_matcher.match(line) 87 if empty_match: 88 entry = KconfigEntry(empty_match.group(1), 'n') 89 kconfig.add_entry(entry) 90 continue 91 92 if line[0] == '#': 93 continue 94 else: 95 raise KconfigParseError('Failed to parse: ' + line) 96 return kconfig 97