qt5.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Copyright 2015 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 mlog
  13. from .. import build
  14. from ..mesonlib import MesonException, Popen_safe
  15. from ..dependencies import Qt5Dependency
  16. from . import ExtensionModule
  17. import xml.etree.ElementTree as ET
  18. from . import ModuleReturnValue
  19. class Qt5Module(ExtensionModule):
  20. tools_detected = False
  21. def _detect_tools(self, env, method):
  22. if self.tools_detected:
  23. return
  24. mlog.log('Detecting Qt5 tools')
  25. # FIXME: We currently require Qt5 to exist while importing the module.
  26. # We should make it gracefully degrade and not create any targets if
  27. # the import is marked as 'optional' (not implemented yet)
  28. kwargs = {'required': 'true', 'modules': 'Core', 'silent': 'true', 'method': method}
  29. qt5 = Qt5Dependency(env, kwargs)
  30. # Get all tools and then make sure that they are the right version
  31. self.moc, self.uic, self.rcc = qt5.compilers_detect()
  32. # Moc, uic and rcc write their version strings to stderr.
  33. # Moc and rcc return a non-zero result when doing so.
  34. # What kind of an idiot thought that was a good idea?
  35. if self.moc.found():
  36. stdout, stderr = Popen_safe(self.moc.get_command() + ['-v'])[1:3]
  37. stdout = stdout.strip()
  38. stderr = stderr.strip()
  39. if 'Qt 5' in stderr:
  40. moc_ver = stderr
  41. elif '5.' in stdout:
  42. moc_ver = stdout
  43. else:
  44. raise MesonException('Moc preprocessor is not for Qt 5. Output:\n%s\n%s' %
  45. (stdout, stderr))
  46. mlog.log(' moc:', mlog.green('YES'), '(%s, %s)' %
  47. (self.moc.get_path(), moc_ver.split()[-1]))
  48. else:
  49. mlog.log(' moc:', mlog.red('NO'))
  50. if self.uic.found():
  51. stdout, stderr = Popen_safe(self.uic.get_command() + ['-v'])[1:3]
  52. stdout = stdout.strip()
  53. stderr = stderr.strip()
  54. if 'version 5.' in stderr:
  55. uic_ver = stderr
  56. elif '5.' in stdout:
  57. uic_ver = stdout
  58. else:
  59. raise MesonException('Uic compiler is not for Qt 5. Output:\n%s\n%s' %
  60. (stdout, stderr))
  61. mlog.log(' uic:', mlog.green('YES'), '(%s, %s)' %
  62. (self.uic.get_path(), uic_ver.split()[-1]))
  63. else:
  64. mlog.log(' uic:', mlog.red('NO'))
  65. if self.rcc.found():
  66. stdout, stderr = Popen_safe(self.rcc.get_command() + ['-v'])[1:3]
  67. stdout = stdout.strip()
  68. stderr = stderr.strip()
  69. if 'version 5.' in stderr:
  70. rcc_ver = stderr
  71. elif '5.' in stdout:
  72. rcc_ver = stdout
  73. else:
  74. raise MesonException('Rcc compiler is not for Qt 5. Output:\n%s\n%s' %
  75. (stdout, stderr))
  76. mlog.log(' rcc:', mlog.green('YES'), '(%s, %s)'
  77. % (self.rcc.get_path(), rcc_ver.split()[-1]))
  78. else:
  79. mlog.log(' rcc:', mlog.red('NO'))
  80. self.tools_detected = True
  81. def parse_qrc(self, state, fname):
  82. abspath = os.path.join(state.environment.source_dir, state.subdir, fname)
  83. relative_part = os.path.split(fname)[0]
  84. try:
  85. tree = ET.parse(abspath)
  86. root = tree.getroot()
  87. result = []
  88. for child in root[0]:
  89. if child.tag != 'file':
  90. mlog.warning("malformed rcc file: ", os.path.join(state.subdir, fname))
  91. break
  92. else:
  93. result.append(os.path.join(state.subdir, relative_part, child.text))
  94. return result
  95. except Exception:
  96. return []
  97. def preprocess(self, state, args, kwargs):
  98. rcc_files = kwargs.pop('qresources', [])
  99. if not isinstance(rcc_files, list):
  100. rcc_files = [rcc_files]
  101. ui_files = kwargs.pop('ui_files', [])
  102. if not isinstance(ui_files, list):
  103. ui_files = [ui_files]
  104. moc_headers = kwargs.pop('moc_headers', [])
  105. if not isinstance(moc_headers, list):
  106. moc_headers = [moc_headers]
  107. moc_sources = kwargs.pop('moc_sources', [])
  108. if not isinstance(moc_sources, list):
  109. moc_sources = [moc_sources]
  110. sources = kwargs.pop('sources', [])
  111. if not isinstance(sources, list):
  112. sources = [sources]
  113. sources += args[1:]
  114. method = kwargs.get('method', 'auto')
  115. self._detect_tools(state.environment, method)
  116. err_msg = "{0} sources specified and couldn't find {1}, " \
  117. "please check your qt5 installation"
  118. if len(moc_headers) + len(moc_sources) > 0 and not self.moc.found():
  119. raise MesonException(err_msg.format('MOC', 'moc-qt5'))
  120. if len(rcc_files) > 0:
  121. if not self.rcc.found():
  122. raise MesonException(err_msg.format('RCC', 'rcc-qt5'))
  123. qrc_deps = []
  124. for i in rcc_files:
  125. qrc_deps += self.parse_qrc(state, i)
  126. if len(args) > 0:
  127. name = args[0]
  128. else:
  129. basename = os.path.split(rcc_files[0])[1]
  130. name = 'qt5-' + basename.replace('.', '_')
  131. rcc_kwargs = {'input': rcc_files,
  132. 'output': name + '.cpp',
  133. 'command': [self.rcc, '-o', '@OUTPUT@', '@INPUT@'],
  134. 'depend_files': qrc_deps}
  135. res_target = build.CustomTarget(name, state.subdir, rcc_kwargs)
  136. sources.append(res_target)
  137. if len(ui_files) > 0:
  138. if not self.uic.found():
  139. raise MesonException(err_msg.format('UIC', 'uic-qt5'))
  140. ui_kwargs = {'output': 'ui_@BASENAME@.h',
  141. 'arguments': ['-o', '@OUTPUT@', '@INPUT@']}
  142. ui_gen = build.Generator([self.uic], ui_kwargs)
  143. ui_output = ui_gen.process_files('Qt5 ui', ui_files, state)
  144. sources.append(ui_output)
  145. if len(moc_headers) > 0:
  146. moc_kwargs = {'output': 'moc_@BASENAME@.cpp',
  147. 'arguments': ['@INPUT@', '-o', '@OUTPUT@']}
  148. moc_gen = build.Generator([self.moc], moc_kwargs)
  149. moc_output = moc_gen.process_files('Qt5 moc header', moc_headers, state)
  150. sources.append(moc_output)
  151. if len(moc_sources) > 0:
  152. moc_kwargs = {'output': '@BASENAME@.moc',
  153. 'arguments': ['@INPUT@', '-o', '@OUTPUT@']}
  154. moc_gen = build.Generator([self.moc], moc_kwargs)
  155. moc_output = moc_gen.process_files('Qt5 moc source', moc_sources, state)
  156. sources.append(moc_output)
  157. return ModuleReturnValue(sources, sources)
  158. def initialize():
  159. mlog.warning('rcc dependencies will not work reliably until this upstream issue is fixed:',
  160. mlog.bold('https://bugreports.qt.io/browse/QTBUG-45460'))
  161. return Qt5Module()