Source code

Revision control

Other Tools

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/.
4
5
from __future__ import absolute_import, print_function, unicode_literals
6
7
import codecs
8
import itertools
9
import logging
10
import os
11
import sys
12
import textwrap
13
14
15
base_dir = os.path.abspath(os.path.dirname(__file__))
16
sys.path.insert(0, os.path.join(base_dir, 'python', 'mozbuild'))
17
sys.path.insert(0, os.path.join(base_dir, 'third_party', 'python', 'six'))
18
from mozbuild.configure import (
19
ConfigureSandbox,
20
TRACE,
21
)
22
from mozbuild.pythonutil import iter_modules_in_path
23
from mozbuild.backend.configenvironment import PartialConfigEnvironment
24
from mozbuild.util import (
25
indented_repr,
26
encode,
27
)
28
import mozpack.path as mozpath
29
30
31
def main(argv):
32
config = {}
33
34
sandbox = ConfigureSandbox(config, os.environ, argv)
35
36
if os.environ.get('MOZ_CONFIGURE_TRACE'):
37
sandbox._logger.setLevel(TRACE)
38
39
sandbox.run(os.path.join(os.path.dirname(__file__), 'moz.configure'))
40
41
if sandbox._help:
42
return 0
43
44
return config_status(config)
45
46
47
def config_status(config):
48
# Sanitize config data to feed config.status
49
# Ideally, all the backend and frontend code would handle the booleans, but
50
# there are so many things involved, that it's easier to keep config.status
51
# untouched for now.
52
def sanitized_bools(v):
53
if v is True:
54
return '1'
55
if v is False:
56
return ''
57
return v
58
59
sanitized_config = {}
60
sanitized_config['substs'] = {
61
k: sanitized_bools(v) for k, v in config.iteritems()
62
if k not in ('DEFINES', 'non_global_defines', 'TOPSRCDIR', 'TOPOBJDIR',
63
'CONFIG_STATUS_DEPS')
64
}
65
sanitized_config['defines'] = {
66
k: sanitized_bools(v) for k, v in config['DEFINES'].iteritems()
67
}
68
sanitized_config['non_global_defines'] = config['non_global_defines']
69
sanitized_config['topsrcdir'] = config['TOPSRCDIR']
70
sanitized_config['topobjdir'] = config['TOPOBJDIR']
71
sanitized_config['mozconfig'] = config.get('MOZCONFIG')
72
73
# Create config.status. Eventually, we'll want to just do the work it does
74
# here, when we're able to skip configure tests/use cached results/not rely
75
# on autoconf.
76
logging.getLogger('moz.configure').info('Creating config.status')
77
encoding = 'mbcs' if sys.platform == 'win32' else 'utf-8'
78
with codecs.open('config.status', 'w', encoding) as fh:
79
fh.write(textwrap.dedent('''\
80
#!%(python)s
81
# coding=%(encoding)s
82
from __future__ import unicode_literals
83
from mozbuild.util import encode
84
encoding = '%(encoding)s'
85
''') % {'python': config['PYTHON'], 'encoding': encoding})
86
# A lot of the build backend code is currently expecting byte
87
# strings and breaks in subtle ways with unicode strings. (bug 1296508)
88
for k, v in sanitized_config.iteritems():
89
fh.write('%s = encode(%s, encoding)\n' % (k, indented_repr(v)))
90
fh.write("__all__ = ['topobjdir', 'topsrcdir', 'defines', "
91
"'non_global_defines', 'substs', 'mozconfig']")
92
93
if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
94
fh.write(textwrap.dedent('''
95
if __name__ == '__main__':
96
from mozbuild.util import patch_main
97
patch_main()
98
from mozbuild.config_status import config_status
99
args = dict([(name, globals()[name]) for name in __all__])
100
config_status(**args)
101
'''))
102
103
partial_config = PartialConfigEnvironment(config['TOPOBJDIR'])
104
partial_config.write_vars(sanitized_config)
105
106
# Write out a file so the build backend knows to re-run configure when
107
# relevant Python changes.
108
with open('config_status_deps.in', 'w') as fh:
109
for f in itertools.chain(config['CONFIG_STATUS_DEPS'],
110
iter_modules_in_path(config['TOPOBJDIR'],
111
config['TOPSRCDIR'])):
112
fh.write('%s\n' % mozpath.normpath(f))
113
114
# Other things than us are going to run this file, so we need to give it
115
# executable permissions.
116
os.chmod('config.status', 0o755)
117
if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
118
from mozbuild.config_status import config_status
119
120
# Some values in sanitized_config also have more complex types, such as
121
# EnumString, which using when calling config_status would currently
122
# break the build, as well as making it inconsistent with re-running
123
# config.status. Fortunately, EnumString derives from unicode, so it's
124
# covered by converting unicode strings.
125
126
# A lot of the build backend code is currently expecting byte strings
127
# and breaks in subtle ways with unicode strings.
128
return config_status(args=[], **encode(sanitized_config, encoding))
129
return 0
130
131
132
if __name__ == '__main__':
133
sys.exit(main(sys.argv))