Source

SCons / test / SWIG / build-dir.py

#!/usr/bin/env python
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

"""
Make sure SWIG works when a VariantDir (or variant_dir) is used.

Test case courtesy Joe Maruszewski.
"""

import os.path
import sys

import TestSCons

test = TestSCons.TestSCons()

swig = test.where_is('swig')

if not swig:
    test.skip_test('Can not find installed "swig", skipping test.\n')

# swig-python expects specific filenames.
# the platform specific suffix won't necessarily work.
if sys.platform == 'win32':
    _dll = '.dll'
else:
    _dll   = '.so' 

test.subdir(['source'])

python_include_dir = test.get_python_inc()

Python_h = os.path.join(python_include_dir, 'Python.h')

if not os.path.exists(Python_h):
    test.skip_test('Can not find %s, skipping test.\n' % Python_h)

python_frameworks_flags = test.get_python_frameworks_flags()

test.write(['SConstruct'], """\
#
# Create the build environment.
#
env = Environment(CPPPATH = [".", r'%(python_include_dir)s'],
                  CPPDEFINES = "NDEBUG",
                  SWIGFLAGS = ["-python", "-c++"],
                  SWIGCXXFILESUFFIX = "_wrap.cpp",
                  LDMODULEPREFIX='_',
                  LDMODULESUFFIX='%(_dll)s',
                  FRAMEWORKSFLAGS='%(python_frameworks_flags)s')

import sys
if sys.version[0] == '1':
    # SWIG requires the -classic flag on pre-2.0 Python versions.
    env.Append(SWIGFLAGS = '-classic')

Export("env")

#
# Build the libraries.
#
SConscript("source/SConscript", variant_dir = "build")
""" % locals())

test.write(['source', 'SConscript'], """\
Import("env")
lib = env.SharedLibrary("_linalg",
                        "linalg.i",
                        SHLIBPREFIX = "",
                        SHLIBSUFFIX = ".pyd")
""")

test.write(['source', 'Vector.hpp'], """\
class Vector
{
 public:
  Vector(int size = 0) : _size(size)
  {
    _v = new double[_size];
    for (int i = 0; i < _size; ++i)
      _v[i] = 0.0;
  }

  ~Vector() { delete [] _v; }

  int size() const { return _size; }

  double&        operator[](int key)       { return _v[key]; }
  double  const& operator[](int key) const { return _v[key]; }

 private:
  int _size;
  double* _v;
};
""")

test.write(['source', 'linalg.i'], """\
%module linalg
%{
#include <sstream>
#include "Vector.hpp"
%}


class Vector
{
public:
  Vector(int n = 0);
  ~Vector();
  
  %extend
  {
    const char* __str__() { return "linalg.Vector()"; }
    
    %pythoncode %{
    def __iter__(self):
        for i in xrange(len(self)):
            yield self[i]
    %}
  }
};
""")

test.write(['source', 'test.py'], """\
#!/usr/bin/env python
import linalg


x = linalg.Vector(5)
print x

x[1] = 99.5
x[3] = 8.3
x[4] = 11.1


for i, v in enumerate(x):
    print "\tx[%d] = %g" % (i, v)

""")

test.run(arguments = '.')

test.up_to_date(arguments = '.')

test.pass_test()