Skip to main content

SCons tool to generate Python modules using SWIG

Project description

PyPi package version Travis CI build status https://ci.appveyor.com/api/projects/status/github/ptomulik/scons-tool-swigpy?svg=true

SCons tool to generate Python modules using Swig.

Installation

There are few ways to install this tool for your project.

From pypi

This method may be preferable if you build your project under a virtualenv. To add swigpy tool from pypi, type (within your wirtualenv):

pip install scons-tool-loader scons-tool-swigpy

or, if your project uses pipenv:

pipenv install --dev scons-tool-loader scons-tool-swigpy

Alternatively, you may add this to your Pipfile

[dev-packages]
scons-tool-loader = "*"
scons-tool-swigpy = "*"

The tool will be installed as a namespaced package sconstool.swigpy in project’s virtual environment. You may further use scons-tool-loader to load the tool.

As a git submodule

  1. Create new git repository:

    mkdir /tmp/prj && cd /tmp/prj
    touch README.rst
    git init
  2. Add the scons-tool-swigpy as a submodule:

    git submodule add git://github.com/ptomulik/scons-tool-swigpy.git site_scons/site_tools/swigpy
  3. For python 2.x create __init__.py in site_tools directory:

    touch site_scons/site_tools/__init__.py

    this will allow to directly import site_tools.swigpy (this may be required by other tools).

Usage example

Git-based projects

  1. Create some source files, for example src/hello.hpp and src/hello.cpp:

    // src/hello.hpp
    #ifndef HELLO_HPP
    #define HELLO_HPP
    void hello();
    #endif
    // src/hello.cpp
    #include "hello.hpp"
    #include <iostream>
    void hello() { std::cout << "Hello" << std::endl; }
  2. Create swig interface file src/hello.i

    // src/hello.i
    %module hello;
    %{
    #include "hello.hpp"
    %}
    %include "hello.hpp"
  3. Write SConstruct file:

    # SConstruct
    env = Environment(tools = [ 'default', 'swigpy' ])
    SConscript('src/SConscript', exports=['env'], variant_dir='build', duplicate=0)
  4. Write src/SConscript:

    # src/SConscript
    Import(['env'])
    env.Append( SWIGPY_CPPPATH = ['.'] )
    env.Append( SWIGPY_LIBPATH = ['.'] )
    env.Append( SWIGPY_SWIGFLAGS = ['-c++'] )
    env.SharedLibrary( 'hello', ['hello.cpp'] )
    env.SwigPyModule( 'hello', SWIGPY_LIBS = ['$SWIGPY_PYTHONLIB', 'hello'] )
  5. Try it out:

    scons

    This shall create a library build/libhello.so and all the files that comprise its python wrapper:

    ptomulik@tea:$ ls build/
    hello.os  hello.pyc  hello_wrap.cc  libhello.so
    hello.py  _hello.so  hello_wrap.os
  6. Test the generated wrapper:

    cd build
    LD_LIBRARY_PATH='.' python
    >>> import hello
    >>> hello.hello()

Details

Module description

The module provides a SwigPyModule() builder which generates python module based on a swig interface *.i file:

SwigPyModule(modname, **overrides)

The modname is a name of the module being generated, for example 'foo' or 'foo.bar' (note, it’s neither the source file name nor target file name). The overrides overwrite construction variables such as SWIGFLAGS or CFLAGS.

Example 1:

The following code expects a foo.i interface file to be present and generates python module defined by this file.

SwigPyModule('foo')

Example 2:

The following code expects a foo/bar.i interface file to be present and generates python module defined by this file undef foo subdirectory.

SwigPyModule('foo.bar')

Construction variables

Construction variables used by SwigPyModule are summarized in the following table. Note that there are two groups of variables. The first group are the well known variables such as CFLAGS or SWIGFLAGS. The second group are the variables prefixed with SWIGPY_. These variables, if defined, overwrite the well known variables when generating python bindings.

Variable

Default

SWIG

SWIGVERSION

SWIGFLAGS

SWIGDIRECTORSUFFIX

SWIGCFILESUFFIX

SWIGCXXFILESUFFIX

SWIGPATH

SWIGINCPREFIX

SWIGINCSUFFIX

SWIGCOM

CPPPATH

SHLIBPREFIX

CCFLAGS

CFLAGS

CXXFLAGS

LIBS

LIBPATH

LDFLAGS

SWIGPY_SWIG

SWIGPY_SWIGVERSION

SWIGPY_SWIGFLAGS

[-python', '-builtin']

SWIGPY_SWIGDIRECTORSUFFIX

SWIGPY_SWIGCFILESUFFIX

SWIGPY_SWIGCXXFILESUFFIX

SWIGPY_SWIGPATH

SWIGPY_SWIGINCPREFIX

SWIGPY_SWIGINCSUFFIX

SWIGPY_SWIGCOM

SWIGPY_CPPPATH

["$SWIGPY_PYTHONINCDIR"]

SWIGPY_SHOBJPREFIX

SWIGPY_SHOBJSUFFIX

SWIGPY_SHLIBPREFIX

'_'

SWIGPY_SHLIBSUFFIX

.pyd on Windows

SWIGPY_LIBPREFIX

'_'

SWIGPY_LIBSUFFIX

SWIGPY_IMPLIBPREFIX

'_'

SWIGPY_IMPLIBSUFFIX

SWIGPY_WINDOWSEXPPREFIX

'_'

SWIGPY_WINDOWSEXPSUFFIX

SWIGPY_CC

SWIGPY_CXX

SWIGPY_SHCC

SWIGPY_SHCXX

SWIGPY_CCFLAGS

SWIGPY_CFLAGS

SWIGPY_CXXFLAGS

SWIGPY_SHCCFLAGS

SWIGPY_SHCFLAGS

SWIGPY_SHCXXFLAGS

SWIGPY_LIBS

["$SWIGPY_PYTHONLIB"]

SWIGPY_LIBPATH

["$SWIGPY_PYTHONLIBDIR"]

SWIGPY_LINKFLAGS

SWIGPY_M2SWIGFILE

lambda parts: path.join(*parts) + '.i'

SWIGPY_M2CFILE

lambda parts: path.join(*parts)

SWIGPY_M2SHLIBFILE

lambda parts: path.join(*parts)

The SWIGPY_M2SWIGFILE lambda determines the name of swig interface (source file). The SWIGPY_M2CFILE determines the name (without suffix) of the C or C++ wrapper file being generated by swig. The SWIGPY_M2SHLIBFILE determines the name of shared library that will contain the wrapper binary code after compilation (without prefix and suffix). The parts provided to any of these macros are the parts of modname, that is they’re result of modname.split('.').

LICENSE

Copyright (c) 2014-2018 by Pawel Tomulik <ptomulik@meil.pw.edu.pl>

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scons-tool-swigpy-0.1.2.tar.gz (8.0 kB view hashes)

Uploaded Source

Built Distribution

scons_tool_swigpy-0.1.2-py3-none-any.whl (9.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page