mwerkscompiler.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 37828 2004-11-10 22:23:15Z loewis $"
  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. import mkcwproject
  18. class MWerksCompiler (CCompiler) :
  19. """Concrete class that implements an interface to MetroWerks CodeWarrior,
  20. as defined by the CCompiler abstract class."""
  21. compiler_type = 'mwerks'
  22. # Just set this so CCompiler's constructor doesn't barf. We currently
  23. # don't use the 'set_executables()' bureaucracy provided by CCompiler,
  24. # as it really isn't necessary for this sort of single-compiler class.
  25. # Would be nice to have a consistent interface with UnixCCompiler,
  26. # though, so it's worth thinking about.
  27. executables = {}
  28. # Private class data (need to distinguish C from C++ source for compiler)
  29. _c_extensions = ['.c']
  30. _cpp_extensions = ['.cc', '.cpp', '.cxx']
  31. _rc_extensions = ['.r']
  32. _exp_extension = '.exp'
  33. # Needed for the filename generation methods provided by the
  34. # base class, CCompiler.
  35. src_extensions = (_c_extensions + _cpp_extensions +
  36. _rc_extensions)
  37. res_extension = '.rsrc'
  38. obj_extension = '.obj' # Not used, really
  39. static_lib_extension = '.lib'
  40. shared_lib_extension = '.slb'
  41. static_lib_format = shared_lib_format = '%s%s'
  42. exe_extension = ''
  43. def __init__ (self,
  44. verbose=0,
  45. dry_run=0,
  46. force=0):
  47. CCompiler.__init__ (self, verbose, dry_run, force)
  48. def compile (self,
  49. sources,
  50. output_dir=None,
  51. macros=None,
  52. include_dirs=None,
  53. debug=0,
  54. extra_preargs=None,
  55. extra_postargs=None,
  56. depends=None):
  57. (output_dir, macros, include_dirs) = \
  58. self._fix_compile_args (output_dir, macros, include_dirs)
  59. self.__sources = sources
  60. self.__macros = macros
  61. self.__include_dirs = include_dirs
  62. # Don't need extra_preargs and extra_postargs for CW
  63. return []
  64. def link (self,
  65. target_desc,
  66. objects,
  67. output_filename,
  68. output_dir=None,
  69. libraries=None,
  70. library_dirs=None,
  71. runtime_library_dirs=None,
  72. export_symbols=None,
  73. debug=0,
  74. extra_preargs=None,
  75. extra_postargs=None,
  76. build_temp=None,
  77. target_lang=None):
  78. # First fixup.
  79. (objects, output_dir) = self._fix_object_args (objects, output_dir)
  80. (libraries, library_dirs, runtime_library_dirs) = \
  81. self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
  82. # First examine a couple of options for things that aren't implemented yet
  83. if not target_desc in (self.SHARED_LIBRARY, self.SHARED_OBJECT):
  84. raise DistutilsPlatformError, 'Can only make SHARED_LIBRARY or SHARED_OBJECT targets on the Mac'
  85. if runtime_library_dirs:
  86. raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
  87. if extra_preargs or extra_postargs:
  88. raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
  89. if len(export_symbols) != 1:
  90. raise DistutilsPlatformError, 'Need exactly one export symbol'
  91. # Next there are various things for which we need absolute pathnames.
  92. # This is because we (usually) create the project in a subdirectory of
  93. # where we are now, and keeping the paths relative is too much work right
  94. # now.
  95. sources = map(self._filename_to_abs, self.__sources)
  96. include_dirs = map(self._filename_to_abs, self.__include_dirs)
  97. if objects:
  98. objects = map(self._filename_to_abs, objects)
  99. else:
  100. objects = []
  101. if build_temp:
  102. build_temp = self._filename_to_abs(build_temp)
  103. else:
  104. build_temp = os.curdir()
  105. if output_dir:
  106. output_filename = os.path.join(output_dir, output_filename)
  107. # The output filename needs special handling: splitting it into dir and
  108. # filename part. Actually I'm not sure this is really needed, but it
  109. # can't hurt.
  110. output_filename = self._filename_to_abs(output_filename)
  111. output_dir, output_filename = os.path.split(output_filename)
  112. # Now we need the short names of a couple of things for putting them
  113. # into the project.
  114. if output_filename[-8:] == '.ppc.slb':
  115. basename = output_filename[:-8]
  116. elif output_filename[-11:] == '.carbon.slb':
  117. basename = output_filename[:-11]
  118. else:
  119. basename = os.path.strip(output_filename)[0]
  120. projectname = basename + '.mcp'
  121. targetname = basename
  122. xmlname = basename + '.xml'
  123. exportname = basename + '.mcp.exp'
  124. prefixname = 'mwerks_%s_config.h'%basename
  125. # Create the directories we need
  126. distutils.dir_util.mkpath(build_temp, dry_run=self.dry_run)
  127. distutils.dir_util.mkpath(output_dir, dry_run=self.dry_run)
  128. # And on to filling in the parameters for the project builder
  129. settings = {}
  130. settings['mac_exportname'] = exportname
  131. settings['mac_outputdir'] = output_dir
  132. settings['mac_dllname'] = output_filename
  133. settings['mac_targetname'] = targetname
  134. settings['sysprefix'] = sys.prefix
  135. settings['mac_sysprefixtype'] = 'Absolute'
  136. sourcefilenames = []
  137. sourcefiledirs = []
  138. for filename in sources + objects:
  139. dirname, filename = os.path.split(filename)
  140. sourcefilenames.append(filename)
  141. if not dirname in sourcefiledirs:
  142. sourcefiledirs.append(dirname)
  143. settings['sources'] = sourcefilenames
  144. settings['libraries'] = libraries
  145. settings['extrasearchdirs'] = sourcefiledirs + include_dirs + library_dirs
  146. if self.dry_run:
  147. print 'CALLING LINKER IN', os.getcwd()
  148. for key, value in settings.items():
  149. print '%20.20s %s'%(key, value)
  150. return
  151. # Build the export file
  152. exportfilename = os.path.join(build_temp, exportname)
  153. log.debug("\tCreate export file %s", exportfilename)
  154. fp = open(exportfilename, 'w')
  155. fp.write('%s\n'%export_symbols[0])
  156. fp.close()
  157. # Generate the prefix file, if needed, and put it in the settings
  158. if self.__macros:
  159. prefixfilename = os.path.join(os.getcwd(), os.path.join(build_temp, prefixname))
  160. fp = open(prefixfilename, 'w')
  161. fp.write('#include "mwerks_shcarbon_config.h"\n')
  162. for name, value in self.__macros:
  163. if value is None:
  164. fp.write('#define %s\n'%name)
  165. else:
  166. fp.write('#define %s %s\n'%(name, value))
  167. fp.close()
  168. settings['prefixname'] = prefixname
  169. # Build the XML file. We need the full pathname (only lateron, really)
  170. # because we pass this pathname to CodeWarrior in an AppleEvent, and CW
  171. # doesn't have a clue about our working directory.
  172. xmlfilename = os.path.join(os.getcwd(), os.path.join(build_temp, xmlname))
  173. log.debug("\tCreate XML file %s", xmlfilename)
  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