qt5.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 Qt5Module():
  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-qt5', silent=True)
  22. if not self.moc.found():
  23. self.moc = dependencies.ExternalProgram('moc', silent=True)
  24. self.uic = dependencies.ExternalProgram('uic-qt5', silent=True)
  25. if not self.uic.found():
  26. self.uic = dependencies.ExternalProgram('uic', silent=True)
  27. self.rcc = dependencies.ExternalProgram('rcc-qt5', 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 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. (' '.join(self.moc.fullpath), moc_ver.split()[-1]))
  48. else:
  49. mlog.log(' moc:', mlog.red('NO'))
  50. if self.uic.found():
  51. up = subprocess.Popen(self.uic.get_command() + ['-v'],
  52. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  53. (stdout, stderr) = up.communicate()
  54. stdout = stdout.decode().strip()
  55. stderr = stderr.decode().strip()
  56. if 'version 5.' in stderr:
  57. uic_ver = stderr
  58. elif '5.' in stdout:
  59. uic_ver = stdout
  60. else:
  61. raise MesonException('Uic compiler is not for Qt 5. Output:\n%s\n%s' %
  62. (stdout, stderr))
  63. mlog.log(' uic:', mlog.green('YES'), '(%s, %s)' % \
  64. (' '.join(self.uic.fullpath), uic_ver.split()[-1]))
  65. else:
  66. mlog.log(' uic:', mlog.red('NO'))
  67. if self.rcc.found():
  68. rp = subprocess.Popen(self.rcc.get_command() + ['-v'],
  69. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  70. (stdout, stderr) = rp.communicate()
  71. stdout = stdout.decode().strip()
  72. stderr = stderr.decode().strip()
  73. if 'version 5.' in stderr:
  74. rcc_ver = stderr
  75. elif '5.' in stdout:
  76. rcc_ver = stdout
  77. else:
  78. raise MesonException('Rcc compiler is not for Qt 5. Output:\n%s\n%s' %
  79. (stdout, stderr))
  80. mlog.log(' rcc:', mlog.green('YES'), '(%s, %s)'\
  81. % (' '.join(self.rcc.fullpath), rcc_ver.split()[-1]))
  82. else:
  83. mlog.log(' rcc:', mlog.red('NO'))
  84. def parse_qrc(self, state, fname):
  85. abspath = os.path.join(state.environment.source_dir, state.subdir, fname)
  86. relative_part = os.path.split(fname)[0]
  87. try:
  88. tree = ET.parse(abspath)
  89. root = tree.getroot()
  90. result = []
  91. for child in root[0]:
  92. if child.tag != 'file':
  93. mlog.log("Warning, malformed rcc file: ", os.path.join(state.subdir, fname))
  94. break
  95. else:
  96. result.append(os.path.join(state.subdir, relative_part, child.text))
  97. return result
  98. except Exception:
  99. return []
  100. def preprocess(self, state, args, kwargs):
  101. rcc_files = kwargs.pop('qresources', [])
  102. if not isinstance(rcc_files, list):
  103. rcc_files = [rcc_files]
  104. ui_files = kwargs.pop('ui_files', [])
  105. if not isinstance(ui_files, list):
  106. ui_files = [ui_files]
  107. moc_headers = kwargs.pop('moc_headers', [])
  108. if not isinstance(moc_headers, list):
  109. moc_headers = [moc_headers]
  110. moc_sources = kwargs.pop('moc_sources', [])
  111. if not isinstance(moc_sources, list):
  112. moc_sources = [moc_sources]
  113. srctmp = kwargs.pop('sources', [])
  114. if not isinstance(srctmp, list):
  115. srctmp = [srctmp]
  116. sources = args[1:] + srctmp
  117. if len(rcc_files) > 0:
  118. qrc_deps = []
  119. for i in rcc_files:
  120. qrc_deps += self.parse_qrc(state, i)
  121. basename = os.path.split(rcc_files[0])[1]
  122. rcc_kwargs = {'input' : rcc_files,
  123. 'output' : basename + '.cpp',
  124. 'command' : [self.rcc, '-o', '@OUTPUT@', '@INPUT@'],
  125. 'depend_files' : qrc_deps,
  126. }
  127. res_target = build.CustomTarget(basename.replace('.', '_'),
  128. state.subdir,
  129. rcc_kwargs)
  130. sources.append(res_target)
  131. if len(ui_files) > 0:
  132. ui_kwargs = {'output' : 'ui_@BASENAME@.h',
  133. 'arguments' : ['-o', '@OUTPUT@', '@INPUT@']}
  134. ui_gen = build.Generator([self.uic], ui_kwargs)
  135. ui_output = build.GeneratedList(ui_gen)
  136. [ui_output.add_file(os.path.join(state.subdir, a)) for a in ui_files]
  137. sources.append(ui_output)
  138. if len(moc_headers) > 0:
  139. moc_kwargs = {'output' : 'moc_@BASENAME@.cpp',
  140. 'arguments' : ['@INPUT@', '-o', '@OUTPUT@']}
  141. moc_gen = build.Generator([self.moc], moc_kwargs)
  142. moc_output = build.GeneratedList(moc_gen)
  143. [moc_output.add_file(os.path.join(state.subdir, a)) for a in moc_headers]
  144. sources.append(moc_output)
  145. if len(moc_sources) > 0:
  146. moc_kwargs = {'output' : '@BASENAME@.moc',
  147. 'arguments' : ['@INPUT@', '-o', '@OUTPUT@']}
  148. moc_gen = build.Generator([self.moc], moc_kwargs)
  149. moc_output = build.GeneratedList(moc_gen)
  150. [moc_output.add_file(os.path.join(state.subdir, a)) for a in moc_sources]
  151. sources.append(moc_output)
  152. return sources
  153. def initialize():
  154. mlog.log('Warning, rcc dependencies will not work reliably until this upstream issue is fixed:',
  155. mlog.bold('https://bugreports.qt.io/browse/QTBUG-45460'))
  156. return Qt5Module()