cbdlib.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #! /usr/bin/env python
  2. import os, sys, imp, re
  3. from waflib import Context, Options, Configure, Utils, Logs
  4. def options(opt):
  5. opt.load('compiler_c')
  6. def configure(conf):
  7. conf.options = Options.options
  8. conf.load('compiler_c')
  9. re_com = re.compile("#.*$", re.M)
  10. def build(bld):
  11. txt = bld.path.find_node('cbit').read()
  12. txt = re_com.sub('', txt)
  13. tg = None
  14. for x in txt.splitlines():
  15. if not x:
  16. continue
  17. elif x.startswith(('\t', ' ')):
  18. tg.rule = x.lstrip()
  19. else:
  20. line = x.split(':')
  21. tgt = line[0].lstrip()
  22. src = line[1].lstrip()
  23. tg = bld()
  24. if src:
  25. tg.source = src
  26. if tgt:
  27. tg.target = tgt
  28. def recurse_rep(x, y):
  29. f = getattr(Context.g_module, x.cmd or x.fun, Utils.nada)
  30. return f(x)
  31. def start(cwd, version, wafdir):
  32. # simple example, the file main.c is hard-coded
  33. try:
  34. os.stat(cwd + os.sep + 'cbit')
  35. except:
  36. print('call from a folder containing a file named "cbit"')
  37. sys.exit(1)
  38. Logs.init_log()
  39. Context.waf_dir = wafdir
  40. Context.top_dir = Context.run_dir = cwd
  41. Context.out_dir = os.path.join(cwd, 'build')
  42. Context.g_module = imp.new_module('wscript')
  43. Context.g_module.root_path = os.path.join(cwd, 'cbit')
  44. Context.Context.recurse = recurse_rep
  45. # this is a fake module, which looks like a standard wscript file
  46. Context.g_module.options = options
  47. Context.g_module.configure = configure
  48. Context.g_module.build = build
  49. Options.OptionsContext().execute()
  50. do_config = 'configure' in sys.argv
  51. try:
  52. os.stat(cwd + os.sep + 'build')
  53. except:
  54. do_config = True
  55. if do_config:
  56. Context.create_context('configure').execute()
  57. if 'clean' in sys.argv:
  58. Context.create_context('clean').execute()
  59. if 'build' in sys.argv:
  60. Context.create_context('build').execute()