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