Bug 1904139 - Don't re-initialize platform font list from GetDefaultFont. r=jfkthame
[gecko.git] / build / moz.configure / init.configure
blob6e02b7312ccd1fff35021198b95411faebcfed5d
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/.
7 include("util.configure")
8 include("checks.configure")
10 # Make `toolkit` available when toolkit/moz.configure is not included.
11 toolkit = dependable(None)
12 # Likewise with `bindgen_config_paths` when
13 # build/moz.configure/bindgen.configure is not included.
14 bindgen_config_paths = dependable(None)
17 @depends("--help")
18 def build_environment(_):
19     topobjdir = os.path.realpath(".")
20     topsrcdir = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
21     dist = os.path.join(topobjdir, "dist")
23     return namespace(
24         topsrcdir=topsrcdir,
25         topobjdir=topobjdir,
26         dist=dist,
27     )
30 @depends(build_environment)
31 @imports(_import="json")
32 @imports(_from="pathlib", _import="Path")
33 def configure_cache(build_environment):
34     """
35     This cache is used to cache the results of compiler invocations
36     between different runs of configure and save them to the disk, which
37     results in a significant speed up of subsequent configure runs (15%-50%).
39     It is not currently thread safe because it is just a simple dictionary
40     wrapped in a dummy class (ConfigureCache). It could be improved to be
41     thread safe in the future, if we ever make configure parallelized, but
42     for now there is no advantage to doing so.
43     """
45     class ConfigureCache(dict):
46         pass
48     cache_file = Path(build_environment.topobjdir) / "configure.cache"
49     if cache_file.exists():
50         with cache_file.open() as f:
51             cache_data = json.load(f)
52     else:
53         cache_data = {}
55     cache = ConfigureCache(cache_data)
56     cache.version_checked_compilers = set()
58     return cache
61 set_config("TOPSRCDIR", build_environment.topsrcdir)
62 set_config("TOPOBJDIR", build_environment.topobjdir)
63 set_config("DIST", build_environment.dist)
65 add_old_configure_assignment("_topsrcdir", build_environment.topsrcdir)
66 add_old_configure_assignment("_objdir", build_environment.topobjdir)
67 add_old_configure_assignment("DIST", build_environment.dist)
69 option(env="MOZ_AUTOMATION", help="Enable options for automated builds")
70 set_config("MOZ_AUTOMATION", depends_if("MOZ_AUTOMATION")(lambda x: True))
73 option(env="OLD_CONFIGURE", nargs=1, help="Path to the old configure script")
75 option(env="MOZCONFIG", nargs=1, help="Mozconfig location")
78 # Read user mozconfig
79 # ==============================================================
80 # Note: the dependency on --help is only there to always read the mozconfig,
81 # even when --help is passed. Without this dependency, the function wouldn't
82 # be called when --help is passed, and the mozconfig wouldn't be read.
85 @depends("MOZCONFIG", "OLD_CONFIGURE", build_environment, "--help")
86 @imports(_from="mozbuild.mozconfig", _import="MozconfigLoader")
87 @imports(_from="mozboot.mozconfig", _import="find_mozconfig")
88 @imports("os")
89 def mozconfig(mozconfig, old_configure, build_env, help):
90     # Don't read the mozconfig for the js configure (yay backwards
91     # compatibility)
92     # While the long term goal is that js and top-level use the same configure
93     # and the same overall setup, including the possibility to use mozconfigs,
94     # figuring out what we want to do wrt mozconfig vs. command line and
95     # environment variable is not a clear-cut case, and it's more important to
96     # fix the immediate problem mozconfig causes to js developers by
97     # "temporarily" returning to the previous behavior of not loading the
98     # mozconfig for the js configure.
99     # Separately to the immediate problem for js developers, there is also the
100     # need to not load a mozconfig when running js configure as a subconfigure.
101     # Unfortunately, there is no direct way to tell whether the running
102     # configure is the js configure. The indirect way is to look at the
103     # OLD_CONFIGURE path, which points to js/src/old-configure.
104     # I expect we'll have figured things out for mozconfigs well before
105     # old-configure dies.
106     if (
107         old_configure
108         and os.path.dirname(os.path.abspath(old_configure[0])).endswith("/js/src")
109         or (mozconfig and mozconfig[0] == os.devnull)
110     ):
111         return {"path": None}
113     topsrcdir = build_env.topsrcdir
114     loader = MozconfigLoader(topsrcdir)
115     mozconfig = mozconfig[0] if mozconfig else None
116     mozconfig = find_mozconfig(topsrcdir, env={"MOZCONFIG": mozconfig})
117     mozconfig = loader.read_mozconfig(mozconfig)
119     return mozconfig
122 set_config("MOZCONFIG", depends(mozconfig)(lambda m: m["path"]))
125 # Mozilla-Build
126 # ==============================================================
127 option(env="MOZILLABUILD", nargs=1, help="Path to Mozilla Build (Windows-only)")
129 option(env="CONFIG_SHELL", nargs=1, help="Path to a POSIX shell")
131 # It feels dirty replicating this from python/mozbuild/mozbuild/mozconfig.py,
132 # but the end goal being that the configure script would go away...
135 @depends("CONFIG_SHELL", "MOZILLABUILD")
136 @checking("for a shell")
137 @imports("sys")
138 @imports(_from="pathlib", _import="Path")
139 def shell(value, mozillabuild):
140     if value:
141         return find_program(value[0])
142     shell = "sh"
143     if mozillabuild:
144         if (Path(mozillabuild[0]) / "msys2").exists():
145             shell = mozillabuild[0] + "/msys2/usr/bin/sh"
146         else:
147             shell = mozillabuild[0] + "/msys/bin/sh"
148     if sys.platform == "win32":
149         shell = shell + ".exe"
150     return find_program(shell)
153 # This defines a reasonable shell for when running with --help.
154 # If one was passed in the environment, though, fall back to that.
155 @depends("--help", "CONFIG_SHELL")
156 def help_shell(help, shell):
157     if help and not shell:
158         return "sh"
161 shell = help_shell | shell
164 # Python 3
165 # ========
166 @dependable
167 @checking("for Python 3", callback=lambda x: "%s (%s)" % (x.path, x.str_version))
168 @imports("sys")
169 @imports(_from="mach.site", _import="PythonVirtualenv")
170 @imports(_from="os.path", _import="realpath")
171 def virtualenv_python3():
172     return namespace(
173         # sys.executable is currently not updated for in-process activations. However,
174         # sys.prefix is, so we can calculate the python executable's path from there.
175         path=normalize_path(PythonVirtualenv(realpath(sys.prefix)).python_path),
176         str_version=".".join(str(i) for i in sys.version_info[0:3]),
177     )
180 set_config("PYTHON3", virtualenv_python3.path)
181 set_config("PYTHON3_VERSION", virtualenv_python3.str_version)
182 add_old_configure_assignment("PYTHON3", virtualenv_python3.path)
185 # Inject mozconfig options
186 # ==============================================================
187 # All options defined above this point can't be injected in mozconfig_options
188 # below, so collect them.
191 @template
192 def early_options():
193     @depends("--help")
194     @imports("__sandbox__")
195     def early_options(_):
196         return set(option.env for option in __sandbox__._options.values() if option.env)
198     return early_options
201 early_options = early_options()
204 @depends(mozconfig, early_options, "MOZ_AUTOMATION", "--help")
205 # This gives access to the sandbox. Don't copy this blindly.
206 @imports("__sandbox__")
207 @imports("os")
208 def mozconfig_options(mozconfig, early_options, automation, help):
209     if mozconfig["path"]:
210         if "MOZ_AUTOMATION_MOZCONFIG" in mozconfig["env"]["added"]:
211             if not automation:
212                 log.error(
213                     "%s directly or indirectly includes an in-tree " "mozconfig.",
214                     mozconfig["path"],
215                 )
216                 log.error(
217                     "In-tree mozconfigs make strong assumptions about "
218                     "and are only meant to be used by Mozilla "
219                     "automation."
220                 )
221                 die("Please don't use them.")
222         helper = __sandbox__._helper
223         log.info("Adding configure options from %s" % mozconfig["path"])
224         for arg in mozconfig["configure_args"]:
225             log.info("  %s" % arg)
226             # We could be using imply_option() here, but it has other
227             # contraints that don't really apply to the command-line
228             # emulation that mozconfig provides.
229             helper.add(arg, origin="mozconfig", args=helper._args)
231         def add(key, value):
232             if key.isupper():
233                 arg = "%s=%s" % (key, value)
234                 log.info("  %s" % arg)
235                 if key not in early_options:
236                     helper.add(arg, origin="mozconfig", args=helper._args)
238         for key, value in mozconfig["env"]["added"].items():
239             add(key, value)
240             os.environ[key] = value
241         for key, (_, value) in mozconfig["env"]["modified"].items():
242             add(key, value)
243             os.environ[key] = value
244         for key, value in mozconfig["vars"]["added"].items():
245             add(key, value)
246         for key, (_, value) in mozconfig["vars"]["modified"].items():
247             add(key, value)
250 @depends(build_environment, "--help")
251 @imports(_from="os.path", _import="exists")
252 def js_package(build_env, help):
253     return not exists(os.path.join(build_env.topsrcdir, "browser"))
256 # Source checkout and version control integration.
257 # ================================================
260 @depends(build_environment, "MOZ_AUTOMATION", js_package, "--help")
261 @checking("for vcs source checkout")
262 @imports("os")
263 def vcs_checkout_type(build_env, automation, js_package, help):
264     if os.path.exists(os.path.join(build_env.topsrcdir, ".hg")):
265         return "hg"
266     elif os.path.exists(os.path.join(build_env.topsrcdir, ".git")):
267         return "git"
268     elif automation and not js_package and not help:
269         raise FatalCheckError(
270             "unable to resolve VCS type; must run "
271             "from a source checkout when MOZ_AUTOMATION "
272             "is set"
273         )
276 # Resolve VCS binary for detected repository type.
279 # TODO remove hg.exe once bug 1382940 addresses ambiguous executables case.
280 hg = check_prog(
281     "HG",
282     (
283         "hg.exe",
284         "hg",
285     ),
286     allow_missing=True,
287     when=depends(vcs_checkout_type)(lambda x: x == "hg"),
289 git = check_prog(
290     "GIT",
291     ("git",),
292     allow_missing=True,
293     when=depends(vcs_checkout_type)(lambda x: x == "git"),
297 @depends_if(hg)
298 @checking("for Mercurial version")
299 @imports("os")
300 @imports("re")
301 def hg_version(hg):
302     # HGPLAIN in Mercurial 1.5+ forces stable output, regardless of set
303     # locale or encoding.
304     env = dict(os.environ)
305     env["HGPLAIN"] = "1"
307     out = check_cmd_output(hg, "--version", env=env)
309     match = re.search(r"Mercurial Distributed SCM \(version ([^\)]+)", out)
311     if not match:
312         raise FatalCheckError("unable to determine Mercurial version: %s" % out)
314     # The version string may be "unknown" for Mercurial run out of its own
315     # source checkout or for bad builds. But LooseVersion handles it.
317     return Version(match.group(1))
320 # Resolve Mercurial config items so other checks have easy access.
321 # Do NOT set this in the config because it may contain sensitive data
322 # like API keys.
325 @depends_all(build_environment, hg, hg_version)
326 @imports("os")
327 def hg_config(build_env, hg, version):
328     env = dict(os.environ)
329     env["HGPLAIN"] = "1"
331     # Warnings may get sent to stderr. But check_cmd_output() ignores
332     # stderr if exit code is 0. And the command should always succeed if
333     # `hg version` worked.
334     out = check_cmd_output(hg, "config", env=env, cwd=build_env.topsrcdir)
336     config = {}
338     for line in out.strip().splitlines():
339         key, value = [s.strip() for s in line.split("=", 1)]
340         config[key] = value
342     return config
345 @depends_if(git)
346 @checking("for Git version")
347 @imports("re")
348 def git_version(git):
349     out = check_cmd_output(git, "--version").rstrip()
351     match = re.search("git version (.*)$", out)
353     if not match:
354         raise FatalCheckError("unable to determine Git version: %s" % out)
356     return Version(match.group(1))
359 # Only set VCS_CHECKOUT_TYPE if we resolved the VCS binary.
360 # Require resolved VCS info when running in automation so automation's
361 # environment is more well-defined.
364 @depends(vcs_checkout_type, hg_version, git_version, "MOZ_AUTOMATION")
365 def exposed_vcs_checkout_type(vcs_checkout_type, hg, git, automation):
366     if vcs_checkout_type == "hg":
367         if hg:
368             return "hg"
370         if automation:
371             raise FatalCheckError("could not resolve Mercurial binary info")
373     elif vcs_checkout_type == "git":
374         if git:
375             return "git"
377         if automation:
378             raise FatalCheckError("could not resolve Git binary info")
379     elif vcs_checkout_type:
380         raise FatalCheckError("unhandled VCS type: %s" % vcs_checkout_type)
383 set_config("VCS_CHECKOUT_TYPE", exposed_vcs_checkout_type)
385 # Obtain a Repository interface for the current VCS repository.
388 @depends(build_environment, exposed_vcs_checkout_type, hg, git)
389 @imports(_from="mozversioncontrol", _import="get_repository_object")
390 def vcs_repository(build_env, vcs_checkout_type, hg, git):
391     if vcs_checkout_type == "hg":
392         return get_repository_object(build_env.topsrcdir, hg=hg)
393     elif vcs_checkout_type == "git":
394         return get_repository_object(build_env.topsrcdir, git=git)
395     elif vcs_checkout_type:
396         raise FatalCheckError("unhandled VCS type: %s" % vcs_checkout_type)
399 @depends_if(vcs_repository)
400 @checking("for sparse checkout")
401 def vcs_sparse_checkout(repo):
402     return repo.sparse_checkout_present()
405 set_config("VCS_SPARSE_CHECKOUT", vcs_sparse_checkout)
407 # The application/project to build
408 # ==============================================================
409 option(
410     "--enable-application",
411     nargs=1,
412     env="MOZ_BUILD_APP",
413     help="Application to build. Same as --enable-project.",
417 @depends("--enable-application")
418 def application(app):
419     if app:
420         return app
423 imply_option("--enable-project", application)
426 @depends(build_environment, js_package)
427 def default_project(build_env, js_package):
428     if js_package or build_env.topobjdir.endswith("/js/src"):
429         return "js"
430     return "browser"
433 option("--enable-project", nargs=1, default=default_project, help="Project to build")
436 # Artifact builds
437 # ==============================================================
439 option(
440     "--enable-artifact-builds",
441     env="MOZ_ARTIFACT_BUILDS",
442     help="Download and use prebuilt binary artifacts.",
446 @depends("--enable-artifact-builds")
447 def artifact_builds(value):
448     if value:
449         return True
452 set_config("MOZ_ARTIFACT_BUILDS", artifact_builds)
454 # Host and target systems
455 # ==============================================================
456 option("--host", nargs=1, help="Define the system type performing the build")
458 option(
459     "--target",
460     nargs=1,
461     help="Define the system type where the resulting executables will be " "used",
465 @imports(_from="mozbuild.configure.constants", _import="Abi")
466 @imports(_from="mozbuild.configure.constants", _import="CPU")
467 @imports(_from="mozbuild.configure.constants", _import="CPU_bitness")
468 @imports(_from="mozbuild.configure.constants", _import="Endianness")
469 @imports(_from="mozbuild.configure.constants", _import="Kernel")
470 @imports(_from="mozbuild.configure.constants", _import="OS")
471 @imports(_from="__builtin__", _import="ValueError")
472 def split_triplet(triplet, allow_wasi=False):
473     # The standard triplet is defined as
474     #   CPU_TYPE-VENDOR-OPERATING_SYSTEM
475     # There is also a quartet form:
476     #   CPU_TYPE-VENDOR-KERNEL-OPERATING_SYSTEM
477     # But we can consider the "KERNEL-OPERATING_SYSTEM" as one.
478     # Additionally, some may omit "unknown" when the vendor
479     # is not specified and emit
480     #   CPU_TYPE-OPERATING_SYSTEM
481     vendor = "unknown"
482     parts = triplet.split("-", 2)
483     if len(parts) == 3:
484         cpu, vendor, os = parts
485     elif len(parts) == 2:
486         cpu, os = parts
487     else:
488         raise ValueError("Unexpected triplet string: %s" % triplet)
490     # Autoconf uses config.sub to validate and canonicalize those triplets,
491     # but the granularity of its results has never been satisfying to our
492     # use, so we've had our own, different, canonicalization. We've also
493     # historically not been very consistent with how we use the canonicalized
494     # values. Hopefully, this will help us make things better.
495     # The tests are inherited from our decades-old autoconf-based configure,
496     # which can probably be improved/cleaned up because they are based on a
497     # mix of uname and config.guess output, while we now only use the latter,
498     # which presumably has a cleaner and leaner output. Let's refine later.
499     raw_os = os = os.replace("/", "_")
500     abi = None
501     sub_configure_alias = triplet
502     if "android" in os:
503         canonical_os = "Android"
504         canonical_kernel = "Linux"
505     elif os.startswith("linux"):
506         canonical_os = "GNU"
507         canonical_kernel = "Linux"
508     elif os.startswith("kfreebsd") and os.endswith("-gnu"):
509         canonical_os = "GNU"
510         canonical_kernel = "kFreeBSD"
511     elif os.startswith("gnu"):
512         canonical_os = canonical_kernel = "GNU"
513     elif os.startswith("mingw") or os in ("windows-msvc", "windows-gnu"):
514         canonical_os = canonical_kernel = "WINNT"
515         if not os.startswith("mingw"):
516             if os == "windows-msvc":
517                 abi = "msvc"
518             elif os == "windows-gnu":
519                 abi = "mingw"
520             # Many things down the line are looking for the string "mingw32"
521             # until they are all fixed, we pretend that's the raw os we had
522             # in the first place, even when we didn't.
523             sub_configure_alias = sub_configure_alias[: -len(os)] + "mingw32"
524             raw_os = "mingw32"
525     elif os.startswith("darwin"):
526         canonical_kernel = "Darwin"
527         canonical_os = "OSX"
528     elif os.startswith("ios"):
529         canonical_kernel = "Darwin"
530         canonical_os = "iOS"
531         # old-configure does plenty of tests against $target and $target_os
532         # and expects darwin for iOS, so make it happy.
533         sub_configure_alias = sub_configure_alias[: -len(os)] + "darwin"
534         # rust knows ios-sim, clang knows ios-simulator. We only take the
535         # former as --target, but we need to make clang happy.
536         if os == "ios-sim":
537             os = "ios-simulator"
538     elif os.startswith("dragonfly"):
539         canonical_os = canonical_kernel = "DragonFly"
540     elif os.startswith("freebsd"):
541         canonical_os = canonical_kernel = "FreeBSD"
542     elif os.startswith("netbsd"):
543         canonical_os = canonical_kernel = "NetBSD"
544     elif os.startswith("openbsd"):
545         canonical_os = canonical_kernel = "OpenBSD"
546     elif os.startswith("solaris"):
547         canonical_os = canonical_kernel = "SunOS"
548     elif os.startswith("wasi") and allow_wasi:
549         canonical_os = canonical_kernel = "WASI"
550     else:
551         raise ValueError("Unknown OS: %s" % os)
553     # The CPU granularity is probably not enough. Moving more things from
554     # old-configure will tell us if we need more
555     if cpu.endswith("86") or (cpu.startswith("i") and "86" in cpu):
556         canonical_cpu = "x86"
557         endianness = "little"
558     elif cpu in ("x86_64", "ia64"):
559         canonical_cpu = cpu
560         endianness = "little"
561     elif cpu in ("s390", "s390x"):
562         canonical_cpu = cpu
563         endianness = "big"
564     elif cpu in ("powerpc64", "ppc64", "powerpc64le", "ppc64le"):
565         canonical_cpu = "ppc64"
566         endianness = "little" if "le" in cpu else "big"
567     elif cpu in ("powerpc", "ppc", "rs6000") or cpu.startswith("powerpc"):
568         canonical_cpu = "ppc"
569         endianness = "big"
570     elif cpu in ("Alpha", "alpha", "ALPHA"):
571         canonical_cpu = "Alpha"
572         endianness = "little"
573     elif cpu.startswith("hppa") or cpu == "parisc":
574         canonical_cpu = "hppa"
575         endianness = "big"
576     elif cpu.startswith("sparc64") or cpu.startswith("sparcv9"):
577         canonical_cpu = "sparc64"
578         endianness = "big"
579     elif cpu.startswith("sparc") or cpu == "sun4u":
580         canonical_cpu = "sparc"
581         endianness = "big"
582     elif cpu.startswith("arm"):
583         canonical_cpu = "arm"
584         endianness = "big" if cpu.startswith(("armeb", "armbe")) else "little"
585     elif cpu in ("m68k"):
586         canonical_cpu = "m68k"
587         endianness = "big"
588     elif cpu in ("mips", "mipsel"):
589         canonical_cpu = "mips32"
590         endianness = "little" if "el" in cpu else "big"
591     elif cpu in ("mips64", "mips64el"):
592         canonical_cpu = "mips64"
593         endianness = "little" if "el" in cpu else "big"
594     elif cpu.startswith("aarch64"):
595         canonical_cpu = "aarch64"
596         endianness = "little"
597     elif cpu in ("riscv64", "riscv64gc"):
598         canonical_cpu = "riscv64"
599         endianness = "little"
600     elif cpu.startswith("loongarch64"):
601         canonical_cpu = "loongarch64"
602         endianness = "little"
603     elif cpu == "sh4":
604         canonical_cpu = "sh4"
605         endianness = "little"
606     elif cpu == "wasm32" and allow_wasi:
607         canonical_cpu = "wasm32"
608         endianness = "little"
609     else:
610         raise ValueError("Unknown CPU type: %s" % cpu)
612     # Toolchains, most notably for cross compilation may use cpu-os
613     # prefixes. We need to be more specific about the LLVM target on Mac
614     # so cross-language LTO will work correctly.
616     if os.startswith(("darwin", "ios")):
617         toolchain = "%s-apple-%s" % (cpu, os)
618     else:
619         toolchain = "%s-%s" % (cpu, os)
621     return namespace(
622         alias=triplet,
623         cpu=CPU(canonical_cpu),
624         bitness=CPU_bitness[canonical_cpu],
625         kernel=Kernel(canonical_kernel),
626         os=OS(canonical_os),
627         endianness=Endianness(endianness),
628         # For now, only report the Windows ABI.
629         abi=abi and Abi(abi),
630         raw_cpu=cpu,
631         raw_os=raw_os,
632         toolchain=toolchain,
633         vendor=vendor,
634         sub_configure_alias=sub_configure_alias,
635     )
638 # This defines a fake target/host namespace for when running with --help
639 # If either --host or --target is passed on the command line, then fall
640 # back to the real deal.
641 @depends("--help", "--host", "--target")
642 def help_host_target(help, host, target):
643     if help and not host and not target:
644         return namespace(
645             alias="unknown-unknown-unknown",
646             cpu="unknown",
647             bitness="unknown",
648             kernel="unknown",
649             os="unknown",
650             endianness="unknown",
651             abi="unknown",
652             raw_cpu="unknown",
653             raw_os="unknown",
654             toolchain="unknown-unknown",
655         )
658 def config_sub(shell, triplet):
659     config_sub = os.path.join(os.path.dirname(__file__), "..", "autoconf", "config.sub")
660     # Config.sub doesn't like the *-windows-msvc/*-windows-gnu/*-ios-sim triplets, so
661     # munge those before and after calling config.sub.
662     suffix = None
663     munging = {
664         "-windows-msvc": "-mingw32",
665         "-windows-gnu": "-mingw32",
666         "-ios-sim": "-ios",
667     }
668     for check_suffix, replacement in munging.items():
669         if triplet.endswith(check_suffix):
670             suffix = check_suffix
671             triplet = triplet[: -len(suffix)] + replacement
672             break
673     result = check_cmd_output(shell, config_sub, triplet).strip()
674     if suffix:
675         assert result.endswith(replacement)
676         result = result[: -len(replacement)] + suffix
677     return result
680 @depends("--host", shell)
681 @checking("for host system type", lambda h: h.alias)
682 @imports("os")
683 @imports("sys")
684 @imports(_from="__builtin__", _import="ValueError")
685 def real_host(value, shell):
686     if not value and sys.platform == "win32":
687         arch = os.environ.get("PROCESSOR_ARCHITEW6432") or os.environ.get(
688             "PROCESSOR_ARCHITECTURE"
689         )
690         if arch == "AMD64":
691             return split_triplet("x86_64-pc-windows-msvc")
692         elif arch == "x86":
693             return split_triplet("i686-pc-windows-msvc")
695     if not value:
696         config_guess = os.path.join(
697             os.path.dirname(__file__), "..", "autoconf", "config.guess"
698         )
700         # Ensure that config.guess is determining the host triplet, not the target
701         # triplet
702         env = os.environ.copy()
703         env.pop("CC_FOR_BUILD", None)
704         env.pop("HOST_CC", None)
705         env.pop("CC", None)
707         host = check_cmd_output(shell, config_guess, env=env).strip()
708         try:
709             return split_triplet(host)
710         except ValueError:
711             pass
712     else:
713         host = value[0]
715     host = config_sub(shell, host)
717     try:
718         return split_triplet(host)
719     except ValueError as e:
720         die(e)
723 host = help_host_target | real_host
726 @depends("--target", real_host, shell, "--enable-project", "--enable-application")
727 @checking("for target system type", lambda t: t.alias)
728 @imports(_from="__builtin__", _import="ValueError")
729 def real_target(value, host, shell, project, application):
730     # Because --enable-project is implied by --enable-application, and
731     # implied options are not currently handled during --help, which is
732     # used get the build target in mozbuild.base, we manually check
733     # whether --enable-application was given, and fall back to
734     # --enable-project if not. Both can't be given contradictory values
735     # under normal circumstances, so it's fine.
736     if application:
737         project = application[0]
738     elif project:
739         project = project[0]
740     if not value:
741         if project == "mobile/android":
742             target_cpu, target_system = {
743                 "aarch64": ("aarch64", "android"),
744                 "x86_64": ("x86_64", "android"),
745             }.get(host.cpu, ("arm", "androideabi"))
746             return split_triplet(f"{target_cpu}-unknown-linux-{target_system}")
747         if project == "mobile/ios":
748             return split_triplet("aarch64-apple-ios")
749         return host
750     # If --target was only given a cpu arch, expand it with the
751     # non-cpu part of the host. For mobile/android, expand it with
752     # unknown-linux-android.
753     target = value[0]
754     if "-" not in target:
755         if project == "mobile/android":
756             rest = "unknown-linux-android"
757             if target.startswith("arm"):
758                 rest += "eabi"
759         elif project == "mobile/ios":
760             rest = "apple-ios"
761         else:
762             cpu, rest = host.alias.split("-", 1)
763         target = "-".join((target, rest))
764         try:
765             return split_triplet(target)
766         except ValueError:
767             pass
769     try:
770         return split_triplet(config_sub(shell, target), allow_wasi=(project == "js"))
771     except ValueError as e:
772         die(e)
775 target = help_host_target | real_target
778 @depends(host, target)
779 @checking("whether cross compiling")
780 def cross_compiling(host, target):
781     return host != target
784 set_config("CROSS_COMPILE", cross_compiling)
785 set_define("CROSS_COMPILE", cross_compiling)
788 @depends(target)
789 def have_64_bit(target):
790     if target.bitness == 64:
791         return True
794 set_config("HAVE_64BIT_BUILD", have_64_bit)
795 set_define("HAVE_64BIT_BUILD", have_64_bit)
796 add_old_configure_assignment("HAVE_64BIT_BUILD", have_64_bit)
798 # Some third-party code bases depend on this being set for big-endians.
799 set_define(
800     "WORDS_BIGENDIAN", True, when=depends(target.endianness)(lambda e: e == "big")
804 # Autoconf needs these set
807 @depends(host)
808 def host_for_sub_configure(host):
809     return "--host=%s" % host.sub_configure_alias
812 @depends(target)
813 def target_for_sub_configure(target):
814     return "--target=%s" % target.sub_configure_alias
817 # These variables are for compatibility with the current moz.builds and
818 # old-configure. Eventually, we'll want to canonicalize better.
819 @depends(target)
820 def target_variables(target):
821     if target.kernel == "kFreeBSD":
822         os_target = "GNU/kFreeBSD"
823         os_arch = "GNU_kFreeBSD"
824     elif target.kernel == "Darwin" or (target.kernel == "Linux" and target.os == "GNU"):
825         os_target = target.kernel
826         os_arch = target.kernel
827     else:
828         os_target = target.os
829         os_arch = target.kernel
831     return namespace(
832         OS_TARGET=str(os_target),
833         OS_ARCH=str(os_arch),
834         INTEL_ARCHITECTURE=target.cpu in ("x86", "x86_64") or None,
835     )
838 set_config("OS_TARGET", target_variables.OS_TARGET)
839 add_old_configure_assignment("OS_TARGET", target_variables.OS_TARGET)
840 set_config("OS_ARCH", target_variables.OS_ARCH)
841 add_old_configure_assignment("OS_ARCH", target_variables.OS_ARCH)
842 obsolete_config("CPU_ARCH", replacement="TARGET_CPU")
843 set_config("INTEL_ARCHITECTURE", target_variables.INTEL_ARCHITECTURE)
844 set_config("TARGET_CPU", target.cpu)
845 add_old_configure_assignment("TARGET_CPU", target.cpu)
846 set_config("TARGET_RAW_CPU", target.raw_cpu)
847 set_config("TARGET_OS", target.os)
848 set_config("TARGET_RAW_OS", target.raw_os)
849 set_config("TARGET_KERNEL", target.kernel)
850 set_config("TARGET_ENDIANNESS", target.endianness)
853 @depends(host)
854 def host_variables(host):
855     if host.kernel == "kFreeBSD":
856         os_arch = "GNU_kFreeBSD"
857     else:
858         os_arch = host.kernel
859     return namespace(
860         HOST_OS_ARCH=os_arch,
861     )
864 set_config("HOST_CPU_ARCH", host.cpu)
865 set_config("HOST_OS_ARCH", host_variables.HOST_OS_ARCH)
866 add_old_configure_assignment("HOST_OS_ARCH", host_variables.HOST_OS_ARCH)
867 set_config("HOST_ALIAS", host.alias)
870 @depends(target)
871 def target_is_windows(target):
872     if target.kernel == "WINNT":
873         return True
876 @depends(host)
877 def host_is_windows(host):
878     if host.kernel == "WINNT":
879         return True
882 set_define("_WINDOWS", target_is_windows)
883 set_define("WIN32", target_is_windows)
884 set_define("XP_WIN", target_is_windows)
887 @depends(target)
888 def target_is_unix(target):
889     if target.kernel != "WINNT":
890         return True
893 set_define("XP_UNIX", target_is_unix)
896 @depends(target)
897 def target_is_darwin(target):
898     if target.kernel == "Darwin":
899         return True
902 set_define("XP_DARWIN", target_is_darwin)
905 @depends(target)
906 def target_is_osx(target):
907     if target.kernel == "Darwin" and target.os == "OSX":
908         return True
911 @depends(host)
912 def host_is_osx(host):
913     if host.os == "OSX":
914         return True
917 set_define("XP_MACOSX", target_is_osx)
920 @depends(target)
921 def target_is_ios(target):
922     if target.kernel == "Darwin" and target.os == "iOS":
923         return True
926 set_define("XP_IOS", target_is_ios)
929 @depends(target)
930 def target_has_linux_kernel(target):
931     if target.kernel == "Linux":
932         return True
935 set_define("XP_LINUX", target_has_linux_kernel)
938 @depends(target)
939 def target_is_linux_or_wasi(target):
940     if (target.kernel == "Linux" and target.os == "GNU") or target.kernel == "WASI":
941         return True
944 @depends(target)
945 def target_is_android(target):
946     if target.os == "Android":
947         return True
950 set_define("ANDROID", target_is_android)
953 @depends(target)
954 def target_is_openbsd(target):
955     if target.kernel == "OpenBSD":
956         return True
959 set_define("XP_OPENBSD", target_is_openbsd)
962 @depends(target)
963 def target_is_netbsd(target):
964     if target.kernel == "NetBSD":
965         return True
968 set_define("XP_NETBSD", target_is_netbsd)
971 @depends(target)
972 def target_is_freebsd(target):
973     if target.kernel == "FreeBSD":
974         return True
977 set_define("XP_FREEBSD", target_is_freebsd)
980 @depends(target)
981 def target_is_solaris(target):
982     if target.kernel == "SunOS":
983         return True
986 set_define("XP_SOLARIS", target_is_solaris)
989 @depends(target)
990 def target_is_sparc(target):
991     if target.cpu == "sparc64":
992         return True
995 set_define("SPARC64", target_is_sparc)
998 @depends(target, when=target_is_android)
999 def android_cpu_arch(target):
1000     d = {
1001         "aarch64": "arm64-v8a",
1002         "arm": "armeabi-v7a",
1003         "x86": "x86",
1004         "x86_64": "x86_64",
1005     }
1006     if target.cpu not in d:
1007         die(f"Cannot determine android_cpu_arch: unknown target.cpu: {target.cpu}")
1008     return d[target.cpu]
1011 set_config("ANDROID_CPU_ARCH", android_cpu_arch)
1014 @depends("--enable-project", build_environment, "--help")
1015 @imports(_from="os.path", _import="exists")
1016 def include_project_configure(project, build_env, help):
1017     if not project:
1018         die("--enable-project is required.")
1020     base_dir = build_env.topsrcdir
1021     path = os.path.join(base_dir, project[0], "moz.configure")
1022     if not exists(path):
1023         die("Cannot find project %s", project[0])
1024     return path
1027 @depends("--enable-project")
1028 def build_project(project):
1029     return project[0]
1032 set_config("MOZ_BUILD_APP", build_project)
1033 set_define("MOZ_BUILD_APP", build_project)
1034 add_old_configure_assignment("MOZ_BUILD_APP", build_project)
1037 option(env="MOZILLA_OFFICIAL", help="Build an official release")
1040 @depends("MOZILLA_OFFICIAL")
1041 def mozilla_official(official):
1042     if official:
1043         return True
1046 set_config("MOZILLA_OFFICIAL", mozilla_official)
1047 set_define("MOZILLA_OFFICIAL", mozilla_official)
1049 # Allow specifying custom paths to the version files used by the milestone() function below.
1050 option(
1051     "--with-version-file-path",
1052     nargs=1,
1053     help="Specify a custom path to app version files instead of auto-detecting",
1054     default=None,
1058 @depends("--with-version-file-path")
1059 def version_path(path):
1060     return path
1063 # Allow to easily build nightly with a release / beta configuration so that we
1064 # can have the same options we'd have on a release version.
1065 # This is useful for performance profiling, as there are things that we don't
1066 # enable in release (like the background hang monitor) that can affect
1067 # performance.
1068 option(
1069     "--as-milestone",
1070     help="Build with another milestone configuration (e.g., as release)",
1071     choices=("early-beta", "late-beta", "release"),
1072     default=None,
1076 # set RELEASE_OR_BETA and NIGHTLY_BUILD variables depending on the cycle we're in
1077 # The logic works like this:
1078 # - if we have "a1" in GRE_MILESTONE, we're building Nightly (define NIGHTLY_BUILD)
1079 # - otherwise, if we have "a" in GRE_MILESTONE, we're building Nightly or Aurora
1080 # - otherwise, we're building Release/Beta (define RELEASE_OR_BETA)
1081 @depends(
1082     build_environment,
1083     build_project,
1084     version_path,
1085     "--as-milestone",
1086     "--help",
1088 @imports(_from="__builtin__", _import="open")
1089 @imports("os")
1090 @imports("re")
1091 def milestone(build_env, build_project, version_path, as_milestone, _):
1092     versions = []
1093     paths = ["config/milestone.txt"]
1094     if build_project == "js":
1095         paths = paths * 3
1096     else:
1097         paths += [
1098             "browser/config/version.txt",
1099             "browser/config/version_display.txt",
1100         ]
1101     if version_path:
1102         version_path = version_path[0]
1103     else:
1104         version_path = os.path.join(build_project, "config")
1105     for f in ("version.txt", "version_display.txt"):
1106         f = os.path.join(version_path, f)
1107         if not os.path.exists(os.path.join(build_env.topsrcdir, f)):
1108             break
1109         paths.append(f)
1111     for p in paths:
1112         with open(os.path.join(build_env.topsrcdir, p), "r") as fh:
1113             content = fh.read().splitlines()
1114             if not content:
1115                 die("Could not find a version number in {}".format(p))
1116             versions.append(content[-1])
1118     is_early_beta_or_earlier = None
1119     if as_milestone:
1120         if "a1" not in versions[0]:
1121             # We could make this work with some effort
1122             die("--as-milestone only works on nightly builds")
1123         as_milestone = as_milestone[0]
1124         as_milestone_flag = "" if as_milestone == "release" else "b1"
1125         versions = [v.replace("a1", as_milestone_flag) for v in versions]
1126         if as_milestone == "early-beta":
1127             is_early_beta_or_earlier = True
1129     milestone, firefox_version, firefox_version_display = versions[:3]
1131     # version.txt content from the project directory if there is one, otherwise
1132     # the firefox version.
1133     app_version = versions[3] if len(versions) > 3 else firefox_version
1134     # version_display.txt content from the project directory if there is one,
1135     # otherwise version.txt content from the project directory, otherwise the
1136     # firefox version for display.
1137     app_version_display = versions[-1] if len(versions) > 3 else firefox_version_display
1139     is_nightly = is_release_or_beta = None
1141     if "a1" in milestone:
1142         is_nightly = True
1143     elif "a" not in milestone:
1144         is_release_or_beta = True
1146     major_version = milestone.split(".")[0]
1147     m = re.search(r"([ab]\d+)", milestone)
1148     ab_patch = m.group(1) if m else ""
1150     if not as_milestone:
1151         defines = os.path.join(build_env.topsrcdir, "build", "defines.sh")
1152         with open(defines, "r") as fh:
1153             for line in fh.read().splitlines():
1154                 line = line.strip()
1155                 if not line or line.startswith("#"):
1156                     continue
1157                 name, _, value = line.partition("=")
1158                 name = name.strip()
1159                 value = value.strip()
1160                 if name != "EARLY_BETA_OR_EARLIER":
1161                     die(
1162                         "Only the EARLY_BETA_OR_EARLIER variable can be set in build/defines.sh"
1163                     )
1164                 if value and any(x in app_version_display for x in "ab"):
1165                     is_early_beta_or_earlier = True
1167     # Only expose the major version milestone in the UA string and hide the
1168     # patch leve (bugs 572659 and 870868).
1169     #
1170     # Only expose major milestone and alpha version in the symbolversion
1171     # string; as the name suggests, we use it for symbol versioning on Linux.
1172     return namespace(
1173         version=milestone,
1174         uaversion="%s.0" % major_version,
1175         symbolversion="%s%s" % (major_version, ab_patch),
1176         is_nightly=is_nightly,
1177         is_release_or_beta=is_release_or_beta,
1178         is_early_beta_or_earlier=is_early_beta_or_earlier,
1179         is_esr=app_version_display.endswith("esr") or None,
1180         app_version=app_version,
1181         app_version_display=app_version_display,
1182     )
1185 set_config("GRE_MILESTONE", milestone.version)
1186 set_config("NIGHTLY_BUILD", milestone.is_nightly)
1187 set_define("NIGHTLY_BUILD", milestone.is_nightly)
1188 set_config("RELEASE_OR_BETA", milestone.is_release_or_beta)
1189 set_define("RELEASE_OR_BETA", milestone.is_release_or_beta)
1190 set_config("MOZ_ESR", milestone.is_esr)
1191 set_define("MOZ_ESR", milestone.is_esr)
1192 set_config("EARLY_BETA_OR_EARLIER", milestone.is_early_beta_or_earlier)
1193 set_define("EARLY_BETA_OR_EARLIER", milestone.is_early_beta_or_earlier)
1194 set_define("MOZILLA_VERSION", depends(milestone)(lambda m: '"%s"' % m.version))
1195 set_config("MOZILLA_VERSION", milestone.version)
1196 set_define("MOZILLA_VERSION_U", milestone.version)
1197 set_define("MOZILLA_UAVERSION", depends(milestone)(lambda m: '"%s"' % m.uaversion))
1198 set_config("MOZILLA_SYMBOLVERSION", milestone.symbolversion)
1199 # JS configure still want to look at this one.
1200 add_old_configure_assignment("MOZILLA_SYMBOLVERSION", milestone.symbolversion)
1202 set_config("MOZ_APP_VERSION", milestone.app_version)
1203 set_config("MOZ_APP_VERSION_DISPLAY", milestone.app_version_display)
1204 add_old_configure_assignment("MOZ_APP_VERSION", milestone.app_version)
1206 add_old_configure_assignment("NIGHTLY_BUILD", milestone.is_nightly)
1208 # The app update channel is 'default' when not supplied. The value is used in
1209 # the application's confvars.sh (and is made available to a project specific
1210 # moz.configure).
1211 option(
1212     "--enable-update-channel",
1213     nargs=1,
1214     help="Select application update channel",
1215     default="default",
1219 @depends("--enable-update-channel")
1220 def update_channel(channel):
1221     if not channel or channel[0] == "":
1222         return "default"
1223     return channel[0].lower()
1226 set_config("MOZ_UPDATE_CHANNEL", update_channel)
1227 set_define("MOZ_UPDATE_CHANNEL", update_channel)
1228 add_old_configure_assignment("MOZ_UPDATE_CHANNEL", update_channel)
1231 option(
1232     env="MOZBUILD_STATE_PATH",
1233     nargs=1,
1234     help="Path to a persistent state directory for the build system "
1235     "and related tools",
1239 @depends("MOZBUILD_STATE_PATH", "--help")
1240 @imports("os")
1241 def mozbuild_state_path(path, _):
1242     if path:
1243         return normalize_path(path[0])
1244     return normalize_path(os.path.expanduser(os.path.join("~", ".mozbuild")))
1247 @depends("MOZILLABUILD", shell, host_is_windows)
1248 @imports(_from="pathlib", _import="Path")
1249 def mozillabuild_bin_paths(mozillabuild, shell, host_is_windows):
1250     paths = []
1251     if not mozillabuild or not host_is_windows:
1252         return paths
1253     paths.append(os.path.dirname(shell))
1254     paths.append(str(Path(mozillabuild[0]) / "bin"))
1255     return paths
1258 @depends(mozillabuild_bin_paths)
1259 @imports("os")
1260 def prefer_mozillabuild_path(mozillabuild_bin_paths):
1261     return mozillabuild_bin_paths + os.environ["PATH"].split(os.pathsep)
1264 # milestone.is_nightly corresponds to cases NIGHTLY_BUILD is set.
1267 @depends(milestone)
1268 def enabled_in_nightly(milestone):
1269     return milestone.is_nightly
1272 # Check if we need the 32-bit Linux SSE2 error dialog
1273 # ===================================================
1274 option(
1275     env="MOZ_LINUX_32_SSE2_STARTUP_ERROR",
1276     help="Add code to perform startup checks to warn if SSE2 is not available",
1277     default=depends("MOZ_AUTOMATION")(lambda x: bool(x)),
1278     when=target_is_unix & depends(target)(lambda target: target.cpu == "x86"),
1280 set_config(
1281     "MOZ_LINUX_32_SSE2_STARTUP_ERROR", True, when="MOZ_LINUX_32_SSE2_STARTUP_ERROR"
1285 # Branding
1286 # ==============================================================
1287 option(
1288     "--with-app-basename",
1289     env="MOZ_APP_BASENAME",
1290     nargs=1,
1291     help="Typically stays consistent for multiple branded versions of a "
1292     'given application (e.g. Aurora and Firefox both use "Firefox"), but '
1293     "may vary for full rebrandings (e.g. Iceweasel). Used for "
1294     'application.ini\'s "Name" field, which controls profile location in '
1295     'the absence of a "Profile" field (see below), and various system '
1296     "integration hooks (Unix remoting, Windows MessageWindow name, etc.",
1300 @depends("--with-app-basename", target_is_android)
1301 def moz_app_basename(value, target_is_android):
1302     if value:
1303         return value[0]
1304     if target_is_android:
1305         return "Fennec"
1306     return "Firefox"
1309 set_config(
1310     "MOZ_APP_BASENAME",
1311     moz_app_basename,
1312     when=depends(build_project)(lambda p: p != "js"),
1316 @depends("MOZILLABUILD", when=host_is_windows)
1317 @checking("for MozillaBuild directory")
1318 @imports("os")
1319 def mozillabuild_dir(mozillabuild):
1320     if mozillabuild and os.path.exists(mozillabuild[0]):
1321         return mozillabuild[0]
1324 @depends_if(mozillabuild_dir)
1325 @checking("for MozillaBuild version")
1326 @imports(_from="__builtin__", _import="Exception")
1327 @imports(_from="__builtin__", _import="open")
1328 def mozillabuild_version(mozillabuild_dir):
1329     try:
1330         with open(os.path.join(mozillabuild_dir, "VERSION"), "r") as fh:
1331             return Version(fh.readline().strip())
1332     except Exception as e:
1333         die(e)
1336 @depends_if(mozillabuild_version)
1337 def valid_mozillabuild_version(mozillabuild_version):
1338     if mozillabuild_version < Version("4.0"):
1339         die(
1340             f"Please upgrade MozillaBuild. You can find a recent version at: https://wiki.mozilla.org/MozillaBuild"
1341         )