9c4755058123013d528695082d71b49cd92ebc49
"""distutils.unixccompiler
Contains the UnixCCompiler class, a subclass of CCompiler that handles
the "typical" Unix-style command-line C compiler:
* macros defined with -Dname[=value]
* macros undefined with -Uname
* include search directories specified with -Idir
* libraries specified with -lllib
* library search directories specified with -Ldir
* compile handled by 'cc' (or similar) executable with -c option:
* link static library handled by 'ar' command (possibly with 'ranlib')
* link shared library handled by 'cc -shared'
__revision__
= "$Id: unixccompiler.py,v 1.56 2004/08/29 16:40:55 loewis Exp $"
from types
import StringType
, NoneType
from distutils
import sysconfig
from distutils
.dep_util
import newer
from distutils
.ccompiler
import \
CCompiler
, gen_preprocess_options
, gen_lib_options
from distutils
.errors
import \
DistutilsExecError
, CompileError
, LibError
, LinkError
from distutils
import log
# XXX Things not currently handled:
# * optimization/debug/warning flags; we just use whatever's in Python's
# Makefile and live with it. Is this adequate? If not, we might
# have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
# SunCCompiler, and I suspect down that road lies madness.
# * even if we don't know a warning flag from an optimization flag,
# we need some way for outsiders to feed preprocessor/compiler/linker
# flags in to us -- eg. a sysadmin might want to mandate certain flags
# via a site config file, or a user might want to set something for
# compiling this module distribution only via the setup.py command
# line, whatever. As long as these options come from something on the
# current system, they can be as system-dependent as they like, and we
# should just happily stuff them into the preprocessor/compiler/linker
class UnixCCompiler(CCompiler
):
# These are used by CCompiler in two places: the constructor sets
# instance attributes 'preprocessor', 'compiler', etc. from them, and
# 'set_executable()' allows any of these to be set. The defaults here
# are pretty generic; they will probably have to be set by an outsider
# (eg. using information discovered by the sysconfig about building
executables
= {'preprocessor' : None,
'linker_so' : ["cc", "-shared"],
'archiver' : ["ar", "-cr"],
if sys
.platform
[:6] == "darwin":
executables
['ranlib'] = ["ranlib"]
# Needed for the filename generation methods provided by the base
# class, CCompiler. NB. whoever instantiates/uses a particular
# UnixCCompiler instance should set 'shared_lib_ext' -- we set a
# reasonable common default here, but it's not necessarily used on all
src_extensions
= [".c",".C",".cc",".cxx",".cpp",".m"]
static_lib_extension
= ".a"
shared_lib_extension
= ".so"
dylib_lib_extension
= ".dylib"
static_lib_format
= shared_lib_format
= dylib_lib_format
= "lib%s%s"
if sys
.platform
== "cygwin":
def preprocess(self
, source
,
output_file
=None, macros
=None, include_dirs
=None,
extra_preargs
=None, extra_postargs
=None):
ignore
, macros
, include_dirs
= \
self
._fix
_compile
_args
(None, macros
, include_dirs
)
pp_opts
= gen_preprocess_options(macros
, include_dirs
)
pp_args
= self
.preprocessor
+ pp_opts
pp_args
.extend(['-o', output_file
])
pp_args
[:0] = extra_preargs
pp_args
.extend(extra_postargs
)
# We need to preprocess: either we're being forced to, or we're
# generating output to stdout, or there's a target output file and
# the source file is newer than the target (or the target doesn't
if self
.force
or output_file
is None or newer(source
, output_file
):
self
.mkpath(os
.path
.dirname(output_file
))
except DistutilsExecError
, msg
:
def _compile(self
, obj
, src
, ext
, cc_args
, extra_postargs
, pp_opts
):
self
.spawn(self
.compiler_so
+ cc_args
+ [src
, '-o', obj
] +
except DistutilsExecError
, msg
:
def create_static_lib(self
, objects
, output_libname
,
output_dir
=None, debug
=0, target_lang
=None):
objects
, output_dir
= self
._fix
_object
_args
(objects
, output_dir
)
self
.library_filename(output_libname
, output_dir
=output_dir
)
if self
._need
_link
(objects
, output_filename
):
self
.mkpath(os
.path
.dirname(output_filename
))
self
.spawn(self
.archiver
+
# Not many Unices required ranlib anymore -- SunOS 4.x is, I
# think the only major Unix that does. Maybe we need some
# platform intelligence here to skip ranlib if it's not
# needed -- or maybe Python's configure script took care of
# it for us, hence the check for leading colon.
self
.spawn(self
.ranlib
+ [output_filename
])
except DistutilsExecError
, msg
:
log
.debug("skipping %s (up-to-date)", output_filename
)
def link(self
, target_desc
, objects
,
output_filename
, output_dir
=None, libraries
=None,
library_dirs
=None, runtime_library_dirs
=None,
export_symbols
=None, debug
=0, extra_preargs
=None,
extra_postargs
=None, build_temp
=None, target_lang
=None):
objects
, output_dir
= self
._fix
_object
_args
(objects
, output_dir
)
libraries
, library_dirs
, runtime_library_dirs
= \
self
._fix
_lib
_args
(libraries
, library_dirs
, runtime_library_dirs
)
lib_opts
= gen_lib_options(self
, library_dirs
, runtime_library_dirs
,
if type(output_dir
) not in (StringType
, NoneType
):
raise TypeError, "'output_dir' must be a string or None"
if output_dir
is not None:
output_filename
= os
.path
.join(output_dir
, output_filename
)
if self
._need
_link
(objects
, output_filename
):
ld_args
= (objects
+ self
.objects
+
lib_opts
+ ['-o', output_filename
])
ld_args
[:0] = extra_preargs
ld_args
.extend(extra_postargs
)
self
.mkpath(os
.path
.dirname(output_filename
))
if target_desc
== CCompiler
.EXECUTABLE
:
linker
= self
.linker_exe
[:]
linker
= self
.linker_so
[:]
if target_lang
== "c++" and self
.compiler_cxx
:
linker
[0] = self
.compiler_cxx
[0]
self
.spawn(linker
+ ld_args
)
except DistutilsExecError
, msg
:
log
.debug("skipping %s (up-to-date)", output_filename
)
# -- Miscellaneous methods -----------------------------------------
# These are all used by the 'gen_lib_options() function, in
def library_dir_option(self
, dir):
def runtime_library_dir_option(self
, dir):
# XXX Hackish, at the very least. See Python bug #445902:
# http://sourceforge.net/tracker/index.php
# ?func=detail&aid=445902&group_id=5470&atid=105470
# Linkers on different platforms need different options to
# specify that directories need to be added to the list of
# directories searched for dependencies when a dynamic library
# is sought. GCC has to be told to pass the -R option through
# to the linker, whereas other compilers just know this.
# Other compilers may need something slightly different. At
# this time, there's no way to determine this information from
# the configuration data stored in the Python installation, so
compiler
= os
.path
.basename(sysconfig
.get_config_var("CC"))
if sys
.platform
[:6] == "darwin":
# MacOSX's linker doesn't understand the -R flag at all
elif sys
.platform
[:5] == "hp-ux":
elif sys
.platform
[:7] == "irix646" or sys
.platform
[:6] == "osf1V5":
elif compiler
[:3] == "gcc" or compiler
[:3] == "g++":
def library_option(self
, lib
):
def find_library_file(self
, dirs
, lib
, debug
=0):
shared_f
= self
.library_filename(lib
, lib_type
='shared')
dylib_f
= self
.library_filename(lib
, lib_type
='dylib')
static_f
= self
.library_filename(lib
, lib_type
='static')
shared
= os
.path
.join(dir, shared_f
)
dylib
= os
.path
.join(dir, dylib_f
)
static
= os
.path
.join(dir, static_f
)
# We're second-guessing the linker here, with not much hard
# data to go on: GCC seems to prefer the shared library, so I'm
# assuming that *all* Unix C compilers do. And of course I'm
# ignoring even GCC's "-static" option. So sue me.
if os
.path
.exists(dylib
):
elif os
.path
.exists(shared
):
elif os
.path
.exists(static
):
# Oops, didn't find it in *any* of 'dirs'