Merged revisions 2647-2719 via svnmerge from
[scons.git] / test / SWIG / build-dir.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 Make sure SWIG works when a VariantDir (or variant_dir) is used.
29
30 Test case courtesy Joe Maruszewski.
31 """
32
33 import os.path
34 import sys
35
36 import TestSCons
37
38 test = TestSCons.TestSCons()
39
40 swig = test.where_is('swig')
41
42 if not swig:
43     test.skip_test('Can not find installed "swig", skipping test.\n')
44
45 # swig-python expects specific filenames.
46 # the platform specific suffix won't necessarily work.
47 if sys.platform == 'win32':
48     _dll = '.dll'
49 else:
50     _dll   = '.so' 
51
52 test.subdir(['source'])
53
54 python_include_dir = test.get_python_inc()
55
56 Python_h = os.path.join(python_include_dir, 'Python.h')
57
58 if not os.path.exists(Python_h):
59     test.skip_test('Can not find %s, skipping test.\n' % Python_h)
60
61 python_frameworks_flags = test.get_python_frameworks_flags()
62
63 test.write(['SConstruct'], """\
64 #
65 # Create the build environment.
66 #
67 env = Environment(CPPPATH = [".", r'%(python_include_dir)s'],
68                   CPPDEFINES = "NDEBUG",
69                   SWIGFLAGS = ["-python", "-c++"],
70                   SWIGCXXFILESUFFIX = "_wrap.cpp",
71                   LDMODULEPREFIX='_',
72                   LDMODULESUFFIX='%(_dll)s',
73                   FRAMEWORKSFLAGS='%(python_frameworks_flags)s')
74
75 import sys
76 if sys.version[0] == '1':
77     # SWIG requires the -classic flag on pre-2.0 Python versions.
78     env.Append(SWIGFLAGS = '-classic')
79
80 Export("env")
81
82 #
83 # Build the libraries.
84 #
85 SConscript("source/SConscript", variant_dir = "build")
86 """ % locals())
87
88 test.write(['source', 'SConscript'], """\
89 Import("env")
90 lib = env.SharedLibrary("_linalg",
91                         "linalg.i",
92                         SHLIBPREFIX = "",
93                         SHLIBSUFFIX = ".pyd")
94 """)
95
96 test.write(['source', 'Vector.hpp'], """\
97 class Vector
98 {
99  public:
100   Vector(int size = 0) : _size(size)
101   {
102     _v = new double[_size];
103     for (int i = 0; i < _size; ++i)
104       _v[i] = 0.0;
105   }
106
107   ~Vector() { delete [] _v; }
108
109   int size() const { return _size; }
110
111   double&        operator[](int key)       { return _v[key]; }
112   double  const& operator[](int key) const { return _v[key]; }
113
114  private:
115   int _size;
116   double* _v;
117 };
118 """)
119
120 test.write(['source', 'linalg.i'], """\
121 %module linalg
122 %{
123 #include <sstream>
124 #include "Vector.hpp"
125 %}
126
127
128 class Vector
129 {
130 public:
131   Vector(int n = 0);
132   ~Vector();
133   
134   %extend
135   {
136     const char* __str__() { return "linalg.Vector()"; }
137     
138     %pythoncode %{
139     def __iter__(self):
140         for i in xrange(len(self)):
141             yield self[i]
142     %}
143   }
144 };
145 """)
146
147 test.write(['source', 'test.py'], """\
148 #!/usr/bin/env python
149 import linalg
150
151
152 x = linalg.Vector(5)
153 print x
154
155 x[1] = 99.5
156 x[3] = 8.3
157 x[4] = 11.1
158
159
160 for i, v in enumerate(x):
161     print "\tx[%d] = %g" % (i, v)
162
163 """)
164
165 test.run(arguments = '.')
166
167 test.up_to_date(arguments = '.')
168
169 test.pass_test()