File: GenerateModuleVerifierInputsTask.py

package info (click to toggle)
webkit2gtk 2.51.90-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 484,192 kB
  • sloc: cpp: 3,930,945; javascript: 197,713; ansic: 167,619; python: 53,160; asm: 21,857; ruby: 18,114; perl: 17,149; xml: 4,631; sh: 2,462; yacc: 2,394; java: 2,032; lex: 1,358; pascal: 372; makefile: 215
file content (200 lines) | stat: -rw-r--r-- 6,902 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import collections.abc
import pathlib
import shlex
import textwrap

import ModuleVerifierLanguage
from ModuleVerifierLanguage import Language, LanguageStandard
from ModuleVerifierTypes import (
    ModuleVerifierInputs,
    ModuleVerifierTarget,
    ModuleVerifierTargetSet,
)


def _module_verifier_target_set_combinations(
    targets: list[ModuleVerifierTarget],
    languages: list[Language],
    standards: list[LanguageStandard],
) -> list[ModuleVerifierTargetSet]:
    result: list[ModuleVerifierTargetSet] = []

    languages_and_standards = [
        (language, language.supported_standards(standards)) for language in languages
    ]

    for target in targets:
        # FIXME: Support Catalyst targets.
        if target.suffix is not None and "macabi" in target.suffix:
            continue

        for language, supported_standards in languages_and_standards:
            for standard in supported_standards:
                result.append(ModuleVerifierTargetSet(target, language, standard))

    return result


class GenerateModuleVerifierInputsTask:
    """
    A type used to generate inputs from an environment, for a module verifier to later consume.

    Each task describes a set of inputs which, when combined, form the following structure:

    ```
    {target_temp_dir}/
    └── VerifyModule/
        └── {product_name}/
            ├── {language}/
            │   ├── Test.{extension}
            │   └── Test.framework/
            │       ├── Headers/
            │       │   └── Test.h
            │       └── Modules/
            │           └── module.modulemap
            └── {language_1}/
                ├── ...
                └── ...
    ```
    """

    target_set: ModuleVerifierTargetSet
    inputs: ModuleVerifierInputs

    @classmethod
    def create_tasks(
        cls, environment: collections.abc.Mapping[str, str]
    ) -> list["GenerateModuleVerifierInputsTask"]:
        """
        Creates a collection of `ModuleVerifierInputGeneratorTask`s using the specified environment.

        The environment variables this routine depends on are:

        ```
        MODULE_VERIFIER_SUPPORTED_LANGUAGES
        MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS
        ARCHS
        LLVM_TARGET_TRIPLE_VENDOR
        LLVM_TARGET_TRIPLE_OS_VERSION
        LLVM_TARGET_TRIPLE_SUFFIX
        PRODUCT_NAME
        TARGET_TEMP_DIR
        ```

        Args:
            environment: Specifies the types and combinations of tasks that should be created.
        Returns:
            The collection of input generator tasks; one for each language in `MODULE_VERIFIER_SUPPORTED_LANGUAGES`.
        Raises:
            AssertionError: If an invalid language or standard is specified, or an invalid combination thereof.
        """

        language_names = shlex.split(
            environment.get("MODULE_VERIFIER_SUPPORTED_LANGUAGES", "")
        )
        languages: list[Language] = []
        for name in language_names:
            try:
                languages.append(Language(name))
            except ValueError:
                raise AssertionError(f"Unsupported module verifier language {name}")

        standard_names = shlex.split(
            environment.get("MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS", "")
        )
        standards: list[LanguageStandard] = []
        for name in standard_names:
            try:
                standards.append(LanguageStandard(name))
            except ValueError:
                raise AssertionError(
                    f"Unsupported module verifier language standard {name}"
                )

        ModuleVerifierLanguage.verify_languages(languages, standards)

        arch_names = shlex.split(environment.get("ARCHS", ""))

        vendor = environment["LLVM_TARGET_TRIPLE_VENDOR"]
        os_version = environment["LLVM_TARGET_TRIPLE_OS_VERSION"]
        suffix = environment.get("LLVM_TARGET_TRIPLE_SUFFIX")

        targets = [
            ModuleVerifierTarget(arch, vendor, os_version, suffix)
            for arch in arch_names
        ]

        product_name = environment["PRODUCT_NAME"]
        target_temp_dir = environment["TARGET_TEMP_DIR"]

        languages_to_inputs: dict[Language, ModuleVerifierInputs] = {}

        for language in languages:
            output_path = pathlib.Path(
                f"{target_temp_dir}/VerifyModule/{product_name}/{language.value}"
            )
            inputs = ModuleVerifierInputs(
                output_path / f"Test.{language.source_file_extension()}",
                output_path / "Test.framework" / "Headers" / "Test.h",
                output_path / "Test.framework" / "Modules" / "module.modulemap",
                output_path,
            )

            languages_to_inputs[language] = inputs

        target_sets = _module_verifier_target_set_combinations(
            targets, languages, standards
        )

        result: list[GenerateModuleVerifierInputsTask] = []

        for target_set in target_sets:
            inputs = languages_to_inputs[target_set.language]
            result.append(GenerateModuleVerifierInputsTask(target_set, inputs))

        return result

    def __init__(
        self, target_set: ModuleVerifierTargetSet, inputs: ModuleVerifierInputs
    ):
        self.target_set = target_set
        self.inputs = inputs

    def perform_action(self, headers: list[str]) -> None:
        """
        Generates the module verifier inputs described by this task, using the specified list of header files.

        The input test framework contains a module map that uses `Test.h` as an umbrella header, which itself includes all files in `headers`. This framework is then included by the translation unit file created for each valid language.

        This routine creates parent directories for each newly created file, if needed.

        Args:
            headers: A list of header files to include when generating the test header input. Each element in this list should be a string of the form `product_name/file.h`.
        """

        assert headers, "The list of headers must not be empty"

        for input_directory in (
            self.inputs.main,
            self.inputs.header,
            self.inputs.module_map,
        ):
            input_directory.parent.mkdir(parents=True, exist_ok=True)

        include = self.target_set.language.include_statement()

        self.inputs.main.write_text(f"{include} <Test/Test.h>")

        output = "\n".join(f"{include} <{header}>" for header in headers)
        self.inputs.header.write_text(output)

        module_map_contents = textwrap.dedent("""\
        framework module Test {
            umbrella header "Test.h"

            export *
            module * { export * }
        }
        """)

        self.inputs.module_map.write_text(module_map_contents)