wscript 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #! /usr/bin/env python
  2. """
  3. Create a few c programs, measure the execution times on "waf times"
  4. """
  5. def options(opt):
  6. opt.load('compiler_c')
  7. def configure(conf):
  8. conf.load('compiler_c')
  9. def build(bld):
  10. # add a task to create a .c file
  11. from waflib.TaskGen import feature, before_method
  12. @feature('foo')
  13. @before_method('process_source')
  14. def add_one_c_file(self):
  15. node = self.path.find_or_declare('main%d.c' % self.num)
  16. self.create_task('write_file', [], node)
  17. self.source = [node] # add the .c file to the list of source
  18. # write a slow main.c file
  19. from waflib.Task import Task
  20. class write_file(Task):
  21. def run(self):
  22. self.outputs[0].write('''
  23. #include <stdio.h>
  24. int main() {
  25. int i;
  26. char buf[50];
  27. for (i=0; i < %d; ++i) {
  28. sprintf(buf, "%%l\\n", i);
  29. }
  30. return 0;
  31. }''' % 2 ** (13 + self.generator.num))
  32. for i in range(10):
  33. # create a few programs
  34. bld(features='foo c cprogram', num=i, target='app%d' % i)
  35. if bld.cmd == 'times':
  36. # measure the execution times when calling "waf times"
  37. def measure(ctx):
  38. for x in range(10):
  39. tg = ctx.get_tgen_by_name('app%d' % x)
  40. name = tg.link_task.outputs[0].abspath()
  41. ctx.exec_command('time %s' % name, shell=True)
  42. bld.add_post_fun(measure)
  43. from waflib.Build import BuildContext
  44. class times(BuildContext):
  45. cmd = 'times'