Bug 1654678 Part 1 - Add generic CUPS printer interface r=nordzilla
[gecko.git] / configure.py
blobe3d914ef65d511b8c8d0d2a9089529fe9cee36f9
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 sorted(
150 itertools.chain(config['CONFIG_STATUS_DEPS'],
151 iter_modules_in_path(config['TOPOBJDIR'],
152 config['TOPSRCDIR']))):
153 fh.write('%s\n' % mozpath.normpath(f))
155 # Other things than us are going to run this file, so we need to give it
156 # executable permissions.
157 os.chmod('config.status', 0o755)
158 if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
159 from mozbuild.config_status import config_status
161 # Some values in sanitized_config also have more complex types, such as
162 # EnumString, which using when calling config_status would currently
163 # break the build, as well as making it inconsistent with re-running
164 # config.status, for which they are normalized to plain strings via
165 # indented_repr. Likewise for non-dict non-string iterables being
166 # converted to lists.
167 def normalize(obj):
168 if isinstance(obj, dict):
169 return {
170 k: normalize(v)
171 for k, v in six.iteritems(obj)
173 if isinstance(obj, six.text_type):
174 return six.text_type(obj)
175 if isinstance(obj, Iterable):
176 return [normalize(o) for o in obj]
177 return obj
178 return config_status(args=[], **normalize(sanitized_config))
179 return 0
182 if __name__ == '__main__':
183 sys.exit(main(sys.argv))