Bug 1632310 [wpt PR 23186] - Add test for computed versus resolved style., a=testonly
[gecko.git] / configure.py
blobef85e246822a84d0e7f40d4be5c262e042df0719
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', 'mozbuild'))
24 sys.path.insert(0, os.path.join(base_dir, 'third_party', 'python', 'six'))
25 from mozbuild.configure import (
26 ConfigureSandbox,
27 TRACE,
29 from mozbuild.pythonutil import iter_modules_in_path
30 from mozbuild.backend.configenvironment import PartialConfigEnvironment
31 from mozbuild.util import (
32 write_indented_repr,
34 import mozpack.path as mozpath
35 import six
38 def main(argv):
39 config = {}
41 sandbox = ConfigureSandbox(config, os.environ, argv)
43 clobber_file = 'CLOBBER'
44 if not os.path.exists(clobber_file):
45 # Simply touch the file.
46 with open(clobber_file, 'a'):
47 pass
49 if os.environ.get('MOZ_CONFIGURE_TRACE'):
50 sandbox._logger.setLevel(TRACE)
52 sandbox.run(os.path.join(os.path.dirname(__file__), 'moz.configure'))
54 if sandbox._help:
55 return 0
57 return config_status(config)
60 def check_unicode(obj):
61 '''Recursively check that all strings in the object are unicode strings.'''
62 if isinstance(obj, dict):
63 result = True
64 for k, v in six.iteritems(obj):
65 if not check_unicode(k):
66 print("%s key is not unicode." % k, file=sys.stderr)
67 result = False
68 elif not check_unicode(v):
69 print("%s value is not unicode." % k, file=sys.stderr)
70 result = False
71 return result
72 if isinstance(obj, bytes):
73 return False
74 if isinstance(obj, six.text_type):
75 return True
76 if isinstance(obj, Iterable):
77 return all(check_unicode(o) for o in obj)
78 return True
81 def config_status(config):
82 # Sanitize config data to feed config.status
83 # Ideally, all the backend and frontend code would handle the booleans, but
84 # there are so many things involved, that it's easier to keep config.status
85 # untouched for now.
86 def sanitize_config(v):
87 if v is True:
88 return '1'
89 if v is False:
90 return ''
91 # Serialize types that look like lists and tuples as lists.
92 if not isinstance(v, (bytes, six.text_type, dict)) and isinstance(v, Iterable):
93 return list(v)
94 return v
96 sanitized_config = {}
97 sanitized_config['substs'] = {
98 k: sanitize_config(v) for k, v in six.iteritems(config)
99 if k not in ('DEFINES', 'non_global_defines', 'TOPSRCDIR', 'TOPOBJDIR',
100 'CONFIG_STATUS_DEPS')
102 sanitized_config['defines'] = {
103 k: sanitize_config(v) for k, v in six.iteritems(config['DEFINES'])
105 sanitized_config['non_global_defines'] = config['non_global_defines']
106 sanitized_config['topsrcdir'] = config['TOPSRCDIR']
107 sanitized_config['topobjdir'] = config['TOPOBJDIR']
108 sanitized_config['mozconfig'] = config.get('MOZCONFIG')
110 if not check_unicode(sanitized_config):
111 print("Configuration should be all unicode.", file=sys.stderr)
112 print("Please file a bug for the above.", file=sys.stderr)
113 sys.exit(1)
115 # Create config.status. Eventually, we'll want to just do the work it does
116 # here, when we're able to skip configure tests/use cached results/not rely
117 # on autoconf.
118 logging.getLogger('moz.configure').info('Creating config.status')
119 with codecs.open('config.status', 'w', 'utf-8') as fh:
120 fh.write(textwrap.dedent('''\
121 #!%(python)s
122 # coding=utf-8
123 from __future__ import unicode_literals
124 ''') % {'python': config['PYTHON3']})
125 for k, v in sorted(six.iteritems(sanitized_config)):
126 fh.write('%s = ' % k)
127 write_indented_repr(fh, v)
128 fh.write("__all__ = ['topobjdir', 'topsrcdir', 'defines', "
129 "'non_global_defines', 'substs', 'mozconfig']")
131 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
132 fh.write(textwrap.dedent('''
133 if __name__ == '__main__':
134 from mozbuild.util import patch_main
135 patch_main()
136 from mozbuild.config_status import config_status
137 args = dict([(name, globals()[name]) for name in __all__])
138 config_status(**args)
139 '''))
141 partial_config = PartialConfigEnvironment(config['TOPOBJDIR'])
142 partial_config.write_vars(sanitized_config)
144 # Write out a file so the build backend knows to re-run configure when
145 # relevant Python changes.
146 with io.open('config_status_deps.in', 'w', encoding='utf-8',
147 newline='\n') as fh:
148 for f in 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))