wscript 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2013 (ita)
  4. VERSION='0.0.1'
  5. APPNAME='dynamic_build3'
  6. """
  7. An advanced dynamic build simulating a call to an external system.
  8. That external build system produces a library which is then used in the current build.
  9. """
  10. import os, shutil, sys
  11. from waflib import Build, Errors, Logs
  12. top = '.'
  13. out = 'build'
  14. def options(opt):
  15. opt.load('compiler_c')
  16. def configure(conf):
  17. conf.load('compiler_c')
  18. def build(bld):
  19. bld.post_mode = Build.POST_LAZY
  20. # declare the temporary build directory for the external library
  21. # it is best to keep it under the project build directory
  22. tmp_dir = bld.bldnode.make_node('external_lib')
  23. # build the external library through an external process
  24. bld(rule=some_fun, target=tmp_dir.make_node('flag.lock'))
  25. # once it is done create a second build group
  26. bld.add_group()
  27. # read the library
  28. bld.read_shlib('foo', paths=[tmp_dir], export_includes=[tmp_dir], export_defines=['A=1'])
  29. # use this library for a target
  30. # no additional build group needed since "app" will wait on "foo" through the use= system
  31. bld.program(source='main.c', target='app', use='foo')
  32. # -----------------------------------------------------------------------------------------
  33. # the following is a pointless exercise simulating the execution of an external buildsystem
  34. # do not spend too much time on it :-)
  35. SNIP = """
  36. top = '.'
  37. out = '.'
  38. def options(opt):
  39. opt.load('compiler_c')
  40. def configure(conf):
  41. conf.load('compiler_c')
  42. def build(bld):
  43. bld.shlib(source='external.c', target='foo', includes='.')
  44. """
  45. def some_fun(task):
  46. # first, clean everything
  47. output_dir = task.outputs[0].parent
  48. shutil.rmtree(output_dir.abspath())
  49. os.makedirs(output_dir.abspath())
  50. # we have a clean directory, create a fake project in it
  51. h_node = output_dir.make_node('external.h')
  52. h_node.write('int zero();\n', flags='w')
  53. c_node = output_dir.make_node('external.c')
  54. c_node.write('int zero() { return 0; }\n', flags='w')
  55. w_node = output_dir.make_node('wscript')
  56. w_node.write(SNIP)
  57. cmd = [sys.executable, sys.argv[0], 'configure', 'build']
  58. cwd = output_dir.abspath()
  59. try:
  60. task.generator.bld.cmd_and_log(cmd, cwd=cwd, quiet=0, output=0)
  61. except Errors.WafError as e:
  62. try:
  63. print(e.stderr)
  64. except AttributeError:
  65. pass
  66. Logs.error("Build of the external library failed")
  67. return -1
  68. Logs.info(' (the external library has been compiled)')
  69. # write a lock file so that a rebuild occurs if files are removed manually
  70. task.outputs[0].write('ok')