1 #=== mingw_setup.py by Phillip J. Eby
2 """Create pythonNN.def and libpythonNN.a in 'PythonNN/libs' directory
4 This script makes it possible to use the MinGW compiler tools to
5 build C extensions that work with the standard Windows Python
8 Before running, you should have installed the MinGW compiler toolset,
9 and placed its 'bin' directory on your PATH. An easy way to do this
10 is to install Cygwin's "binutils", "gcc", and "mingw-*" packages,
11 then run this script from the Cygwin shell. (Be sure to use *Windows*
12 Python, not Cygwin python!)
14 Once this script has been run, you should be able to build extensions
15 using distutils in the standard way, as long as you select the
16 'mingw32' compiler, and the required tools are on your PATH. See
17 the "Installing Python Modules" manual for more information on
22 from distutils.spawn import find_executable
23 from distutils.sysconfig import get_config_var
24 from distutils import __file__ as distutils_file
27 if sys.platform=='cygwin':
28 print "Please run this script using Windows python,",
29 print "not Cygwin python. E.g, try:"
31 print "/cygdrive/c/PythonNN/python", " ".join(sys.argv)
33 print "(where NN is the major/minor python version number)"
36 basename = 'python%d%d' % sys.version_info[:2]
38 libs_dir = os.path.join(get_config_var('prefix'),'libs')
39 lib_file = os.path.join(libs_dir,basename+'.lib')
40 def_file = os.path.join(libs_dir,basename+'.def')
41 ming_lib = os.path.join(libs_dir,'lib%s.a' % basename)
43 distutils_cfg = os.path.join(os.path.dirname(distutils_file),'distutils.cfg')
45 export_match = re.compile(r"^_imp__(.*) in python\d+\.dll").match
47 nm = find_executable('nm')
48 dlltool = find_executable('dlltool')
50 if not nm or not dlltool:
51 print "'nm' and/or 'dlltool' were not found;"
52 print "Please make sure they're on your PATH."
55 nm_command = '%s -Cs %s' % (nm, lib_file)
57 print "Building", def_file, "using", nm_command
58 f = open(def_file,'w')
59 print >>f, "LIBRARY %s.dll" % basename
63 nm_pipe = os.popen(nm_command)
64 for line in nm_pipe.readlines():
65 m = export_match(line)
70 exit = nm_pipe.close()
72 print "nm exited with status", exit
73 print "Please check that", lib_file, "exists and is valid."
77 print "Building",ming_lib,"using",dlltool
78 dlltool_pipe = os.popen(
79 "%s --dllname %s.dll --def %s --output-lib %s" %
80 (dlltool, basename, def_file, ming_lib)
83 dlltool_pipe.readlines()
84 exit = dlltool_pipe.close()
86 print "dlltool exited with status", exit
87 print "Unable to proceed."
91 print "Installation complete. You may wish to add the following"
92 print "lines to", distutils_cfg, ':'
95 print "compiler = mingw32"
97 print "This will make the distutils use MinGW as the default"
98 print "compiler, so that you don't need to configure this for"
99 print "every 'setup.py' you run."