October 23rd 2007 05:24 pm

Building with Scons and an optional SWIG

I now regularly use Scons as a cross-platform software construction tool. It is easy, written in Python, and I know Python, so no problem learning a new language as for CMake. In some cases when I use SWIG, the target platform does not have the SWIG executable. But when compiling a module, Scons must use this executable, whatever you try to do. In this case, one need to create a new SharedLibrary builder, so that this attribute will determine if SWIG is present or if the generated .c or .cpp files must be used instead.

As the modules I try to create are shared libraries, I have to modify the SharedLibrary builder defined in env['BUILDERS']['SharedLibrary']. A decorator will do the trick :

?View Code PYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import re
import os.path
 
def SWIGSharedLibrary(env, library, sources, **args):
  swigre = re.compile('(.*).i')
  if env.WhereIs('swig') is None:
    sourcesbis = []
    for source in sources:
      cName = swigre.sub(r'\1_wrap.c', source)
      cppName = swigre.sub(r'\1_wrap.cpp', source)
      if os.path.exists(cName):
        sourcesbis.append(cName)
      elif os.path.exists(cppName):
        sourcesbis.append(cppName)
      else:
        sourcesbis.append(source)
  else:
    sourcesbis = sources
  cat=env.BeforeSWIGSharedLibrary(library, sourcesbis, **args)
  return cat
 
env['BUILDERS']['BeforeSWIGSharedLibrary'] = env['BUILDERS']['SharedLibrary']
env['BUILDERS']['SharedLibrary'] = SWIGSharedLibrary

Here, the new SharedLibrary builder is the SWIGSharedLibrary function. This function determines first if SWIG is present. If it is, nothing is done and the shared library is built as usual. If it is not, the corresponding _wrap.c or _wrap.cpp must be present. This is what the regular expression will do. Then, depending on which file is present, it is added to the sources list.Note that if the regular expression did not match, the name of the file is not modified, so other files than the *.i files are added to the sources list directly.

Finally, the function calls a method named BeforeSWIGSharedLibrary() which will be the previous SharedLibrary builder. This change of name and the actual registration of the new SharedLibrary builder is done with the last two lines.

A final note on this is that the created _wrap.* files must be compilable on the Windows platform. If the source file was created by SWIG under Linux and that specific functions are used under Linux and not Windows, the compilation may fail.

3 Comments »

3 Responses to “Building with Scons and an optional SWIG”

  1. music on 07 Jan 2008 at 5:59 pm #

    very interesting.
    i’m adding in RSS Reader

  2. music on 31 Jan 2008 at 12:52 am #

    What do you mean ?

  3. Matt’s blog » Creating a Python module with Scons and SWIG on 28 Mar 2008 at 8:53 am #

    [...] times ago, I proposed an optional build for SWIG if the SWIG binary was not found on the system. Here I propose an enhancement, a new library [...]

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.

« Welcome ! | Deformation fields with thin-plates »