winres.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Brant Young, 2007
  4. "Process *.rc* files for C/C++: X{.rc -> [.res|.rc.o]}"
  5. import re
  6. from waflib import Task
  7. from waflib.TaskGen import extension
  8. from waflib.Tools import c_preproc
  9. @extension('.rc')
  10. def rc_file(self, node):
  11. """
  12. Binds the .rc extension to a winrc task
  13. """
  14. obj_ext = '.rc.o'
  15. if self.env.WINRC_TGT_F == '/fo':
  16. obj_ext = '.res'
  17. rctask = self.create_task('winrc', node, node.change_ext(obj_ext))
  18. try:
  19. self.compiled_tasks.append(rctask)
  20. except AttributeError:
  21. self.compiled_tasks = [rctask]
  22. re_lines = re.compile(
  23. '(?:^[ \t]*(#|%:)[ \t]*(ifdef|ifndef|if|else|elif|endif|include|import|define|undef|pragma)[ \t]*(.*?)\s*$)|'\
  24. '(?:^\w+[ \t]*(ICON|BITMAP|CURSOR|HTML|FONT|MESSAGETABLE|TYPELIB|REGISTRY|D3DFX)[ \t]*(.*?)\s*$)',
  25. re.IGNORECASE | re.MULTILINE)
  26. class rc_parser(c_preproc.c_parser):
  27. """
  28. Calculates dependencies in .rc files
  29. """
  30. def filter_comments(self, node):
  31. """
  32. Overrides :py:meth:`waflib.Tools.c_preproc.c_parser.filter_comments`
  33. """
  34. code = node.read()
  35. if c_preproc.use_trigraphs:
  36. for (a, b) in c_preproc.trig_def:
  37. code = code.split(a).join(b)
  38. code = c_preproc.re_nl.sub('', code)
  39. code = c_preproc.re_cpp.sub(c_preproc.repl, code)
  40. ret = []
  41. for m in re.finditer(re_lines, code):
  42. if m.group(2):
  43. ret.append((m.group(2), m.group(3)))
  44. else:
  45. ret.append(('include', m.group(5)))
  46. return ret
  47. class winrc(Task.Task):
  48. """
  49. Compiles resource files
  50. """
  51. run_str = '${WINRC} ${WINRCFLAGS} ${CPPPATH_ST:INCPATHS} ${DEFINES_ST:DEFINES} ${WINRC_TGT_F} ${TGT} ${WINRC_SRC_F} ${SRC}'
  52. color = 'BLUE'
  53. def scan(self):
  54. tmp = rc_parser(self.generator.includes_nodes)
  55. tmp.start(self.inputs[0], self.env)
  56. return (tmp.nodes, tmp.names)
  57. def configure(conf):
  58. """
  59. Detects the programs RC or windres, depending on the C/C++ compiler in use
  60. """
  61. v = conf.env
  62. if not v.WINRC:
  63. if v.CC_NAME == 'msvc':
  64. conf.find_program('RC', var='WINRC', path_list=v.PATH)
  65. v.WINRC_TGT_F = '/fo'
  66. v.WINRC_SRC_F = ''
  67. else:
  68. conf.find_program('windres', var='WINRC', path_list=v.PATH)
  69. v.WINRC_TGT_F = '-o'
  70. v.WINRC_SRC_F = '-i'