Bug 1837620 - Part 1.5: Rename IC flag to 'mayHaveFoldedStub' to make it clear that...
[gecko.git] / build / moz.configure / old.configure
blobba213b33b4385cb3f2e784c76bd738cadd2efe58
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 m4 = check_prog(
9     "M4",
10     (
11         "gm4",
12         "m4",
13     ),
14     paths=prefer_mozillabuild_path,
18 @depends(mozconfig)
19 def prepare_mozconfig(mozconfig):
20     if mozconfig["path"]:
21         items = {}
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)
29         return items
32 @depends("OLD_CONFIGURE", build_project)
33 def old_configure(old_configure, build_project):
34     if not old_configure:
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
40     # directory.
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)
47             )
48     return 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:")
59         def inject(command):
60             print(command, file=out)  # noqa Python 2vs3
61             log.debug("| %s", command)
63         if mozconfig:
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 ") :]))
68                 else:
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)))
77 @template
78 def old_configure_options(*options):
79     for opt in options:
80         option(opt, nargs="*", help="Help missing for old configure options")
82     @dependable
83     def all_options():
84         return list(options)
86     return depends(
87         host_for_sub_configure, target_for_sub_configure, all_options, *options
88     )
91 @old_configure_options(
92     "--cache-file",
93     "--datadir",
94     "--enable-official-branding",
95     "--includedir",
96     "--libdir",
97     "--prefix",
98     "--with-branding",
99     "--with-distribution-id",
100     "--with-macbundlename-prefix",
101     "--x-includes",
102     "--x-libraries",
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.
108     options = [
109         value.format(name)
110         for name, value in zip(all_options, options)
111         if value.origin != "default"
112     ] + [host, target]
114     return namespace(options=options, all_options=all_options)
117 @template
118 def old_configure_for(old_configure_path, extra_env=None):
119     if extra_env is None:
120         extra_env = dependable(None)
122     @depends(
123         prepare_configure,
124         prepare_configure_options,
125         prefer_mozillabuild_path,
126         altered_path,
127         extra_env,
128         build_environment,
129         old_configure_path,
130         awk,
131         m4,
132         shell,
133     )
134     @imports(_from="__builtin__", _import="compile")
135     @imports(_from="__builtin__", _import="open")
136     @imports(_from="__builtin__", _import="OSError")
137     @imports("glob")
138     @imports("itertools")
139     @imports("logging")
140     @imports("os")
141     @imports("subprocess")
142     @imports("sys")
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")
148     def old_configure(
149         prepare_configure,
150         prepare_configure_options,
151         prefer_mozillabuild_path,
152         altered_path,
153         extra_env,
154         build_env,
155         old_configure,
156         awk,
157         m4,
158         shell,
159     ):
160         # Use prepare_configure to make lint happy
161         prepare_configure
163         if altered_path:
164             path = altered_path
165         else:
166             path = os.pathsep.join(prefer_mozillabuild_path)
168         refresh = True
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(
173                 (
174                     old_configure + ".in",
175                     os.path.join(os.path.dirname(old_configure), "aclocal.m4"),
176                 ),
177                 glob.iglob(aclocal),
178             ):
179                 if os.path.getmtime(input) > mtime:
180                     break
181             else:
182                 refresh = False
184         if refresh:
185             autoconf = os.path.join(
186                 build_env.topsrcdir, "build", "autoconf", "autoconf.sh"
187             )
188             log.info("Refreshing %s with %s", old_configure, autoconf)
189             env = dict(os.environ)
190             env["M4"] = m4
191             env["AWK"] = awk
192             env["AC_MACRODIR"] = os.path.join(build_env.topsrcdir, "build", "autoconf")
193             env["PATH"] = path
195             try:
196                 script = subprocess.check_output(
197                     [
198                         shell,
199                         autoconf,
200                         "--localdir=%s" % os.path.dirname(old_configure),
201                         old_configure + ".in",
202                     ],
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),
207                     env=env,
208                 )
209             except CalledProcessError as exc:
210                 die("autoconf exited with return code {}".format(exc.returncode))
212             if not script:
213                 die(
214                     "Generated old-configure is empty! Check that your autoconf 2.13 program works!"
215                 )
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(
222                 mode="wb",
223                 prefix=os.path.basename(old_configure),
224                 dir=os.path.dirname(old_configure),
225                 delete=False,
226             ) as fh:
227                 fh.write(script)
229             try:
230                 os.rename(fh.name, old_configure)
231             except OSError:
232                 try:
233                     # Likely the file already existed (on Windows). Retry after removing it.
234                     os.remove(old_configure)
235                     os.rename(fh.name, old_configure)
236                 except OSError as e:
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")
249         config_log = None
250         for handler in logger.handlers:
251             if isinstance(handler, logging.FileHandler):
252                 config_log = handler
253                 config_log.close()
254                 logger.removeHandler(config_log)
255                 env["CONFIG_LOG"] = config_log.baseFilename
256                 log_size = os.path.getsize(config_log.baseFilename)
257                 break
259         env["PATH"] = path
261         if extra_env:
262             env.update(extra_env)
264         env["OLD_CONFIGURE_VARS"] = os.path.join(
265             build_env.topobjdir, "old-configure.vars"
266         )
267         proc = subprocess.Popen(
268             cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env
269         )
270         while True:
271             line = proc.stdout.readline()
272             if not line:
273                 break
274             log.info(line.rstrip())
276         ret = proc.wait()
277         if ret:
278             with log.queue_debug():
279                 if config_log:
280                     with open(config_log.baseFilename, "r") as fh:
281                         fh.seek(log_size)
282                         for line in fh:
283                             log.debug(line.rstrip())
284                 log.error("old-configure failed")
285             sys.exit(ret)
287         if config_log:
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)
293         raw_config = {
294             "split": split,
295             "unique_list": unique_list,
296         }
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:
306                 die(
307                     "Missing option in `@old_configure_options` in %s: %s",
308                     __file__,
309                     flag,
310                 )
312         # If the code execution above fails, we want to keep the file around for
313         # debugging.
314         os.remove("config.data")
316         return namespace(
317             **{
318                 c: [
319                     (k[1:-1], v[1:-1] if isinstance(v, str) else v)
320                     for k, v in raw_config[c]
321                 ]
322                 for c in ("substs", "defines")
323             }
324         )
326     return old_configure
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.
340 @depends("--help")
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":
347             continue
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()