You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
7.4 KiB

6 years ago
  1. """SCons.Tool.swig
  2. Tool-specific initialization for swig.
  3. There normally shouldn't be any need to import this module directly.
  4. It will usually be imported through the generic SCons.Tool.Tool()
  5. selection method.
  6. """
  7. from __future__ import print_function
  8. #
  9. # Copyright (c) 2001 - 2017 The SCons Foundation
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining
  12. # a copy of this software and associated documentation files (the
  13. # "Software"), to deal in the Software without restriction, including
  14. # without limitation the rights to use, copy, modify, merge, publish,
  15. # distribute, sublicense, and/or sell copies of the Software, and to
  16. # permit persons to whom the Software is furnished to do so, subject to
  17. # the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included
  20. # in all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  23. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  24. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. #
  30. __revision__ = "src/engine/SCons/Tool/swig.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  31. import os.path
  32. import re
  33. import subprocess
  34. import SCons.Action
  35. import SCons.Defaults
  36. import SCons.Tool
  37. import SCons.Util
  38. import SCons.Node
  39. verbose = False
  40. swigs = [ 'swig', 'swig3.0', 'swig2.0' ]
  41. SwigAction = SCons.Action.Action('$SWIGCOM', '$SWIGCOMSTR')
  42. def swigSuffixEmitter(env, source):
  43. if '-c++' in SCons.Util.CLVar(env.subst("$SWIGFLAGS", source=source)):
  44. return '$SWIGCXXFILESUFFIX'
  45. else:
  46. return '$SWIGCFILESUFFIX'
  47. # Match '%module test', as well as '%module(directors="1") test'
  48. # Also allow for test to be quoted (SWIG permits double quotes, but not single)
  49. # Also allow for the line to have spaces after test if not quoted
  50. _reModule = re.compile(r'%module(\s*\(.*\))?\s+("?)(\S+)\2')
  51. def _find_modules(src):
  52. """Find all modules referenced by %module lines in `src`, a SWIG .i file.
  53. Returns a list of all modules, and a flag set if SWIG directors have
  54. been requested (SWIG will generate an additional header file in this
  55. case.)"""
  56. directors = 0
  57. mnames = []
  58. try:
  59. matches = _reModule.findall(open(src).read())
  60. except IOError:
  61. # If the file's not yet generated, guess the module name from the file stem
  62. matches = []
  63. mnames.append(os.path.splitext(os.path.basename(src))[0])
  64. for m in matches:
  65. mnames.append(m[2])
  66. directors = directors or m[0].find('directors') >= 0
  67. return mnames, directors
  68. def _add_director_header_targets(target, env):
  69. # Directors only work with C++ code, not C
  70. suffix = env.subst(env['SWIGCXXFILESUFFIX'])
  71. # For each file ending in SWIGCXXFILESUFFIX, add a new target director
  72. # header by replacing the ending with SWIGDIRECTORSUFFIX.
  73. for x in target[:]:
  74. n = x.name
  75. d = x.dir
  76. if n[-len(suffix):] == suffix:
  77. target.append(d.File(n[:-len(suffix)] + env['SWIGDIRECTORSUFFIX']))
  78. def _swigEmitter(target, source, env):
  79. swigflags = env.subst("$SWIGFLAGS", target=target, source=source)
  80. flags = SCons.Util.CLVar(swigflags)
  81. for src in source:
  82. src = str(src.rfile())
  83. mnames = None
  84. if "-python" in flags and "-noproxy" not in flags:
  85. if mnames is None:
  86. mnames, directors = _find_modules(src)
  87. if directors:
  88. _add_director_header_targets(target, env)
  89. python_files = [m + ".py" for m in mnames]
  90. outdir = env.subst('$SWIGOUTDIR', target=target, source=source)
  91. # .py files should be generated in SWIGOUTDIR if specified,
  92. # otherwise in the same directory as the target
  93. if outdir:
  94. python_files = [env.fs.File(os.path.join(outdir, j)) for j in python_files]
  95. else:
  96. python_files = [target[0].dir.File(m) for m in python_files]
  97. target.extend(python_files)
  98. if "-java" in flags:
  99. if mnames is None:
  100. mnames, directors = _find_modules(src)
  101. if directors:
  102. _add_director_header_targets(target, env)
  103. java_files = [[m + ".java", m + "JNI.java"] for m in mnames]
  104. java_files = SCons.Util.flatten(java_files)
  105. outdir = env.subst('$SWIGOUTDIR', target=target, source=source)
  106. if outdir:
  107. java_files = [os.path.join(outdir, j) for j in java_files]
  108. java_files = list(map(env.fs.File, java_files))
  109. def t_from_s(t, p, s, x):
  110. return t.dir
  111. tsm = SCons.Node._target_from_source_map
  112. tkey = len(tsm)
  113. tsm[tkey] = t_from_s
  114. for jf in java_files:
  115. jf._func_target_from_source = tkey
  116. target.extend(java_files)
  117. return (target, source)
  118. def _get_swig_version(env, swig):
  119. """Run the SWIG command line tool to get and return the version number"""
  120. swig = env.subst(swig)
  121. pipe = SCons.Action._subproc(env, SCons.Util.CLVar(swig) + ['-version'],
  122. stdin = 'devnull',
  123. stderr = 'devnull',
  124. stdout = subprocess.PIPE)
  125. if pipe.wait() != 0: return
  126. # MAYBE: out = SCons.Util.to_str (pipe.stdout.read())
  127. out = SCons.Util.to_str(pipe.stdout.read())
  128. match = re.search('SWIG Version\s+(\S+).*', out, re.MULTILINE)
  129. if match:
  130. if verbose: print("Version is:%s"%match.group(1))
  131. return match.group(1)
  132. else:
  133. if verbose: print("Unable to detect version: [%s]"%out)
  134. def generate(env):
  135. """Add Builders and construction variables for swig to an Environment."""
  136. c_file, cxx_file = SCons.Tool.createCFileBuilders(env)
  137. c_file.suffix['.i'] = swigSuffixEmitter
  138. cxx_file.suffix['.i'] = swigSuffixEmitter
  139. c_file.add_action('.i', SwigAction)
  140. c_file.add_emitter('.i', _swigEmitter)
  141. cxx_file.add_action('.i', SwigAction)
  142. cxx_file.add_emitter('.i', _swigEmitter)
  143. java_file = SCons.Tool.CreateJavaFileBuilder(env)
  144. java_file.suffix['.i'] = swigSuffixEmitter
  145. java_file.add_action('.i', SwigAction)
  146. java_file.add_emitter('.i', _swigEmitter)
  147. if 'SWIG' not in env:
  148. env['SWIG'] = env.Detect(swigs) or swigs[0]
  149. env['SWIGVERSION'] = _get_swig_version(env, env['SWIG'])
  150. env['SWIGFLAGS'] = SCons.Util.CLVar('')
  151. env['SWIGDIRECTORSUFFIX'] = '_wrap.h'
  152. env['SWIGCFILESUFFIX'] = '_wrap$CFILESUFFIX'
  153. env['SWIGCXXFILESUFFIX'] = '_wrap$CXXFILESUFFIX'
  154. env['_SWIGOUTDIR'] = r'${"-outdir \"%s\"" % SWIGOUTDIR}'
  155. env['SWIGPATH'] = []
  156. env['SWIGINCPREFIX'] = '-I'
  157. env['SWIGINCSUFFIX'] = ''
  158. env['_SWIGINCFLAGS'] = '$( ${_concat(SWIGINCPREFIX, SWIGPATH, SWIGINCSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'
  159. env['SWIGCOM'] = '$SWIG -o $TARGET ${_SWIGOUTDIR} ${_SWIGINCFLAGS} $SWIGFLAGS $SOURCES'
  160. def exists(env):
  161. swig = env.get('SWIG') or env.Detect(['swig'])
  162. return swig
  163. # Local Variables:
  164. # tab-width:4
  165. # indent-tabs-mode:nil
  166. # End:
  167. # vim: set expandtab tabstop=4 shiftwidth=4: