qt4.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. from .. import dependencies, mlog
  12. import os, subprocess
  13. from .. import build
  14. from ..mesonlib import MesonException
  15. import xml.etree.ElementTree as ET
  16. class Qt4Module():
  17. def __init__(self):
  18. mlog.log('Detecting Qt tools.')
  19. # The binaries have different names on different
  20. # distros. Joy.
  21. self.moc = dependencies.ExternalProgram('moc-qt4', silent=True)
  22. if not self.moc.found():
  23. self.moc = dependencies.ExternalProgram('moc', silent=True)
  24. self.uic = dependencies.ExternalProgram('uic-qt4', silent=True)
  25. if not self.uic.found():
  26. self.uic = dependencies.ExternalProgram('uic', silent=True)
  27. self.rcc = dependencies.ExternalProgram('rcc-qt4', silent=True)
  28. if not self.rcc.found():
  29. self.rcc = dependencies.ExternalProgram('rcc', silent=True)
  30. # Moc, uic and rcc write their version strings to stderr.
  31. # Moc and rcc return a non-zero result when doing so.
  32. # What kind of an idiot thought that was a good idea?
  33. if self.moc.found():
  34. mp = subprocess.Popen(self.moc.get_command() + ['-v'],
  35. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  36. (stdout, stderr) = mp.communicate()
  37. stdout = stdout.decode().strip()
  38. stderr = stderr.decode().strip()
  39. if 'Qt Meta' in stderr:
  40. moc_ver = stderr
  41. else:
  42. raise MesonException('Moc preprocessor is not for Qt 4. Output:\n%s\n%s' %
  43. (stdout, stderr))
  44. mlog.log(' moc:', mlog.green('YES'), '(%s, %s)' % \
  45. (' '.join(self.moc.fullpath), moc_ver.split()[-1]))
  46. else:
  47. mlog.log(' moc:', mlog.red('NO'))
  48. if self.uic.found():
  49. up = subprocess.Popen(self.uic.get_command() + ['-v'],
  50. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  51. (stdout, stderr) = up.communicate()
  52. stdout = stdout.decode().strip()
  53. stderr = stderr.decode().strip()
  54. if 'version 4.' in stderr:
  55. uic_ver = stderr
  56. else:
  57. raise MesonException('Uic compiler is not for Qt4. Output:\n%s\n%s' %
  58. (stdout, stderr))
  59. mlog.log(' uic:', mlog.green('YES'), '(%s, %s)' % \
  60. (' '.join(self.uic.fullpath), uic_ver.split()[-1]))
  61. else:
  62. mlog.log(' uic:', mlog.red('NO'))
  63. if self.rcc.found():
  64. rp = subprocess.Popen(self.rcc.get_command() + ['-v'],
  65. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  66. (stdout, stderr) = rp.communicate()
  67. stdout = stdout.decode().strip()
  68. stderr = stderr.decode().strip()
  69. if 'version 4.' in stderr:
  70. rcc_ver = stderr
  71. else:
  72. raise MesonException('Rcc compiler is not for Qt 4. Output:\n%s\n%s' %
  73. (stdout, stderr))
  74. mlog.log(' rcc:', mlog.green('YES'), '(%s, %s)'\
  75. % (' '.join(self.rcc.fullpath), rcc_ver.split()[-1]))
  76. else:
  77. mlog.log(' rcc:', mlog.red('NO'))
  78. def parse_qrc(self, state, fname):
  79. abspath = os.path.join(state.environment.source_dir, state.subdir, fname)
  80. relative_part = os.path.split(fname)[0]
  81. try:
  82. tree = ET.parse(abspath)
  83. root = tree.getroot()
  84. result = []
  85. for child in root[0]:
  86. if child.tag != 'file':
  87. mlog.log("Warning, malformed rcc file: ", os.path.join(state.subdir, fname))
  88. break
  89. else:
  90. result.append(os.path.join(state.subdir, relative_part, child.text))
  91. return result
  92. except Exception:
  93. return []
  94. def preprocess(self, state, args, kwargs):
  95. rcc_files = kwargs.pop('qresources', [])
  96. if not isinstance(rcc_files, list):
  97. rcc_files = [rcc_files]
  98. ui_files = kwargs.pop('ui_files', [])
  99. if not isinstance(ui_files, list):
  100. ui_files = [ui_files]
  101. moc_headers = kwargs.pop('moc_headers', [])
  102. if not isinstance(moc_headers, list):
  103. moc_headers = [moc_headers]
  104. moc_sources = kwargs.pop('moc_sources', [])
  105. if not isinstance(moc_sources, list):
  106. moc_sources = [moc_sources]
  107. srctmp = kwargs.pop('sources', [])
  108. if not isinstance(srctmp, list):
  109. srctmp = [srctmp]
  110. sources = args[1:] + srctmp
  111. if len(rcc_files) > 0:
  112. rcc_kwargs = {'output' : '@BASENAME@.cpp',
  113. 'arguments' : ['@INPUT@', '-o', '@OUTPUT@']}
  114. rcc_gen = build.Generator([self.rcc], rcc_kwargs)
  115. rcc_output = build.GeneratedList(rcc_gen)
  116. qrc_deps = []
  117. for i in rcc_files:
  118. qrc_deps += self.parse_qrc(state, i)
  119. rcc_output.extra_depends = qrc_deps
  120. [rcc_output.add_file(os.path.join(state.subdir, a)) for a in rcc_files]
  121. sources.append(rcc_output)
  122. if len(ui_files) > 0:
  123. ui_kwargs = {'output' : 'ui_@BASENAME@.h',
  124. 'arguments' : ['-o', '@OUTPUT@', '@INPUT@']}
  125. ui_gen = build.Generator([self.uic], ui_kwargs)
  126. ui_output = build.GeneratedList(ui_gen)
  127. [ui_output.add_file(os.path.join(state.subdir, a)) for a in ui_files]
  128. sources.append(ui_output)
  129. if len(moc_headers) > 0:
  130. moc_kwargs = {'output' : 'moc_@BASENAME@.cpp',
  131. 'arguments' : ['@INPUT@', '-o', '@OUTPUT@']}
  132. moc_gen = build.Generator([self.moc], moc_kwargs)
  133. moc_output = build.GeneratedList(moc_gen)
  134. [moc_output.add_file(os.path.join(state.subdir, a)) for a in moc_headers]
  135. sources.append(moc_output)
  136. if len(moc_sources) > 0:
  137. moc_kwargs = {'output' : '@BASENAME@.moc',
  138. 'arguments' : ['@INPUT@', '-o', '@OUTPUT@']}
  139. moc_gen = build.Generator([self.moc], moc_kwargs)
  140. moc_output = build.GeneratedList(moc_gen)
  141. [moc_output.add_file(os.path.join(state.subdir, a)) for a in moc_sources]
  142. sources.append(moc_output)
  143. return sources
  144. def initialize():
  145. mlog.log('Warning, rcc dependencies will not work properly until this upstream issue is fixed:',
  146. mlog.bold('https://bugreports.qt.io/browse/QTBUG-45460'))
  147. return Qt4Module()