| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright © 2021-2024 Intel Corporation |
| 3 |
|
| 4 |
"""Accumulator for p1689r5 module dependencies. |
| 5 |
|
| 6 |
See: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1689r5.html |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
import json |
| 11 |
import re |
| 12 |
import textwrap |
| 13 |
import typing as T |
| 14 |
|
| 15 |
if T.TYPE_CHECKING: |
| 16 |
from .depscan import Description, Rule |
| 17 |
|
| 18 |
# The quoting logic has been copied from the ninjabackend to avoid having to |
| 19 |
# import half of Meson just to quote outputs, which is a performance problem |
| 20 |
_QUOTE_PAT = re.compile(r'[$ :\n]') |
| 21 |
|
| 22 |
|
| 23 |
def quote(text: str) -> str: |
| 24 |
# Fast path for when no quoting is necessary |
| 25 |
if not _QUOTE_PAT.search(text): |
| 26 |
return text |
| 27 |
if '\n' in text: |
| 28 |
errmsg = textwrap.dedent(f'''\ |
| 29 |
Ninja does not support newlines in rules. The content was: |
| 30 |
|
| 31 |
{text} |
| 32 |
|
| 33 |
Please report this error with a test case to the Meson bug tracker.''') |
| 34 |
raise RuntimeError(errmsg) |
| 35 |
return _QUOTE_PAT.sub(r'$\g<0>', text) |
| 36 |
|
| 37 |
|
| 38 |
_PROVIDER_CACHE: T.Dict[str, str] = {} |
| 39 |
|
| 40 |
|
| 41 |
def get_provider(rules: T.List[Rule], name: str) -> T.Optional[str]: |
| 42 |
"""Get the object that a module from another Target provides |
| 43 |
|
| 44 |
We must rely on the object file here instead of the module itself, because |
| 45 |
the object rule is part of the generated build.ninja, while the module is |
| 46 |
only declared inside a dyndep. This creates for the dyndep generator to |
| 47 |
depend on previous dyndeps as order deps. Since the module |
| 48 |
interface file will be generated when the object is generated we can rely on |
| 49 |
that in proxy and simplify generation. |
| 50 |
|
| 51 |
:param rules: The list of rules to check |
| 52 |
:param name: The logical-name to look for |
| 53 |
:raises RuntimeError: If no provider can be found |
| 54 |
:return: The object file of the rule providing the module |
| 55 |
""" |
| 56 |
# Cache the result for performance reasons |
| 57 |
if name in _PROVIDER_CACHE: |
| 58 |
return _PROVIDER_CACHE[name] |
| 59 |
|
| 60 |
for r in rules: |
| 61 |
for p in r.get('provides', []): |
| 62 |
if p['logical-name'] == name: |
| 63 |
obj = r['primary-output'] |
| 64 |
_PROVIDER_CACHE[name] = obj |
| 65 |
return obj |
| 66 |
return None |
| 67 |
|
| 68 |
|
| 69 |
def process_rules(rules: T.List[Rule], |
| 70 |
extra_rules: T.List[Rule], |
| 71 |
) -> T.Iterable[T.Tuple[str, T.Optional[T.List[str]], T.List[str]]]: |
| 72 |
"""Process the rules for this Target |
| 73 |
|
| 74 |
:param rules: the rules for this target |
| 75 |
:param extra_rules: the rules for all of the targets this one links with, to use their provides |
| 76 |
:yield: A tuple of the output, the exported modules, and the consumed modules |
| 77 |
""" |
| 78 |
for rule in rules: |
| 79 |
prov: T.Optional[T.List[str]] = None |
| 80 |
req: T.List[str] = [] |
| 81 |
if 'provides' in rule: |
| 82 |
prov = [p['compiled-module-path'] for p in rule['provides']] |
| 83 |
if 'requires' in rule: |
| 84 |
for p in rule['requires']: |
| 85 |
modfile = p.get('compiled-module-path') |
| 86 |
if modfile is not None: |
| 87 |
req.append(modfile) |
| 88 |
else: |
| 89 |
# We can't error if this is not found because of compiler |
| 90 |
# provided modules |
| 91 |
found = get_provider(extra_rules, p['logical-name']) |
| 92 |
if found: |
| 93 |
req.append(found) |
| 94 |
yield rule['primary-output'], prov, req |
| 95 |
|
| 96 |
|
| 97 |
def formatter(files: T.Optional[T.List[str]]) -> str: |
| 98 |
if files: |
| 99 |
fmt = ' '.join(quote(f) for f in files) |
| 100 |
return f'| {fmt}' |
| 101 |
return '' |
| 102 |
|
| 103 |
|
| 104 |
def gen(outfile: str, desc: Description, extra_rules: T.List[Rule]) -> int: |
| 105 |
with open(outfile, 'w', encoding='utf-8') as f: |
| 106 |
f.write('ninja_dyndep_version = 1\n\n') |
| 107 |
|
| 108 |
for obj, provides, requires in process_rules(desc['rules'], extra_rules): |
| 109 |
ins = formatter(requires) |
| 110 |
out = formatter(provides) |
| 111 |
f.write(f'build {quote(obj)} {out}: dyndep {ins}\n\n') |
| 112 |
|
| 113 |
return 0 |
| 114 |
|
| 115 |
|
| 116 |
def run(args: T.List[str]) -> int: |
| 117 |
assert len(args) >= 2, 'got wrong number of arguments!' |
| 118 |
outfile, jsonfile, *jsondeps = args |
| 119 |
with open(jsonfile, 'r', encoding='utf-8') as f: |
| 120 |
desc: Description = json.load(f) |
| 121 |
|
| 122 |
# All rules, necessary for fulfilling across TU and target boundaries |
| 123 |
rules = desc['rules'].copy() |
| 124 |
for dep in jsondeps: |
| 125 |
with open(dep, encoding='utf-8') as f: |
| 126 |
d: Description = json.load(f) |
| 127 |
rules.extend(d['rules']) |
| 128 |
|
| 129 |
return gen(outfile, desc, rules) |