PyXR

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



0001 """distutils.mwerkscompiler
0002 
0003 Contains MWerksCompiler, an implementation of the abstract CCompiler class
0004 for MetroWerks CodeWarrior on the Macintosh. Needs work to support CW on
0005 Windows."""
0006 
0007 # This module should be kept compatible with Python 1.5.2.
0008 
0009 __revision__ = "$Id: mwerkscompiler.py,v 1.12 2002/11/19 13:12:27 akuchling Exp $"
0010 
0011 import sys, os, string
0012 from types import *
0013 from distutils.errors import \
0014      DistutilsExecError, DistutilsPlatformError, \
0015      CompileError, LibError, LinkError
0016 from distutils.ccompiler import \
0017      CCompiler, gen_preprocess_options, gen_lib_options
0018 import distutils.util
0019 import distutils.dir_util
0020 from distutils import log
0021 import mkcwproject
0022 
0023 class MWerksCompiler (CCompiler) :
0024     """Concrete class that implements an interface to MetroWerks CodeWarrior,
0025        as defined by the CCompiler abstract class."""
0026 
0027     compiler_type = 'mwerks'
0028 
0029     # Just set this so CCompiler's constructor doesn't barf.  We currently
0030     # don't use the 'set_executables()' bureaucracy provided by CCompiler,
0031     # as it really isn't necessary for this sort of single-compiler class.
0032     # Would be nice to have a consistent interface with UnixCCompiler,
0033     # though, so it's worth thinking about.
0034     executables = {}
0035 
0036     # Private class data (need to distinguish C from C++ source for compiler)
0037     _c_extensions = ['.c']
0038     _cpp_extensions = ['.cc', '.cpp', '.cxx']
0039     _rc_extensions = ['.r']
0040     _exp_extension = '.exp'
0041 
0042     # Needed for the filename generation methods provided by the
0043     # base class, CCompiler.
0044     src_extensions = (_c_extensions + _cpp_extensions +
0045                       _rc_extensions)
0046     res_extension = '.rsrc'
0047     obj_extension = '.obj' # Not used, really
0048     static_lib_extension = '.lib'
0049     shared_lib_extension = '.slb'
0050     static_lib_format = shared_lib_format = '%s%s'
0051     exe_extension = ''
0052 
0053 
0054     def __init__ (self,
0055                   verbose=0,
0056                   dry_run=0,
0057                   force=0):
0058 
0059         CCompiler.__init__ (self, verbose, dry_run, force)
0060 
0061 
0062     def compile (self,
0063                  sources,
0064                  output_dir=None,
0065                  macros=None,
0066                  include_dirs=None,
0067                  debug=0,
0068                  extra_preargs=None,
0069                  extra_postargs=None,
0070                  depends=None):
0071         (output_dir, macros, include_dirs) = \
0072            self._fix_compile_args (output_dir, macros, include_dirs)
0073         self.__sources = sources
0074         self.__macros = macros
0075         self.__include_dirs = include_dirs
0076         # Don't need extra_preargs and extra_postargs for CW
0077         return []
0078 
0079     def link (self,
0080               target_desc,
0081               objects,
0082               output_filename,
0083               output_dir=None,
0084               libraries=None,
0085               library_dirs=None,
0086               runtime_library_dirs=None,
0087               export_symbols=None,
0088               debug=0,
0089               extra_preargs=None,
0090               extra_postargs=None,
0091               build_temp=None,
0092               target_lang=None):
0093         # First fixup.
0094         (objects, output_dir) = self._fix_object_args (objects, output_dir)
0095         (libraries, library_dirs, runtime_library_dirs) = \
0096             self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
0097 
0098         # First examine a couple of options for things that aren't implemented yet
0099         if not target_desc in (self.SHARED_LIBRARY, self.SHARED_OBJECT):
0100             raise DistutilsPlatformError, 'Can only make SHARED_LIBRARY or SHARED_OBJECT targets on the Mac'
0101         if runtime_library_dirs:
0102             raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
0103         if extra_preargs or extra_postargs:
0104             raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
0105         if len(export_symbols) != 1:
0106             raise DistutilsPlatformError, 'Need exactly one export symbol'
0107         # Next there are various things for which we need absolute pathnames.
0108         # This is because we (usually) create the project in a subdirectory of
0109         # where we are now, and keeping the paths relative is too much work right
0110         # now.
0111         sources = map(self._filename_to_abs, self.__sources)
0112         include_dirs = map(self._filename_to_abs, self.__include_dirs)
0113         if objects:
0114             objects = map(self._filename_to_abs, objects)
0115         else:
0116             objects = []
0117         if build_temp:
0118             build_temp = self._filename_to_abs(build_temp)
0119         else:
0120             build_temp = os.curdir()
0121         if output_dir:
0122             output_filename = os.path.join(output_dir, output_filename)
0123         # The output filename needs special handling: splitting it into dir and
0124         # filename part. Actually I'm not sure this is really needed, but it
0125         # can't hurt.
0126         output_filename = self._filename_to_abs(output_filename)
0127         output_dir, output_filename = os.path.split(output_filename)
0128         # Now we need the short names of a couple of things for putting them
0129         # into the project.
0130         if output_filename[-8:] == '.ppc.slb':
0131             basename = output_filename[:-8]
0132         elif output_filename[-11:] == '.carbon.slb':
0133             basename = output_filename[:-11]
0134         else:
0135             basename = os.path.strip(output_filename)[0]
0136         projectname = basename + '.mcp'
0137         targetname = basename
0138         xmlname = basename + '.xml'
0139         exportname = basename + '.mcp.exp'
0140         prefixname = 'mwerks_%s_config.h'%basename
0141         # Create the directories we need
0142         distutils.dir_util.mkpath(build_temp, dry_run=self.dry_run)
0143         distutils.dir_util.mkpath(output_dir, dry_run=self.dry_run)
0144         # And on to filling in the parameters for the project builder
0145         settings = {}
0146         settings['mac_exportname'] = exportname
0147         settings['mac_outputdir'] = output_dir
0148         settings['mac_dllname'] = output_filename
0149         settings['mac_targetname'] = targetname
0150         settings['sysprefix'] = sys.prefix
0151         settings['mac_sysprefixtype'] = 'Absolute'
0152         sourcefilenames = []
0153         sourcefiledirs = []
0154         for filename in sources + objects:
0155             dirname, filename = os.path.split(filename)
0156             sourcefilenames.append(filename)
0157             if not dirname in sourcefiledirs:
0158                 sourcefiledirs.append(dirname)
0159         settings['sources'] = sourcefilenames
0160         settings['libraries'] = libraries
0161         settings['extrasearchdirs'] = sourcefiledirs + include_dirs + library_dirs
0162         if self.dry_run:
0163             print 'CALLING LINKER IN', os.getcwd()
0164             for key, value in settings.items():
0165                 print '%20.20s %s'%(key, value)
0166             return
0167         # Build the export file
0168         exportfilename = os.path.join(build_temp, exportname)
0169         log.debug("\tCreate export file %s", exportfilename)
0170         fp = open(exportfilename, 'w')
0171         fp.write('%s\n'%export_symbols[0])
0172         fp.close()
0173         # Generate the prefix file, if needed, and put it in the settings
0174         if self.__macros:
0175             prefixfilename = os.path.join(os.getcwd(), os.path.join(build_temp, prefixname))
0176             fp = open(prefixfilename, 'w')
0177             fp.write('#include "mwerks_shcarbon_config.h"\n')
0178             for name, value in self.__macros:
0179                 if value is None:
0180                     fp.write('#define %s\n'%name)
0181                 else:
0182                     fp.write('#define %s %s\n'%(name, value))
0183             fp.close()
0184             settings['prefixname'] = prefixname
0185 
0186         # Build the XML file. We need the full pathname (only lateron, really)
0187         # because we pass this pathname to CodeWarrior in an AppleEvent, and CW
0188         # doesn't have a clue about our working directory.
0189         xmlfilename = os.path.join(os.getcwd(), os.path.join(build_temp, xmlname))
0190         log.debug("\tCreate XML file %s", xmlfilename)
0191         xmlbuilder = mkcwproject.cwxmlgen.ProjectBuilder(settings)
0192         xmlbuilder.generate()
0193         xmldata = settings['tmp_projectxmldata']
0194         fp = open(xmlfilename, 'w')
0195         fp.write(xmldata)
0196         fp.close()
0197         # Generate the project. Again a full pathname.
0198         projectfilename = os.path.join(os.getcwd(), os.path.join(build_temp, projectname))
0199         log.debug('\tCreate project file %s', projectfilename)
0200         mkcwproject.makeproject(xmlfilename, projectfilename)
0201         # And build it
0202         log.debug('\tBuild project')
0203         mkcwproject.buildproject(projectfilename)
0204 
0205     def _filename_to_abs(self, filename):
0206         # Some filenames seem to be unix-like. Convert to Mac names.
0207 ##        if '/' in filename and ':' in filename:
0208 ##           raise DistutilsPlatformError, 'Filename may be Unix or Mac style: %s'%filename
0209 ##        if '/' in filename:
0210 ##           filename = macurl2path(filename)
0211         filename = distutils.util.convert_path(filename)
0212         if not os.path.isabs(filename):
0213             curdir = os.getcwd()
0214             filename = os.path.join(curdir, filename)
0215         # Finally remove .. components
0216         components = string.split(filename, ':')
0217         for i in range(1, len(components)):
0218             if components[i] == '..':
0219                 components[i] = ''
0220         return string.join(components, ':')
0221 
0222     def library_dir_option (self, dir):
0223         """Return the compiler option to add 'dir' to the list of
0224         directories searched for libraries.
0225         """
0226         return # XXXX Not correct...
0227 
0228     def runtime_library_dir_option (self, dir):
0229         """Return the compiler option to add 'dir' to the list of
0230         directories searched for runtime libraries.
0231         """
0232         # Nothing needed or Mwerks/Mac.
0233         return
0234 
0235     def library_option (self, lib):
0236         """Return the compiler option to add 'dir' to the list of libraries
0237         linked into the shared library or executable.
0238         """
0239         return
0240 
0241     def find_library_file (self, dirs, lib, debug=0):
0242         """Search the specified list of directories for a static or shared
0243         library file 'lib' and return the full path to that file.  If
0244         'debug' true, look for a debugging version (if that makes sense on
0245         the current platform).  Return None if 'lib' wasn't found in any of
0246         the specified directories.
0247         """
0248         return 0
0249 

Generated by PyXR 0.9.4
SourceForge.net Logo