"""distutils.cygwinccompiler
Provides the CygwinCCompiler class, a subclass of UnixCCompiler that
handles the Cygwin port of the GNU C compiler to Windows. It also contains
the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
cygwin in no-cygwin mode).
# * if you use a msvc compiled python version (1.5.2)
# 1. you have to insert a __GNUC__ section in its config.h
# 2. you have to generate a import library for its dll
# - create a def-file for python??.dll
# - create a import library using
# dlltool --dllname python15.dll --def python15.def \
# --output-lib libpython15.a
# see also http://starship.python.net/crew/kernr/mingw32/Notes.html
# * We put export_symbols in a def-file, and don't use
# --export-all-symbols because it doesn't worked reliable in some
# tested configurations. And because other windows compilers also
# need their symbols specified this no serious problem.
# * cygwin gcc 2.91.57/ld 2.9.4/dllwrap 0.2.4 works
# (after patching python's config.h and for C++ some other include files)
# see also http://starship.python.net/crew/kernr/mingw32/Notes.html
# * mingw32 gcc 2.95.2/ld 2.9.4/dllwrap 0.2.4 works
# (ld doesn't support -shared, so we use dllwrap)
# * cygwin gcc 2.95.2/ld 2.10.90/dllwrap 2.10.90 works now
# - its dllwrap doesn't work, there is a bug in binutils 2.10.90
# see also http://sources.redhat.com/ml/cygwin/2000-06/msg01274.html
# - using gcc -mdll instead dllwrap doesn't work without -static because
# it tries to link against dlls instead their import libraries. (If
# it finds the dll first.)
# By specifying -static we force ld to link against the import libraries,
# this is windows standard and there are normally not the necessary symbols
# *** only the version of June 2000 shows these problems
# * cygwin gcc 3.2/ld 2.13.90 works
# * mingw gcc 3.2/ld 2.13 works
# This module should be kept compatible with Python 2.1.
__revision__
= "$Id: cygwinccompiler.py,v 1.29 2004/11/10 22:23:14 loewis Exp $"
from distutils
.ccompiler
import gen_preprocess_options
, gen_lib_options
from distutils
.unixccompiler
import UnixCCompiler
from distutils
.file_util
import write_file
from distutils
.errors
import DistutilsExecError
, CompileError
, UnknownFileError
from distutils
import log
class CygwinCCompiler (UnixCCompiler
):
static_lib_extension
= ".a"
shared_lib_extension
= ".dll"
static_lib_format
= "lib%s%s"
shared_lib_format
= "%s%s"
def __init__ (self
, verbose
=0, dry_run
=0, force
=0):
UnixCCompiler
.__init
__ (self
, verbose
, dry_run
, force
)
(status
, details
) = check_config_h()
self
.debug_print("Python's GCC status: %s (details: %s)" %
if status
is not CONFIG_H_OK
:
"Python's pyconfig.h doesn't seem to support your compiler. "
"Compiling may fail because of undefined preprocessor macros."
self
.gcc_version
, self
.ld_version
, self
.dllwrap_version
= \
self
.debug_print(self
.compiler_type
+ ": gcc %s, ld %s, dllwrap %s\n" %
# ld_version >= "2.10.90" and < "2.13" should also be able to use
# gcc -mdll instead of dllwrap
# Older dllwraps had own version numbers, newer ones use the
# same as the rest of binutils ( also ld )
# dllwrap 2.10.90 is buggy
if self
.ld_version
>= "2.10.90":
self
.linker_dll
= "dllwrap"
# ld_version >= "2.13" support -shared so use it instead of
if self
.ld_version
>= "2.13":
shared_option
= "-shared"
shared_option
= "-mdll -static"
# Hard-code GCC because that's what this is all about.
# XXX optimization, warnings etc. should be customizable.
self
.set_executables(compiler
='gcc -mcygwin -O -Wall',
compiler_so
='gcc -mcygwin -mdll -O -Wall',
compiler_cxx
='g++ -mcygwin -O -Wall',
linker_exe
='gcc -mcygwin',
linker_so
=('%s -mcygwin %s' %
(self
.linker_dll
, shared_option
)))
# cygwin and mingw32 need different sets of libraries
if self
.gcc_version
== "2.91.57":
# cygwin shouldn't need msvcrt, but without the dlls will crash
# (gcc version 2.91.57) -- perhaps something about initialization
self
.dll_libraries
=["msvcrt"]
"Consider upgrading to a newer version of gcc")
# Include the appropriate MSVC runtime library if Python was built
msc_pos
= sys
.version
.find('MSC v.')
msc_ver
= sys
.version
[msc_pos
+6:msc_pos
+10]
self
.dll_libraries
= ['msvcr70']
self
.dll_libraries
= ['msvcr71']
def _compile(self
, obj
, src
, ext
, cc_args
, extra_postargs
, pp_opts
):
if ext
== '.rc' or ext
== '.res':
# gcc needs '.res' and '.rc' compiled to object files !!!
self
.spawn(["windres", "-i", src
, "-o", obj
])
except DistutilsExecError
, msg
:
else: # for other files use the C-compiler
self
.spawn(self
.compiler_so
+ cc_args
+ [src
, '-o', obj
] +
except DistutilsExecError
, msg
:
runtime_library_dirs
=None,
# use separate copies, so we can modify the lists
extra_preargs
= copy
.copy(extra_preargs
or [])
libraries
= copy
.copy(libraries
or [])
objects
= copy
.copy(objects
or [])
libraries
.extend(self
.dll_libraries
)
# handle export symbols by creating a def-file
# with executables this only works with gcc/ld as linker
if ((export_symbols
is not None) and
(target_desc
!= self
.EXECUTABLE
or self
.linker_dll
== "gcc")):
# (The linker doesn't do anything if output is up-to-date.
# So it would probably better to check if we really need this,
# but for this we had to insert some unchanged parts of
# UnixCCompiler, and this is not what we want.)
# we want to put some files in the same directory as the
# object files are, build_temp doesn't help much
# where are the object files
temp_dir
= os
.path
.dirname(objects
[0])
# name of dll to give the helper files the same base name
(dll_name
, dll_extension
) = os
.path
.splitext(
os
.path
.basename(output_filename
))
# generate the filenames for these files
def_file
= os
.path
.join(temp_dir
, dll_name
+ ".def")
lib_file
= os
.path
.join(temp_dir
, 'lib' + dll_name
+ ".a")
"LIBRARY %s" % os
.path
.basename(output_filename
),
for sym
in export_symbols
:
self
.execute(write_file
, (def_file
, contents
),
# next add options for def-file and to creating import libraries
# dllwrap uses different options than gcc/ld
if self
.linker_dll
== "dllwrap":
extra_preargs
.extend(["--output-lib", lib_file
])
# for dllwrap we have to use a special option
extra_preargs
.extend(["--def", def_file
])
# we use gcc/ld here and can be sure ld is >= 2.9.10
# doesn't work: bfd_close build\...\libfoo.a: Invalid operation
#extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
# for gcc/ld the def-file is specified as any object files
#end: if ((export_symbols is not None) and
# (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
# who wants symbols and a many times larger output file
# should explicitly switch the debug mode on
# otherwise we let dllwrap/ld strip the output file
# (On my machine: 10KB < stripped_file < ??100KB
# unstripped_file = stripped_file + XXX KB
# ( XXX=254 for a typical python extension))
extra_preargs
.append("-s")
None, # export_symbols, we do this in our def-file
# -- Miscellaneous methods -----------------------------------------
# overwrite the one from CCompiler to support rc and res-files
def object_filenames (self
,
if output_dir
is None: output_dir
= ''
for src_name
in source_filenames
:
# use normcase to make sure '.rc' is really '.rc' and not '.RC'
(base
, ext
) = os
.path
.splitext (os
.path
.normcase(src_name
))
if ext
not in (self
.src_extensions
+ ['.rc','.res']):
raise UnknownFileError
, \
"unknown file type '%s' (from '%s')" % \
base
= os
.path
.basename (base
)
if ext
== '.res' or ext
== '.rc':
# these need to be compiled to object files
obj_names
.append (os
.path
.join (output_dir
,
base
+ ext
+ self
.obj_extension
))
obj_names
.append (os
.path
.join (output_dir
,
base
+ self
.obj_extension
))
# the same as cygwin plus some additional parameters
class Mingw32CCompiler (CygwinCCompiler
):
compiler_type
= 'mingw32'
CygwinCCompiler
.__init
__ (self
, verbose
, dry_run
, force
)
# ld_version >= "2.13" support -shared so use it instead of
if self
.ld_version
>= "2.13":
shared_option
= "-shared"
shared_option
= "-mdll -static"
# A real mingw32 doesn't need to specify a different entry point,
# but cygwin 2.91.57 in no-cygwin-mode needs it.
if self
.gcc_version
<= "2.91.57":
entry_point
= '--entry _DllMain@12'
self
.set_executables(compiler
='gcc -mno-cygwin -O -Wall',
compiler_so
='gcc -mno-cygwin -mdll -O -Wall',
compiler_cxx
='g++ -mno-cygwin -O -Wall',
linker_exe
='gcc -mno-cygwin',
linker_so
='%s -mno-cygwin %s %s'
% (self
.linker_dll
, shared_option
,
# Maybe we should also append -mthreads, but then the finished
# dlls need another dll (mingwm10.dll see Mingw32 docs)
# (-mthreads: Support thread-safe exception handling on `Mingw32')
# no additional libraries needed
# Include the appropriate MSVC runtime library if Python was built
msc_pos
= sys
.version
.find('MSC v.')
msc_ver
= sys
.version
[msc_pos
+6:msc_pos
+10]
self
.dll_libraries
= ['msvcr70']
self
.dll_libraries
= ['msvcr71']
# Because these compilers aren't configured in Python's pyconfig.h file by
# default, we should at least warn the user if he is using a unmodified
CONFIG_H_NOTOK
= "not ok"
CONFIG_H_UNCERTAIN
= "uncertain"
"""Check if the current Python installation (specifically, pyconfig.h)
appears amenable to building extensions with GCC. Returns a tuple
(status, details), where 'status' is one of the following constants:
all is well, go ahead and compile
not sure -- unable to read pyconfig.h
'details' is a human-readable string explaining the situation.
Note there are two ways to conclude "OK": either 'sys.version' contains
the string "GCC" (implying that this Python was built with GCC), or the
installed "pyconfig.h" contains the string "__GNUC__".
# XXX since this function also checks sys.version, it's not strictly a
# "pyconfig.h" check -- should probably be renamed...
from distutils
import sysconfig
# if sys.version contains GCC then python was compiled with
# GCC, and the pyconfig.h file should be OK
if string
.find(sys
.version
,"GCC") >= 0:
return (CONFIG_H_OK
, "sys.version mentions 'GCC'")
fn
= sysconfig
.get_config_h_filename()
# It would probably better to read single lines to search.
# But we do this only once, and it is fast enough
# if we can't read this file, we cannot say it is wrong
# the compiler will complain later about this file as missing
return (CONFIG_H_UNCERTAIN
,
"couldn't read '%s': %s" % (fn
, exc
.strerror
))
# "pyconfig.h" contains an "#ifdef __GNUC__" or something similar
if string
.find(s
,"__GNUC__") >= 0:
return (CONFIG_H_OK
, "'%s' mentions '__GNUC__'" % fn
)
return (CONFIG_H_NOTOK
, "'%s' does not mention '__GNUC__'" % fn
)
""" Try to find out the versions of gcc, ld and dllwrap.
If not possible it returns None for it.
from distutils
.version
import StrictVersion
from distutils
.spawn
import find_executable
gcc_exe
= find_executable('gcc')
out
= os
.popen(gcc_exe
+ ' -dumpversion','r')
result
= re
.search('(\d+\.\d+(\.\d+)*)',out_string
)
gcc_version
= StrictVersion(result
.group(1))
ld_exe
= find_executable('ld')
out
= os
.popen(ld_exe
+ ' -v','r')
result
= re
.search('(\d+\.\d+(\.\d+)*)',out_string
)
ld_version
= StrictVersion(result
.group(1))
dllwrap_exe
= find_executable('dllwrap')
out
= os
.popen(dllwrap_exe
+ ' --version','r')
result
= re
.search(' (\d+\.\d+(\.\d+)*)',out_string
)
dllwrap_version
= StrictVersion(result
.group(1))
return (gcc_version
, ld_version
, dllwrap_version
)