|
| 1 | +# Copyright (c) 2018 Open Source Foundries Limited. |
| 2 | +# Copyright (c) 2018 Bobby Noelte. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +# CMakeCacheEntry and CMakeCache are taken from scripts/zephyr_run.py. |
| 7 | +# |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import re |
| 12 | +from collections import OrderedDict |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +class CMakeCacheEntry: |
| 17 | + '''Represents a CMake cache entry. |
| 18 | + This class understands the type system in a CMakeCache.txt, and |
| 19 | + converts the following cache types to Python types: |
| 20 | + Cache Type Python type |
| 21 | + ---------- ------------------------------------------- |
| 22 | + FILEPATH str |
| 23 | + PATH str |
| 24 | + STRING str OR list of str (if ';' is in the value) |
| 25 | + BOOL bool |
| 26 | + INTERNAL str OR list of str (if ';' is in the value) |
| 27 | + ---------- ------------------------------------------- |
| 28 | + ''' |
| 29 | + |
| 30 | + # Regular expression for a cache entry. |
| 31 | + # |
| 32 | + # CMake variable names can include escape characters, allowing a |
| 33 | + # wider set of names than is easy to match with a regular |
| 34 | + # expression. To be permissive here, use a non-greedy match up to |
| 35 | + # the first colon (':'). This breaks if the variable name has a |
| 36 | + # colon inside, but it's good enough. |
| 37 | + CACHE_ENTRY = re.compile( |
| 38 | + r'''(?P<name>.*?) # name |
| 39 | + :(?P<type>FILEPATH|PATH|STRING|BOOL|INTERNAL) # type |
| 40 | + =(?P<value>.*) # value |
| 41 | + ''', re.X) |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def _to_bool(cls, val): |
| 45 | + # Convert a CMake BOOL string into a Python bool. |
| 46 | + # |
| 47 | + # "True if the constant is 1, ON, YES, TRUE, Y, or a |
| 48 | + # non-zero number. False if the constant is 0, OFF, NO, |
| 49 | + # FALSE, N, IGNORE, NOTFOUND, the empty string, or ends in |
| 50 | + # the suffix -NOTFOUND. Named boolean constants are |
| 51 | + # case-insensitive. If the argument is not one of these |
| 52 | + # constants, it is treated as a variable." |
| 53 | + # |
| 54 | + # https://cmake.org/cmake/help/v3.0/command/if.html |
| 55 | + val = val.upper() |
| 56 | + if val in ('ON', 'YES', 'TRUE', 'Y'): |
| 57 | + return True |
| 58 | + elif val in ('OFF', 'NO', 'FALSE', 'N', 'IGNORE', 'NOTFOUND', ''): |
| 59 | + return False |
| 60 | + elif val.endswith('-NOTFOUND'): |
| 61 | + return False |
| 62 | + else: |
| 63 | + try: |
| 64 | + v = int(val) |
| 65 | + return v != 0 |
| 66 | + except ValueError as exc: |
| 67 | + raise ValueError('invalid bool {}'.format(val)) from exc |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def from_line(cls, line, line_no): |
| 71 | + # Comments can only occur at the beginning of a line. |
| 72 | + # (The value of an entry could contain a comment character). |
| 73 | + if line.startswith('//') or line.startswith('#'): |
| 74 | + return None |
| 75 | + |
| 76 | + # Whitespace-only lines do not contain cache entries. |
| 77 | + if not line.strip(): |
| 78 | + return None |
| 79 | + |
| 80 | + m = cls.CACHE_ENTRY.match(line) |
| 81 | + if not m: |
| 82 | + return None |
| 83 | + |
| 84 | + name, type_, value = (m.group(g) for g in ('name', 'type', 'value')) |
| 85 | + if type_ == 'BOOL': |
| 86 | + try: |
| 87 | + value = cls._to_bool(value) |
| 88 | + except ValueError as exc: |
| 89 | + args = exc.args + ('on line {}: {}'.format(line_no, line),) |
| 90 | + raise ValueError(args) from exc |
| 91 | + elif type_ == 'STRING' or type_ == 'INTERNAL': |
| 92 | + # If the value is a CMake list (i.e. is a string which |
| 93 | + # contains a ';'), convert to a Python list. |
| 94 | + if ';' in value: |
| 95 | + value = value.split(';') |
| 96 | + |
| 97 | + return CMakeCacheEntry(name, value) |
| 98 | + |
| 99 | + def __init__(self, name, value): |
| 100 | + self.name = name |
| 101 | + self.value = value |
| 102 | + |
| 103 | + def __str__(self): |
| 104 | + fmt = 'CMakeCacheEntry(name={}, value={})' |
| 105 | + return fmt.format(self.name, self.value) |
| 106 | + |
| 107 | + |
| 108 | +class CMakeCache: |
| 109 | + '''Parses and represents a CMake cache file.''' |
| 110 | + |
| 111 | + def __init__(self, cache_file): |
| 112 | + self.load(cache_file) |
| 113 | + |
| 114 | + def load(self, cache_file): |
| 115 | + entries = [] |
| 116 | + with open(str(cache_file), 'r') as cache: |
| 117 | + for line_no, line in enumerate(cache): |
| 118 | + entry = CMakeCacheEntry.from_line(line, line_no) |
| 119 | + if entry: |
| 120 | + entries.append(entry) |
| 121 | + self._entries = OrderedDict((e.name, e) for e in entries) |
| 122 | + |
| 123 | + def get(self, name, default=None): |
| 124 | + entry = self._entries.get(name) |
| 125 | + if entry is not None: |
| 126 | + return entry.value |
| 127 | + else: |
| 128 | + return default |
| 129 | + |
| 130 | + def get_list(self, name, default=None): |
| 131 | + if default is None: |
| 132 | + default = [] |
| 133 | + entry = self._entries.get(name) |
| 134 | + if entry is not None: |
| 135 | + value = entry.value |
| 136 | + if isinstance(value, list): |
| 137 | + return value |
| 138 | + elif isinstance(value, str): |
| 139 | + return [value] |
| 140 | + else: |
| 141 | + msg = 'invalid value {} type {}' |
| 142 | + raise RuntimeError(msg.format(value, type(value))) |
| 143 | + else: |
| 144 | + return default |
| 145 | + |
| 146 | + def __getitem__(self, name): |
| 147 | + return self._entries[name].value |
| 148 | + |
| 149 | + def __setitem__(self, name, entry): |
| 150 | + if not isinstance(entry, CMakeCacheEntry): |
| 151 | + msg = 'improper type {} for value {}, expecting CMakeCacheEntry' |
| 152 | + raise TypeError(msg.format(type(entry), entry)) |
| 153 | + self._entries[name] = entry |
| 154 | + |
| 155 | + def __delitem__(self, name): |
| 156 | + del self._entries[name] |
| 157 | + |
| 158 | + def __iter__(self): |
| 159 | + return iter(self._entries.values()) |
| 160 | + |
| 161 | + |
| 162 | +class CMakeMixin(object): |
| 163 | + __slots__ = [] |
| 164 | + |
| 165 | + _cmake_cache = None |
| 166 | + |
| 167 | + def cmake_variable(self, variable_name, default="<unset>"): |
| 168 | + variable_value = self.options.defines.get(variable_name, default) |
| 169 | + if variable_value == "<unset>": |
| 170 | + raise self._get_error_exception( |
| 171 | + "CMake variable '{}' not defined.".format(variable_name), 1) |
| 172 | + return variable_value |
| 173 | + |
| 174 | + def cmake_cache_variable(self, variable_name, default="<unset>"): |
| 175 | + if self._cmake_cache is None: |
| 176 | + cache_file = Path(self.options.cmakecache_file) |
| 177 | + if not cache_file.is_file(): |
| 178 | + raise self._get_error_exception( |
| 179 | + "CMake cache file '{}' does not exist or is no file.". |
| 180 | + format(cache_file), 1) |
| 181 | + self._cmake_cache = CMakeCache(cache_file) |
| 182 | + try: |
| 183 | + return self._cmake_cache.get(variable_name) |
| 184 | + except: |
| 185 | + if default == "<unset>": |
| 186 | + raise self._get_error_exception( |
| 187 | + "CMake variable '{}' not defined in cache file.". |
| 188 | + format(variable_name), 1) |
| 189 | + return default |
| 190 | + |
0 commit comments