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
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 (
23 from mozbuild
.pythonutil
import iter_modules_in_path
24 from mozbuild
.backend
.configenvironment
import PartialConfigEnvironment
25 from mozbuild
.util
import (
28 import mozpack
.path
as mozpath
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'))
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):
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
)
56 elif not check_unicode(v
):
57 print("%s value is not unicode." % k
, file=sys
.stderr
)
60 if isinstance(obj
, bytes
):
62 if isinstance(obj
, six
.text_type
):
64 if isinstance(obj
, Iterable
):
65 return all(check_unicode(o
) for o
in obj
)
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
74 def sanitized_bools(v
):
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',
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
)
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
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('''\
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
120 from mozbuild.config_status import config_status
121 args = dict([(name, globals()[name]) for name in __all__])
122 config_status(**args)
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.
149 if isinstance(obj
, dict):
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
]
159 return config_status(args
=[], **normalize(sanitized_config
))
163 if __name__
== '__main__':
164 sys
.exit(main(sys
.argv
))