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/.
6
7
8@depends(check_build_environment)
9@imports("logging")
10@imports(_from="__builtin__", _import="object")
11@imports(_from="mozbuild.configure.util", _import="ConfigureOutputHandler")
12def old_js_configure(build_env):
13    class PrefixOutput(object):
14        def __init__(self, prefix, fh):
15            self._fh = fh
16            self._begin_line = True
17            self._prefix = prefix
18
19        def write(self, content):
20            if self._begin_line:
21                self._fh.write(self._prefix)
22            self._fh.write(("\n" + self._prefix).join(content.splitlines()))
23            self._begin_line = content.endswith("\n")
24            if self._begin_line:
25                self._fh.write("\n")
26
27        def flush(self):
28            self._fh.flush()
29
30    logger = logging.getLogger("moz.configure")
31    formatter = logging.Formatter("js/src> %(levelname)s: %(message)s")
32    for handler in logger.handlers:
33        handler.setFormatter(formatter)
34        if isinstance(handler, ConfigureOutputHandler):
35            handler._stdout = PrefixOutput("js/src> ", handler._stdout)
36    return os.path.join(build_env.topsrcdir, "js", "src", "old-configure")
37
38
39@depends(old_configure.substs, mozconfig)
40def old_js_configure_env(substs, mozconfig):
41    substs = dict(substs)
42    # Here, we mimic what we used to do from old-configure, which makes this
43    # all awkward.
44
45    # Variables that were explicitly exported from old-configure, and those
46    # explicitly set in the environment when invoking old-configure, were
47    # automatically inherited from subconfigure. We assume the relevant ones
48    # have a corresponding AC_SUBST in old-configure, making them available
49    # in `substs`.
50    extra_env = {}
51
52    for var in (
53        "MOZ_DEV_EDITION",
54        "STLPORT_LIBS",
55        "MOZ_LINKER",
56        "ZLIB_IN_MOZGLUE",
57        "RANLIB",
58    ):
59        if var in substs:
60            value = substs[var]
61        elif (
62            mozconfig
63            and var in mozconfig
64            and not mozconfig[var][1].startswith("removed")
65        ):
66            value = mozconfig[var][0]
67        else:
68            continue
69        if isinstance(value, list):
70            value = " ".join(value)
71        extra_env[var] = value
72
73    return extra_env
74
75
76old_js_configure = old_configure_for(old_js_configure, extra_env=old_js_configure_env)
77set_config("OLD_JS_CONFIGURE_SUBSTS", old_js_configure.substs)
78set_config("OLD_JS_CONFIGURE_DEFINES", old_js_configure.defines)
79
80
81@dependable
82@imports("logging")
83@imports(_from="mozbuild.configure.util", _import="ConfigureOutputHandler")
84def post_old_js_configure():
85    # Restore unprefixed logging.
86    formatter = logging.Formatter("%(levelname)s: %(message)s")
87    logger = logging.getLogger("moz.configure")
88    for handler in logger.handlers:
89        handler.setFormatter(formatter)
90        if isinstance(handler, ConfigureOutputHandler):
91            handler._stdout.flush()
92            handler._stdout = handler._stdout._fh
93