configure.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. from __future__ import print_function, unicode_literals
  5. import codecs
  6. import os
  7. import subprocess
  8. import sys
  9. import textwrap
  10. base_dir = os.path.abspath(os.path.dirname(__file__))
  11. sys.path.insert(0, os.path.join(base_dir, 'python', 'mozbuild'))
  12. from mozbuild.configure import ConfigureSandbox
  13. from mozbuild.util import (
  14. indented_repr,
  15. encode,
  16. )
  17. def main(argv):
  18. config = {}
  19. sandbox = ConfigureSandbox(config, os.environ, argv)
  20. sandbox.run(os.path.join(os.path.dirname(__file__), 'moz.configure'))
  21. if sandbox._help:
  22. return 0
  23. return config_status(config)
  24. def config_status(config):
  25. # Sanitize config data to feed config.status
  26. # Ideally, all the backend and frontend code would handle the booleans, but
  27. # there are so many things involved, that it's easier to keep config.status
  28. # untouched for now.
  29. def sanitized_bools(v):
  30. if v is True:
  31. return '1'
  32. if v is False:
  33. return ''
  34. return v
  35. sanitized_config = {}
  36. sanitized_config['substs'] = {
  37. k: sanitized_bools(v) for k, v in config.iteritems()
  38. if k not in ('DEFINES', 'non_global_defines', 'TOPSRCDIR', 'TOPOBJDIR')
  39. }
  40. sanitized_config['defines'] = {
  41. k: sanitized_bools(v) for k, v in config['DEFINES'].iteritems()
  42. }
  43. sanitized_config['non_global_defines'] = config['non_global_defines']
  44. sanitized_config['topsrcdir'] = config['TOPSRCDIR']
  45. sanitized_config['topobjdir'] = config['TOPOBJDIR']
  46. sanitized_config['mozconfig'] = config.get('MOZCONFIG')
  47. # Create config.status. Eventually, we'll want to just do the work it does
  48. # here, when we're able to skip configure tests/use cached results/not rely
  49. # on autoconf.
  50. print("Creating config.status", file=sys.stderr)
  51. encoding = 'mbcs' if sys.platform == 'win32' else 'utf-8'
  52. with codecs.open('config.status', 'w', encoding) as fh:
  53. fh.write(textwrap.dedent('''\
  54. #!%(python)s
  55. # coding=%(encoding)s
  56. from __future__ import unicode_literals
  57. from mozbuild.util import encode
  58. encoding = '%(encoding)s'
  59. ''') % {'python': config['PYTHON'], 'encoding': encoding})
  60. # A lot of the build backend code is currently expecting byte
  61. # strings and breaks in subtle ways with unicode strings. (bug 1296508)
  62. for k, v in sanitized_config.iteritems():
  63. fh.write('%s = encode(%s, encoding)\n' % (k, indented_repr(v)))
  64. fh.write("__all__ = ['topobjdir', 'topsrcdir', 'defines', "
  65. "'non_global_defines', 'substs', 'mozconfig']")
  66. if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
  67. fh.write(textwrap.dedent('''
  68. if __name__ == '__main__':
  69. from mozbuild.config_status import config_status
  70. args = dict([(name, globals()[name]) for name in __all__])
  71. config_status(**args)
  72. '''))
  73. # Other things than us are going to run this file, so we need to give it
  74. # executable permissions.
  75. os.chmod('config.status', 0o755)
  76. if config.get('MOZ_BUILD_APP') != 'js' or config.get('JS_STANDALONE'):
  77. os.environ[b'WRITE_MOZINFO'] = b'1'
  78. from mozbuild.config_status import config_status
  79. # Some values in sanitized_config also have more complex types, such as
  80. # EnumString, which using when calling config_status would currently
  81. # break the build, as well as making it inconsistent with re-running
  82. # config.status. Fortunately, EnumString derives from unicode, so it's
  83. # covered by converting unicode strings.
  84. # A lot of the build backend code is currently expecting byte strings
  85. # and breaks in subtle ways with unicode strings.
  86. return config_status(args=[], **encode(sanitized_config, encoding))
  87. return 0
  88. if __name__ == '__main__':
  89. sys.exit(main(sys.argv))