[Distutils] patch for more SWIG support

Lars Immisch lars at ibp.de
Wed Oct 13 22:18:07 CEST 2004


Hi Anthony,

> http://sourceforge.net/tracker/index.php?func=detail&aid=1046404&group_id=5470&atid=105470 

I've found a nuisance.

The following works:

python setup.py build_ext --swig-opts=-I../include

What does not work is something like:

from distutils.core import setup,Extension

setup (name = "aculab",
        ext_modules = [Extension("aculab._lowlevel", ['acu.i'],
                       swig_opts=['-I../include', '-modern']],
        packages = ["aculab"])

To do this, one needs to modify extension.py and core.py. In a nutshell, 
core.py needs 'swig_opts' in extension_keywords and extension.py needs 
it in the argument list of Extension.__init__. Also, 
build_ext.swig_sources needs an extra argument containing the extension.

I have attached a patch that supersedes (i.e. includes) your patch.

It's against 2.3.4.

Your patch didn't work against 2.4a2 and I didn't have time yet to get 
CVS HEAD.

I will send the same against CVS HEAD in an hour or so.

Lars
-------------- next part --------------
--- Lib/distutils/core.orig	2003-02-19 15:16:00.000000000 +0100
+++ Lib/distutils/core.py	2004-10-13 22:02:56.460291200 +0200
@@ -54,7 +54,7 @@
                       'define_macros', 'undef_macros',
                       'library_dirs', 'libraries', 'runtime_library_dirs',
                       'extra_objects', 'extra_compile_args', 'extra_link_args',
-                      'export_symbols', 'depends', 'language')
+                      'export_symbols', 'swig_opts', 'depends', 'language')
 
 def setup (**attrs):
     """The gateway to the Distutils: do everything your setup script needs
--- Lib/distutils/extension.orig	2003-01-27 17:30:36.000000000 +0100
+++ Lib/distutils/extension.py	2004-10-13 22:03:13.665030400 +0200
@@ -70,6 +70,9 @@
         when linking object files together to create the extension (or
         to create a new static Python interpreter).  Similar
         interpretation as for 'extra_compile_args'.
+      swig_opts : [string]
+        any extra options to pass to SWIG if a source file has the .i
+        extension.
       export_symbols : [string]
         list of symbols to be exported from a shared extension.  Not
         used on all platforms, and not generally necessary for Python
@@ -94,6 +97,7 @@
                   extra_objects=None,
                   extra_compile_args=None,
                   extra_link_args=None,
+                  swig_opts=None,
                   export_symbols=None,
                   depends=None,
                   language=None,
@@ -115,6 +119,7 @@
         self.extra_objects = extra_objects or []
         self.extra_compile_args = extra_compile_args or []
         self.extra_link_args = extra_link_args or []
+        self.swig_opts = swig_opts or []
         self.export_symbols = export_symbols or []
         self.depends = depends or []
         self.language = language
--- Lib/distutils/command/build_ext.orig	2002-11-19 14:12:28.000000000 +0100
+++ Lib/distutils/command/build_ext.py	2004-10-13 22:06:30.327817600 +0200
@@ -81,6 +81,11 @@
          "specify the compiler type"),
         ('swig-cpp', None,
          "make SWIG create C++ files (default is C)"),
+        ('swig-opts=', None,
+         "list of SWIG command line options"),
+        ('swig=', None,
+         "path to the SWIG executable"),
+
         ]
 
     boolean_options = ['inplace', 'debug', 'force', 'swig-cpp']
@@ -108,6 +113,8 @@
         self.force = None
         self.compiler = None
         self.swig_cpp = None
+        self.swig_opts = None
+        self.swig = None
 
 
     def finalize_options (self):
@@ -205,6 +212,12 @@
         if self.undef:
             self.undef = string.split(self.undef, ',')
 
+        if self.swig_opts is None:
+            self.swig_opts = []
+        else:
+            self.swig_opts = self.swig_opts.split(' ')
+
+
     # finalize_options ()
 
 
@@ -429,7 +442,7 @@
         # First, scan the sources for SWIG definition files (.i), run
         # SWIG on 'em to create .c files, and modify the sources list
         # accordingly.
-        sources = self.swig_sources(sources)
+        sources = self.swig_sources(sources, ext)
 
         # Next, compile the source code to object files.
 
@@ -492,7 +505,7 @@
             target_lang=language)
 
 
-    def swig_sources (self, sources):
+    def swig_sources (self, sources, extension):
 
         """Walk the list of source files in 'sources', looking for SWIG
         interface (.i) files.  Run SWIG on all that are found, and
@@ -509,7 +522,10 @@
         # source -- but there should be an option to put SWIG output in
         # the temp dir.
 
-        if self.swig_cpp:
+        if self.swig_cpp: 
+            log.warn("--swig-cpp is deprecated - use --swig-opts=-c++")
+
+        if self.swig_cpp or ('-c++' in self.swig_opts):
             target_ext = '.cpp'
         else:
             target_ext = '.c'
@@ -526,11 +542,15 @@
         if not swig_sources:
             return new_sources
 
-        swig = self.find_swig()
+        swig = self.swig or self.find_swig()
         swig_cmd = [swig, "-python"]
+        swig_cmd.extend(self.swig_opts)
         if self.swig_cpp:
             swig_cmd.append("-c++")
 
+        for o in extension.swig_opts:
+            swig_cmd.append(o)
+
         for source in swig_sources:
             target = swig_targets[source]
             log.info("swigging %s to %s", source, target)


More information about the Distutils-SIG mailing list