Bug 1588242 [wpt PR 19652] - [tools] Correct filtering logic for building docs, a...
[gecko.git] / configure.py
blob5643b03babf0ec1228da2246aa1e91e1dbd56d60
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 if os.environ.get('MOZ_CONFIGURE_TRACE'):
38 sandbox._logger.setLevel(TRACE)
40 sandbox.run(os.path.join(os.path.dirname(__file__), 'moz.configure'))
42 if sandbox._help:
43 return 0
45 return config_status(config)
48 def check_unicode(obj):
49 '''Recursively check that all strings in the object are unicode strings.'''
50 if isinstance(obj, dict):
51 result = True
52 for k, v in six.iteritems(obj):
53 if not check_unicode(k):
54 print("%s key is not unicode." % k, file=sys.stderr)
55 result = False
56 elif not check_unicode(v):
57 print("%s value is not unicode." % k, file=sys.stderr)
58 result = False
59 return result
60 if isinstance(obj, bytes):
61 return False
62 if isinstance(obj, six.text_type):
63 return True
64 if isinstance(obj, Iterable):
65 return all(check_unicode(o) for o in obj)
66 return True
69 def config_status(config):
70 # Sanitize config data to feed config.status
71 # Ideally, all the backend and frontend code would handle the booleans, but
72 # there are so many things involved, that it's easier to keep config.status
73 # untouched for now.
74 def sanitized_bools(v):
75 if v is True:
76 return '1'
77 if v is False:
78 return ''
79 return v
81 sanitized_config = {}
82 sanitized_config['substs'] = {
83 k: sanitized_bools(v) for k, v in six.iteritems(config)
84 if k not in ('DEFINES', 'non_global_defines', 'TOPSRCDIR', 'TOPOBJDIR',
85 'CONFIG_STATUS_DEPS')
87 sanitized_config['defines'] = {
88 k: sanitized_bools(v) for k, v in six.iteritems(config['DEFINES'])
90 sanitized_config['non_global_defines'] = config['non_global_defines']
91 sanitized_config['topsrcdir'] = config['TOPSRCDIR']
92 sanitized_config['topobjdir'] = config['TOPOBJDIR']
93 sanitized_config['mozconfig'] = config.get('MOZCONFIG')
95 if not check_unicode(sanitized_config):
96 print("Configuration should be all unicode.", file=sys.stderr)
97 print("Please file a bug for the above.", file=sys.stderr)
98 sys.exit(1)
100 # Create config.status. Eventually, we'll want to just do the work it does
101 # here, when we're able to skip configure tests/use cached results/not rely
102 # on autoconf.
103 logging.getLogger('moz.configure').info('Creating config.status')
104 with codecs.open('config.status', 'w', 'utf-8') as fh:
105 fh.write(textwrap.dedent('''\
106 #!%(python)s
107 # coding=utf-8
108 from __future__ import unicode_literals
109 ''') % {'python': config['PYTHON']})
110 for k, v in six.iteritems(sanitized_config):
111 fh.write('%s = %s\n' % (k, indented_repr(v)))
112 fh.write("__all__ = ['topobjdir', 'topsrcdir', 'defines', "
113 "'non_global_defines', 'substs', 'mozconfig']")
115 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
116 fh.write(textwrap.dedent('''
117 if __name__ == '__main__':
118 from mozbuild.util import patch_main
119 patch_main()
120 from mozbuild.config_status import config_status
121 args = dict([(name, globals()[name]) for name in __all__])
122 config_status(**args)
123 '''))
125 partial_config = PartialConfigEnvironment(config['TOPOBJDIR'])
126 partial_config.write_vars(sanitized_config)
128 # Write out a file so the build backend knows to re-run configure when
129 # relevant Python changes.
130 with open('config_status_deps.in', 'w') as fh:
131 for f in itertools.chain(config['CONFIG_STATUS_DEPS'],
132 iter_modules_in_path(config['TOPOBJDIR'],
133 config['TOPSRCDIR'])):
134 fh.write('%s\n' % mozpath.normpath(f))
136 # Other things than us are going to run this file, so we need to give it
137 # executable permissions.
138 os.chmod('config.status', 0o755)
139 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
140 from mozbuild.config_status import config_status
142 # Some values in sanitized_config also have more complex types, such as
143 # EnumString, which using when calling config_status would currently
144 # break the build, as well as making it inconsistent with re-running
145 # config.status, for which they are normalized to plain strings via
146 # indented_repr. Likewise for non-dict non-string iterables being
147 # converted to lists.
148 def normalize(obj):
149 if isinstance(obj, dict):
150 return {
151 k: normalize(v)
152 for k, v in six.iteritems(obj)
154 if isinstance(obj, six.text_type):
155 return six.text_type(obj)
156 if isinstance(obj, Iterable):
157 return [normalize(o) for o in obj]
158 return obj
159 return config_status(args=[], **normalize(sanitized_config))
160 return 0
163 if __name__ == '__main__':
164 sys.exit(main(sys.argv))