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', 'mozbuild'))
24 sys
.path
.insert(0, os
.path
.join(base_dir
, 'third_party', 'python', 'six'))
25 from mozbuild
.configure
import (
29 from mozbuild
.pythonutil
import iter_modules_in_path
30 from mozbuild
.backend
.configenvironment
import PartialConfigEnvironment
31 from mozbuild
.util
import (
34 import mozpack
.path
as mozpath
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'):
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'))
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):
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
)
68 elif not check_unicode(v
):
69 print("%s value is not unicode." % k
, file=sys
.stderr
)
72 if isinstance(obj
, bytes
):
74 if isinstance(obj
, six
.text_type
):
76 if isinstance(obj
, Iterable
):
77 return all(check_unicode(o
) for o
in obj
)
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
86 def sanitize_config(v
):
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
):
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
)
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
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('''\
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
136 from mozbuild.config_status import config_status
137 args = dict([(name, globals()[name]) for name in __all__])
138 config_status(**args)
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',
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.
166 if isinstance(obj
, dict):
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
]
176 return config_status(args
=[], **normalize(sanitized_config
))
180 if __name__
== '__main__':
181 sys
.exit(main(sys
.argv
))