jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

914 lines
26KB

  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 = kw.get('path', self.bld.path) # by default, 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.TaskGen 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 rebuilds, 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. deps = getattr(self.generator, 'deps', None)
  543. nodes = self.generator.to_nodes(deps)
  544. return [nodes, []]
  545. cls.scan = scan
  546. if use_cache:
  547. cache[key] = cls
  548. # now create one instance
  549. tsk = self.create_task(name)
  550. for x in ('after', 'before', 'ext_in', 'ext_out'):
  551. setattr(tsk, x, getattr(self, x, []))
  552. if hasattr(self, 'stdout'):
  553. tsk.stdout = self.stdout
  554. if hasattr(self, 'stderr'):
  555. tsk.stderr = self.stderr
  556. if getattr(self, 'timeout', None):
  557. tsk.timeout = self.timeout
  558. if getattr(self, 'always', None):
  559. tsk.always_run = True
  560. if getattr(self, 'target', None):
  561. if isinstance(self.target, str):
  562. self.target = self.target.split()
  563. if not isinstance(self.target, list):
  564. self.target = [self.target]
  565. for x in self.target:
  566. if isinstance(x, str):
  567. tsk.outputs.append(self.path.find_or_declare(x))
  568. else:
  569. x.parent.mkdir() # if a node was given, create the required folders
  570. tsk.outputs.append(x)
  571. if getattr(self, 'install_path', None):
  572. self.install_task = self.add_install_files(install_to=self.install_path,
  573. install_from=tsk.outputs, chmod=getattr(self, 'chmod', Utils.O644))
  574. if getattr(self, 'source', None):
  575. tsk.inputs = self.to_nodes(self.source)
  576. # bypass the execution of process_source by setting the source to an empty list
  577. self.source = []
  578. if getattr(self, 'cwd', None):
  579. tsk.cwd = self.cwd
  580. if isinstance(tsk.run, functools.partial):
  581. # Python documentation says: "partial objects defined in classes
  582. # behave like static methods and do not transform into bound
  583. # methods during instance attribute look-up."
  584. tsk.run = functools.partial(tsk.run, tsk)
  585. @feature('seq')
  586. def sequence_order(self):
  587. """
  588. Adds a strict sequential constraint between the tasks generated by task generators.
  589. It works because task generators are posted in order.
  590. It will not post objects which belong to other folders.
  591. Example::
  592. bld(features='javac seq')
  593. bld(features='jar seq')
  594. To start a new sequence, set the attribute seq_start, for example::
  595. obj = bld(features='seq')
  596. obj.seq_start = True
  597. Note that the method is executed in last position. This is more an
  598. example than a widely-used solution.
  599. """
  600. if self.meths and self.meths[-1] != 'sequence_order':
  601. self.meths.append('sequence_order')
  602. return
  603. if getattr(self, 'seq_start', None):
  604. return
  605. # all the tasks previously declared must be run before these
  606. if getattr(self.bld, 'prev', None):
  607. self.bld.prev.post()
  608. for x in self.bld.prev.tasks:
  609. for y in self.tasks:
  610. y.set_run_after(x)
  611. self.bld.prev = self
  612. re_m4 = re.compile(r'@(\w+)@', re.M)
  613. class subst_pc(Task.Task):
  614. """
  615. Creates *.pc* files from *.pc.in*. The task is executed whenever an input variable used
  616. in the substitution changes.
  617. """
  618. def force_permissions(self):
  619. "Private for the time being, we will probably refactor this into run_str=[run1,chmod]"
  620. if getattr(self.generator, 'chmod', None):
  621. for x in self.outputs:
  622. os.chmod(x.abspath(), self.generator.chmod)
  623. def run(self):
  624. "Substitutes variables in a .in file"
  625. if getattr(self.generator, 'is_copy', None):
  626. for i, x in enumerate(self.outputs):
  627. x.write(self.inputs[i].read('rb'), 'wb')
  628. stat = os.stat(self.inputs[i].abspath()) # Preserve mtime of the copy
  629. os.utime(self.outputs[i].abspath(), (stat.st_atime, stat.st_mtime))
  630. self.force_permissions()
  631. return None
  632. if getattr(self.generator, 'fun', None):
  633. ret = self.generator.fun(self)
  634. if not ret:
  635. self.force_permissions()
  636. return ret
  637. code = self.inputs[0].read(encoding=getattr(self.generator, 'encoding', 'latin-1'))
  638. if getattr(self.generator, 'subst_fun', None):
  639. code = self.generator.subst_fun(self, code)
  640. if code is not None:
  641. self.outputs[0].write(code, encoding=getattr(self.generator, 'encoding', 'latin-1'))
  642. self.force_permissions()
  643. return None
  644. # replace all % by %% to prevent errors by % signs
  645. code = code.replace('%', '%%')
  646. # extract the vars foo into lst and replace @foo@ by %(foo)s
  647. lst = []
  648. def repl(match):
  649. g = match.group
  650. if g(1):
  651. lst.append(g(1))
  652. return "%%(%s)s" % g(1)
  653. return ''
  654. code = getattr(self.generator, 're_m4', re_m4).sub(repl, code)
  655. try:
  656. d = self.generator.dct
  657. except AttributeError:
  658. d = {}
  659. for x in lst:
  660. tmp = getattr(self.generator, x, '') or self.env[x] or self.env[x.upper()]
  661. try:
  662. tmp = ''.join(tmp)
  663. except TypeError:
  664. tmp = str(tmp)
  665. d[x] = tmp
  666. code = code % d
  667. self.outputs[0].write(code, encoding=getattr(self.generator, 'encoding', 'latin-1'))
  668. self.generator.bld.raw_deps[self.uid()] = lst
  669. # make sure the signature is updated
  670. try:
  671. delattr(self, 'cache_sig')
  672. except AttributeError:
  673. pass
  674. self.force_permissions()
  675. def sig_vars(self):
  676. """
  677. Compute a hash (signature) of the variables used in the substitution
  678. """
  679. bld = self.generator.bld
  680. env = self.env
  681. upd = self.m.update
  682. if getattr(self.generator, 'fun', None):
  683. upd(Utils.h_fun(self.generator.fun).encode())
  684. if getattr(self.generator, 'subst_fun', None):
  685. upd(Utils.h_fun(self.generator.subst_fun).encode())
  686. # raw_deps: persistent custom values returned by the scanner
  687. vars = self.generator.bld.raw_deps.get(self.uid(), [])
  688. # hash both env vars and task generator attributes
  689. act_sig = bld.hash_env_vars(env, vars)
  690. upd(act_sig)
  691. lst = [getattr(self.generator, x, '') for x in vars]
  692. upd(Utils.h_list(lst))
  693. return self.m.digest()
  694. @extension('.pc.in')
  695. def add_pcfile(self, node):
  696. """
  697. Processes *.pc.in* files to *.pc*. Installs the results to ``${PREFIX}/lib/pkgconfig/`` by default
  698. def build(bld):
  699. bld(source='foo.pc.in', install_path='${LIBDIR}/pkgconfig/')
  700. """
  701. tsk = self.create_task('subst_pc', node, node.change_ext('.pc', '.pc.in'))
  702. self.install_task = self.add_install_files(
  703. install_to=getattr(self, 'install_path', '${LIBDIR}/pkgconfig/'), install_from=tsk.outputs)
  704. class subst(subst_pc):
  705. pass
  706. @feature('subst')
  707. @before_method('process_source', 'process_rule')
  708. def process_subst(self):
  709. """
  710. Defines a transformation that substitutes the contents of *source* files to *target* files::
  711. def build(bld):
  712. bld(
  713. features='subst',
  714. source='foo.c.in',
  715. target='foo.c',
  716. install_path='${LIBDIR}/pkgconfig',
  717. VAR = 'val'
  718. )
  719. The input files are supposed to contain macros of the form *@VAR@*, where *VAR* is an argument
  720. of the task generator object.
  721. This method overrides the processing by :py:meth:`waflib.TaskGen.process_source`.
  722. """
  723. src = Utils.to_list(getattr(self, 'source', []))
  724. if isinstance(src, Node.Node):
  725. src = [src]
  726. tgt = Utils.to_list(getattr(self, 'target', []))
  727. if isinstance(tgt, Node.Node):
  728. tgt = [tgt]
  729. if len(src) != len(tgt):
  730. raise Errors.WafError('invalid number of source/target for %r' % self)
  731. for x, y in zip(src, tgt):
  732. if not x or not y:
  733. raise Errors.WafError('null source or target for %r' % self)
  734. a, b = None, None
  735. if isinstance(x, str) and isinstance(y, str) and x == y:
  736. a = self.path.find_node(x)
  737. b = self.path.get_bld().make_node(y)
  738. if not os.path.isfile(b.abspath()):
  739. b.parent.mkdir()
  740. else:
  741. if isinstance(x, str):
  742. a = self.path.find_resource(x)
  743. elif isinstance(x, Node.Node):
  744. a = x
  745. if isinstance(y, str):
  746. b = self.path.find_or_declare(y)
  747. elif isinstance(y, Node.Node):
  748. b = y
  749. if not a:
  750. raise Errors.WafError('could not find %r for %r' % (x, self))
  751. tsk = self.create_task('subst', a, b)
  752. for k in ('after', 'before', 'ext_in', 'ext_out'):
  753. val = getattr(self, k, None)
  754. if val:
  755. setattr(tsk, k, val)
  756. # paranoid safety measure for the general case foo.in->foo.h with ambiguous dependencies
  757. for xt in HEADER_EXTS:
  758. if b.name.endswith(xt):
  759. tsk.ext_out = tsk.ext_out + ['.h']
  760. break
  761. inst_to = getattr(self, 'install_path', None)
  762. if inst_to:
  763. self.install_task = self.add_install_files(install_to=inst_to,
  764. install_from=b, chmod=getattr(self, 'chmod', Utils.O644))
  765. self.source = []