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 def _try_compile_or_link(
27 @depends(dependable(flags))
29 flags = list(flags or [])
35 @depends(compiler, dependable(ldflags), dependable(flags), when=when)
36 def flags(compiler, ldflags, flags):
37 if compiler.type == "clang-cl":
39 "checking linker flags for clang-cl is not supported yet"
42 flags = list(flags or [])
46 @depends(dependable(includes))
48 includes = includes or []
49 return ["#include <%s>" % f for f in includes]
60 # Generates a test program and attempts to compile it. In case of
61 # failure, the resulting check will return None. If the test program
62 # succeeds, it will return the output of the test program.
63 # - `includes` are the includes (as file names) that will appear at the
64 # top of the generated test program.
65 # - `body` is the code that will appear in the main function of the
66 # generated test program. `return 0;` is appended to the function
68 # - `flags` are the flags to be passed to the compiler, in addition to
70 # - `check_msg` is the message to be printed to accompany compiling the
81 return self._try_compile_or_link(
91 # Same steps as try_compile but doesn't add "-c"
100 onerror=lambda: None,
102 return self._try_compile_or_link(
112 # Generates a test program and run the compiler against it. In case of
113 # failure, the resulting check will return None.
114 # - `header` is code that will appear at the top of the generated test
116 # - `body` is the code that will appear in the main function of the
117 # generated test program. `return 0;` is appended to the function
118 # body automatically.
119 # - `flags` are the flags to be passed to the compiler.
120 # - `check_msg` is the message to be printed to accompany compiling the
122 # - `onerror` is a function called when the check fails.
130 onerror=lambda: None,
132 source = textwrap.dedent(
148 return checking(check_msg)(fn)
155 # We accept onerror being a @depends function that returns a callable.
156 # So, create a similar @depends function when it's not already one.
157 if not isinstance(onerror, SandboxDependsFunction):
158 onerror = dependable(lambda: onerror)
163 extra_toolchain_flags,
178 flags = list(flags or [])
180 flags += extra_flags or []
181 header = header or ""
182 if isinstance(header, (list, tuple)):
183 header = "\n".join(header)
190 [compiler.compiler] + compiler.flags,
195 wrapper=compiler.wrapper,
203 compiler.__class__ = Compiler