/Lib/distutils/mwerkscompiler.py

http://unladen-swallow.googlecode.com/ · Python · 248 lines · 185 code · 18 blank · 45 comment · 24 complexity · 1c499c66930c349383474c8e59c868bf MD5 · raw file

  1. """distutils.mwerkscompiler
  2. Contains MWerksCompiler, an implementation of the abstract CCompiler class
  3. for MetroWerks CodeWarrior on the Macintosh. Needs work to support CW on
  4. Windows."""
  5. # This module should be kept compatible with Python 2.1.
  6. __revision__ = "$Id: mwerkscompiler.py 55881 2007-06-11 05:28:45Z neal.norwitz $"
  7. import sys, os, string
  8. from types import *
  9. from distutils.errors import \
  10. DistutilsExecError, DistutilsPlatformError, \
  11. CompileError, LibError, LinkError
  12. from distutils.ccompiler import \
  13. CCompiler, gen_preprocess_options, gen_lib_options
  14. import distutils.util
  15. import distutils.dir_util
  16. from distutils import log
  17. class MWerksCompiler (CCompiler) :
  18. """Concrete class that implements an interface to MetroWerks CodeWarrior,
  19. as defined by the CCompiler abstract class."""
  20. compiler_type = 'mwerks'
  21. # Just set this so CCompiler's constructor doesn't barf. We currently
  22. # don't use the 'set_executables()' bureaucracy provided by CCompiler,
  23. # as it really isn't necessary for this sort of single-compiler class.
  24. # Would be nice to have a consistent interface with UnixCCompiler,
  25. # though, so it's worth thinking about.
  26. executables = {}
  27. # Private class data (need to distinguish C from C++ source for compiler)
  28. _c_extensions = ['.c']
  29. _cpp_extensions = ['.cc', '.cpp', '.cxx']
  30. _rc_extensions = ['.r']
  31. _exp_extension = '.exp'
  32. # Needed for the filename generation methods provided by the
  33. # base class, CCompiler.
  34. src_extensions = (_c_extensions + _cpp_extensions +
  35. _rc_extensions)
  36. res_extension = '.rsrc'
  37. obj_extension = '.obj' # Not used, really
  38. static_lib_extension = '.lib'
  39. shared_lib_extension = '.slb'
  40. static_lib_format = shared_lib_format = '%s%s'
  41. exe_extension = ''
  42. def __init__ (self,
  43. verbose=0,
  44. dry_run=0,
  45. force=0):
  46. CCompiler.__init__ (self, verbose, dry_run, force)
  47. def compile (self,
  48. sources,
  49. output_dir=None,
  50. macros=None,
  51. include_dirs=None,
  52. debug=0,
  53. extra_preargs=None,
  54. extra_postargs=None,
  55. depends=None):
  56. (output_dir, macros, include_dirs) = \
  57. self._fix_compile_args (output_dir, macros, include_dirs)
  58. self.__sources = sources
  59. self.__macros = macros
  60. self.__include_dirs = include_dirs
  61. # Don't need extra_preargs and extra_postargs for CW
  62. return []
  63. def link (self,
  64. target_desc,
  65. objects,
  66. output_filename,
  67. output_dir=None,
  68. libraries=None,
  69. library_dirs=None,
  70. runtime_library_dirs=None,
  71. export_symbols=None,
  72. debug=0,
  73. extra_preargs=None,
  74. extra_postargs=None,
  75. build_temp=None,
  76. target_lang=None):
  77. # First fixup.
  78. (objects, output_dir) = self._fix_object_args (objects, output_dir)
  79. (libraries, library_dirs, runtime_library_dirs) = \
  80. self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
  81. # First examine a couple of options for things that aren't implemented yet
  82. if not target_desc in (self.SHARED_LIBRARY, self.SHARED_OBJECT):
  83. raise DistutilsPlatformError, 'Can only make SHARED_LIBRARY or SHARED_OBJECT targets on the Mac'
  84. if runtime_library_dirs:
  85. raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
  86. if extra_preargs or extra_postargs:
  87. raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
  88. if len(export_symbols) != 1:
  89. raise DistutilsPlatformError, 'Need exactly one export symbol'
  90. # Next there are various things for which we need absolute pathnames.
  91. # This is because we (usually) create the project in a subdirectory of
  92. # where we are now, and keeping the paths relative is too much work right
  93. # now.
  94. sources = map(self._filename_to_abs, self.__sources)
  95. include_dirs = map(self._filename_to_abs, self.__include_dirs)
  96. if objects:
  97. objects = map(self._filename_to_abs, objects)
  98. else:
  99. objects = []
  100. if build_temp:
  101. build_temp = self._filename_to_abs(build_temp)
  102. else:
  103. build_temp = os.curdir()
  104. if output_dir:
  105. output_filename = os.path.join(output_dir, output_filename)
  106. # The output filename needs special handling: splitting it into dir and
  107. # filename part. Actually I'm not sure this is really needed, but it
  108. # can't hurt.
  109. output_filename = self._filename_to_abs(output_filename)
  110. output_dir, output_filename = os.path.split(output_filename)
  111. # Now we need the short names of a couple of things for putting them
  112. # into the project.
  113. if output_filename[-8:] == '.ppc.slb':
  114. basename = output_filename[:-8]
  115. elif output_filename[-11:] == '.carbon.slb':
  116. basename = output_filename[:-11]
  117. else:
  118. basename = os.path.strip(output_filename)[0]
  119. projectname = basename + '.mcp'
  120. targetname = basename
  121. xmlname = basename + '.xml'
  122. exportname = basename + '.mcp.exp'
  123. prefixname = 'mwerks_%s_config.h'%basename
  124. # Create the directories we need
  125. distutils.dir_util.mkpath(build_temp, dry_run=self.dry_run)
  126. distutils.dir_util.mkpath(output_dir, dry_run=self.dry_run)
  127. # And on to filling in the parameters for the project builder
  128. settings = {}
  129. settings['mac_exportname'] = exportname
  130. settings['mac_outputdir'] = output_dir
  131. settings['mac_dllname'] = output_filename
  132. settings['mac_targetname'] = targetname
  133. settings['sysprefix'] = sys.prefix
  134. settings['mac_sysprefixtype'] = 'Absolute'
  135. sourcefilenames = []
  136. sourcefiledirs = []
  137. for filename in sources + objects:
  138. dirname, filename = os.path.split(filename)
  139. sourcefilenames.append(filename)
  140. if not dirname in sourcefiledirs:
  141. sourcefiledirs.append(dirname)
  142. settings['sources'] = sourcefilenames
  143. settings['libraries'] = libraries
  144. settings['extrasearchdirs'] = sourcefiledirs + include_dirs + library_dirs
  145. if self.dry_run:
  146. print 'CALLING LINKER IN', os.getcwd()
  147. for key, value in settings.items():
  148. print '%20.20s %s'%(key, value)
  149. return
  150. # Build the export file
  151. exportfilename = os.path.join(build_temp, exportname)
  152. log.debug("\tCreate export file %s", exportfilename)
  153. fp = open(exportfilename, 'w')
  154. fp.write('%s\n'%export_symbols[0])
  155. fp.close()
  156. # Generate the prefix file, if needed, and put it in the settings
  157. if self.__macros:
  158. prefixfilename = os.path.join(os.getcwd(), os.path.join(build_temp, prefixname))
  159. fp = open(prefixfilename, 'w')
  160. fp.write('#include "mwerks_shcarbon_config.h"\n')
  161. for name, value in self.__macros:
  162. if value is None:
  163. fp.write('#define %s\n'%name)
  164. else:
  165. fp.write('#define %s %s\n'%(name, value))
  166. fp.close()
  167. settings['prefixname'] = prefixname
  168. # Build the XML file. We need the full pathname (only lateron, really)
  169. # because we pass this pathname to CodeWarrior in an AppleEvent, and CW
  170. # doesn't have a clue about our working directory.
  171. xmlfilename = os.path.join(os.getcwd(), os.path.join(build_temp, xmlname))
  172. log.debug("\tCreate XML file %s", xmlfilename)
  173. import mkcwproject
  174. xmlbuilder = mkcwproject.cwxmlgen.ProjectBuilder(settings)
  175. xmlbuilder.generate()
  176. xmldata = settings['tmp_projectxmldata']
  177. fp = open(xmlfilename, 'w')
  178. fp.write(xmldata)
  179. fp.close()
  180. # Generate the project. Again a full pathname.
  181. projectfilename = os.path.join(os.getcwd(), os.path.join(build_temp, projectname))
  182. log.debug('\tCreate project file %s', projectfilename)
  183. mkcwproject.makeproject(xmlfilename, projectfilename)
  184. # And build it
  185. log.debug('\tBuild project')
  186. mkcwproject.buildproject(projectfilename)
  187. def _filename_to_abs(self, filename):
  188. # Some filenames seem to be unix-like. Convert to Mac names.
  189. ## if '/' in filename and ':' in filename:
  190. ## raise DistutilsPlatformError, 'Filename may be Unix or Mac style: %s'%filename
  191. ## if '/' in filename:
  192. ## filename = macurl2path(filename)
  193. filename = distutils.util.convert_path(filename)
  194. if not os.path.isabs(filename):
  195. curdir = os.getcwd()
  196. filename = os.path.join(curdir, filename)
  197. # Finally remove .. components
  198. components = string.split(filename, ':')
  199. for i in range(1, len(components)):
  200. if components[i] == '..':
  201. components[i] = ''
  202. return string.join(components, ':')
  203. def library_dir_option (self, dir):
  204. """Return the compiler option to add 'dir' to the list of
  205. directories searched for libraries.
  206. """
  207. return # XXXX Not correct...
  208. def runtime_library_dir_option (self, dir):
  209. """Return the compiler option to add 'dir' to the list of
  210. directories searched for runtime libraries.
  211. """
  212. # Nothing needed or Mwerks/Mac.
  213. return
  214. def library_option (self, lib):
  215. """Return the compiler option to add 'dir' to the list of libraries
  216. linked into the shared library or executable.
  217. """
  218. return
  219. def find_library_file (self, dirs, lib, debug=0):
  220. """Search the specified list of directories for a static or shared
  221. library file 'lib' and return the full path to that file. If
  222. 'debug' true, look for a debugging version (if that makes sense on
  223. the current platform). Return None if 'lib' wasn't found in any of
  224. the specified directories.
  225. """
  226. return 0