3 Tool-specific initialization for swig.
5 There normally shouldn't be any need to import this module directly.
6 It will usually be imported through the generic SCons.Tool.Tool()
12 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
14 # Permission is hereby granted, free of charge, to any person obtaining
15 # a copy of this software and associated documentation files (the
16 # "Software"), to deal in the Software without restriction, including
17 # without limitation the rights to use, copy, modify, merge, publish,
18 # distribute, sublicense, and/or sell copies of the Software, and to
19 # permit persons to whom the Software is furnished to do so, subject to
20 # the following conditions:
22 # The above copyright notice and this permission notice shall be included
23 # in all copies or substantial portions of the Software.
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
26 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
27 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 __revision__ = "src/engine/SCons/Tool/swig.py 3842 2008/12/20 22:59:52 scons"
45 SwigAction = SCons.Action.Action('$SWIGCOM', '$SWIGCOMSTR')
47 def swigSuffixEmitter(env, source):
48 if '-c++' in SCons.Util.CLVar(env.subst("$SWIGFLAGS", source=source)):
49 return '$SWIGCXXFILESUFFIX'
51 return '$SWIGCFILESUFFIX'
53 # Match '%module test', as well as '%module(directors="1") test'
54 _reModule = re.compile(r'%module(?:\s*\(.*\))?\s+(.+)')
56 def _swigEmitter(target, source, env):
57 swigflags = env.subst("$SWIGFLAGS", target=target, source=source)
58 flags = SCons.Util.CLVar(swigflags)
60 src = str(src.rfile())
62 if "-python" in flags and "-noproxy" not in flags:
64 mnames = _reModule.findall(open(src).read())
65 target.extend(map(lambda m, d=target[0].dir:
66 d.File(m + ".py"), mnames))
69 mnames = _reModule.findall(open(src).read())
70 java_files = map(lambda m: [m + ".java", m + "JNI.java"], mnames)
71 java_files = SCons.Util.flatten(java_files)
72 outdir = env.subst('$SWIGOUTDIR', target=target, source=source)
74 java_files = map(lambda j, o=outdir: os.path.join(o, j), java_files)
75 java_files = map(env.fs.File, java_files)
77 t_from_s = lambda t, p, s, x: t.dir
78 SCons.Util.AddMethod(jf, t_from_s, 'target_from_source')
79 target.extend(java_files)
80 return (target, source)
83 """Add Builders and construction variables for swig to an Environment."""
84 c_file, cxx_file = SCons.Tool.createCFileBuilders(env)
86 c_file.suffix['.i'] = swigSuffixEmitter
87 cxx_file.suffix['.i'] = swigSuffixEmitter
89 c_file.add_action('.i', SwigAction)
90 c_file.add_emitter('.i', _swigEmitter)
91 cxx_file.add_action('.i', SwigAction)
92 cxx_file.add_emitter('.i', _swigEmitter)
94 java_file = SCons.Tool.CreateJavaFileBuilder(env)
96 java_file.suffix['.i'] = swigSuffixEmitter
98 java_file.add_action('.i', SwigAction)
99 java_file.add_emitter('.i', _swigEmitter)
102 env['SWIGFLAGS'] = SCons.Util.CLVar('')
103 env['SWIGCFILESUFFIX'] = '_wrap$CFILESUFFIX'
104 env['SWIGCXXFILESUFFIX'] = '_wrap$CXXFILESUFFIX'
105 env['_SWIGOUTDIR'] = '${"-outdir " + str(SWIGOUTDIR)}'
107 env['SWIGINCPREFIX'] = '-I'
108 env['SWIGINCSUFFIX'] = ''
109 env['_SWIGINCFLAGS'] = '$( ${_concat(SWIGINCPREFIX, SWIGPATH, SWIGINCSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'
110 env['SWIGCOM'] = '$SWIG -o $TARGET ${_SWIGOUTDIR} ${_SWIGINCFLAGS} $SWIGFLAGS $SOURCES'
112 expr = '^[ \t]*%[ \t]*(?:include|import|extern)[ \t]*(<|"?)([^>\s"]+)(?:>|"?)'
113 scanner = SCons.Scanner.ClassicCPP("SWIGScan", ".i", "SWIGPATH", expr)
115 env.Append(SCANNERS = scanner)
118 return env.Detect(['swig'])