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/.
14 paths=prefer_mozillabuild_path,
19 def prepare_mozconfig(mozconfig):
22 for key, value in mozconfig["vars"]["added"].items():
23 items[key] = (value, "added")
24 for key, (old, value) in mozconfig["vars"]["modified"].items():
25 items[key] = (value, "modified")
26 for t in ("env", "vars"):
27 for key in mozconfig[t]["removed"].keys():
28 items[key] = (None, "removed " + t)
32 @depends("OLD_CONFIGURE", build_project)
33 def old_configure(old_configure, build_project):
35 die("The OLD_CONFIGURE environment variable must be set")
37 # os.path.abspath in the sandbox will ensure forward slashes on Windows,
38 # which is actually necessary because this path actually ends up literally
39 # as $0, and backslashes there breaks autoconf's detection of the source
41 old_configure = os.path.abspath(old_configure[0])
42 if build_project == "js":
43 old_configure_dir = os.path.dirname(old_configure)
44 if not old_configure_dir.endswith("/js/src"):
45 old_configure = os.path.join(
46 old_configure_dir, "js", "src", os.path.basename(old_configure)
51 @depends(prepare_mozconfig, old_configure_assignments)
52 @imports(_from="__builtin__", _import="open")
53 @imports(_from="__builtin__", _import="print")
54 @imports(_from="mozbuild.shellutil", _import="quote")
55 def prepare_configure(mozconfig, old_configure_assignments):
56 with open("old-configure.vars", "w") as out:
57 log.debug("Injecting the following to old-configure:")
60 print(command, file=out) # noqa Python 2vs3
61 log.debug("| %s", command)
64 inject("# start of mozconfig values")
65 for key, (value, action) in sorted(mozconfig.items()):
66 if action.startswith("removed "):
67 inject("unset %s # from %s" % (key, action[len("removed ") :]))
69 inject("%s=%s # %s" % (key, quote(value), action))
71 inject("# end of mozconfig values")
73 for k, v in old_configure_assignments:
74 inject("%s=%s" % (k, quote(v)))
78 def old_configure_options(*options):
80 option(opt, nargs="*", help="Help missing for old configure options")
87 host_for_sub_configure, target_for_sub_configure, all_options, *options
91 @old_configure_options(
94 "--enable-official-branding",
99 "--with-distribution-id",
100 "--with-macbundlename-prefix",
104 def prepare_configure_options(host, target, all_options, *options):
105 # old-configure only supports the options listed in @old_configure_options
106 # so we don't need to pass it every single option we've been passed. Only
107 # the ones that are not supported by python configure need to.
110 for name, value in zip(all_options, options)
111 if value.origin != "default"
114 return namespace(options=options, all_options=all_options)
118 def old_configure_for(old_configure_path, extra_env=None):
119 if extra_env is None:
120 extra_env = dependable(None)
124 prepare_configure_options,
125 prefer_mozillabuild_path,
134 @imports(_from="__builtin__", _import="compile")
135 @imports(_from="__builtin__", _import="open")
136 @imports(_from="__builtin__", _import="OSError")
138 @imports("itertools")
141 @imports("subprocess")
143 @imports(_from="mozbuild.shellutil", _import="quote")
144 @imports(_from="mozbuild.shellutil", _import="split")
145 @imports(_from="tempfile", _import="NamedTemporaryFile")
146 @imports(_from="subprocess", _import="CalledProcessError")
147 @imports(_from="__builtin__", _import="exec")
150 prepare_configure_options,
151 prefer_mozillabuild_path,
160 # Use prepare_configure to make lint happy
166 path = os.pathsep.join(prefer_mozillabuild_path)
169 if os.path.exists(old_configure):
170 mtime = os.path.getmtime(old_configure)
171 aclocal = os.path.join(build_env.topsrcdir, "build", "autoconf", "*.m4")
172 for input in itertools.chain(
174 old_configure + ".in",
175 os.path.join(os.path.dirname(old_configure), "aclocal.m4"),
179 if os.path.getmtime(input) > mtime:
185 autoconf = os.path.join(
186 build_env.topsrcdir, "build", "autoconf", "autoconf.sh"
188 log.info("Refreshing %s with %s", old_configure, autoconf)
189 env = dict(os.environ)
192 env["AC_MACRODIR"] = os.path.join(build_env.topsrcdir, "build", "autoconf")
196 script = subprocess.check_output(
200 "--localdir=%s" % os.path.dirname(old_configure),
201 old_configure + ".in",
203 # Fix the working directory, so that when m4 is called, that
204 # includes of relative paths are deterministically resolved
205 # relative to the directory containing old-configure.
206 cwd=os.path.dirname(old_configure),
209 except CalledProcessError as exc:
210 die("autoconf exited with return code {}".format(exc.returncode))
214 "Generated old-configure is empty! Check that your autoconf 2.13 program works!"
217 # Make old-configure append to config.log, where we put our own log.
218 # This could be done with a m4 macro, but it's way easier this way
219 script = script.replace(b">./config.log", b">>${CONFIG_LOG=./config.log}")
221 with NamedTemporaryFile(
223 prefix=os.path.basename(old_configure),
224 dir=os.path.dirname(old_configure),
230 os.rename(fh.name, old_configure)
233 # Likely the file already existed (on Windows). Retry after removing it.
234 os.remove(old_configure)
235 os.rename(fh.name, old_configure)
237 die("Failed re-creating old-configure: %s" % e.message)
239 cmd = [shell, old_configure] + prepare_configure_options.options
241 env = dict(os.environ)
243 # For debugging purpose, in case it's not what we'd expect.
244 log.debug("Running %s", quote(*cmd))
246 # Our logging goes to config.log, the same file old.configure uses.
247 # We can't share the handle on the file, so close it.
248 logger = logging.getLogger("moz.configure")
250 for handler in logger.handlers:
251 if isinstance(handler, logging.FileHandler):
254 logger.removeHandler(config_log)
255 env["CONFIG_LOG"] = config_log.baseFilename
256 log_size = os.path.getsize(config_log.baseFilename)
262 env.update(extra_env)
264 env["OLD_CONFIGURE_VARS"] = os.path.join(
265 build_env.topobjdir, "old-configure.vars"
267 proc = subprocess.Popen(
268 cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env
271 line = proc.stdout.readline()
274 log.info(line.rstrip())
278 with log.queue_debug():
280 with open(config_log.baseFilename, "r") as fh:
283 log.debug(line.rstrip())
284 log.error("old-configure failed")
288 # Create a new handler in append mode
289 handler = logging.FileHandler(config_log.baseFilename, mode="a", delay=True)
290 handler.setFormatter(config_log.formatter)
291 logger.addHandler(handler)
295 "unique_list": unique_list,
297 with open("config.data", "r") as fh:
298 code = compile(fh.read(), "config.data", "exec")
299 exec(code, raw_config)
301 # Ensure all the flags known to old-configure appear in the
302 # @old_configure_options above.
303 all_options = set(prepare_configure_options.all_options)
304 for flag in raw_config["flags"]:
305 if flag not in all_options:
307 "Missing option in `@old_configure_options` in %s: %s",
312 # If the code execution above fails, we want to keep the file around for
314 os.remove("config.data")
319 (k[1:-1], v[1:-1] if isinstance(v, str) else v)
320 for k, v in raw_config[c]
322 for c in ("substs", "defines")
329 old_configure = old_configure_for(old_configure)
330 set_config("OLD_CONFIGURE_SUBSTS", old_configure.substs)
331 set_config("OLD_CONFIGURE_DEFINES", old_configure.defines)
334 # Assuming no other option is declared after this function, handle the
335 # env options that were injected by mozconfig_options by creating dummy
336 # Option instances and having the sandbox's CommandLineHelper handle
337 # them. We only do so for options that haven't been declared so far,
338 # which should be a proxy for the options that old-configure handles
339 # and that we don't know anything about.
341 @imports("__sandbox__")
342 @imports(_from="mozbuild.configure.options", _import="Option")
343 def remaining_mozconfig_options(_):
344 helper = __sandbox__._helper
345 for arg in list(helper):
346 if helper._origins[arg] != "mozconfig":
348 name = arg.split("=", 1)[0]
349 if name.isupper() and name not in __sandbox__._options:
350 option = Option(env=name, nargs="*", help=name)
351 helper.handle(option)
354 # Please do not add anything after remaining_mozconfig_options()