Bug 1665795 [wpt PR 25608] - Fix clear-window-name.https.html fail message, a=testonly
[gecko.git] / configure.py
blobbbad45a792b4e9278851c35afa1c2ab05ffc3f5c
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from __future__ import absolute_import, print_function, unicode_literals
7 import codecs
8 import io
9 import itertools
10 import logging
11 import os
12 import sys
13 import textwrap
16 try:
17 from collections.abc import Iterable
18 except ImportError:
19 from collections import Iterable
22 base_dir = os.path.abspath(os.path.dirname(__file__))
23 sys.path.insert(0, os.path.join(base_dir, 'python', 'mozboot'))
24 sys.path.insert(0, os.path.join(base_dir, 'python', 'mozbuild'))
25 sys.path.insert(0, os.path.join(base_dir, 'third_party', 'python', 'six'))
26 from mozbuild.configure import (
27 ConfigureSandbox,
28 TRACE,
30 from mozbuild.pythonutil import iter_modules_in_path
31 from mozbuild.backend.configenvironment import PartialConfigEnvironment
32 from mozbuild.util import (
33 write_indented_repr,
35 import mozpack.path as mozpath
36 import six
39 def main(argv):
40 config = {}
42 sandbox = ConfigureSandbox(config, os.environ, argv)
44 clobber_file = 'CLOBBER'
45 if not os.path.exists(clobber_file):
46 # Simply touch the file.
47 with open(clobber_file, 'a'):
48 pass
50 if os.environ.get('MOZ_CONFIGURE_TRACE'):
51 sandbox._logger.setLevel(TRACE)
53 sandbox.run(os.path.join(os.path.dirname(__file__), 'moz.configure'))
55 if sandbox._help:
56 return 0
58 return config_status(config)
61 def check_unicode(obj):
62 '''Recursively check that all strings in the object are unicode strings.'''
63 if isinstance(obj, dict):
64 result = True
65 for k, v in six.iteritems(obj):
66 if not check_unicode(k):
67 print("%s key is not unicode." % k, file=sys.stderr)
68 result = False
69 elif not check_unicode(v):
70 print("%s value is not unicode." % k, file=sys.stderr)
71 result = False
72 return result
73 if isinstance(obj, bytes):
74 return False
75 if isinstance(obj, six.text_type):
76 return True
77 if isinstance(obj, Iterable):
78 return all(check_unicode(o) for o in obj)
79 return True
82 def config_status(config):
83 # Sanitize config data to feed config.status
84 # Ideally, all the backend and frontend code would handle the booleans, but
85 # there are so many things involved, that it's easier to keep config.status
86 # untouched for now.
87 def sanitize_config(v):
88 if v is True:
89 return '1'
90 if v is False:
91 return ''
92 # Serialize types that look like lists and tuples as lists.
93 if not isinstance(v, (bytes, six.text_type, dict)) and isinstance(v, Iterable):
94 return list(v)
95 return v
97 sanitized_config = {}
98 sanitized_config['substs'] = {
99 k: sanitize_config(v) for k, v in six.iteritems(config)
100 if k not in ('DEFINES', 'TOPSRCDIR', 'TOPOBJDIR', 'CONFIG_STATUS_DEPS')
102 sanitized_config['defines'] = {
103 k: sanitize_config(v) for k, v in six.iteritems(config['DEFINES'])
105 sanitized_config['topsrcdir'] = config['TOPSRCDIR']
106 sanitized_config['topobjdir'] = config['TOPOBJDIR']
107 sanitized_config['mozconfig'] = config.get('MOZCONFIG')
109 if not check_unicode(sanitized_config):
110 print("Configuration should be all unicode.", file=sys.stderr)
111 print("Please file a bug for the above.", file=sys.stderr)
112 sys.exit(1)
114 # Create config.status. Eventually, we'll want to just do the work it does
115 # here, when we're able to skip configure tests/use cached results/not rely
116 # on autoconf.
117 logging.getLogger('moz.configure').info('Creating config.status')
118 with codecs.open('config.status', 'w', 'utf-8') as fh:
119 fh.write(textwrap.dedent('''\
120 #!%(python)s
121 # coding=utf-8
122 from __future__ import unicode_literals
123 ''') % {'python': config['PYTHON3']})
124 for k, v in sorted(six.iteritems(sanitized_config)):
125 fh.write('%s = ' % k)
126 write_indented_repr(fh, v)
127 fh.write("__all__ = ['topobjdir', 'topsrcdir', 'defines', "
128 "'substs', 'mozconfig']")
130 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
131 fh.write(textwrap.dedent('''
132 if __name__ == '__main__':
133 from mozbuild.util import patch_main
134 patch_main()
135 from mozbuild.config_status import config_status
136 args = dict([(name, globals()[name]) for name in __all__])
137 config_status(**args)
138 '''))
140 partial_config = PartialConfigEnvironment(config['TOPOBJDIR'])
141 partial_config.write_vars(sanitized_config)
143 # Write out a file so the build backend knows to re-run configure when
144 # relevant Python changes.
145 with io.open('config_status_deps.in', 'w', encoding='utf-8',
146 newline='\n') as fh:
147 for f in sorted(
148 itertools.chain(config['CONFIG_STATUS_DEPS'],
149 iter_modules_in_path(config['TOPOBJDIR'],
150 config['TOPSRCDIR']))):
151 fh.write('%s\n' % mozpath.normpath(f))
153 # Other things than us are going to run this file, so we need to give it
154 # executable permissions.
155 os.chmod('config.status', 0o755)
156 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
157 from mozbuild.config_status import config_status
159 # Some values in sanitized_config also have more complex types, such as
160 # EnumString, which using when calling config_status would currently
161 # break the build, as well as making it inconsistent with re-running
162 # config.status, for which they are normalized to plain strings via
163 # indented_repr. Likewise for non-dict non-string iterables being
164 # converted to lists.
165 def normalize(obj):
166 if isinstance(obj, dict):
167 return {
168 k: normalize(v)
169 for k, v in six.iteritems(obj)
171 if isinstance(obj, six.text_type):
172 return six.text_type(obj)
173 if isinstance(obj, Iterable):
174 return [normalize(o) for o in obj]
175 return obj
176 return config_status(args=[], **normalize(sanitized_config))
177 return 0
180 if __name__ == '__main__':
181 sys.exit(main(sys.argv))