File: VerifyModuleTask.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 (154 lines) | stat: -rw-r--r-- 5,329 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
import collections.abc
import pathlib
import shlex
import subprocess

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


class VerifyModuleTask:
    """
    A type used to verify a module for correctness by compiling a test framework from a set of
    inputs with modules enabled.

    The test framework should contain include statements for each exposed header in the target
    library product. If any of the exposed headers are missing from the library module map, or
    if there are any other module-related issues in any header, running this task will indicate
    the verification has failed.
    """

    output_path: pathlib.Path
    target: str
    other_c_flags: list[str]
    other_verifier_flags: list[str]
    other_cxx_flags: list[str]
    language: Language
    standard: LanguageStandard
    sys_root: str
    framework_search_paths: list[str]
    source_file: str

    def __init__(
        self,
        target_set: ModuleVerifierTargetSet,
        inputs: ModuleVerifierInputs,
        environment: collections.abc.Mapping[str, str],
    ):
        """
        Creates a new VerifyModuleTask whose properties are derived using the specified target set, inputs, and environment.

        The environment variables this routine depends on are:

        ```
        PRODUCT_NAME
        TARGET_TEMP_DIR
        OTHER_CFLAGS
        OTHER_MODULE_VERIFIER_FLAGS
        OTHER_CPLUSPLUSFLAGS
        SDKROOT
        FRAMEWORK_SEARCH_PATHS
        ```

        Args:
            target_set: The target set used to derive the language, language standards, and target properties.
            inputs: The set of input paths used to verify the module.
            environment: Specifies the configuration options to use for the verification command.
        """

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

        self.language = target_set.language
        self.standard = target_set.standard

        self.output_path = (
            pathlib.Path(target_temp_dir)
            / "VerifyModule"
            / product_name
            / target_set.path_component()
        )

        self.target = target_set.target.value()
        self.source_file = str(inputs.main)

        self.other_c_flags = shlex.split(environment.get("OTHER_CFLAGS", ""))
        self.other_c_flags += [
            f"-Wsystem-headers-in-module={product_name}",
            "-Werror=non-modular-include-in-module",
            "-Werror=non-modular-include-in-framework-module",
            "-Werror=incomplete-umbrella",
            "-Werror=quoted-include-in-framework-header",
            "-Werror=atimport-in-framework-header",
            "-Werror=framework-include-private-from-public",
            "-Werror=incomplete-framework-module-declaration",
            "-Wundef-prefix=TARGET_OS",
            "-Werror=undef-prefix",
            "-Werror=module-import-in-extern-c",
            "-ferror-limit=0",
        ]

        self.other_verifier_flags = shlex.split(
            environment.get("OTHER_MODULE_VERIFIER_FLAGS", "")
        )
        if len(self.other_verifier_flags) > 1:
            self.other_c_flags += self.other_verifier_flags[1:]

        self.other_cxx_flags = shlex.split(environment.get("OTHER_CPLUSPLUSFLAGS", ""))
        self.other_cxx_flags += ["-fcxx-modules"]
        self.other_cxx_flags += self.other_c_flags

        self.sys_root = environment["SDKROOT"]

        self.framework_search_paths = shlex.split(
            environment.get("FRAMEWORK_SEARCH_PATHS", "")
        )
        self.framework_search_paths += [str(inputs.directory)]

    def create_command(self) -> list[str]:
        """
        Creates the set of command arguments described by this task to be evaluated.

        Returns:
            A set of arguments describing a clang invocation, suitable to pass to a process to be run.
        """

        arguments: list[str] = []
        arguments += ["-x", self.language.value]
        arguments += ["-target", self.target]
        arguments += [f"-std={self.standard.value}"]
        arguments += [
            "-fmodules",
            "-fsyntax-only",
            "-fdiagnostics-show-note-include-stack",
        ]

        if (
            self.language == Language.C_PLUS_PLUS
            or self.language == Language.OBJECTIVE_C_PLUS_PLUS
        ):
            arguments += self.other_cxx_flags
        else:
            arguments += self.other_c_flags

        arguments += ["-isysroot", self.sys_root]

        for framework_search_path in self.framework_search_paths:
            arguments += [f"-F{framework_search_path}"]

        return ["xcrun", "clang"] + arguments + [self.source_file]

    def perform_action(self) -> subprocess.CompletedProcess[str]:
        """
        Verifies the module by running a clang command produced by this task.

        Returns:
            The result of running the command. If the return code of the result is `0`, the module succeeded in verification.
            Otherwise, the module failed verification, and `stderr` describes the errors.
        """

        command = self.create_command()
        return subprocess.run(command, stderr=subprocess.PIPE, text=True)