[Python-Dev] SUCCESS! 2.4 extensions w/mingw (was Re: Switching to VC.NET 2003)

Phillip J. Eby pje at telecommunity.com
Sun Jan 4 15:35:46 EST 2004

At 11:00 AM 1/4/04 -0500, Phillip J. Eby wrote:
>Hm.  Really, it begins to sound as though there should either be a 
>separate distutils command to build the library, or just a script you can 
>run to build and/or install the library.  I'll consider writing something 
>-- at least a doc patch, if not a script -- for 2.4.

After a bit more research, I have a short Python script to prepare a 
Windows Python installation for using the mingw32 compiler option.  It does 
not require a separate download of the 'pexports' program; it requires only 
that the 'nm' and 'dlltool' executables (installed with the Cygwin 
'binutils' package) are available on the system PATH, and that the user 
running it have write access to the PythonNN/libs directory.

Should this, or something like it, be distributed with Windows Python?  If 
so, where in the distribution should it go?  Should I create a SF patch for 

#=== mingw_setup.py
"""Create pythonNN.def and libpythonNN.a in 'PythonNN/libs' directory

This script makes it possible to use the MinGW compiler tools to
build C extensions that work with the standard Windows Python

Before running, you should have installed the MinGW compiler toolset,
and placed its 'bin' directory on your PATH.  An easy way to do this
is to install Cygwin's "binutils", "gcc", and "mingw-*" packages,
then run this script from the Cygwin shell.  (Be sure to use *Windows*
Python, not Cygwin python!)

Once this script has been run, you should be able to build extensions
using distutils in the standard way, as long as you select the
'mingw32' compiler, and the required tools are on your PATH.  See
the "Installing Python Modules" manual for more information on
selecting a compiler.

from distutils.spawn import find_executable
from distutils.sysconfig import get_config_var
from distutils import __file__ as distutils_file
import os, re, sys

if sys.platform=='cygwin':
     print "Please run this script using Windows python,",
     print "not Cygwin python.  E.g, try:"
     print "/cygdrive/c/PythonNN/python", " ".join(sys.argv)
     print "(where NN is the major/minor python version number)"

basename = 'python%d%d' % sys.version_info[:2]

libs_dir = os.path.join(get_config_var('prefix'),'libs')
lib_file = os.path.join(libs_dir,basename+'.lib')
def_file = os.path.join(libs_dir,basename+'.def')
ming_lib = os.path.join(libs_dir,'lib%s.a' % basename)

distutils_cfg = os.path.join(os.path.dirname(distutils_file),'distutils.cfg')

export_match = re.compile(r"^_imp__(.*) in python\d+\.dll").match

nm      = find_executable('nm')
dlltool = find_executable('dlltool')

if not nm or not dlltool:
     print "'nm' and/or 'dlltool' were not found;"
     print "Please make sure they're on your PATH."

nm_command = '%s -Cs %s' % (nm, lib_file)

print "Building", def_file, "using", nm_command
f = open(def_file,'w')
print >>f, "LIBRARY %s.dll" % basename
print >>f, "EXPORTS"

nm_pipe = os.popen(nm_command)
for line in nm_pipe.readlines():
     m = export_match(line)
     if m:
         print >>f, m.group(1)

exit = nm_pipe.close()
if exit:
     print "nm exited with status", exit
     print "Please check that", lib_file, "exists and is valid."

print "Building",ming_lib,"using",dlltool
dlltool_pipe = os.popen(
     "%s --dllname %s.dll --def %s --output-lib %s" %
     (dlltool, basename, def_file, ming_lib)

exit = dlltool_pipe.close()
if exit:
     print "dlltool exited with status", exit
     print "Unable to proceed."

print "Installation complete.  You may wish to add the following"
print "lines to", distutils_cfg, ':'
print "[build]"
print "compiler = mingw32"
print "This will make the distutils use MinGW as the default"
print "compiler, so that you don't need to configure this for"
print "every 'setup.py' you run."

More information about the Python-Dev mailing list