You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB

  1. """ Functions for working with Makefiles
  2. """
  3. from pathlib import Path
  4. def parse_rules_mk_file(file, rules_mk=None):
  5. """Turn a rules.mk file into a dictionary.
  6. Args:
  7. file: path to the rules.mk file
  8. rules_mk: already parsed rules.mk the new file should be merged with
  9. Returns:
  10. a dictionary with the file's content
  11. """
  12. if not rules_mk:
  13. rules_mk = {}
  14. file = Path(file)
  15. if file.exists():
  16. rules_mk_lines = file.read_text().split("\n")
  17. for line in rules_mk_lines:
  18. # Filter out comments
  19. if line.strip().startswith("#"):
  20. continue
  21. # Strip in-line comments
  22. if '#' in line:
  23. line = line[:line.index('#')].strip()
  24. if '=' in line:
  25. # Append
  26. if '+=' in line:
  27. key, value = line.split('+=', 1)
  28. if key.strip() not in rules_mk:
  29. rules_mk[key.strip()] = value.strip()
  30. else:
  31. rules_mk[key.strip()] += ' ' + value.strip()
  32. # Set if absent
  33. elif "?=" in line:
  34. key, value = line.split('?=', 1)
  35. if key.strip() not in rules_mk:
  36. rules_mk[key.strip()] = value.strip()
  37. else:
  38. if ":=" in line:
  39. line.replace(":", "")
  40. key, value = line.split('=', 1)
  41. rules_mk[key.strip()] = value.strip()
  42. return rules_mk