sourceset.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # Copyright 2019 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 collections import namedtuple
  12. from .. import mesonlib
  13. from ..mesonlib import listify
  14. from . import ExtensionModule
  15. from ..interpreterbase import (
  16. noPosargs, noKwargs, permittedKwargs,
  17. InterpreterObject, MutableInterpreterObject, ObjectHolder,
  18. InterpreterException, InvalidArguments, InvalidCode, FeatureNew,
  19. )
  20. from ..interpreter import (
  21. GeneratedListHolder, CustomTargetHolder,
  22. CustomTargetIndexHolder
  23. )
  24. SourceSetRule = namedtuple('SourceSetRule', 'keys sources if_false sourcesets dependencies extra_deps')
  25. SourceFiles = namedtuple('SourceFiles', 'sources dependencies')
  26. class SourceSetHolder(MutableInterpreterObject, ObjectHolder):
  27. def __init__(self, interpreter):
  28. MutableInterpreterObject.__init__(self)
  29. ObjectHolder.__init__(self, list())
  30. self.subproject = interpreter.subproject
  31. self.environment = interpreter.environment
  32. self.subdir = interpreter.subdir
  33. self.frozen = False
  34. self.methods.update({
  35. 'add': self.add_method,
  36. 'add_all': self.add_all_method,
  37. 'all_sources': self.all_sources_method,
  38. 'all_dependencies': self.all_dependencies_method,
  39. 'apply': self.apply_method,
  40. })
  41. def check_source_files(self, arg, allow_deps):
  42. sources = []
  43. deps = []
  44. for x in arg:
  45. if isinstance(x, (str, mesonlib.File,
  46. GeneratedListHolder, CustomTargetHolder,
  47. CustomTargetIndexHolder)):
  48. sources.append(x)
  49. elif hasattr(x, 'found'):
  50. if not allow_deps:
  51. msg = 'Dependencies are not allowed in the if_false argument.'
  52. raise InvalidArguments(msg)
  53. deps.append(x)
  54. else:
  55. msg = 'Sources must be strings or file-like objects.'
  56. raise InvalidArguments(msg)
  57. mesonlib.check_direntry_issues(sources)
  58. return sources, deps
  59. def check_conditions(self, arg):
  60. keys = []
  61. deps = []
  62. for x in listify(arg):
  63. if isinstance(x, str):
  64. keys.append(x)
  65. elif hasattr(x, 'found'):
  66. deps.append(x)
  67. else:
  68. raise InvalidArguments('Conditions must be strings or dependency object')
  69. return keys, deps
  70. @permittedKwargs(['when', 'if_false', 'if_true'])
  71. def add_method(self, args, kwargs):
  72. if self.frozen:
  73. raise InvalidCode('Tried to use \'add\' after querying the source set')
  74. when = listify(kwargs.get('when', []))
  75. if_true = listify(kwargs.get('if_true', []))
  76. if_false = listify(kwargs.get('if_false', []))
  77. if not when and not if_true and not if_false:
  78. if_true = args
  79. elif args:
  80. raise InterpreterException('add called with both positional and keyword arguments')
  81. keys, dependencies = self.check_conditions(when)
  82. sources, extra_deps = self.check_source_files(if_true, True)
  83. if_false, _ = self.check_source_files(if_false, False)
  84. self.held_object.append(SourceSetRule(keys, sources, if_false, [], dependencies, extra_deps))
  85. @permittedKwargs(['when', 'if_true'])
  86. def add_all_method(self, args, kwargs):
  87. if self.frozen:
  88. raise InvalidCode('Tried to use \'add_all\' after querying the source set')
  89. when = listify(kwargs.get('when', []))
  90. if_true = listify(kwargs.get('if_true', []))
  91. if not when and not if_true:
  92. if_true = args
  93. elif args:
  94. raise InterpreterException('add_all called with both positional and keyword arguments')
  95. keys, dependencies = self.check_conditions(when)
  96. for s in if_true:
  97. if not isinstance(s, SourceSetHolder):
  98. raise InvalidCode('Arguments to \'add_all\' after the first must be source sets')
  99. s.frozen = True
  100. self.held_object.append(SourceSetRule(keys, [], [], if_true, dependencies, []))
  101. def collect(self, enabled_fn, all_sources, into=None):
  102. if not into:
  103. into = SourceFiles(set(), set())
  104. for entry in self.held_object:
  105. if all(x.found() for x in entry.dependencies) and \
  106. all(enabled_fn(key) for key in entry.keys):
  107. into.sources.update(entry.sources)
  108. into.dependencies.update(entry.dependencies)
  109. into.dependencies.update(entry.extra_deps)
  110. for ss in entry.sourcesets:
  111. ss.collect(enabled_fn, all_sources, into)
  112. if not all_sources:
  113. continue
  114. into.sources.update(entry.if_false)
  115. return into
  116. @noKwargs
  117. @noPosargs
  118. def all_sources_method(self, args, kwargs):
  119. self.frozen = True
  120. files = self.collect(lambda x: True, True)
  121. return list(files.sources)
  122. @noKwargs
  123. @noPosargs
  124. @FeatureNew('source_set.all_dependencies() method', '0.52.0')
  125. def all_dependencies_method(self, args, kwargs):
  126. self.frozen = True
  127. files = self.collect(lambda x: True, True)
  128. return list(files.dependencies)
  129. @permittedKwargs(['strict'])
  130. def apply_method(self, args, kwargs):
  131. if len(args) != 1:
  132. raise InterpreterException('Apply takes exactly one argument')
  133. config_data = args[0]
  134. self.frozen = True
  135. strict = kwargs.get('strict', True)
  136. if isinstance(config_data, dict):
  137. def _get_from_config_data(key):
  138. if strict and key not in config_data:
  139. raise InterpreterException('Entry {} not in configuration dictionary.'.format(key))
  140. return config_data.get(key, False)
  141. else:
  142. config_cache = dict()
  143. def _get_from_config_data(key):
  144. nonlocal config_cache
  145. if key not in config_cache:
  146. args = [key] if strict else [key, False]
  147. config_cache[key] = config_data.get_method(args, {})
  148. return config_cache[key]
  149. files = self.collect(_get_from_config_data, False)
  150. res = SourceFilesHolder(files)
  151. return res
  152. class SourceFilesHolder(InterpreterObject, ObjectHolder):
  153. def __init__(self, files):
  154. InterpreterObject.__init__(self)
  155. ObjectHolder.__init__(self, files)
  156. self.methods.update({
  157. 'sources': self.sources_method,
  158. 'dependencies': self.dependencies_method,
  159. })
  160. @noPosargs
  161. @noKwargs
  162. def sources_method(self, args, kwargs):
  163. return list(self.held_object.sources)
  164. @noPosargs
  165. @noKwargs
  166. def dependencies_method(self, args, kwargs):
  167. return list(self.held_object.dependencies)
  168. class SourceSetModule(ExtensionModule):
  169. @FeatureNew('SourceSet module', '0.51.0')
  170. def __init__(self, *args, **kwargs):
  171. super().__init__(*args, **kwargs)
  172. self.snippets.add('source_set')
  173. @noKwargs
  174. @noPosargs
  175. def source_set(self, interpreter, state, args, kwargs):
  176. return SourceSetHolder(interpreter)
  177. def initialize(*args, **kwargs):
  178. return SourceSetModule(*args, **kwargs)