1# Copyright (c) 2021 The Linux Foundation
2#
3# SPDX-License-Identifier: Apache-2.0
4
5from west import log
6
7
8# Parse a CMakeCache file and return a dict of key:value (discarding
9# type hints).
10def parseCMakeCacheFile(filePath):
11    log.dbg(f"parsing CMake cache file at {filePath}")
12    kv = {}
13    try:
14        with open(filePath, "r") as f:
15            # should be a short file, so we'll use readlines
16            lines = f.readlines()
17
18            # walk through and look for non-comment, non-empty lines
19            for line in lines:
20                sline = line.strip()
21                if sline == "":
22                    continue
23                if sline.startswith("#") or sline.startswith("//"):
24                    continue
25
26                # parse out : and = characters
27                pline1 = sline.split(":", maxsplit=1)
28                if len(pline1) != 2:
29                    continue
30                pline2 = pline1[1].split("=", maxsplit=1)
31                if len(pline2) != 2:
32                    continue
33                kv[pline1[0]] = pline2[1]
34
35            return kv
36
37    except OSError as e:
38        log.err(f"Error loading {filePath}: {str(e)}")
39        return {}
40