PyXR

c:\python24\lib \ distutils \ unixccompiler.py



0001 """distutils.unixccompiler
0002 
0003 Contains the UnixCCompiler class, a subclass of CCompiler that handles
0004 the "typical" Unix-style command-line C compiler:
0005   * macros defined with -Dname[=value]
0006   * macros undefined with -Uname
0007   * include search directories specified with -Idir
0008   * libraries specified with -lllib
0009   * library search directories specified with -Ldir
0010   * compile handled by 'cc' (or similar) executable with -c option:
0011     compiles .c to .o
0012   * link static library handled by 'ar' command (possibly with 'ranlib')
0013   * link shared library handled by 'cc -shared'
0014 """
0015 
0016 __revision__ = "$Id: unixccompiler.py,v 1.56 2004/08/29 16:40:55 loewis Exp $"
0017 
0018 import os, sys
0019 from types import StringType, NoneType
0020 from copy import copy
0021 
0022 from distutils import sysconfig
0023 from distutils.dep_util import newer
0024 from distutils.ccompiler import \
0025      CCompiler, gen_preprocess_options, gen_lib_options
0026 from distutils.errors import \
0027      DistutilsExecError, CompileError, LibError, LinkError
0028 from distutils import log
0029 
0030 # XXX Things not currently handled:
0031 #   * optimization/debug/warning flags; we just use whatever's in Python's
0032 #     Makefile and live with it.  Is this adequate?  If not, we might
0033 #     have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
0034 #     SunCCompiler, and I suspect down that road lies madness.
0035 #   * even if we don't know a warning flag from an optimization flag,
0036 #     we need some way for outsiders to feed preprocessor/compiler/linker
0037 #     flags in to us -- eg. a sysadmin might want to mandate certain flags
0038 #     via a site config file, or a user might want to set something for
0039 #     compiling this module distribution only via the setup.py command
0040 #     line, whatever.  As long as these options come from something on the
0041 #     current system, they can be as system-dependent as they like, and we
0042 #     should just happily stuff them into the preprocessor/compiler/linker
0043 #     options and carry on.
0044 
0045 class UnixCCompiler(CCompiler):
0046 
0047     compiler_type = 'unix'
0048 
0049     # These are used by CCompiler in two places: the constructor sets
0050     # instance attributes 'preprocessor', 'compiler', etc. from them, and
0051     # 'set_executable()' allows any of these to be set.  The defaults here
0052     # are pretty generic; they will probably have to be set by an outsider
0053     # (eg. using information discovered by the sysconfig about building
0054     # Python extensions).
0055     executables = {'preprocessor' : None,
0056                    'compiler'     : ["cc"],
0057                    'compiler_so'  : ["cc"],
0058                    'compiler_cxx' : ["cc"],
0059                    'linker_so'    : ["cc", "-shared"],
0060                    'linker_exe'   : ["cc"],
0061                    'archiver'     : ["ar", "-cr"],
0062                    'ranlib'       : None,
0063                   }
0064 
0065     if sys.platform[:6] == "darwin":
0066         executables['ranlib'] = ["ranlib"]
0067 
0068     # Needed for the filename generation methods provided by the base
0069     # class, CCompiler.  NB. whoever instantiates/uses a particular
0070     # UnixCCompiler instance should set 'shared_lib_ext' -- we set a
0071     # reasonable common default here, but it's not necessarily used on all
0072     # Unices!
0073 
0074     src_extensions = [".c",".C",".cc",".cxx",".cpp",".m"]
0075     obj_extension = ".o"
0076     static_lib_extension = ".a"
0077     shared_lib_extension = ".so"
0078     dylib_lib_extension = ".dylib"
0079     static_lib_format = shared_lib_format = dylib_lib_format = "lib%s%s"
0080     if sys.platform == "cygwin":
0081         exe_extension = ".exe"
0082 
0083     def preprocess(self, source,
0084                    output_file=None, macros=None, include_dirs=None,
0085                    extra_preargs=None, extra_postargs=None):
0086         ignore, macros, include_dirs = \
0087             self._fix_compile_args(None, macros, include_dirs)
0088         pp_opts = gen_preprocess_options(macros, include_dirs)
0089         pp_args = self.preprocessor + pp_opts
0090         if output_file:
0091             pp_args.extend(['-o', output_file])
0092         if extra_preargs:
0093             pp_args[:0] = extra_preargs
0094         if extra_postargs:
0095             pp_args.extend(extra_postargs)
0096         pp_args.append(source)
0097 
0098         # We need to preprocess: either we're being forced to, or we're
0099         # generating output to stdout, or there's a target output file and
0100         # the source file is newer than the target (or the target doesn't
0101         # exist).
0102         if self.force or output_file is None or newer(source, output_file):
0103             if output_file:
0104                 self.mkpath(os.path.dirname(output_file))
0105             try:
0106                 self.spawn(pp_args)
0107             except DistutilsExecError, msg:
0108                 raise CompileError, msg
0109 
0110     def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
0111         try:
0112             self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
0113                        extra_postargs)
0114         except DistutilsExecError, msg:
0115             raise CompileError, msg
0116 
0117     def create_static_lib(self, objects, output_libname,
0118                           output_dir=None, debug=0, target_lang=None):
0119         objects, output_dir = self._fix_object_args(objects, output_dir)
0120 
0121         output_filename = \
0122             self.library_filename(output_libname, output_dir=output_dir)
0123 
0124         if self._need_link(objects, output_filename):
0125             self.mkpath(os.path.dirname(output_filename))
0126             self.spawn(self.archiver +
0127                        [output_filename] +
0128                        objects + self.objects)
0129 
0130             # Not many Unices required ranlib anymore -- SunOS 4.x is, I
0131             # think the only major Unix that does.  Maybe we need some
0132             # platform intelligence here to skip ranlib if it's not
0133             # needed -- or maybe Python's configure script took care of
0134             # it for us, hence the check for leading colon.
0135             if self.ranlib:
0136                 try:
0137                     self.spawn(self.ranlib + [output_filename])
0138                 except DistutilsExecError, msg:
0139                     raise LibError, msg
0140         else:
0141             log.debug("skipping %s (up-to-date)", output_filename)
0142 
0143     def link(self, target_desc, objects,
0144              output_filename, output_dir=None, libraries=None,
0145              library_dirs=None, runtime_library_dirs=None,
0146              export_symbols=None, debug=0, extra_preargs=None,
0147              extra_postargs=None, build_temp=None, target_lang=None):
0148         objects, output_dir = self._fix_object_args(objects, output_dir)
0149         libraries, library_dirs, runtime_library_dirs = \
0150             self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
0151 
0152         lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs,
0153                                    libraries)
0154         if type(output_dir) not in (StringType, NoneType):
0155             raise TypeError, "'output_dir' must be a string or None"
0156         if output_dir is not None:
0157             output_filename = os.path.join(output_dir, output_filename)
0158 
0159         if self._need_link(objects, output_filename):
0160             ld_args = (objects + self.objects +
0161                        lib_opts + ['-o', output_filename])
0162             if debug:
0163                 ld_args[:0] = ['-g']
0164             if extra_preargs:
0165                 ld_args[:0] = extra_preargs
0166             if extra_postargs:
0167                 ld_args.extend(extra_postargs)
0168             self.mkpath(os.path.dirname(output_filename))
0169             try:
0170                 if target_desc == CCompiler.EXECUTABLE:
0171                     linker = self.linker_exe[:]
0172                 else:
0173                     linker = self.linker_so[:]
0174                 if target_lang == "c++" and self.compiler_cxx:
0175                     linker[0] = self.compiler_cxx[0]
0176                 self.spawn(linker + ld_args)
0177             except DistutilsExecError, msg:
0178                 raise LinkError, msg
0179         else:
0180             log.debug("skipping %s (up-to-date)", output_filename)
0181 
0182     # -- Miscellaneous methods -----------------------------------------
0183     # These are all used by the 'gen_lib_options() function, in
0184     # ccompiler.py.
0185 
0186     def library_dir_option(self, dir):
0187         return "-L" + dir
0188 
0189     def runtime_library_dir_option(self, dir):
0190         # XXX Hackish, at the very least.  See Python bug #445902:
0191         # http://sourceforge.net/tracker/index.php
0192         #   ?func=detail&aid=445902&group_id=5470&atid=105470
0193         # Linkers on different platforms need different options to
0194         # specify that directories need to be added to the list of
0195         # directories searched for dependencies when a dynamic library
0196         # is sought.  GCC has to be told to pass the -R option through
0197         # to the linker, whereas other compilers just know this.
0198         # Other compilers may need something slightly different.  At
0199         # this time, there's no way to determine this information from
0200         # the configuration data stored in the Python installation, so
0201         # we use this hack.
0202         compiler = os.path.basename(sysconfig.get_config_var("CC"))
0203         if sys.platform[:6] == "darwin":
0204             # MacOSX's linker doesn't understand the -R flag at all
0205             return "-L" + dir
0206         elif sys.platform[:5] == "hp-ux":
0207             return "+s -L" + dir
0208         elif sys.platform[:7] == "irix646" or sys.platform[:6] == "osf1V5":
0209             return ["-rpath", dir]
0210         elif compiler[:3] == "gcc" or compiler[:3] == "g++":
0211             return "-Wl,-R" + dir
0212         else:
0213             return "-R" + dir
0214 
0215     def library_option(self, lib):
0216         return "-l" + lib
0217 
0218     def find_library_file(self, dirs, lib, debug=0):
0219         shared_f = self.library_filename(lib, lib_type='shared')
0220         dylib_f = self.library_filename(lib, lib_type='dylib')
0221         static_f = self.library_filename(lib, lib_type='static')
0222 
0223         for dir in dirs:
0224             shared = os.path.join(dir, shared_f)
0225             dylib = os.path.join(dir, dylib_f)
0226             static = os.path.join(dir, static_f)
0227             # We're second-guessing the linker here, with not much hard
0228             # data to go on: GCC seems to prefer the shared library, so I'm
0229             # assuming that *all* Unix C compilers do.  And of course I'm
0230             # ignoring even GCC's "-static" option.  So sue me.
0231             if os.path.exists(dylib):
0232                 return dylib
0233             elif os.path.exists(shared):
0234                 return shared
0235             elif os.path.exists(static):
0236                 return static
0237 
0238         # Oops, didn't find it in *any* of 'dirs'
0239         return None
0240 

Generated by PyXR 0.9.4
SourceForge.net Logo