Move 2.0 changes collected in branches/pending back to trunk for further
[scons.git] / test / SWIG / live.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Test SWIG behavior with a live, installed SWIG.
29 """
30
31 import os.path
32 import sys
33
34 import TestSCons
35
36 # swig-python expects specific filenames.
37 # the platform specific suffix won't necessarily work.
38 if sys.platform == 'win32':
39     _dll = '.dll'
40 else:
41     _dll   = '.so'
42
43 test = TestSCons.TestSCons()
44
45 swig = test.where_is('swig')
46 if not swig:
47     test.skip_test('Can not find installed "swig", skipping test.\n')
48
49 python, python_include, python_libpath, python_lib = \
50              test.get_platform_python_info()
51 Python_h = os.path.join(python_include, 'Python.h')
52 if not os.path.exists(Python_h):
53     test.skip_test('Can not find %s, skipping test.\n' % Python_h)
54
55 # handle testing on other platforms:
56 ldmodule_prefix = '_'
57
58 test.write("wrapper.py",
59 """import os
60 import sys
61 open('%s', 'wb').write("wrapper.py\\n")
62 os.system(" ".join(sys.argv[1:]))
63 """ % test.workpath('wrapper.out').replace('\\', '\\\\'))
64
65 test.write('SConstruct', """\
66 foo = Environment(SWIGFLAGS='-python',
67                   CPPPATH=[r'%(python_include)s'],
68                   LDMODULEPREFIX='%(ldmodule_prefix)s',
69                   LDMODULESUFFIX='%(_dll)s',
70                   SWIG=[r'%(swig)s'],
71                   LIBPATH=[r'%(python_libpath)s'],
72                   LIBS='%(python_lib)s',
73                   )
74
75 import sys
76 if sys.version[0] == '1':
77     # SWIG requires the -classic flag on pre-2.0 Python versions.
78     foo.Append(SWIGFLAGS = ' -classic')
79
80 swig = foo.Dictionary('SWIG')
81 bar = foo.Clone(SWIG = [r'%(python)s', 'wrapper.py', swig])
82 foo.LoadableModule(target = 'foo', source = ['foo.c', 'foo.i'])
83 bar.LoadableModule(target = 'bar', source = ['bar.c', 'bar.i'])
84 """ % locals())
85
86 test.write("foo.c", """\
87 char *
88 foo_string()
89 {
90     return "This is foo.c!";
91 }
92 """)
93
94 test.write("foo.i", """\
95 %module foo
96 %{
97 /* Put header files here (optional) */
98 /*
99  * This duplication shouldn't be necessary, I guess, but it seems
100  * to suppress "cast to pointer from integer of different size"
101  * warning messages on some systems.
102  */
103 extern char *foo_string();
104 %}
105
106 extern char *foo_string();
107 """)
108
109 test.write("bar.c", """\
110 char *
111 bar_string()
112 {
113     return "This is bar.c!";
114 }
115 """)
116
117 test.write("bar.i", """\
118 %module \t bar
119 %{
120 /* Put header files here (optional) */
121 /*
122  * This duplication shouldn't be necessary, I guess, but it seems
123  * to suppress "cast to pointer from integer of different size"
124  * warning messages on some systems.
125  */
126 extern char *bar_string();
127 %}
128
129 extern char *bar_string();
130 """)
131
132 test.run(arguments = ldmodule_prefix+'foo' + _dll)
133
134 test.must_not_exist(test.workpath('wrapper.out'))
135
136 test.run(program = python, stdin = """\
137 import foo
138 print foo.foo_string()
139 """, stdout="""\
140 This is foo.c!
141 """)
142
143 test.up_to_date(arguments = ldmodule_prefix+'foo' + _dll)
144
145 test.run(arguments = ldmodule_prefix+'bar' + _dll)
146
147 test.must_match('wrapper.out', "wrapper.py\n")
148
149 test.run(program = python, stdin = """\
150 import foo
151 import bar
152 print foo.foo_string()
153 print bar.bar_string()
154 """, stdout="""\
155 This is foo.c!
156 This is bar.c!
157 """)
158
159 test.up_to_date(arguments = '.')
160
161
162 test.pass_test()
163
164 # Local Variables:
165 # tab-width:4
166 # indent-tabs-mode:nil
167 # End:
168 # vim: set expandtab tabstop=4 shiftwidth=4: