TaskGen.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2005-2018 (ita)
  4. """
  5. Task generators
  6. The class :py:class:`waflib.TaskGen.task_gen` encapsulates the creation of task objects (low-level code)
  7. The instances can have various parameters, but the creation of task nodes (Task.py)
  8. is deferred. To achieve this, various methods are called from the method "apply"
  9. """
  10. import copy, re, os, functools
  11. from waflib import Task, Utils, Logs, Errors, ConfigSet, Node
  12. feats = Utils.defaultdict(set)
  13. """remember the methods declaring features"""
  14. HEADER_EXTS = ['.h', '.hpp', '.hxx', '.hh']
  15. class task_gen(object):
  16. """
  17. Instances of this class create :py:class:`waflib.Task.Task` when
  18. calling the method :py:meth:`waflib.TaskGen.task_gen.post` from the main thread.
  19. A few notes:
  20. * The methods to call (*self.meths*) can be specified dynamically (removing, adding, ..)
  21. * The 'features' are used to add methods to self.meths and then execute them
  22. * The attribute 'path' is a node representing the location of the task generator
  23. * The tasks created are added to the attribute *tasks*
  24. * The attribute 'idx' is a counter of task generators in the same path
  25. """
  26. mappings = Utils.ordered_iter_dict()
  27. """Mappings are global file extension mappings that are retrieved in the order of definition"""
  28. prec = Utils.defaultdict(set)
  29. """Dict that holds the precedence execution rules for task generator methods"""
  30. def __init__(self, *k, **kw):
  31. """
  32. Task generator objects predefine various attributes (source, target) for possible
  33. processing by process_rule (make-like rules) or process_source (extensions, misc methods)
  34. Tasks are stored on the attribute 'tasks'. They are created by calling methods
  35. listed in ``self.meths`` or referenced in the attribute ``features``
  36. A topological sort is performed to execute the methods in correct order.
  37. The extra key/value elements passed in ``kw`` are set as attributes
  38. """
  39. self.source = []
  40. self.target = ''
  41. self.meths = []
  42. """
  43. List of method names to execute (internal)
  44. """
  45. self.features = []
  46. """
  47. List of feature names for bringing new methods in
  48. """
  49. self.tasks = []
  50. """
  51. Tasks created are added to this list
  52. """
  53. if not 'bld' in kw:
  54. # task generators without a build context :-/
  55. self.env = ConfigSet.ConfigSet()
  56. self.idx = 0
  57. self.path = None
  58. else:
  59. self.bld = kw['bld']
  60. self.env = self.bld.env.derive()
  61. self.path = self.bld.path # emulate chdir when reading scripts
  62. # Provide a unique index per folder
  63. # This is part of a measure to prevent output file name collisions
  64. path = self.path.abspath()
  65. try:
  66. self.idx = self.bld.idx[path] = self.bld.idx.get(path, 0) + 1
  67. except AttributeError:
  68. self.bld.idx = {}
  69. self.idx = self.bld.idx[path] = 1
  70. # Record the global task generator count
  71. try:
  72. self.tg_idx_count = self.bld.tg_idx_count = self.bld.tg_idx_count + 1
  73. except AttributeError:
  74. self.tg_idx_count = self.bld.tg_idx_count = 1
  75. for key, val in kw.items():
  76. setattr(self, key, val)
  77. def __str__(self):
  78. """Debugging helper"""
  79. return "<task_gen %r declared in %s>" % (self.name, self.path.abspath())
  80. def __repr__(self):
  81. """Debugging helper"""
  82. lst = []
  83. for x in self.__dict__:
  84. if x not in ('env', 'bld', 'compiled_tasks', 'tasks'):
  85. lst.append("%s=%s" % (x, repr(getattr(self, x))))
  86. return "bld(%s) in %s" % (", ".join(lst), self.path.abspath())
  87. def get_cwd(self):
  88. """
  89. Current working directory for the task generator, defaults to the build directory.
  90. This is still used in a few places but it should disappear at some point as the classes
  91. define their own working directory.
  92. :rtype: :py:class:`waflib.Node.Node`
  93. """
  94. return self.bld.bldnode
  95. def get_name(self):
  96. """
  97. If the attribute ``name`` is not set on the instance,
  98. the name is computed from the target name::
  99. def build(bld):
  100. x = bld(name='foo')
  101. x.get_name() # foo
  102. y = bld(target='bar')
  103. y.get_name() # bar
  104. :rtype: string
  105. :return: name of this task generator
  106. """
  107. try:
  108. return self._name
  109. except AttributeError:
  110. if isinstance(self.target, list):
  111. lst = [str(x) for x in self.target]
  112. name = self._name = ','.join(lst)
  113. else:
  114. name = self._name = str(self.target)
  115. return name
  116. def set_name(self, name):
  117. self._name = name
  118. name = property(get_name, set_name)
  119. def to_list(self, val):
  120. """
  121. Ensures that a parameter is a list, see :py:func:`waflib.Utils.to_list`
  122. :type val: string or list of string
  123. :param val: input to return as a list
  124. :rtype: list
  125. """
  126. if isinstance(val, str):
  127. return val.split()
  128. else:
  129. return val
  130. def post(self):
  131. """
  132. Creates tasks for this task generators. The following operations are performed:
  133. #. The body of this method is called only once and sets the attribute ``posted``
  134. #. The attribute ``features`` is used to add more methods in ``self.meths``
  135. #. The methods are sorted by the precedence table ``self.prec`` or `:waflib:attr:waflib.TaskGen.task_gen.prec`
  136. #. The methods are then executed in order
  137. #. The tasks created are added to :py:attr:`waflib.TaskGen.task_gen.tasks`
  138. """
  139. if getattr(self, 'posted', None):
  140. return False
  141. self.posted = True
  142. keys = set(self.meths)
  143. keys.update(feats['*'])
  144. # add the methods listed in the features
  145. self.features = Utils.to_list(self.features)
  146. for x in self.features:
  147. st = feats[x]
  148. if st:
  149. keys.update(st)
  150. elif not x in Task.classes:
  151. Logs.warn('feature %r does not exist - bind at least one method to it?', x)
  152. # copy the precedence table
  153. prec = {}
  154. prec_tbl = self.prec
  155. for x in prec_tbl:
  156. if x in keys:
  157. prec[x] = prec_tbl[x]
  158. # elements disconnected
  159. tmp = []
  160. for a in keys:
  161. for x in prec.values():
  162. if a in x:
  163. break
  164. else:
  165. tmp.append(a)
  166. tmp.sort(reverse=True)
  167. # topological sort
  168. out = []
  169. while tmp:
  170. e = tmp.pop()
  171. if e in keys:
  172. out.append(e)
  173. try:
  174. nlst = prec[e]
  175. except KeyError:
  176. pass
  177. else:
  178. del prec[e]
  179. for x in nlst:
  180. for y in prec:
  181. if x in prec[y]:
  182. break
  183. else:
  184. tmp.append(x)
  185. tmp.sort(reverse=True)
  186. if prec:
  187. buf = ['Cycle detected in the method execution:']
  188. for k, v in prec.items():
  189. buf.append('- %s after %s' % (k, [x for x in v if x in prec]))
  190. raise Errors.WafError('\n'.join(buf))
  191. self.meths = out
  192. # then we run the methods in order
  193. Logs.debug('task_gen: posting %s %d', self, id(self))
  194. for x in out:
  195. try:
  196. v = getattr(self, x)
  197. except AttributeError:
  198. raise Errors.WafError('%r is not a valid task generator method' % x)
  199. Logs.debug('task_gen: -> %s (%d)', x, id(self))
  200. v()
  201. Logs.debug('task_gen: posted %s', self.name)
  202. return True
  203. def get_hook(self, node):
  204. """
  205. Returns the ``@extension`` method to call for a Node of a particular extension.
  206. :param node: Input file to process
  207. :type node: :py:class:`waflib.Tools.Node.Node`
  208. :return: A method able to process the input node by looking at the extension
  209. :rtype: function
  210. """
  211. name = node.name
  212. for k in self.mappings:
  213. try:
  214. if name.endswith(k):
  215. return self.mappings[k]
  216. except TypeError:
  217. # regexps objects
  218. if k.match(name):
  219. return self.mappings[k]
  220. keys = list(self.mappings.keys())
  221. raise Errors.WafError("File %r has no mapping in %r (load a waf tool?)" % (node, keys))
  222. def create_task(self, name, src=None, tgt=None, **kw):
  223. """
  224. Creates task instances.
  225. :param name: task class name
  226. :type name: string
  227. :param src: input nodes
  228. :type src: list of :py:class:`waflib.Tools.Node.Node`
  229. :param tgt: output nodes
  230. :type tgt: list of :py:class:`waflib.Tools.Node.Node`
  231. :return: A task object
  232. :rtype: :py:class:`waflib.Task.Task`
  233. """
  234. task = Task.classes[name](env=self.env.derive(), generator=self)
  235. if src:
  236. task.set_inputs(src)
  237. if tgt:
  238. task.set_outputs(tgt)
  239. task.__dict__.update(kw)
  240. self.tasks.append(task)
  241. return task
  242. def clone(self, env):
  243. """
  244. Makes a copy of a task generator. Once the copy is made, it is necessary to ensure that the
  245. it does not create the same output files as the original, or the same files may
  246. be compiled several times.
  247. :param env: A configuration set
  248. :type env: :py:class:`waflib.ConfigSet.ConfigSet`
  249. :return: A copy
  250. :rtype: :py:class:`waflib.TaskGen.task_gen`
  251. """
  252. newobj = self.bld()
  253. for x in self.__dict__:
  254. if x in ('env', 'bld'):
  255. continue
  256. elif x in ('path', 'features'):
  257. setattr(newobj, x, getattr(self, x))
  258. else:
  259. setattr(newobj, x, copy.copy(getattr(self, x)))
  260. newobj.posted = False
  261. if isinstance(env, str):
  262. newobj.env = self.bld.all_envs[env].derive()
  263. else:
  264. newobj.env = env.derive()
  265. return newobj
  266. def declare_chain(name='', rule=None, reentrant=None, color='BLUE',
  267. ext_in=[], ext_out=[], before=[], after=[], decider=None, scan=None, install_path=None, shell=False):
  268. """
  269. Creates a new mapping and a task class for processing files by extension.
  270. See Tools/flex.py for an example.
  271. :param name: name for the task class
  272. :type name: string
  273. :param rule: function to execute or string to be compiled in a function
  274. :type rule: string or function
  275. :param reentrant: re-inject the output file in the process (done automatically, set to 0 to disable)
  276. :type reentrant: int
  277. :param color: color for the task output
  278. :type color: string
  279. :param ext_in: execute the task only after the files of such extensions are created
  280. :type ext_in: list of string
  281. :param ext_out: execute the task only before files of such extensions are processed
  282. :type ext_out: list of string
  283. :param before: execute instances of this task before classes of the given names
  284. :type before: list of string
  285. :param after: execute instances of this task after classes of the given names
  286. :type after: list of string
  287. :param decider: if present, function that returns a list of output file extensions (overrides ext_out for output files, but not for the build order)
  288. :type decider: function
  289. :param scan: scanner function for the task
  290. :type scan: function
  291. :param install_path: installation path for the output nodes
  292. :type install_path: string
  293. """
  294. ext_in = Utils.to_list(ext_in)
  295. ext_out = Utils.to_list(ext_out)
  296. if not name:
  297. name = rule
  298. cls = Task.task_factory(name, rule, color=color, ext_in=ext_in, ext_out=ext_out, before=before, after=after, scan=scan, shell=shell)
  299. def x_file(self, node):
  300. if ext_in:
  301. _ext_in = ext_in[0]
  302. tsk = self.create_task(name, node)
  303. cnt = 0
  304. ext = decider(self, node) if decider else cls.ext_out
  305. for x in ext:
  306. k = node.change_ext(x, ext_in=_ext_in)
  307. tsk.outputs.append(k)
  308. if reentrant != None:
  309. if cnt < int(reentrant):
  310. self.source.append(k)
  311. else:
  312. # reinject downstream files into the build
  313. for y in self.mappings: # ~ nfile * nextensions :-/
  314. if k.name.endswith(y):
  315. self.source.append(k)
  316. break
  317. cnt += 1
  318. if install_path:
  319. self.install_task = self.add_install_files(install_to=install_path, install_from=tsk.outputs)
  320. return tsk
  321. for x in cls.ext_in:
  322. task_gen.mappings[x] = x_file
  323. return x_file
  324. def taskgen_method(func):
  325. """
  326. Decorator that registers method as a task generator method.
  327. The function must accept a task generator as first parameter::
  328. from waflib.TaskGen import taskgen_method
  329. @taskgen_method
  330. def mymethod(self):
  331. pass
  332. :param func: task generator method to add
  333. :type func: function
  334. :rtype: function
  335. """
  336. setattr(task_gen, func.__name__, func)
  337. return func
  338. def feature(*k):
  339. """
  340. Decorator that registers a task generator method that will be executed when the
  341. object attribute ``feature`` contains the corresponding key(s)::
  342. from waflib.Task import feature
  343. @feature('myfeature')
  344. def myfunction(self):
  345. print('that is my feature!')
  346. def build(bld):
  347. bld(features='myfeature')
  348. :param k: feature names
  349. :type k: list of string
  350. """
  351. def deco(func):
  352. setattr(task_gen, func.__name__, func)
  353. for name in k:
  354. feats[name].update([func.__name__])
  355. return func
  356. return deco
  357. def before_method(*k):
  358. """
  359. Decorator that registera task generator method which will be executed
  360. before the functions of given name(s)::
  361. from waflib.TaskGen import feature, before
  362. @feature('myfeature')
  363. @before_method('fun2')
  364. def fun1(self):
  365. print('feature 1!')
  366. @feature('myfeature')
  367. def fun2(self):
  368. print('feature 2!')
  369. def build(bld):
  370. bld(features='myfeature')
  371. :param k: method names
  372. :type k: list of string
  373. """
  374. def deco(func):
  375. setattr(task_gen, func.__name__, func)
  376. for fun_name in k:
  377. task_gen.prec[func.__name__].add(fun_name)
  378. return func
  379. return deco
  380. before = before_method
  381. def after_method(*k):
  382. """
  383. Decorator that registers a task generator method which will be executed
  384. after the functions of given name(s)::
  385. from waflib.TaskGen import feature, after
  386. @feature('myfeature')
  387. @after_method('fun2')
  388. def fun1(self):
  389. print('feature 1!')
  390. @feature('myfeature')
  391. def fun2(self):
  392. print('feature 2!')
  393. def build(bld):
  394. bld(features='myfeature')
  395. :param k: method names
  396. :type k: list of string
  397. """
  398. def deco(func):
  399. setattr(task_gen, func.__name__, func)
  400. for fun_name in k:
  401. task_gen.prec[fun_name].add(func.__name__)
  402. return func
  403. return deco
  404. after = after_method
  405. def extension(*k):
  406. """
  407. Decorator that registers a task generator method which will be invoked during
  408. the processing of source files for the extension given::
  409. from waflib import Task
  410. class mytask(Task):
  411. run_str = 'cp ${SRC} ${TGT}'
  412. @extension('.moo')
  413. def create_maa_file(self, node):
  414. self.create_task('mytask', node, node.change_ext('.maa'))
  415. def build(bld):
  416. bld(source='foo.moo')
  417. """
  418. def deco(func):
  419. setattr(task_gen, func.__name__, func)
  420. for x in k:
  421. task_gen.mappings[x] = func
  422. return func
  423. return deco
  424. @taskgen_method
  425. def to_nodes(self, lst, path=None):
  426. """
  427. Flatten the input list of string/nodes/lists into a list of nodes.
  428. It is used by :py:func:`waflib.TaskGen.process_source` and :py:func:`waflib.TaskGen.process_rule`.
  429. It is designed for source files, for folders, see :py:func:`waflib.Tools.ccroot.to_incnodes`:
  430. :param lst: input list
  431. :type lst: list of string and nodes
  432. :param path: path from which to search the nodes (by default, :py:attr:`waflib.TaskGen.task_gen.path`)
  433. :type path: :py:class:`waflib.Tools.Node.Node`
  434. :rtype: list of :py:class:`waflib.Tools.Node.Node`
  435. """
  436. tmp = []
  437. path = path or self.path
  438. find = path.find_resource
  439. if isinstance(lst, Node.Node):
  440. lst = [lst]
  441. for x in Utils.to_list(lst):
  442. if isinstance(x, str):
  443. node = find(x)
  444. elif hasattr(x, 'name'):
  445. node = x
  446. else:
  447. tmp.extend(self.to_nodes(x))
  448. continue
  449. if not node:
  450. raise Errors.WafError('source not found: %r in %r' % (x, self))
  451. tmp.append(node)
  452. return tmp
  453. @feature('*')
  454. def process_source(self):
  455. """
  456. Processes each element in the attribute ``source`` by extension.
  457. #. The *source* list is converted through :py:meth:`waflib.TaskGen.to_nodes` to a list of :py:class:`waflib.Node.Node` first.
  458. #. File extensions are mapped to methods having the signature: ``def meth(self, node)`` by :py:meth:`waflib.TaskGen.extension`
  459. #. The method is retrieved through :py:meth:`waflib.TaskGen.task_gen.get_hook`
  460. #. When called, the methods may modify self.source to append more source to process
  461. #. The mappings can map an extension or a filename (see the code below)
  462. """
  463. self.source = self.to_nodes(getattr(self, 'source', []))
  464. for node in self.source:
  465. self.get_hook(node)(self, node)
  466. @feature('*')
  467. @before_method('process_source')
  468. def process_rule(self):
  469. """
  470. Processes the attribute ``rule``. When present, :py:meth:`waflib.TaskGen.process_source` is disabled::
  471. def build(bld):
  472. bld(rule='cp ${SRC} ${TGT}', source='wscript', target='bar.txt')
  473. Main attributes processed:
  474. * rule: command to execute, it can be a tuple of strings for multiple commands
  475. * chmod: permissions for the resulting files (integer value such as Utils.O755)
  476. * shell: set to False to execute the command directly (default is True to use a shell)
  477. * scan: scanner function
  478. * vars: list of variables to trigger rebuilts, such as CFLAGS
  479. * cls_str: string to display when executing the task
  480. * cls_keyword: label to display when executing the task
  481. * cache_rule: by default, try to re-use similar classes, set to False to disable
  482. * source: list of Node or string objects representing the source files required by this task
  483. * target: list of Node or string objects representing the files that this task creates
  484. * cwd: current working directory (Node or string)
  485. * stdout: standard output, set to None to prevent waf from capturing the text
  486. * stderr: standard error, set to None to prevent waf from capturing the text
  487. * timeout: timeout for command execution (Python 3)
  488. * always: whether to always run the command (False by default)
  489. * deep_inputs: whether the task must depend on the input file tasks too (False by default)
  490. """
  491. if not getattr(self, 'rule', None):
  492. return
  493. # create the task class
  494. name = str(getattr(self, 'name', None) or self.target or getattr(self.rule, '__name__', self.rule))
  495. # or we can put the class in a cache for performance reasons
  496. try:
  497. cache = self.bld.cache_rule_attr
  498. except AttributeError:
  499. cache = self.bld.cache_rule_attr = {}
  500. chmod = getattr(self, 'chmod', None)
  501. shell = getattr(self, 'shell', True)
  502. color = getattr(self, 'color', 'BLUE')
  503. scan = getattr(self, 'scan', None)
  504. _vars = getattr(self, 'vars', [])
  505. cls_str = getattr(self, 'cls_str', None)
  506. cls_keyword = getattr(self, 'cls_keyword', None)
  507. use_cache = getattr(self, 'cache_rule', 'True')
  508. deep_inputs = getattr(self, 'deep_inputs', False)
  509. scan_val = has_deps = hasattr(self, 'deps')
  510. if scan:
  511. scan_val = id(scan)
  512. key = Utils.h_list((name, self.rule, chmod, shell, color, cls_str, cls_keyword, scan_val, _vars, deep_inputs))
  513. cls = None
  514. if use_cache:
  515. try:
  516. cls = cache[key]
  517. except KeyError:
  518. pass
  519. if not cls:
  520. rule = self.rule
  521. if chmod is not None:
  522. def chmod_fun(tsk):
  523. for x in tsk.outputs:
  524. os.chmod(x.abspath(), tsk.generator.chmod)
  525. if isinstance(rule, tuple):
  526. rule = list(rule)
  527. rule.append(chmod_fun)
  528. rule = tuple(rule)
  529. else:
  530. rule = (rule, chmod_fun)
  531. cls = Task.task_factory(name, rule, _vars, shell=shell, color=color)
  532. if cls_str:
  533. setattr(cls, '__str__', self.cls_str)
  534. if cls_keyword:
  535. setattr(cls, 'keyword', self.cls_keyword)
  536. if deep_inputs:
  537. Task.deep_inputs(cls)
  538. if scan:
  539. cls.scan = self.scan
  540. elif has_deps:
  541. def scan(self):
  542. nodes = []
  543. for x in self.generator.to_list(getattr(self.generator, 'deps', None)):
  544. node = self.generator.path.find_resource(x)
  545. if not node:
  546. self.generator.bld.fatal('Could not find %r (was it declared?)' % x)
  547. nodes.append(node)
  548. return [nodes, []]
  549. cls.scan = scan
  550. if use_cache:
  551. cache[key] = cls
  552. # now create one instance
  553. tsk = self.create_task(name)
  554. for x in ('after', 'before', 'ext_in', 'ext_out'):
  555. setattr(tsk, x, getattr(self, x, []))
  556. if hasattr(self, 'stdout'):
  557. tsk.stdout = self.stdout
  558. if hasattr(self, 'stderr'):
  559. tsk.stderr = self.stderr
  560. if getattr(self, 'timeout', None):
  561. tsk.timeout = self.timeout
  562. if getattr(self, 'always', None):
  563. tsk.always_run = True
  564. if getattr(self, 'target', None):
  565. if isinstance(self.target, str):
  566. self.target = self.target.split()
  567. if not isinstance(self.target, list):
  568. self.target = [self.target]
  569. for x in self.target:
  570. if isinstance(x, str):
  571. tsk.outputs.append(self.path.find_or_declare(x))
  572. else:
  573. x.parent.mkdir() # if a node was given, create the required folders
  574. tsk.outputs.append(x)
  575. if getattr(self, 'install_path', None):
  576. self.install_task = self.add_install_files(install_to=self.install_path,
  577. install_from=tsk.outputs, chmod=getattr(self, 'chmod', Utils.O644))
  578. if getattr(self, 'source', None):
  579. tsk.inputs = self.to_nodes(self.source)
  580. # bypass the execution of process_source by setting the source to an empty list
  581. self.source = []
  582. if getattr(self, 'cwd', None):
  583. tsk.cwd = self.cwd
  584. if isinstance(tsk.run, functools.partial):
  585. # Python documentation says: "partial objects defined in classes
  586. # behave like static methods and do not transform into bound
  587. # methods during instance attribute look-up."
  588. tsk.run = functools.partial(tsk.run, tsk)
  589. @feature('seq')
  590. def sequence_order(self):
  591. """
  592. Adds a strict sequential constraint between the tasks generated by task generators.
  593. It works because task generators are posted in order.
  594. It will not post objects which belong to other folders.
  595. Example::
  596. bld(features='javac seq')
  597. bld(features='jar seq')
  598. To start a new sequence, set the attribute seq_start, for example::
  599. obj = bld(features='seq')
  600. obj.seq_start = True
  601. Note that the method is executed in last position. This is more an
  602. example than a widely-used solution.
  603. """
  604. if self.meths and self.meths[-1] != 'sequence_order':
  605. self.meths.append('sequence_order')
  606. return
  607. if getattr(self, 'seq_start', None):
  608. return
  609. # all the tasks previously declared must be run before these
  610. if getattr(self.bld, 'prev', None):
  611. self.bld.prev.post()
  612. for x in self.bld.prev.tasks:
  613. for y in self.tasks:
  614. y.set_run_after(x)
  615. self.bld.prev = self
  616. re_m4 = re.compile('@(\w+)@', re.M)
  617. class subst_pc(Task.Task):
  618. """
  619. Creates *.pc* files from *.pc.in*. The task is executed whenever an input variable used
  620. in the substitution changes.
  621. """
  622. def force_permissions(self):
  623. "Private for the time being, we will probably refactor this into run_str=[run1,chmod]"
  624. if getattr(self.generator, 'chmod', None):
  625. for x in self.outputs:
  626. os.chmod(x.abspath(), self.generator.chmod)
  627. def run(self):
  628. "Substitutes variables in a .in file"
  629. if getattr(self.generator, 'is_copy', None):
  630. for i, x in enumerate(self.outputs):
  631. x.write(self.inputs[i].read('rb'), 'wb')
  632. stat = os.stat(self.inputs[i].abspath()) # Preserve mtime of the copy
  633. os.utime(self.outputs[i].abspath(), (stat.st_atime, stat.st_mtime))
  634. self.force_permissions()
  635. return None
  636. if getattr(self.generator, 'fun', None):
  637. ret = self.generator.fun(self)
  638. if not ret:
  639. self.force_permissions()
  640. return ret
  641. code = self.inputs[0].read(encoding=getattr(self.generator, 'encoding', 'latin-1'))
  642. if getattr(self.generator, 'subst_fun', None):
  643. code = self.generator.subst_fun(self, code)
  644. if code is not None:
  645. self.outputs[0].write(code, encoding=getattr(self.generator, 'encoding', 'latin-1'))
  646. self.force_permissions()
  647. return None
  648. # replace all % by %% to prevent errors by % signs
  649. code = code.replace('%', '%%')
  650. # extract the vars foo into lst and replace @foo@ by %(foo)s
  651. lst = []
  652. def repl(match):
  653. g = match.group
  654. if g(1):
  655. lst.append(g(1))
  656. return "%%(%s)s" % g(1)
  657. return ''
  658. code = getattr(self.generator, 're_m4', re_m4).sub(repl, code)
  659. try:
  660. d = self.generator.dct
  661. except AttributeError:
  662. d = {}
  663. for x in lst:
  664. tmp = getattr(self.generator, x, '') or self.env[x] or self.env[x.upper()]
  665. try:
  666. tmp = ''.join(tmp)
  667. except TypeError:
  668. tmp = str(tmp)
  669. d[x] = tmp
  670. code = code % d
  671. self.outputs[0].write(code, encoding=getattr(self.generator, 'encoding', 'latin-1'))
  672. self.generator.bld.raw_deps[self.uid()] = lst
  673. # make sure the signature is updated
  674. try:
  675. delattr(self, 'cache_sig')
  676. except AttributeError:
  677. pass
  678. self.force_permissions()
  679. def sig_vars(self):
  680. """
  681. Compute a hash (signature) of the variables used in the substitution
  682. """
  683. bld = self.generator.bld
  684. env = self.env
  685. upd = self.m.update
  686. if getattr(self.generator, 'fun', None):
  687. upd(Utils.h_fun(self.generator.fun).encode())
  688. if getattr(self.generator, 'subst_fun', None):
  689. upd(Utils.h_fun(self.generator.subst_fun).encode())
  690. # raw_deps: persistent custom values returned by the scanner
  691. vars = self.generator.bld.raw_deps.get(self.uid(), [])
  692. # hash both env vars and task generator attributes
  693. act_sig = bld.hash_env_vars(env, vars)
  694. upd(act_sig)
  695. lst = [getattr(self.generator, x, '') for x in vars]
  696. upd(Utils.h_list(lst))
  697. return self.m.digest()
  698. @extension('.pc.in')
  699. def add_pcfile(self, node):
  700. """
  701. Processes *.pc.in* files to *.pc*. Installs the results to ``${PREFIX}/lib/pkgconfig/`` by default
  702. def build(bld):
  703. bld(source='foo.pc.in', install_path='${LIBDIR}/pkgconfig/')
  704. """
  705. tsk = self.create_task('subst_pc', node, node.change_ext('.pc', '.pc.in'))
  706. self.install_task = self.add_install_files(
  707. install_to=getattr(self, 'install_path', '${LIBDIR}/pkgconfig/'), install_from=tsk.outputs)
  708. class subst(subst_pc):
  709. pass
  710. @feature('subst')
  711. @before_method('process_source', 'process_rule')
  712. def process_subst(self):
  713. """
  714. Defines a transformation that substitutes the contents of *source* files to *target* files::
  715. def build(bld):
  716. bld(
  717. features='subst',
  718. source='foo.c.in',
  719. target='foo.c',
  720. install_path='${LIBDIR}/pkgconfig',
  721. VAR = 'val'
  722. )
  723. The input files are supposed to contain macros of the form *@VAR@*, where *VAR* is an argument
  724. of the task generator object.
  725. This method overrides the processing by :py:meth:`waflib.TaskGen.process_source`.
  726. """
  727. src = Utils.to_list(getattr(self, 'source', []))
  728. if isinstance(src, Node.Node):
  729. src = [src]
  730. tgt = Utils.to_list(getattr(self, 'target', []))
  731. if isinstance(tgt, Node.Node):
  732. tgt = [tgt]
  733. if len(src) != len(tgt):
  734. raise Errors.WafError('invalid number of source/target for %r' % self)
  735. for x, y in zip(src, tgt):
  736. if not x or not y:
  737. raise Errors.WafError('null source or target for %r' % self)
  738. a, b = None, None
  739. if isinstance(x, str) and isinstance(y, str) and x == y:
  740. a = self.path.find_node(x)
  741. b = self.path.get_bld().make_node(y)
  742. if not os.path.isfile(b.abspath()):
  743. b.parent.mkdir()
  744. else:
  745. if isinstance(x, str):
  746. a = self.path.find_resource(x)
  747. elif isinstance(x, Node.Node):
  748. a = x
  749. if isinstance(y, str):
  750. b = self.path.find_or_declare(y)
  751. elif isinstance(y, Node.Node):
  752. b = y
  753. if not a:
  754. raise Errors.WafError('could not find %r for %r' % (x, self))
  755. tsk = self.create_task('subst', a, b)
  756. for k in ('after', 'before', 'ext_in', 'ext_out'):
  757. val = getattr(self, k, None)
  758. if val:
  759. setattr(tsk, k, val)
  760. # paranoid safety measure for the general case foo.in->foo.h with ambiguous dependencies
  761. for xt in HEADER_EXTS:
  762. if b.name.endswith(xt):
  763. tsk.ext_in = tsk.ext_in + ['.h']
  764. break
  765. inst_to = getattr(self, 'install_path', None)
  766. if inst_to:
  767. self.install_task = self.add_install_files(install_to=inst_to,
  768. install_from=b, chmod=getattr(self, 'chmod', Utils.O644))
  769. self.source = []