qt4.py 7.4 KB

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