cmakecache.py 1.2 KB

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