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/.
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
23 # - `flags` are the flags to be passed to the compiler, in addition to
25 # - `check_msg` is the message to be printed to accompany compiling the
36 @depends(dependable(flags))
38 flags = list(flags or [])
42 @depends(dependable(includes))
44 includes = includes or []
45 return ["#include <%s>" % f for f in includes]
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
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
63 # - `flags` are the flags to be passed to the compiler.
64 # - `check_msg` is the message to be printed to accompany compiling the
66 # - `onerror` is a function called when the check fails.
76 source = textwrap.dedent(
92 return checking(check_msg)(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)
107 extra_toolchain_flags,
122 flags = list(flags or [])
124 flags += extra_flags or []
125 header = header or ""
126 if isinstance(header, (list, tuple)):
127 header = "\n".join(header)
134 [compiler.compiler] + compiler.flags,
139 wrapper=compiler.wrapper,
147 compiler.__class__ = Compiler