asm.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2008-2018 (ita)
  4. """
  5. Assembly support, used by tools such as gas and nasm
  6. To declare targets using assembly::
  7. def configure(conf):
  8. conf.load('gcc gas')
  9. def build(bld):
  10. bld(
  11. features='c cstlib asm',
  12. source = 'test.S',
  13. target = 'asmtest')
  14. bld(
  15. features='asm asmprogram',
  16. source = 'test.S',
  17. target = 'asmtest')
  18. Support for pure asm programs and libraries should also work::
  19. def configure(conf):
  20. conf.load('nasm')
  21. conf.find_program('ld', 'ASLINK')
  22. def build(bld):
  23. bld(
  24. features='asm asmprogram',
  25. source = 'test.S',
  26. target = 'asmtest')
  27. """
  28. import re
  29. from waflib import Errors, Logs, Task
  30. from waflib.Tools.ccroot import link_task, stlink_task
  31. from waflib.TaskGen import extension
  32. from waflib.Tools import c_preproc
  33. re_lines = re.compile(
  34. '^[ \t]*(?:%)[ \t]*(ifdef|ifndef|if|else|elif|endif|include|import|define|undef)[ \t]*(.*)\r*$',
  35. re.IGNORECASE | re.MULTILINE)
  36. class asm_parser(c_preproc.c_parser):
  37. def filter_comments(self, node):
  38. code = node.read()
  39. code = c_preproc.re_nl.sub('', code)
  40. code = c_preproc.re_cpp.sub(c_preproc.repl, code)
  41. return re_lines.findall(code)
  42. class asm(Task.Task):
  43. """
  44. Compiles asm files by gas/nasm/yasm/...
  45. """
  46. color = 'BLUE'
  47. run_str = '${AS} ${ASFLAGS} ${ASMPATH_ST:INCPATHS} ${ASMDEFINES_ST:DEFINES} ${AS_SRC_F}${SRC} ${AS_TGT_F}${TGT}'
  48. def scan(self):
  49. if self.env.ASM_NAME == 'gas':
  50. return c_preproc.scan(self)
  51. elif self.env.ASM_NAME == 'nasm':
  52. Logs.warn('The Nasm dependency scanner is incomplete!')
  53. try:
  54. incn = self.generator.includes_nodes
  55. except AttributeError:
  56. raise Errors.WafError('%r is missing the "asm" feature' % self.generator)
  57. if c_preproc.go_absolute:
  58. nodepaths = incn
  59. else:
  60. nodepaths = [x for x in incn if x.is_child_of(x.ctx.srcnode) or x.is_child_of(x.ctx.bldnode)]
  61. tmp = asm_parser(nodepaths)
  62. tmp.start(self.inputs[0], self.env)
  63. return (tmp.nodes, tmp.names)
  64. @extension('.s', '.S', '.asm', '.ASM', '.spp', '.SPP')
  65. def asm_hook(self, node):
  66. """
  67. Binds the asm extension to the asm task
  68. :param node: input file
  69. :type node: :py:class:`waflib.Node.Node`
  70. """
  71. return self.create_compiled_task('asm', node)
  72. class asmprogram(link_task):
  73. "Links object files into a c program"
  74. run_str = '${ASLINK} ${ASLINKFLAGS} ${ASLNK_TGT_F}${TGT} ${ASLNK_SRC_F}${SRC}'
  75. ext_out = ['.bin']
  76. inst_to = '${BINDIR}'
  77. class asmshlib(asmprogram):
  78. "Links object files into a c shared library"
  79. inst_to = '${LIBDIR}'
  80. class asmstlib(stlink_task):
  81. "Links object files into a c static library"
  82. pass # do not remove
  83. def configure(conf):
  84. conf.env.ASMPATH_ST = '-I%s'
  85. conf.env.ASMDEFINES_ST = '-D%s'