Bug 1614879 [wpt PR 21750] - Set request mode for beacon request with non-cors-safeli...
[gecko.git] / configure.py
blob7b7b6a16f579d83488c288da53b7f3c85d73837e
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 itertools
9 import logging
10 import os
11 import sys
12 import textwrap
13 from collections import Iterable
16 base_dir = os.path.abspath(os.path.dirname(__file__))
17 sys.path.insert(0, os.path.join(base_dir, 'python', 'mozbuild'))
18 sys.path.insert(0, os.path.join(base_dir, 'third_party', 'python', 'six'))
19 from mozbuild.configure import (
20 ConfigureSandbox,
21 TRACE,
23 from mozbuild.pythonutil import iter_modules_in_path
24 from mozbuild.backend.configenvironment import PartialConfigEnvironment
25 from mozbuild.util import (
26 indented_repr,
28 import mozpack.path as mozpath
29 import six
32 def main(argv):
33 config = {}
35 sandbox = ConfigureSandbox(config, os.environ, argv)
37 clobber_file = 'CLOBBER'
38 if not os.path.exists(clobber_file):
39 # Simply touch the file.
40 with open(clobber_file, 'a'):
41 pass
43 if os.environ.get('MOZ_CONFIGURE_TRACE'):
44 sandbox._logger.setLevel(TRACE)
46 sandbox.run(os.path.join(os.path.dirname(__file__), 'moz.configure'))
48 if sandbox._help:
49 return 0
51 return config_status(config)
54 def check_unicode(obj):
55 '''Recursively check that all strings in the object are unicode strings.'''
56 if isinstance(obj, dict):
57 result = True
58 for k, v in six.iteritems(obj):
59 if not check_unicode(k):
60 print("%s key is not unicode." % k, file=sys.stderr)
61 result = False
62 elif not check_unicode(v):
63 print("%s value is not unicode." % k, file=sys.stderr)
64 result = False
65 return result
66 if isinstance(obj, bytes):
67 return False
68 if isinstance(obj, six.text_type):
69 return True
70 if isinstance(obj, Iterable):
71 return all(check_unicode(o) for o in obj)
72 return True
75 def config_status(config):
76 # Sanitize config data to feed config.status
77 # Ideally, all the backend and frontend code would handle the booleans, but
78 # there are so many things involved, that it's easier to keep config.status
79 # untouched for now.
80 def sanitized_bools(v):
81 if v is True:
82 return '1'
83 if v is False:
84 return ''
85 return v
87 sanitized_config = {}
88 sanitized_config['substs'] = {
89 k: sanitized_bools(v) for k, v in six.iteritems(config)
90 if k not in ('DEFINES', 'non_global_defines', 'TOPSRCDIR', 'TOPOBJDIR',
91 'CONFIG_STATUS_DEPS')
93 sanitized_config['defines'] = {
94 k: sanitized_bools(v) for k, v in six.iteritems(config['DEFINES'])
96 sanitized_config['non_global_defines'] = config['non_global_defines']
97 sanitized_config['topsrcdir'] = config['TOPSRCDIR']
98 sanitized_config['topobjdir'] = config['TOPOBJDIR']
99 sanitized_config['mozconfig'] = config.get('MOZCONFIG')
101 if not check_unicode(sanitized_config):
102 print("Configuration should be all unicode.", file=sys.stderr)
103 print("Please file a bug for the above.", file=sys.stderr)
104 sys.exit(1)
106 # Create config.status. Eventually, we'll want to just do the work it does
107 # here, when we're able to skip configure tests/use cached results/not rely
108 # on autoconf.
109 logging.getLogger('moz.configure').info('Creating config.status')
110 with codecs.open('config.status', 'w', 'utf-8') as fh:
111 fh.write(textwrap.dedent('''\
112 #!%(python)s
113 # coding=utf-8
114 from __future__ import unicode_literals
115 ''') % {'python': config['PYTHON']})
116 for k, v in six.iteritems(sanitized_config):
117 fh.write('%s = %s\n' % (k, indented_repr(v)))
118 fh.write("__all__ = ['topobjdir', 'topsrcdir', 'defines', "
119 "'non_global_defines', 'substs', 'mozconfig']")
121 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
122 fh.write(textwrap.dedent('''
123 if __name__ == '__main__':
124 from mozbuild.util import patch_main
125 patch_main()
126 from mozbuild.config_status import config_status
127 args = dict([(name, globals()[name]) for name in __all__])
128 config_status(**args)
129 '''))
131 partial_config = PartialConfigEnvironment(config['TOPOBJDIR'])
132 partial_config.write_vars(sanitized_config)
134 # Write out a file so the build backend knows to re-run configure when
135 # relevant Python changes.
136 with open('config_status_deps.in', 'w') as fh:
137 for f in itertools.chain(config['CONFIG_STATUS_DEPS'],
138 iter_modules_in_path(config['TOPOBJDIR'],
139 config['TOPSRCDIR'])):
140 fh.write('%s\n' % mozpath.normpath(f))
142 # Other things than us are going to run this file, so we need to give it
143 # executable permissions.
144 os.chmod('config.status', 0o755)
145 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
146 from mozbuild.config_status import config_status
148 # Some values in sanitized_config also have more complex types, such as
149 # EnumString, which using when calling config_status would currently
150 # break the build, as well as making it inconsistent with re-running
151 # config.status, for which they are normalized to plain strings via
152 # indented_repr. Likewise for non-dict non-string iterables being
153 # converted to lists.
154 def normalize(obj):
155 if isinstance(obj, dict):
156 return {
157 k: normalize(v)
158 for k, v in six.iteritems(obj)
160 if isinstance(obj, six.text_type):
161 return six.text_type(obj)
162 if isinstance(obj, Iterable):
163 return [normalize(o) for o in obj]
164 return obj
165 return config_status(args=[], **normalize(sanitized_config))
166 return 0
169 if __name__ == '__main__':
170 sys.exit(main(sys.argv))