qt.py 8.8 KB

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