Add some documentation to the SCons-version-switching hack
[senf.git] / scons / scons-1.2.0 / engine / SCons / Tool / swig.py
1 """SCons.Tool.swig
2
3 Tool-specific initialization for swig.
4
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()
7 selection method.
8
9 """
10
11 #
12 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
13 #
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:
21 #
22 # The above copyright notice and this permission notice shall be included
23 # in all copies or substantial portions of the Software.
24 #
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.
32 #
33
34 __revision__ = "src/engine/SCons/Tool/swig.py 3842 2008/12/20 22:59:52 scons"
35
36 import os.path
37 import re
38
39 import SCons.Action
40 import SCons.Defaults
41 import SCons.Scanner
42 import SCons.Tool
43 import SCons.Util
44
45 SwigAction = SCons.Action.Action('$SWIGCOM', '$SWIGCOMSTR')
46
47 def swigSuffixEmitter(env, source):
48     if '-c++' in SCons.Util.CLVar(env.subst("$SWIGFLAGS", source=source)):
49         return '$SWIGCXXFILESUFFIX'
50     else:
51         return '$SWIGCFILESUFFIX'
52
53 # Match '%module test', as well as '%module(directors="1") test'
54 _reModule = re.compile(r'%module(?:\s*\(.*\))?\s+(.+)')
55
56 def _swigEmitter(target, source, env):
57     swigflags = env.subst("$SWIGFLAGS", target=target, source=source)
58     flags = SCons.Util.CLVar(swigflags)
59     for src in source:
60         src = str(src.rfile())
61         mnames = None
62         if "-python" in flags and "-noproxy" not in flags:
63             if mnames is None:
64                 mnames = _reModule.findall(open(src).read())
65             target.extend(map(lambda m, d=target[0].dir:
66                                      d.File(m + ".py"), mnames))
67         if "-java" in flags:
68             if mnames is None:
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)
73             if outdir:
74                  java_files = map(lambda j, o=outdir: os.path.join(o, j), java_files)
75             java_files = map(env.fs.File, java_files)
76             for jf in 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)
81
82 def generate(env):
83     """Add Builders and construction variables for swig to an Environment."""
84     c_file, cxx_file = SCons.Tool.createCFileBuilders(env)
85
86     c_file.suffix['.i'] = swigSuffixEmitter
87     cxx_file.suffix['.i'] = swigSuffixEmitter
88
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)
93
94     java_file = SCons.Tool.CreateJavaFileBuilder(env)
95
96     java_file.suffix['.i'] = swigSuffixEmitter
97
98     java_file.add_action('.i', SwigAction)
99     java_file.add_emitter('.i', _swigEmitter)
100
101     env['SWIG']              = 'swig'
102     env['SWIGFLAGS']         = SCons.Util.CLVar('')
103     env['SWIGCFILESUFFIX']   = '_wrap$CFILESUFFIX'
104     env['SWIGCXXFILESUFFIX'] = '_wrap$CXXFILESUFFIX'
105     env['_SWIGOUTDIR']       = '${"-outdir " + str(SWIGOUTDIR)}'
106     env['SWIGPATH']          = []
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'
111
112     expr = '^[ \t]*%[ \t]*(?:include|import|extern)[ \t]*(<|"?)([^>\s"]+)(?:>|"?)'
113     scanner = SCons.Scanner.ClassicCPP("SWIGScan", ".i", "SWIGPATH", expr)
114
115     env.Append(SCANNERS = scanner)
116
117 def exists(env):
118     return env.Detect(['swig'])