no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / build / templates.mozbuild
blob443c706a31e2379c5e6fd0f246bf46c65a0212d3
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 def Binary():
10     """Generic template for target binaries. Meant to be used by other
11     templates."""
13     # Add -llog by default, since we use it all over the place.
14     if CONFIG["OS_TARGET"] == "Android":
15         OS_LIBS += ["log"]
18 @template
19 def Program(name):
20     """Template for program executables."""
21     PROGRAM = name
23     Binary()
26 @template
27 def SimplePrograms(names, ext=".cpp"):
28     """Template for simple program executables.
30     Those have a single source with the same base name as the executable.
31     """
32     SIMPLE_PROGRAMS += names
33     SOURCES += ["%s%s" % (name, ext) for name in names]
35     Binary()
38 @template
39 def CppUnitTests(names, ext=".cpp"):
40     """Template for C++ unit tests.
42     Those have a single source with the same base name as the executable.
43     """
44     COMPILE_FLAGS["EXTRA_INCLUDES"] = [
45         "-I%s/dist/include" % TOPOBJDIR,
46         "-I%s/dist/include/testing" % TOPOBJDIR,
47     ]
48     CPP_UNIT_TESTS += names
49     SOURCES += ["%s%s" % (name, ext) for name in names]
51     Binary()
54 @template
55 def Library(name):
56     """Template for libraries."""
57     LIBRARY_NAME = name
60 @template
61 def AllowCompilerWarnings():
62     COMPILE_FLAGS["WARNINGS_AS_ERRORS"] = []
63     WASM_FLAGS["WARNINGS_AS_ERRORS"] = []
66 @template
67 def DisableCompilerWarnings():
68     # Keep the -Wno-* flags to disable warnings that may be enabled through other means.
69     def filter(flags):
70         return [f for f in flags or [] if f.startswith("-Wno-")]
72     COMPILE_FLAGS["WARNINGS_CFLAGS"] = filter(CONFIG["WARNINGS_CFLAGS"])
73     COMPILE_FLAGS["WARNINGS_CXXFLAGS"] = filter(CONFIG["WARNINGS_CXXFLAGS"])
74     HOST_COMPILE_FLAGS["WARNINGS_CFLAGS"] = filter(CONFIG["WARNINGS_HOST_CFLAGS"])
75     HOST_COMPILE_FLAGS["WARNINGS_CXXFLAGS"] = filter(CONFIG["WARNINGS_HOST_CXXFLAGS"])
78 @template
79 def DisableWarningsForRust():
80     """Template to disable warnings when compiling rust (rust build scripts
81     compile C/C++ sources which we can't control easily). Meant to be used by
82     other templates.
83     """
84     # Some Rust build scripts compile C/C++ sources, don't error on warnings for them.
85     AllowCompilerWarnings()
87     # And furthermore, don't even show warnings for them, so they don't regress
88     # the Compiler Warnings build metric
89     # <https://developer.mozilla.org/en-US/docs/Mozilla/Performance/Automated_Performance_Testing_and_Sheriffing/Build_Metrics#compiler_warnings>.
90     DisableCompilerWarnings()
93 @template
94 def RustLibrary(name, features=None, output_category=None, is_gkrust=False):
95     """Template for Rust libraries."""
96     Library(name)
98     IS_RUST_LIBRARY = True
100     DisableWarningsForRust()
102     if features:
103         RUST_LIBRARY_FEATURES = features
105     if output_category:
106         RUST_LIBRARY_OUTPUT_CATEGORY = output_category
108     if is_gkrust:
109         IS_GKRUST = True
112 @template
113 def RustProgram(name):
114     """Template for Rust programs."""
115     RUST_PROGRAMS += [name]
116     DisableWarningsForRust()
119 @template
120 def SharedLibrary(name, output_category=None):
121     """Template for shared libraries."""
122     Library(name)
124     FORCE_SHARED_LIB = True
126     if output_category:
127         SHARED_LIBRARY_OUTPUT_CATEGORY = output_category
129     Binary()
132 @template
133 def Framework(name, output_category=None):
134     """Template for OSX Frameworks."""
135     SharedLibrary(name, output_category)
137     IS_FRAMEWORK = True
140 @template
141 def HostProgram(name):
142     """Template for build tools executables."""
143     HOST_PROGRAM = name
146 @template
147 def HostSimplePrograms(names, ext=".cpp"):
148     """Template for simple build tools executables.
150     Those have a single source with the same base name as the executable.
151     """
152     HOST_SIMPLE_PROGRAMS += names
153     HOST_SOURCES += ["%s%s" % (name.replace("host_", ""), ext) for name in names]
156 @template
157 def HostSharedLibrary(name):
158     """Template for build tools libraries."""
159     if name != "clang-plugin":
160         error(
161             "Please make sure host shared library support is complete "
162             "before using for something else than the clang plugin"
163         )
165     HOST_LIBRARY_NAME = name
167     FORCE_SHARED_LIB = True
170 @template
171 def HostLibrary(name):
172     """Template for build tools libraries."""
173     HOST_LIBRARY_NAME = name
176 @template
177 def HostRustLibrary(name, features=None):
178     """Template for host Rust libraries."""
179     HostLibrary(name)
181     IS_RUST_LIBRARY = True
182     # Some Rust build scripts compile C/C++ sources, don't error on warnings for them.
183     AllowCompilerWarnings()
185     if features:
186         HOST_RUST_LIBRARY_FEATURES = features
189 @template
190 def DisableStlWrapping():
191     COMPILE_FLAGS["STL"] = []
194 @template
195 def NoVisibilityFlags():
196     COMPILE_FLAGS["VISIBILITY"] = []
199 @template
200 def ForceInclude(*headers):
201     """Force includes a set of header files in C++ compilations"""
202     if CONFIG["CC_TYPE"] == "clang-cl":
203         include_flag = "-FI"
204     else:
205         include_flag = "-include"
206     for header in headers:
207         CXXFLAGS += [include_flag, header]
210 @template
211 def GeneratedFile(name, *names, **kwargs):
212     """Add one or more GENERATED_FILES with the given attributes.
214     You must pass in at least one generated file (the "name" argument). Other
215     names can be included as positional arguments after "name"."""
216     script = kwargs.pop("script", None)
217     entry_point = kwargs.pop("entry_point", None)
218     inputs = kwargs.pop("inputs", [])
219     flags = kwargs.pop("flags", [])
220     force = kwargs.pop("force", False)
221     if kwargs:
222         error("Unrecognized argument(s) to GeneratedFile: %s" % ", ".join(kwargs))
223     if entry_point and not script:
224         error("entry_point cannot be provided if script is not provided")
225     if script and ":" in script:
226         error(
227             "script should not include a `:`. If you want to provide an "
228             "alternative entry point for your script, use the entry_point "
229             "parameter."
230         )
232     key = (name,) + names if names else name
233     GENERATED_FILES += [key]
234     generated_file = GENERATED_FILES[key]
235     if script and not entry_point:
236         generated_file.script = script
237     if script and entry_point:
238         generated_file.script = script + ":" + entry_point
239     generated_file.inputs = inputs
240     generated_file.flags = flags
241     generated_file.force = force
244 @template
245 def CbindgenHeader(name, inputs):
246     """Add one GENERATED_FILES by running RunCbindgen.py"""
248     inputs = ["!/config/cbindgen-metadata.json"] + inputs
249     GeneratedFile(
250         name, script="/build/RunCbindgen.py", entry_point="generate", inputs=inputs
251     )
254 include("gecko_templates.mozbuild")