wscript 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #! /usr/bin/env python
  2. # encoding: UTF-8
  3. # Peter Forai
  4. # Thomas Nagy, 2008
  5. """
  6. Demonstrates how to create a c++ app that runs python scripts
  7. Useful for apps providing script extensions
  8. """
  9. VERSION='0.0.1'
  10. APPNAME='swig_test'
  11. top = '.'
  12. out = 'build'
  13. def options(opt):
  14. opt.load('g++ python')
  15. def configure(conf):
  16. conf.load('g++ python')
  17. conf.check_python_version((2,4,2))
  18. conf.check_python_headers()
  19. conf.load('swig')
  20. if conf.check_swig_version() < (1, 2, 27):
  21. conf.fatal('this swig version is too old')
  22. try:
  23. conf.load('java')
  24. # on mandriva, at least, libjvm.so is difficult to find
  25. #conf.env.LIBPATH_JAVA = "/usr/lib/jvm/java-1.6.0-sun-1.6.0.13/jre/lib/amd64/server/"
  26. conf.check_jni_headers()
  27. conf.env.HAVE_JAVA = True
  28. except conf.errors.ConfigurationError:
  29. conf.env.HAVE_JAVA = False
  30. def build(bld):
  31. # embedding
  32. #
  33. # use swig_flags = '-c++ -python -debug-classes' for debugging
  34. obj = bld(
  35. features = 'cxx cxxprogram pyembed',
  36. source = 'embed/src1.cpp embed/bind.swig',
  37. target = 'embed/embed_demo',
  38. swig_flags = '-c++ -python -Wall',
  39. includes = '. embed')
  40. # extending
  41. #
  42. # be careful that the .py produced by swig is mandatory for using the library
  43. #
  44. # it is possible to disable 'mylib', and to add extend/a.cpp
  45. # to the source of extend/python/_test_swig_waf and remove use
  46. bld(
  47. features = 'cxx cxxshlib',
  48. source = 'extend/a.cpp',
  49. target = 'extend/mylib',
  50. includes = 'extend',
  51. export_includes = 'extend',
  52. vnum = '1.2.3',
  53. name = 'mylib')
  54. bld(
  55. features = 'cxx cxxshlib pyext',
  56. source = 'extend/python/test_swig_waf.i',
  57. target = 'extend/python/_test_swig_waf',
  58. swig_flags = '-c++ -python -Wall',
  59. includes = 'extend',
  60. vnum = '1.2.3',
  61. use = 'mylib')
  62. bld.add_group()
  63. python_site_package = '${PREFIX}/lib/python%s/site-packages' % bld.env.PYTHON_VERSION
  64. generated_py = bld.path.find_or_declare('extend/python/test_swig_waf.py')
  65. bld(features='py', source=generated_py, install_path=python_site_package, install_from=bld.path.get_bld())
  66. bld.add_post_fun(exec_test_python)
  67. # some java stuff
  68. if not bld.env.HAVE_JAVA:
  69. return
  70. from waflib.extras import swig
  71. srcdir = bld.path.get_bld().make_node('extend/java') # destination for generated java file
  72. #""" # BEGIN BLOCK 1
  73. d = bld.path.make_node('extend/java/foo/bar/pouet')
  74. javanodes = [d.find_or_declare(x) for x in 'A.java test_swig_waf.java test_swig_wafJNI.java'.split()]
  75. dec = bld.tools['swig'].swigf
  76. #@dec <- python 2.3 does not support the @decorator notation
  77. def swig_java(tsk):
  78. tsk.outputs.extend(javanodes)
  79. bld.tools['swig'].swigf(swig_java)
  80. """ # END BLOCK 1
  81. #"""# do not remove
  82. bld(
  83. features = 'cxx cxxshlib',
  84. source = 'extend/java/test_swig_waf.i',
  85. target = 'extend/java/_test_swig_waf',
  86. swig_flags = '-c++ -java -package foo.bar.pouet -outdir extend/java/foo/bar/pouet',
  87. includes = 'extend',
  88. vnum = '1.2.3',
  89. uselib = 'JAVA',
  90. use = 'mylib')
  91. #""" # BEGIN BLOCK 2
  92. """ # END BLOCK 2
  93. def move_java_files(task):
  94. import os, shutil
  95. from waflib import Utils
  96. node = srcdir.make_node('foo/bar/pouet/')
  97. files = Utils.listdir(node.abspath())
  98. for x in files:
  99. if x.endswith('.java'):
  100. # create a node in the directory we want to
  101. j = node.make_node(x) # create a node
  102. # depend on the .i file to make sure the .java files are copied after swig is executed
  103. bld(name='move_and_read', rule=move_java_files, source='extend/java/test_swig_waf.i', after=['swig'], before=['javac'])
  104. #"""
  105. bld(rule='cp ${SRC} ${TGT}', source=bld.path.find_resource('extend/java/Foo.java'), target=srcdir.make_node('foo/bar/pouet/Foo.java'), before=['javac'], after=['swig'])
  106. tmp = bld.path.get_bld().make_node('maha')
  107. bld(features = 'javac jar',
  108. srcdir = srcdir,
  109. sourcepath = [],
  110. outdir = tmp, # we do need another folder here
  111. basedir = tmp,
  112. destfile = 'maha.jar'
  113. )
  114. bld.add_post_fun(exec_test_java)
  115. #########################################
  116. # listing the java nodes is required to ensure the swig task
  117. # is executed whenever the java files are removed from
  118. # the build directory
  119. #
  120. # to list the java files automatically, comment the starting character '#' in the lines "BEGIN BLOCK 1" and "BEGIN BLOCK 2"
  121. def exec_test_java(bld):
  122. try:
  123. bld.cmd_and_log('LD_LIBRARY_PATH=$LD_LIBRARY_PATH:build/extend/java:build/extend java -classpath "build/maha.jar:." foo.bar.pouet.Foo')
  124. except:
  125. pass
  126. def exec_test_python(bld):
  127. import os, stat
  128. try:
  129. import subprocess
  130. proc = subprocess.Popen('''
  131. PYTHONPATH=$PYTHONPATH:build/extend/python
  132. LD_LIBRARY_PATH=$LD_LIBRARY_PATH:build/extend/python:build/extend
  133. python -c "import test_swig_waf; a=test_swig_waf.A(); print('Testing: a.add(2, 3) -> %r' % a.add(2, 3))"
  134. '''.replace('\n', ' '), shell=True)
  135. proc.wait()
  136. except:
  137. pass
  138. # why does this fail now on mandriva???
  139. try:
  140. os.stat('build/embed/embed_demo')
  141. bld.cmd_and_log('PYTHONPATH=$PYTHONPATH:build/embed/ build/embed/embed_demo')
  142. except:
  143. pass