mconf.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # Copyright 2014-2016 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import os
  12. from . import (coredata, mesonlib, build)
  13. from . import mintro
  14. def add_arguments(parser):
  15. coredata.register_builtin_arguments(parser)
  16. parser.add_argument('builddir', nargs='?', default='.')
  17. parser.add_argument('--clearcache', action='store_true', default=False,
  18. help='Clear cached state (e.g. found dependencies)')
  19. def make_lower_case(val):
  20. if isinstance(val, bool):
  21. return str(val).lower()
  22. elif isinstance(val, list):
  23. return [make_lower_case(i) for i in val]
  24. else:
  25. return str(val)
  26. class ConfException(mesonlib.MesonException):
  27. pass
  28. class Conf:
  29. def __init__(self, build_dir):
  30. self.build_dir = build_dir
  31. if not os.path.isdir(os.path.join(build_dir, 'meson-private')):
  32. raise ConfException('Directory %s does not seem to be a Meson build directory.' % build_dir)
  33. self.build = build.load(self.build_dir)
  34. self.coredata = coredata.load(self.build_dir)
  35. def clear_cache(self):
  36. self.coredata.deps = {}
  37. def set_options(self, options):
  38. self.coredata.set_options(options)
  39. def save(self):
  40. # Only called if something has changed so overwrite unconditionally.
  41. coredata.save(self.coredata, self.build_dir)
  42. # We don't write the build file because any changes to it
  43. # are erased when Meson is executed the next time, i.e. when
  44. # Ninja is run.
  45. @staticmethod
  46. def print_aligned(arr):
  47. if not arr:
  48. return
  49. titles = {'name': 'Option', 'descr': 'Description', 'value': 'Current Value', 'choices': 'Possible Values'}
  50. name_col = [titles['name'], '-' * len(titles['name'])]
  51. value_col = [titles['value'], '-' * len(titles['value'])]
  52. choices_col = [titles['choices'], '-' * len(titles['choices'])]
  53. descr_col = [titles['descr'], '-' * len(titles['descr'])]
  54. choices_found = False
  55. for opt in arr:
  56. name_col.append(opt['name'])
  57. descr_col.append(opt['descr'])
  58. if isinstance(opt['value'], list):
  59. value_col.append('[{0}]'.format(', '.join(make_lower_case(opt['value']))))
  60. else:
  61. value_col.append(make_lower_case(opt['value']))
  62. if opt['choices']:
  63. choices_found = True
  64. if isinstance(opt['choices'], list):
  65. choices_col.append('[{0}]'.format(', '.join(make_lower_case(opt['choices']))))
  66. else:
  67. choices_col.append(make_lower_case(opt['choices']))
  68. else:
  69. choices_col.append('')
  70. col_widths = (max([len(i) for i in name_col], default=0),
  71. max([len(i) for i in value_col], default=0),
  72. max([len(i) for i in choices_col], default=0),
  73. max([len(i) for i in descr_col], default=0))
  74. for line in zip(name_col, value_col, choices_col, descr_col):
  75. if choices_found:
  76. print(' {0:{width[0]}} {1:{width[1]}} {2:{width[2]}} {3:{width[3]}}'.format(*line, width=col_widths))
  77. else:
  78. print(' {0:{width[0]}} {1:{width[1]}} {3:{width[3]}}'.format(*line, width=col_widths))
  79. def print_options(self, title, options):
  80. print('\n{}:'.format(title))
  81. if not options:
  82. print(' No {}\n'.format(title.lower()))
  83. arr = []
  84. for k in sorted(options):
  85. o = options[k]
  86. d = o.description
  87. v = o.printable_value()
  88. c = o.choices
  89. arr.append({'name': k, 'descr': d, 'value': v, 'choices': c})
  90. self.print_aligned(arr)
  91. def print_conf(self):
  92. print('Core properties:')
  93. print(' Source dir', self.build.environment.source_dir)
  94. print(' Build dir ', self.build.environment.build_dir)
  95. dir_option_names = ['bindir',
  96. 'datadir',
  97. 'includedir',
  98. 'infodir',
  99. 'libdir',
  100. 'libexecdir',
  101. 'localedir',
  102. 'localstatedir',
  103. 'mandir',
  104. 'prefix',
  105. 'sbindir',
  106. 'sharedstatedir',
  107. 'sysconfdir']
  108. test_option_names = ['errorlogs',
  109. 'stdsplit']
  110. core_option_names = [k for k in self.coredata.builtins if k not in dir_option_names + test_option_names]
  111. dir_options = {k: o for k, o in self.coredata.builtins.items() if k in dir_option_names}
  112. test_options = {k: o for k, o in self.coredata.builtins.items() if k in test_option_names}
  113. core_options = {k: o for k, o in self.coredata.builtins.items() if k in core_option_names}
  114. self.print_options('Core options', core_options)
  115. self.print_options('Backend options', self.coredata.backend_options)
  116. self.print_options('Base options', self.coredata.base_options)
  117. # TODO others
  118. self.print_options('Compiler options', self.coredata.compiler_options.build)
  119. self.print_options('Directories', dir_options)
  120. self.print_options('Project options', self.coredata.user_options)
  121. self.print_options('Testing options', test_options)
  122. def run(options):
  123. coredata.parse_cmd_line_options(options)
  124. builddir = os.path.abspath(os.path.realpath(options.builddir))
  125. c = None
  126. try:
  127. c = Conf(builddir)
  128. save = False
  129. if len(options.cmd_line_options) > 0:
  130. c.set_options(options.cmd_line_options)
  131. if not c.build.environment.is_cross_build():
  132. # TODO think about cross and command-line interface.
  133. c.coredata.compiler_options.host = c.coredata.compiler_options.build
  134. coredata.update_cmd_line_file(builddir, options)
  135. save = True
  136. elif options.clearcache:
  137. c.clear_cache()
  138. save = True
  139. else:
  140. c.print_conf()
  141. if save:
  142. c.save()
  143. mintro.update_build_options(c.coredata, c.build.environment.info_dir)
  144. mintro.write_meson_info_file(c.build, [])
  145. except ConfException as e:
  146. print('Meson configurator encountered an error:')
  147. if c is not None and c.build is not None:
  148. mintro.write_meson_info_file(c.build, [e])
  149. raise e
  150. return 0