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.

74 lines
1.7KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2006-2010 (ita)
  4. """
  5. Dumb C/C++ preprocessor for finding dependencies
  6. It will look at all include files it can find after removing the comments, so the following
  7. will always add the dependency on both "a.h" and "b.h"::
  8. #include "a.h"
  9. #ifdef B
  10. #include "b.h"
  11. #endif
  12. int main() {
  13. return 0;
  14. }
  15. To use::
  16. def configure(conf):
  17. conf.load('compiler_c')
  18. conf.load('c_dumbpreproc')
  19. """
  20. import re, sys, os, string, traceback
  21. from waflib import Logs, Build, Utils, Errors
  22. from waflib.Logs import debug, error
  23. from waflib.Tools import c_preproc
  24. re_inc = re.compile(
  25. '^[ \t]*(#|%:)[ \t]*(include)[ \t]*[<"](.*)[>"]\r*$',
  26. re.IGNORECASE | re.MULTILINE)
  27. def lines_includes(node):
  28. code = node.read()
  29. if c_preproc.use_trigraphs:
  30. for (a, b) in c_preproc.trig_def: code = code.split(a).join(b)
  31. code = c_preproc.re_nl.sub('', code)
  32. code = c_preproc.re_cpp.sub(c_preproc.repl, code)
  33. return [(m.group(2), m.group(3)) for m in re.finditer(re_inc, code)]
  34. parser = c_preproc.c_parser
  35. class dumb_parser(parser):
  36. def addlines(self, node):
  37. if node in self.nodes[:-1]:
  38. return
  39. self.currentnode_stack.append(node.parent)
  40. # Avoid reading the same files again
  41. try:
  42. lines = self.parse_cache[node]
  43. except KeyError:
  44. lines = self.parse_cache[node] = lines_includes(node)
  45. self.lines = lines + [(c_preproc.POPFILE, '')] + self.lines
  46. def start(self, node, env):
  47. try:
  48. self.parse_cache = node.ctx.parse_cache
  49. except AttributeError:
  50. self.parse_cache = node.ctx.parse_cache = {}
  51. self.addlines(node)
  52. while self.lines:
  53. (x, y) = self.lines.pop(0)
  54. if x == c_preproc.POPFILE:
  55. self.currentnode_stack.pop()
  56. continue
  57. self.tryfind(y)
  58. c_preproc.c_parser = dumb_parser