2019-10-13 19:07:22 +02:00
|
|
|
""" Functions for working with Makefiles
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
|
|
|
|
import qmk.path
|
|
|
|
from qmk.errors import NoSuchKeyboardError
|
|
|
|
|
2020-01-07 21:54:21 +01:00
|
|
|
|
2019-10-28 09:23:06 +01:00
|
|
|
def parse_rules_mk_file(file, rules_mk=None):
|
|
|
|
"""Turn a rules.mk file into a dictionary.
|
2019-10-13 19:07:22 +02:00
|
|
|
|
|
|
|
Args:
|
2019-10-28 09:23:06 +01:00
|
|
|
file: path to the rules.mk file
|
|
|
|
rules_mk: already parsed rules.mk the new file should be merged with
|
2019-10-13 19:07:22 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
a dictionary with the file's content
|
|
|
|
"""
|
2019-10-28 09:23:06 +01:00
|
|
|
if not rules_mk:
|
|
|
|
rules_mk = {}
|
2019-10-13 19:07:22 +02:00
|
|
|
|
2019-10-28 09:23:06 +01:00
|
|
|
if os.path.exists(file):
|
|
|
|
rules_mk_lines = qmk.path.file_lines(file)
|
2019-10-13 19:07:22 +02:00
|
|
|
|
2019-10-28 09:23:06 +01:00
|
|
|
for line in rules_mk_lines:
|
|
|
|
# Filter out comments
|
|
|
|
if line.strip().startswith("#"):
|
|
|
|
continue
|
2019-10-13 19:07:22 +02:00
|
|
|
|
2019-10-28 09:23:06 +01: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-07 21:54:21 +01:00
|
|
|
line.replace(":", "")
|
2019-10-28 09:23:06 +01:00
|
|
|
key, value = line.split('=', 1)
|
|
|
|
rules_mk[key.strip()] = value.strip()
|
2019-10-13 19:07:22 +02:00
|
|
|
|
2019-10-28 09:23:06 +01:00
|
|
|
return rules_mk
|
|
|
|
|
2020-01-07 21:54:21 +01:00
|
|
|
|
2019-10-28 09:23:06 +01:00
|
|
|
def get_rules_mk(keyboard):
|
2019-10-13 19:07:22 +02:00
|
|
|
""" Get a rules.mk for a keyboard
|
|
|
|
|
|
|
|
Args:
|
|
|
|
keyboard: name of the keyboard
|
2019-10-28 09:23:06 +01:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
NoSuchKeyboardError: when the keyboard does not exists
|
2019-10-13 19:07:22 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
a dictionary with the content of the rules.mk file
|
|
|
|
"""
|
2019-10-28 09:23:06 +01:00
|
|
|
# Start with qmk_firmware/keyboards
|
|
|
|
kb_path = os.path.join(os.getcwd(), "keyboards")
|
|
|
|
# walk down the directory tree
|
|
|
|
# and collect all rules.mk files
|
|
|
|
if os.path.exists(os.path.join(kb_path, keyboard)):
|
|
|
|
rules_mk = dict()
|
|
|
|
for directory in keyboard.split(os.path.sep):
|
|
|
|
kb_path = os.path.join(kb_path, directory)
|
|
|
|
rules_mk_path = os.path.join(kb_path, "rules.mk")
|
|
|
|
if os.path.exists(rules_mk_path):
|
|
|
|
rules_mk = parse_rules_mk_file(rules_mk_path, rules_mk)
|
2019-10-13 19:07:22 +02:00
|
|
|
else:
|
|
|
|
raise NoSuchKeyboardError("The requested keyboard and/or revision does not exist.")
|
|
|
|
|
|
|
|
return rules_mk
|