[Distutils] Duelling SWIG patches

Thomas Heller thomas.heller@ion-tof.com
Mon, 26 Jun 2000 18:25:10 +0200


This is a multi-part message in MIME format.

------=_NextPart_000_0261_01BFDF9B.DCFFCDA0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I wrote:
> > (Sidenote: I also needed a swig-object in build_ext for my setup-script
> > for win32all. I had to extend distutils in the setup-script
> > because there are also exe-files to be built from C++
> > and swig sources).
but this is of course nonsense. Swig is used to build
python-extensions, not exe-files. Sorry for that.

Greg:
> I thought I mentioned this recently: there *is* a 'link_executable()'
> method in MSVCCompiler.  If it works, why not use it?  If it doesn't
> work, could you submit a patch instead of implementing your own?  (And
> if the interface specified by CCompiler is insufficient, I'd like to
> find out *now*!)
Yes, I used compile() and link_shared_object() in my win32 setup script.
No need to worry about it.

>
> > As I pointed out in a separate post, there should also be an option
> > to specify C or C++ processing.
>
> Agreed -- yet another good argument for a SWIG class.
>
> > If I find time, I will submit a new patch.
>
> Cool.  My time is probably better spent learning SWIG.  No wait, I mean
> documenting and testing the Distutils... sigh...

Well, actually I also don't use swig normally. I was forced to use it
for Marks win32 stuff. He lets swig generate c++ code, so...

Appended is a patch for your swig patches (;-) which adds an --swig-cpp
command line option to build_ext, so that I can build the win32 stuff again.
There should probably better be an option for the Extension() class, but
(hopefully) someone else can do this.
This is in chunks 1, 2, 4, 5.

Chunks 3 and 6 contain a change for building extensions on NT:
MSVC generates .lib and .exp files when building shared libraries.
The .exp files are usually unneeded, the .lib files are import libraries,
which may be needed for (other) dlls linking to the first one.
Up to now these files were created in the
build/lib.win32/Release or build/lib.win32/Debug directories.
Sometimes python extensions are dependend on each other.
One example is the ODBC stuff included in Marks code:
The odbc extension links dynamically to the dbi extension.
So I changed build_ext to create the .lib (and .exp) files
in the build/lib.win32 directory. Since they have different
names for debug and release builds, no conficts occur: dbi.lib, dbi_d.lib.

Thomas

------=_NextPart_000_0261_01BFDF9B.DCFFCDA0
Content-Type: text/plain;
	name="build_ext.diff.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="build_ext.diff.txt"

Index: build_ext.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/python/distutils/distutils/command/build_ext.py,v
retrieving revision 1.48
diff -c -r1.48 build_ext.py
*** build_ext.py	2000/06/25 02:30:15	1.48
--- build_ext.py	2000/06/26 16:15:18
***************
*** 77,82 ****
--- 77,84 ----
           "forcibly build everything (ignore file timestamps)"),
          ('compiler=3D', 'c',
           "specify the compiler type"),
+         ('swig-cpp', None,
+          "let swig create c++ files"),
          ]
 =20
      help_options =3D [
***************
*** 101,106 ****
--- 103,109 ----
          self.debug =3D None
          self.force =3D None
          self.compiler =3D None
+         self.swig_cpp =3D None
 =20
 =20
      def finalize_options (self):
***************
*** 152,157 ****
--- 155,161 ----
          # also Python's library directory must be appended to =
library_dirs
          if os.name =3D=3D 'nt':
              self.library_dirs.append (os.path.join(sys.exec_prefix, =
'libs'))
+             self.implib_dir =3D self.build_temp
              if self.debug:
                  self.build_temp =3D os.path.join (self.build_temp, =
"Debug")
              else:
***************
*** 443,457 ****
          swig_sources =3D []
          swig_targets =3D {}
 =20
!         # XXX this drops generated C files into the source tree, which
          # is fine for developers who want to distribute the generated
          # source -- but there should be an option to put SWIG output =
in
          # the temp dir.
 =20
          for source in sources:
              (base, ext) =3D os.path.splitext(source)
              if ext =3D=3D ".i":             # SWIG interface file
!                 new_sources.append(base + ".c") # umm, what if it's =
C++?
                  swig_sources.append(source)
                  swig_targets[source] =3D new_sources[-1]
              else:
--- 447,466 ----
          swig_sources =3D []
          swig_targets =3D {}
 =20
!         # XXX this drops generated C/C++ files into the source tree, =
which
          # is fine for developers who want to distribute the generated
          # source -- but there should be an option to put SWIG output =
in
          # the temp dir.
 =20
+         if self.swig_cpp:
+             target_ext =3D '.cpp'
+         else:
+             target_ext =3D '.c'
+=20
          for source in sources:
              (base, ext) =3D os.path.splitext(source)
              if ext =3D=3D ".i":             # SWIG interface file
!                 new_sources.append(base + target_ext)
                  swig_sources.append(source)
                  swig_targets[source] =3D new_sources[-1]
              else:
***************
*** 461,471 ****
              return new_sources
 =20
          swig =3D self.find_swig()
!         swig_cmd =3D [swig, "-python", "-dnone", "-ISWIG"] # again, =
C++?!?
 =20
          for source in swig_sources:
!             self.announce ("swigging %s to %s" % (src, obj))
!             self.spawn(swig_cmd + ["-o", swig_targets[source], =
source])
 =20
          return new_sources
 =20
--- 470,483 ----
              return new_sources
 =20
          swig =3D self.find_swig()
!         swig_cmd =3D [swig, "-python", "-dnone", "-ISWIG"]
!         if self.swig_cpp:
!             swig_cmd.append ("-c++")
 =20
          for source in swig_sources:
!             target =3D swig_targets[source]
!             self.announce ("swigging %s to %s" % (source, target))
!             self.spawn(swig_cmd + ["-o", target, source])
 =20
          return new_sources
 =20
***************
*** 528,542 ****
              modname =3D string.split (ext.name, '.')[-1]
              extra_args.append('/export:init%s' % modname)
 =20
!         # The MSVC linker generates unneeded .lib and .exp files,
!         # which cannot be suppressed by any linker switches.  So
!         # make sure they are generated in the temporary build
!         # directory.
          implib_file =3D os.path.join (
!             self.build_temp,
              self.get_ext_libname (ext.name))
          extra_args.append ('/IMPLIB:' + implib_file)
          self.mkpath (os.path.dirname (implib_file))
 =20
      # msvc_prelink_hack ()
 =20
--- 540,557 ----
              modname =3D string.split (ext.name, '.')[-1]
              extra_args.append('/export:init%s' % modname)
 =20
!         # The MSVC linker generates .lib and .exp files,
!         # which cannot be suppressed by any linker switches. The .lib
!         # files may even be needed! Make sure they are generated in
!         # the temporary build directory. Since they have different
!         # names for debug and release builds, they can go
!         # into the same directory.
          implib_file =3D os.path.join (
!             self.implib_dir,
              self.get_ext_libname (ext.name))
          extra_args.append ('/IMPLIB:' + implib_file)
          self.mkpath (os.path.dirname (implib_file))
+=20
 =20
      # msvc_prelink_hack ()
 =20

------=_NextPart_000_0261_01BFDF9B.DCFFCDA0--