1999-07-10 02:04:22 +00:00
|
|
|
"""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:
|
|
|
|
compiles .c to .o
|
|
|
|
* link static library handled by 'ar' command (possibly with 'ranlib')
|
|
|
|
* link shared library handled by 'cc -shared'
|
|
|
|
"""
|
|
|
|
|
|
|
|
# created 1999/07/05, Greg Ward
|
|
|
|
|
2000-03-02 01:49:45 +00:00
|
|
|
__revision__ = "$Id$"
|
1999-07-10 02:04:22 +00:00
|
|
|
|
1999-09-13 03:12:53 +00:00
|
|
|
import string, re, os
|
1999-07-10 02:04:22 +00:00
|
|
|
from types import *
|
1999-09-13 03:12:53 +00:00
|
|
|
from copy import copy
|
2000-06-21 02:58:46 +00:00
|
|
|
from distutils.dep_util import newer
|
2000-05-30 01:56:44 +00:00
|
|
|
from distutils.ccompiler import \
|
2000-05-30 02:02:49 +00:00
|
|
|
CCompiler, gen_preprocess_options, gen_lib_options
|
|
|
|
from distutils.errors import \
|
|
|
|
DistutilsExecError, CompileError, LibError, LinkError
|
1999-07-10 02:04:22 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
# options and carry on.
|
|
|
|
|
|
|
|
|
|
|
|
class UnixCCompiler (CCompiler):
|
|
|
|
|
1999-09-29 12:22:50 +00:00
|
|
|
compiler_type = 'unix'
|
|
|
|
|
2000-06-25 02:05:29 +00:00
|
|
|
# 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
|
|
|
|
# Python extensions).
|
|
|
|
executables = {'preprocessor' : None,
|
|
|
|
'compiler' : ["cc"],
|
|
|
|
'compiler_so' : ["cc"],
|
|
|
|
'linker_so' : ["cc", "-shared"],
|
|
|
|
'linker_exe' : ["cc"],
|
|
|
|
'archiver' : ["ar", "-cr"],
|
|
|
|
'ranlib' : None,
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|
|
|
|
# Unices!
|
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
src_extensions = [".c",".C",".cc",".cxx",".cpp"]
|
|
|
|
obj_extension = ".o"
|
|
|
|
static_lib_extension = ".a"
|
2000-06-25 02:05:29 +00:00
|
|
|
shared_lib_extension = ".so"
|
2000-03-06 03:40:29 +00:00
|
|
|
static_lib_format = shared_lib_format = "lib%s%s"
|
2000-01-09 22:47:53 +00:00
|
|
|
|
|
|
|
|
2000-06-28 01:29:09 +00:00
|
|
|
|
1999-08-14 23:53:53 +00:00
|
|
|
def __init__ (self,
|
|
|
|
verbose=0,
|
1999-10-03 20:45:33 +00:00
|
|
|
dry_run=0,
|
|
|
|
force=0):
|
|
|
|
CCompiler.__init__ (self, verbose, dry_run, force)
|
1999-07-10 02:04:22 +00:00
|
|
|
|
|
|
|
|
2000-06-21 02:58:46 +00:00
|
|
|
def preprocess (self,
|
|
|
|
source,
|
|
|
|
output_file=None,
|
|
|
|
macros=None,
|
|
|
|
include_dirs=None,
|
|
|
|
extra_preargs=None,
|
|
|
|
extra_postargs=None):
|
|
|
|
|
|
|
|
(_, macros, include_dirs) = \
|
|
|
|
self._fix_compile_args (None, macros, include_dirs)
|
|
|
|
pp_opts = gen_preprocess_options (macros, include_dirs)
|
2000-06-25 02:05:29 +00:00
|
|
|
pp_args = self.preprocessor + pp_opts
|
2000-06-21 02:58:46 +00:00
|
|
|
if output_file:
|
2000-06-25 02:05:29 +00:00
|
|
|
pp_args.extend(['-o', output_file])
|
2000-06-21 02:58:46 +00:00
|
|
|
if extra_preargs:
|
2000-06-25 02:05:29 +00:00
|
|
|
pp_args[:0] = extra_preargs
|
2000-06-21 02:58:46 +00:00
|
|
|
if extra_postargs:
|
|
|
|
extra_postargs.extend(extra_postargs)
|
|
|
|
|
|
|
|
# We need to preprocess: either we're being forced to, or the
|
|
|
|
# source file is newer than the target (or the target doesn't
|
|
|
|
# exist).
|
|
|
|
if self.force or (output_file and newer(source, output_file)):
|
|
|
|
if output_file:
|
|
|
|
self.mkpath(os.path.dirname(output_file))
|
|
|
|
try:
|
2000-06-25 02:05:29 +00:00
|
|
|
self.spawn (pp_args)
|
2000-06-21 02:58:46 +00:00
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise CompileError, msg
|
|
|
|
|
|
|
|
|
1999-07-10 02:04:22 +00:00
|
|
|
def compile (self,
|
|
|
|
sources,
|
1999-09-13 03:12:53 +00:00
|
|
|
output_dir=None,
|
1999-08-14 23:53:53 +00:00
|
|
|
macros=None,
|
1999-12-12 16:57:47 +00:00
|
|
|
include_dirs=None,
|
2000-02-09 02:17:00 +00:00
|
|
|
debug=0,
|
1999-09-29 12:22:50 +00:00
|
|
|
extra_preargs=None,
|
|
|
|
extra_postargs=None):
|
1999-08-14 23:53:53 +00:00
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
(output_dir, macros, include_dirs) = \
|
|
|
|
self._fix_compile_args (output_dir, macros, include_dirs)
|
|
|
|
(objects, skip_sources) = self._prep_compile (sources, output_dir)
|
2000-02-03 23:07:19 +00:00
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
# Figure out the options for the compiler command line.
|
|
|
|
pp_opts = gen_preprocess_options (macros, include_dirs)
|
2000-06-25 02:05:29 +00:00
|
|
|
cc_args = pp_opts + ['-c']
|
2000-02-09 02:17:00 +00:00
|
|
|
if debug:
|
|
|
|
cc_args[:0] = ['-g']
|
2000-02-03 23:07:19 +00:00
|
|
|
if extra_preargs:
|
|
|
|
cc_args[:0] = extra_preargs
|
|
|
|
if extra_postargs is None:
|
|
|
|
extra_postargs = []
|
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
# Compile all source files that weren't eliminated by
|
|
|
|
# '_prep_compile()'.
|
|
|
|
for i in range (len (sources)):
|
|
|
|
src = sources[i] ; obj = objects[i]
|
|
|
|
if skip_sources[src]:
|
|
|
|
self.announce ("skipping %s (%s up-to-date)" % (src, obj))
|
|
|
|
else:
|
|
|
|
self.mkpath (os.path.dirname (obj))
|
2000-05-30 01:56:44 +00:00
|
|
|
try:
|
2000-06-25 02:05:29 +00:00
|
|
|
self.spawn (self.compiler_so + cc_args +
|
2000-05-30 01:56:44 +00:00
|
|
|
[src, '-o', obj] +
|
|
|
|
extra_postargs)
|
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise CompileError, msg
|
2000-01-09 22:47:53 +00:00
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
# Return *all* object filenames, not just the ones we just built.
|
|
|
|
return objects
|
2000-01-09 22:47:53 +00:00
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
# compile ()
|
|
|
|
|
2000-01-09 22:47:53 +00:00
|
|
|
|
2000-03-10 01:48:32 +00:00
|
|
|
def create_static_lib (self,
|
|
|
|
objects,
|
|
|
|
output_libname,
|
|
|
|
output_dir=None,
|
|
|
|
debug=0):
|
2000-01-09 22:47:53 +00:00
|
|
|
|
2000-03-26 21:40:19 +00:00
|
|
|
(objects, output_dir) = self._fix_object_args (objects, output_dir)
|
2000-01-09 22:47:53 +00:00
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
output_filename = \
|
|
|
|
self.library_filename (output_libname, output_dir=output_dir)
|
2000-01-09 22:47:53 +00:00
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
if self._need_link (objects, output_filename):
|
2000-03-01 14:43:49 +00:00
|
|
|
self.mkpath (os.path.dirname (output_filename))
|
2000-06-25 02:05:29 +00:00
|
|
|
self.spawn (self.archiver +
|
|
|
|
[output_filename] +
|
2000-03-06 03:40:29 +00:00
|
|
|
objects + self.objects)
|
2000-04-14 00:48:15 +00:00
|
|
|
|
2000-04-14 13:53:34 +00:00
|
|
|
# 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.
|
2000-06-25 02:05:29 +00:00
|
|
|
if self.ranlib:
|
2000-05-30 01:56:44 +00:00
|
|
|
try:
|
2000-06-25 02:05:29 +00:00
|
|
|
self.spawn (self.ranlib + [output_filename])
|
2000-05-30 01:56:44 +00:00
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise LibError, msg
|
2000-01-09 22:47:53 +00:00
|
|
|
else:
|
|
|
|
self.announce ("skipping %s (up-to-date)" % output_filename)
|
|
|
|
|
2000-03-10 01:48:32 +00:00
|
|
|
# create_static_lib ()
|
2000-01-09 22:47:53 +00:00
|
|
|
|
1999-07-10 02:04:22 +00:00
|
|
|
|
|
|
|
def link_shared_lib (self,
|
|
|
|
objects,
|
|
|
|
output_libname,
|
1999-09-13 03:12:53 +00:00
|
|
|
output_dir=None,
|
1999-07-10 02:04:22 +00:00
|
|
|
libraries=None,
|
1999-08-29 18:23:32 +00:00
|
|
|
library_dirs=None,
|
2000-03-26 21:40:19 +00:00
|
|
|
runtime_library_dirs=None,
|
2000-05-20 13:23:21 +00:00
|
|
|
export_symbols=None,
|
2000-02-09 02:17:00 +00:00
|
|
|
debug=0,
|
1999-09-29 12:22:50 +00:00
|
|
|
extra_preargs=None,
|
2000-06-28 01:29:09 +00:00
|
|
|
extra_postargs=None,
|
|
|
|
build_temp=None):
|
|
|
|
|
1999-09-13 03:12:53 +00:00
|
|
|
self.link_shared_object (
|
|
|
|
objects,
|
2000-03-06 03:40:29 +00:00
|
|
|
self.shared_library_filename (output_libname),
|
1999-09-13 03:12:53 +00:00
|
|
|
output_dir,
|
|
|
|
libraries,
|
|
|
|
library_dirs,
|
2000-03-26 21:40:19 +00:00
|
|
|
runtime_library_dirs,
|
2000-05-20 13:23:21 +00:00
|
|
|
export_symbols,
|
2000-02-09 02:17:00 +00:00
|
|
|
debug,
|
1999-09-29 12:22:50 +00:00
|
|
|
extra_preargs,
|
2000-06-28 01:29:09 +00:00
|
|
|
extra_postargs,
|
|
|
|
build_temp)
|
1999-09-13 03:12:53 +00:00
|
|
|
|
1999-07-10 02:04:22 +00:00
|
|
|
|
|
|
|
def link_shared_object (self,
|
|
|
|
objects,
|
|
|
|
output_filename,
|
1999-09-13 03:12:53 +00:00
|
|
|
output_dir=None,
|
1999-08-14 23:53:53 +00:00
|
|
|
libraries=None,
|
1999-08-29 18:23:32 +00:00
|
|
|
library_dirs=None,
|
2000-03-26 21:40:19 +00:00
|
|
|
runtime_library_dirs=None,
|
2000-05-20 13:23:21 +00:00
|
|
|
export_symbols=None,
|
2000-02-09 02:17:00 +00:00
|
|
|
debug=0,
|
1999-09-29 12:22:50 +00:00
|
|
|
extra_preargs=None,
|
2000-06-28 01:29:09 +00:00
|
|
|
extra_postargs=None,
|
|
|
|
build_temp=None):
|
1999-08-14 23:53:53 +00:00
|
|
|
|
2000-03-26 21:40:19 +00:00
|
|
|
(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)
|
1999-12-12 16:57:47 +00:00
|
|
|
|
2000-03-18 15:19:51 +00:00
|
|
|
lib_opts = gen_lib_options (self,
|
2000-03-26 21:40:19 +00:00
|
|
|
library_dirs, runtime_library_dirs,
|
2000-03-18 15:19:51 +00:00
|
|
|
libraries)
|
2000-02-10 02:51:32 +00:00
|
|
|
if type (output_dir) not in (StringType, NoneType):
|
|
|
|
raise TypeError, "'output_dir' must be a string or None"
|
1999-09-13 03:12:53 +00:00
|
|
|
if output_dir is not None:
|
|
|
|
output_filename = os.path.join (output_dir, output_filename)
|
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
if self._need_link (objects, output_filename):
|
2000-06-25 02:05:29 +00:00
|
|
|
ld_args = (objects + self.objects +
|
2000-03-06 03:40:29 +00:00
|
|
|
lib_opts + ['-o', output_filename])
|
2000-02-09 02:17:00 +00:00
|
|
|
if debug:
|
|
|
|
ld_args[:0] = ['-g']
|
1999-09-29 12:22:50 +00:00
|
|
|
if extra_preargs:
|
|
|
|
ld_args[:0] = extra_preargs
|
|
|
|
if extra_postargs:
|
|
|
|
ld_args.extend (extra_postargs)
|
2000-03-01 14:43:49 +00:00
|
|
|
self.mkpath (os.path.dirname (output_filename))
|
2000-05-30 01:56:44 +00:00
|
|
|
try:
|
2000-06-25 02:05:29 +00:00
|
|
|
self.spawn (self.linker_so + ld_args)
|
2000-05-30 01:56:44 +00:00
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise LinkError, msg
|
1999-09-13 03:12:53 +00:00
|
|
|
else:
|
|
|
|
self.announce ("skipping %s (up-to-date)" % output_filename)
|
1999-08-14 23:53:53 +00:00
|
|
|
|
1999-10-03 20:45:33 +00:00
|
|
|
# link_shared_object ()
|
|
|
|
|
|
|
|
|
2000-01-09 22:47:53 +00:00
|
|
|
def link_executable (self,
|
|
|
|
objects,
|
|
|
|
output_progname,
|
|
|
|
output_dir=None,
|
|
|
|
libraries=None,
|
|
|
|
library_dirs=None,
|
2000-03-26 21:40:19 +00:00
|
|
|
runtime_library_dirs=None,
|
2000-02-09 02:17:00 +00:00
|
|
|
debug=0,
|
2000-01-09 22:47:53 +00:00
|
|
|
extra_preargs=None,
|
|
|
|
extra_postargs=None):
|
|
|
|
|
2000-03-26 21:40:19 +00:00
|
|
|
(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)
|
2000-01-09 22:47:53 +00:00
|
|
|
|
2000-03-18 15:19:51 +00:00
|
|
|
lib_opts = gen_lib_options (self,
|
2000-03-26 21:40:19 +00:00
|
|
|
library_dirs, runtime_library_dirs,
|
2000-03-18 15:19:51 +00:00
|
|
|
libraries)
|
2000-01-09 22:47:53 +00:00
|
|
|
output_filename = output_progname # Unix-ism!
|
|
|
|
if output_dir is not None:
|
|
|
|
output_filename = os.path.join (output_dir, output_filename)
|
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
if self._need_link (objects, output_filename):
|
|
|
|
ld_args = objects + self.objects + lib_opts + ['-o', output_filename]
|
2000-02-09 02:17:00 +00:00
|
|
|
if debug:
|
|
|
|
ld_args[:0] = ['-g']
|
2000-01-09 22:47:53 +00:00
|
|
|
if extra_preargs:
|
|
|
|
ld_args[:0] = extra_preargs
|
|
|
|
if extra_postargs:
|
|
|
|
ld_args.extend (extra_postargs)
|
2000-03-01 14:43:49 +00:00
|
|
|
self.mkpath (os.path.dirname (output_filename))
|
2000-05-30 01:56:44 +00:00
|
|
|
try:
|
2000-06-25 02:05:29 +00:00
|
|
|
self.spawn (self.linker_exe + ld_args)
|
2000-05-30 01:56:44 +00:00
|
|
|
except DistutilsExecError, msg:
|
|
|
|
raise LinkError, msg
|
2000-01-09 22:47:53 +00:00
|
|
|
else:
|
|
|
|
self.announce ("skipping %s (up-to-date)" % output_filename)
|
|
|
|
|
|
|
|
# link_executable ()
|
|
|
|
|
|
|
|
|
2000-03-06 03:40:29 +00:00
|
|
|
# -- Miscellaneous methods -----------------------------------------
|
|
|
|
# These are all used by the 'gen_lib_options() function, in
|
|
|
|
# ccompiler.py.
|
|
|
|
|
1999-10-03 20:45:33 +00:00
|
|
|
def library_dir_option (self, dir):
|
|
|
|
return "-L" + dir
|
|
|
|
|
2000-03-18 15:19:51 +00:00
|
|
|
def runtime_library_dir_option (self, dir):
|
|
|
|
return "-R" + dir
|
|
|
|
|
1999-10-03 20:45:33 +00:00
|
|
|
def library_option (self, lib):
|
|
|
|
return "-l" + lib
|
|
|
|
|
|
|
|
|
|
|
|
def find_library_file (self, dirs, lib):
|
|
|
|
|
|
|
|
for dir in dirs:
|
|
|
|
shared = os.path.join (dir, self.shared_library_filename (lib))
|
|
|
|
static = os.path.join (dir, self.library_filename (lib))
|
|
|
|
|
|
|
|
# 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 (shared):
|
|
|
|
return shared
|
|
|
|
elif os.path.exists (static):
|
|
|
|
return static
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Oops, didn't find it in *any* of 'dirs'
|
|
|
|
return None
|
|
|
|
|
|
|
|
# find_library_file ()
|
1999-08-29 18:23:32 +00:00
|
|
|
|
1999-07-10 02:04:22 +00:00
|
|
|
# class UnixCCompiler
|