Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / build / moz.configure / compilers-util.configure
blob0f4c4fb2eef3ea9a200335791e9751d2820c200d
1 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
2 # vim: set filetype=python:
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 @template
9 @imports("textwrap")
10 @imports(_from="mozbuild.configure", _import="SandboxDependsFunction")
11 def compiler_class(compiler, host_or_target):
12     is_target = host_or_target is target
14     class Compiler(SandboxDependsFunction):
15         # Generates a test program and attempts to compile it. In case of
16         # failure, the resulting check will return None. If the test program
17         # succeeds, it will return the output of the test program.
18         # - `includes` are the includes (as file names) that will appear at the
19         #   top of the generated test program.
20         # - `body` is the code that will appear in the main function of the
21         #   generated test program. `return 0;` is appended to the function
22         #   body automatically.
23         # - `flags` are the flags to be passed to the compiler, in addition to
24         #   `-c`.
25         # - `check_msg` is the message to be printed to accompany compiling the
26         #   test program.
27         def try_compile(
28             self,
29             includes=None,
30             body="",
31             flags=None,
32             check_msg=None,
33             when=None,
34             onerror=lambda: None,
35         ):
36             @depends(dependable(flags))
37             def flags(flags):
38                 flags = list(flags or [])
39                 flags.append("-c")
40                 return flags
42             @depends(dependable(includes))
43             def header(includes):
44                 includes = includes or []
45                 return ["#include <%s>" % f for f in includes]
47             return self.try_run(
48                 header=header,
49                 body=body,
50                 flags=flags,
51                 check_msg=check_msg,
52                 when=when,
53                 onerror=onerror,
54             )
56         # Generates a test program and run the compiler against it. In case of
57         # failure, the resulting check will return None.
58         # - `header` is code that will appear at the top of the generated test
59         #   program.
60         # - `body` is the code that will appear in the main function of the
61         #   generated test program. `return 0;` is appended to the function
62         #   body automatically.
63         # - `flags` are the flags to be passed to the compiler.
64         # - `check_msg` is the message to be printed to accompany compiling the
65         #   test program.
66         # - `onerror` is a function called when the check fails.
67         def try_run(
68             self,
69             header=None,
70             body="",
71             flags=None,
72             check_msg=None,
73             when=None,
74             onerror=lambda: None,
75         ):
76             source = textwrap.dedent(
77                 """\
78                 int
79                 main(void)
80                 {
81                 %s
82                   ;
83                   return 0;
84                 }
85             """
86                 % body
87             )
89             if check_msg:
91                 def checking_fn(fn):
92                     return checking(check_msg)(fn)
94             else:
96                 def checking_fn(fn):
97                     return fn
99             # We accept onerror being a @depends function that returns a callable.
100             # So, create a similar @depends function when it's not already one.
101             if not isinstance(onerror, SandboxDependsFunction):
102                 onerror = dependable(lambda: onerror)
104             @depends(
105                 self,
106                 dependable(flags),
107                 extra_toolchain_flags,
108                 dependable(header),
109                 onerror,
110                 configure_cache,
111                 when=when,
112             )
113             @checking_fn
114             def func(
115                 compiler,
116                 flags,
117                 extra_flags,
118                 header,
119                 onerror,
120                 configure_cache,
121             ):
122                 flags = list(flags or [])
123                 if is_target:
124                     flags += extra_flags or []
125                 header = header or ""
126                 if isinstance(header, (list, tuple)):
127                     header = "\n".join(header)
128                 if header:
129                     header += "\n"
131                 if (
132                     try_invoke_compiler(
133                         configure_cache,
134                         [compiler.compiler] + compiler.flags,
135                         compiler.language,
136                         header + source,
137                         flags,
138                         onerror=onerror,
139                         wrapper=compiler.wrapper,
140                     )
141                     is not None
142                 ):
143                     return True
145             return func
147     compiler.__class__ = Compiler
148     return compiler