Bug 1645920 [wpt PR 24157] - [AspectRatio] Make intrinsic sizes respect aspect-ratio...
[gecko.git] / configure.py
blobfe55a35810dde536da4462572bf892554945a986
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', 'non_global_defines', 'TOPSRCDIR', 'TOPOBJDIR',
101 'CONFIG_STATUS_DEPS')
103 sanitized_config['defines'] = {
104 k: sanitize_config(v) for k, v in six.iteritems(config['DEFINES'])
106 sanitized_config['non_global_defines'] = config['non_global_defines']
107 sanitized_config['topsrcdir'] = config['TOPSRCDIR']
108 sanitized_config['topobjdir'] = config['TOPOBJDIR']
109 sanitized_config['mozconfig'] = config.get('MOZCONFIG')
111 if not check_unicode(sanitized_config):
112 print("Configuration should be all unicode.", file=sys.stderr)
113 print("Please file a bug for the above.", file=sys.stderr)
114 sys.exit(1)
116 # Create config.status. Eventually, we'll want to just do the work it does
117 # here, when we're able to skip configure tests/use cached results/not rely
118 # on autoconf.
119 logging.getLogger('moz.configure').info('Creating config.status')
120 with codecs.open('config.status', 'w', 'utf-8') as fh:
121 fh.write(textwrap.dedent('''\
122 #!%(python)s
123 # coding=utf-8
124 from __future__ import unicode_literals
125 ''') % {'python': config['PYTHON3']})
126 for k, v in sorted(six.iteritems(sanitized_config)):
127 fh.write('%s = ' % k)
128 write_indented_repr(fh, v)
129 fh.write("__all__ = ['topobjdir', 'topsrcdir', 'defines', "
130 "'non_global_defines', 'substs', 'mozconfig']")
132 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
133 fh.write(textwrap.dedent('''
134 if __name__ == '__main__':
135 from mozbuild.util import patch_main
136 patch_main()
137 from mozbuild.config_status import config_status
138 args = dict([(name, globals()[name]) for name in __all__])
139 config_status(**args)
140 '''))
142 partial_config = PartialConfigEnvironment(config['TOPOBJDIR'])
143 partial_config.write_vars(sanitized_config)
145 # Write out a file so the build backend knows to re-run configure when
146 # relevant Python changes.
147 with io.open('config_status_deps.in', 'w', encoding='utf-8',
148 newline='\n') as fh:
149 for f in itertools.chain(config['CONFIG_STATUS_DEPS'],
150 iter_modules_in_path(config['TOPOBJDIR'],
151 config['TOPSRCDIR'])):
152 fh.write('%s\n' % mozpath.normpath(f))
154 # Other things than us are going to run this file, so we need to give it
155 # executable permissions.
156 os.chmod('config.status', 0o755)
157 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
158 from mozbuild.config_status import config_status
160 # Some values in sanitized_config also have more complex types, such as
161 # EnumString, which using when calling config_status would currently
162 # break the build, as well as making it inconsistent with re-running
163 # config.status, for which they are normalized to plain strings via
164 # indented_repr. Likewise for non-dict non-string iterables being
165 # converted to lists.
166 def normalize(obj):
167 if isinstance(obj, dict):
168 return {
169 k: normalize(v)
170 for k, v in six.iteritems(obj)
172 if isinstance(obj, six.text_type):
173 return six.text_type(obj)
174 if isinstance(obj, Iterable):
175 return [normalize(o) for o in obj]
176 return obj
177 return config_status(args=[], **normalize(sanitized_config))
178 return 0
181 if __name__ == '__main__':
182 sys.exit(main(sys.argv))