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 :
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.
very interesting.
i’m adding in RSS Reader
What do you mean ?