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
17 from collections
.abc
import Iterable
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 (
30 from mozbuild
.pythonutil
import iter_modules_in_path
31 from mozbuild
.backend
.configenvironment
import PartialConfigEnvironment
32 from mozbuild
.util
import (
35 import mozpack
.path
as mozpath
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'):
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'))
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):
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
)
69 elif not check_unicode(v
):
70 print("%s value is not unicode." % k
, file=sys
.stderr
)
73 if isinstance(obj
, bytes
):
75 if isinstance(obj
, six
.text_type
):
77 if isinstance(obj
, Iterable
):
78 return all(check_unicode(o
) for o
in obj
)
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
87 def sanitize_config(v
):
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
):
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
)
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
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('''\
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
137 from mozbuild.config_status import config_status
138 args = dict([(name, globals()[name]) for name in __all__])
139 config_status(**args)
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',
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.
168 if isinstance(obj
, dict):
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
]
178 return config_status(args
=[], **normalize(sanitized_config
))
182 if __name__
== '__main__':
183 sys
.exit(main(sys
.argv
))