Skip to content

Commit

Permalink
deplist.py: support pattern dependency hints.
Browse files Browse the repository at this point in the history
  • Loading branch information
skosukhin committed Apr 26, 2024
1 parent 88b5742 commit a4f4f88
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
2 changes: 1 addition & 1 deletion mkhelper.mk.in
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ sanitize-mod-proxies:
extra_f90.d: mkhelper.mk
$(silent_DEPGEN):;{ \
echo '#-hint src/program/main.o: src/program/implicit_external.o'; \
echo '#-hint src/program/main.o: src/modules/submo_test_submodule.o'; \
echo '#-hint <*.o: $(moddir)/mo_test_submodule.@[email protected]>.targets: src/modules/submo_test_submodule.o'; \
} >$@

# Configure delayed bundled libraries:
Expand Down
77 changes: 68 additions & 9 deletions mkhelper/deplist.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,38 @@
import re
import sys

_re_rule = re.compile(
r"^(?P<hint>#-hint )?"
# targets
r"[ ]*(?P<targets>[-+\w./]+(?:[ ]+[-+\w./]+)*)[ ]*"
# normal prerequisites
r":[ ]*(?P<normal>[-+\w./]+(?:[ ]+[-+\w./]+)*)?[ ]*"
# order-only prerequisites
r"(?:\|[ ]*(?P<order_only>[-+\w./]+(?:[ ]+[-+\w./]+)*))?"
_re_hint = r"^#-hint[ ]+"
_re_targets_or_prereqs = r"[-+\w./]+(?:[ ]+[-+\w./]+)*"

_repat_rule_or_plain_hint = re.compile(
r"^(" + _re_hint + r")?[ ]*"
r"(?P<targets>" + _re_targets_or_prereqs + ")"
r"[ ]*:[ ]*"
r"(?P<normal>" + _re_targets_or_prereqs + ")?"
r"[ ]*(?:\|[ ]*"
r"(?P<order_only>" + _re_targets_or_prereqs + ")"
r")?"
)

# Supported pattern hints:
# 1) <p1 : p2>.targets: X - all targets that match shell-like wildcard p1 and
# have a prerequisite that matches shell-like
# wildcard p2 depend on X

PatternHint = collections.namedtuple(
"Hint", ["target_pattern", "prereq_pattern", "extra_prereqs"]
)

_repat_pattern_hint = re.compile(
_re_hint + r"<[ ]*"
r"(?P<target_pattern>.*[^ ])"
r"[ ]*:[ ]*"
r"(?P<prereq_pattern>.*[^ ])"
r"[ ]*>"
r"\.(?P<request>targets)"
r"[ ]*:[ ]*"
r"(?P<extra_prereqs>" + _re_targets_or_prereqs + ")"
r"[ ]*$"
)

_meta_root = 0
Expand Down Expand Up @@ -207,9 +231,29 @@ def convert_arg_line_to_args(self, arg_line):
return args


def apply_pattern_hints(graph, hints):
extra_rules = collections.defaultdict(list)

for hint in hints:
for target, prereqs in graph.items():
if fnmatch.fnmatch(target, hint.target_pattern) and fnmatch.filter(
prereqs, hint.prereq_pattern
):
extra_prereqs = [
prereq for prereq in hint.extra_prereqs if prereq != target
]
if extra_prereqs:
extra_rules[target].extend(extra_prereqs)

for target, extra_prereqs in extra_rules.items():
graph[target].extend(extra_prereqs)


def read_makefiles(makefiles, inc_order_only):
result = collections.defaultdict(list)

pattern_hints = []

for mkf in makefiles:
if mkf == "-":
stream = sys.stdin
Expand All @@ -228,7 +272,7 @@ def read_makefiles(makefiles, inc_order_only):
except StopIteration:
break

match = _re_rule.match(line)
match = _repat_rule_or_plain_hint.match(line)
if match:
targets = set(match.group("targets").split())
prereqs = []
Expand All @@ -244,8 +288,23 @@ def read_makefiles(makefiles, inc_order_only):

for target in targets:
result[target].extend(prereqs)
continue

match = _repat_pattern_hint.match(line)
if match:
pattern_hints.append(
PatternHint(
match.group("target_pattern"),
match.group("prereq_pattern"),
match.group("extra_prereqs").split(),
)
)
continue

stream.close()

apply_pattern_hints(result, pattern_hints)

return result


Expand Down

0 comments on commit a4f4f88

Please sign in to comment.