flex.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # John O'Meara, 2006
  4. # Thomas Nagy, 2006-2018 (ita)
  5. """
  6. The **flex** program is a code generator which creates C or C++ files.
  7. The generated files are compiled into object files.
  8. """
  9. import os, re
  10. from waflib import Task, TaskGen
  11. from waflib.Tools import ccroot
  12. def decide_ext(self, node):
  13. if 'cxx' in self.features:
  14. return ['.lex.cc']
  15. return ['.lex.c']
  16. def flexfun(tsk):
  17. env = tsk.env
  18. bld = tsk.generator.bld
  19. wd = bld.variant_dir
  20. def to_list(xx):
  21. if isinstance(xx, str):
  22. return [xx]
  23. return xx
  24. tsk.last_cmd = lst = []
  25. lst.extend(to_list(env.FLEX))
  26. lst.extend(to_list(env.FLEXFLAGS))
  27. inputs = [a.path_from(tsk.get_cwd()) for a in tsk.inputs]
  28. if env.FLEX_MSYS:
  29. inputs = [x.replace(os.sep, '/') for x in inputs]
  30. lst.extend(inputs)
  31. lst = [x for x in lst if x]
  32. txt = bld.cmd_and_log(lst, cwd=wd, env=env.env or None, quiet=0)
  33. tsk.outputs[0].write(txt.replace('\r\n', '\n').replace('\r', '\n')) # issue #1207
  34. TaskGen.declare_chain(
  35. name = 'flex',
  36. rule = flexfun, # issue #854
  37. ext_in = '.l',
  38. decider = decide_ext,
  39. )
  40. # To support the following:
  41. # bld(features='c', flexflags='-P/foo')
  42. Task.classes['flex'].vars = ['FLEXFLAGS', 'FLEX']
  43. ccroot.USELIB_VARS['c'].add('FLEXFLAGS')
  44. ccroot.USELIB_VARS['cxx'].add('FLEXFLAGS')
  45. def configure(conf):
  46. """
  47. Detect the *flex* program
  48. """
  49. conf.find_program('flex', var='FLEX')
  50. conf.env.FLEXFLAGS = ['-t']
  51. if re.search (r"\\msys\\[0-9.]+\\bin\\flex.exe$", conf.env.FLEX[0]):
  52. # this is the flex shipped with MSYS
  53. conf.env.FLEX_MSYS = True