2019-10-13 23:07:22 +06:00
|
|
|
""" Functions for working with Makefiles
|
|
|
|
"""
|
2020-01-12 17:36:41 +06:00
|
|
|
from pathlib import Path
|
2019-10-13 23:07:22 +06:00
|
|
|
|
2020-01-08 02:54:21 +06:00
|
|
|
|
2019-10-28 14:23:06 +06:00
|
|
|
def parse_rules_mk_file(file, rules_mk=None):
|
|
|
|
"""Turn a rules.mk file into a dictionary.
|
2019-10-13 23:07:22 +06:00
|
|
|
|
|
|
|
Args:
|
2019-10-28 14:23:06 +06:00
|
|
|
file: path to the rules.mk file
|
|
|
|
rules_mk: already parsed rules.mk the new file should be merged with
|
2019-10-13 23:07:22 +06:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
a dictionary with the file's content
|
|
|
|
"""
|
2019-10-28 14:23:06 +06:00
|
|
|
if not rules_mk:
|
|
|
|
rules_mk = {}
|
2019-10-13 23:07:22 +06:00
|
|
|
|
2020-01-12 17:36:41 +06:00
|
|
|
file = Path(file)
|
|
|
|
if file.exists():
|
|
|
|
rules_mk_lines = file.read_text().split("\n")
|
2019-10-13 23:07:22 +06:00
|
|
|
|
2019-10-28 14:23:06 +06:00
|
|
|
for line in rules_mk_lines:
|
|
|
|
# Filter out comments
|
|
|
|
if line.strip().startswith("#"):
|
|
|
|
continue
|
2019-10-13 23:07:22 +06:00
|
|
|
|
2019-10-28 14:23:06 +06:00
|
|
|
# Strip in-line comments
|
|
|
|
if '#' in line:
|
|
|
|
line = line[:line.index('#')].strip()
|
|
|
|
|
|
|
|
if '=' in line:
|
|
|
|
# Append
|
|
|
|
if '+=' in line:
|
|
|
|
key, value = line.split('+=', 1)
|
|
|
|
if key.strip() not in rules_mk:
|
|
|
|
rules_mk[key.strip()] = value.strip()
|
|
|
|
else:
|
|
|
|
rules_mk[key.strip()] += ' ' + value.strip()
|
|
|
|
# Set if absent
|
|
|
|
elif "?=" in line:
|
|
|
|
key, value = line.split('?=', 1)
|
|
|
|
if key.strip() not in rules_mk:
|
|
|
|
rules_mk[key.strip()] = value.strip()
|
|
|
|
else:
|
|
|
|
if ":=" in line:
|
2020-01-08 02:54:21 +06:00
|
|
|
line.replace(":", "")
|
2019-10-28 14:23:06 +06:00
|
|
|
key, value = line.split('=', 1)
|
|
|
|
rules_mk[key.strip()] = value.strip()
|
2019-10-13 23:07:22 +06:00
|
|
|
|
2019-10-28 14:23:06 +06:00
|
|
|
return rules_mk
|