blob: 662046ae22345673cc344aa299b40e36f49abf90 [file] [log] [blame]
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +00001"""distutils.mwerkscompiler
2
3Contains MWerksCompiler, an implementation of the abstract CCompiler class
4for MetroWerks CodeWarrior on the Macintosh. Needs work to support CW on
5Windows."""
6
Martin v. Löwis5a6601c2004-11-10 22:23:15 +00007# This module should be kept compatible with Python 2.1.
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00008
9__revision__ = "$Id$"
10
Neal Norwitz9d72bb42007-04-17 08:48:32 +000011import sys, os
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000012from types import *
13from distutils.errors import \
14 DistutilsExecError, DistutilsPlatformError, \
15 CompileError, LibError, LinkError
16from distutils.ccompiler import \
17 CCompiler, gen_preprocess_options, gen_lib_options
18import distutils.util
19import distutils.dir_util
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000020from distutils import log
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000021
22class MWerksCompiler (CCompiler) :
Jack Jansen92c2ebf2001-11-10 23:20:22 +000023 """Concrete class that implements an interface to MetroWerks CodeWarrior,
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000024 as defined by the CCompiler abstract class."""
25
26 compiler_type = 'mwerks'
27
28 # Just set this so CCompiler's constructor doesn't barf. We currently
29 # don't use the 'set_executables()' bureaucracy provided by CCompiler,
30 # as it really isn't necessary for this sort of single-compiler class.
31 # Would be nice to have a consistent interface with UnixCCompiler,
32 # though, so it's worth thinking about.
33 executables = {}
34
35 # Private class data (need to distinguish C from C++ source for compiler)
36 _c_extensions = ['.c']
37 _cpp_extensions = ['.cc', '.cpp', '.cxx']
38 _rc_extensions = ['.r']
39 _exp_extension = '.exp'
40
41 # Needed for the filename generation methods provided by the
42 # base class, CCompiler.
43 src_extensions = (_c_extensions + _cpp_extensions +
44 _rc_extensions)
45 res_extension = '.rsrc'
46 obj_extension = '.obj' # Not used, really
47 static_lib_extension = '.lib'
48 shared_lib_extension = '.slb'
49 static_lib_format = shared_lib_format = '%s%s'
50 exe_extension = ''
51
52
53 def __init__ (self,
54 verbose=0,
55 dry_run=0,
56 force=0):
57
58 CCompiler.__init__ (self, verbose, dry_run, force)
Fred Drakeb94b8492001-12-06 20:51:35 +000059
60
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000061 def compile (self,
62 sources,
63 output_dir=None,
64 macros=None,
65 include_dirs=None,
66 debug=0,
67 extra_preargs=None,
Jeremy Hylton6864d302002-06-13 17:27:13 +000068 extra_postargs=None,
69 depends=None):
Fred Drakeb94b8492001-12-06 20:51:35 +000070 (output_dir, macros, include_dirs) = \
71 self._fix_compile_args (output_dir, macros, include_dirs)
72 self.__sources = sources
73 self.__macros = macros
74 self.__include_dirs = include_dirs
75 # Don't need extra_preargs and extra_postargs for CW
76 return []
77
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000078 def link (self,
79 target_desc,
80 objects,
81 output_filename,
82 output_dir=None,
83 libraries=None,
84 library_dirs=None,
85 runtime_library_dirs=None,
86 export_symbols=None,
87 debug=0,
88 extra_preargs=None,
89 extra_postargs=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +000090 build_temp=None,
91 target_lang=None):
Jack Jansen9020bce2001-06-19 21:23:11 +000092 # First fixup.
93 (objects, output_dir) = self._fix_object_args (objects, output_dir)
94 (libraries, library_dirs, runtime_library_dirs) = \
95 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
96
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000097 # First examine a couple of options for things that aren't implemented yet
98 if not target_desc in (self.SHARED_LIBRARY, self.SHARED_OBJECT):
99 raise DistutilsPlatformError, 'Can only make SHARED_LIBRARY or SHARED_OBJECT targets on the Mac'
100 if runtime_library_dirs:
101 raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
102 if extra_preargs or extra_postargs:
103 raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
104 if len(export_symbols) != 1:
105 raise DistutilsPlatformError, 'Need exactly one export symbol'
106 # Next there are various things for which we need absolute pathnames.
107 # This is because we (usually) create the project in a subdirectory of
108 # where we are now, and keeping the paths relative is too much work right
109 # now.
110 sources = map(self._filename_to_abs, self.__sources)
111 include_dirs = map(self._filename_to_abs, self.__include_dirs)
112 if objects:
113 objects = map(self._filename_to_abs, objects)
114 else:
115 objects = []
116 if build_temp:
117 build_temp = self._filename_to_abs(build_temp)
118 else:
119 build_temp = os.curdir()
120 if output_dir:
121 output_filename = os.path.join(output_dir, output_filename)
122 # The output filename needs special handling: splitting it into dir and
123 # filename part. Actually I'm not sure this is really needed, but it
124 # can't hurt.
125 output_filename = self._filename_to_abs(output_filename)
126 output_dir, output_filename = os.path.split(output_filename)
127 # Now we need the short names of a couple of things for putting them
128 # into the project.
129 if output_filename[-8:] == '.ppc.slb':
130 basename = output_filename[:-8]
Jack Jansendd13a202001-05-17 12:52:01 +0000131 elif output_filename[-11:] == '.carbon.slb':
132 basename = output_filename[:-11]
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000133 else:
134 basename = os.path.strip(output_filename)[0]
135 projectname = basename + '.mcp'
136 targetname = basename
137 xmlname = basename + '.xml'
138 exportname = basename + '.mcp.exp'
139 prefixname = 'mwerks_%s_config.h'%basename
140 # Create the directories we need
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000141 distutils.dir_util.mkpath(build_temp, dry_run=self.dry_run)
142 distutils.dir_util.mkpath(output_dir, dry_run=self.dry_run)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000143 # And on to filling in the parameters for the project builder
144 settings = {}
145 settings['mac_exportname'] = exportname
146 settings['mac_outputdir'] = output_dir
147 settings['mac_dllname'] = output_filename
148 settings['mac_targetname'] = targetname
149 settings['sysprefix'] = sys.prefix
150 settings['mac_sysprefixtype'] = 'Absolute'
151 sourcefilenames = []
152 sourcefiledirs = []
153 for filename in sources + objects:
154 dirname, filename = os.path.split(filename)
155 sourcefilenames.append(filename)
156 if not dirname in sourcefiledirs:
157 sourcefiledirs.append(dirname)
158 settings['sources'] = sourcefilenames
Jack Jansen92c2ebf2001-11-10 23:20:22 +0000159 settings['libraries'] = libraries
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000160 settings['extrasearchdirs'] = sourcefiledirs + include_dirs + library_dirs
161 if self.dry_run:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000162 print('CALLING LINKER IN', os.getcwd())
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000163 for key, value in settings.items():
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000164 print('%20.20s %s'%(key, value))
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000165 return
166 # Build the export file
167 exportfilename = os.path.join(build_temp, exportname)
Jack Jansenab5320b2002-06-26 15:42:49 +0000168 log.debug("\tCreate export file %s", exportfilename)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000169 fp = open(exportfilename, 'w')
170 fp.write('%s\n'%export_symbols[0])
171 fp.close()
172 # Generate the prefix file, if needed, and put it in the settings
173 if self.__macros:
174 prefixfilename = os.path.join(os.getcwd(), os.path.join(build_temp, prefixname))
175 fp = open(prefixfilename, 'w')
Jack Jansen2bb59802002-06-27 22:10:19 +0000176 fp.write('#include "mwerks_shcarbon_config.h"\n')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000177 for name, value in self.__macros:
178 if value is None:
179 fp.write('#define %s\n'%name)
180 else:
Just van Rossum92c5bdb2001-06-19 19:44:02 +0000181 fp.write('#define %s %s\n'%(name, value))
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000182 fp.close()
183 settings['prefixname'] = prefixname
184
185 # Build the XML file. We need the full pathname (only lateron, really)
186 # because we pass this pathname to CodeWarrior in an AppleEvent, and CW
187 # doesn't have a clue about our working directory.
188 xmlfilename = os.path.join(os.getcwd(), os.path.join(build_temp, xmlname))
Jack Jansenab5320b2002-06-26 15:42:49 +0000189 log.debug("\tCreate XML file %s", xmlfilename)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000190 import mkcwproject
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000191 xmlbuilder = mkcwproject.cwxmlgen.ProjectBuilder(settings)
192 xmlbuilder.generate()
193 xmldata = settings['tmp_projectxmldata']
194 fp = open(xmlfilename, 'w')
195 fp.write(xmldata)
196 fp.close()
197 # Generate the project. Again a full pathname.
198 projectfilename = os.path.join(os.getcwd(), os.path.join(build_temp, projectname))
Jack Jansenab5320b2002-06-26 15:42:49 +0000199 log.debug('\tCreate project file %s', projectfilename)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000200 mkcwproject.makeproject(xmlfilename, projectfilename)
201 # And build it
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000202 log.debug('\tBuild project')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000203 mkcwproject.buildproject(projectfilename)
Fred Drakeb94b8492001-12-06 20:51:35 +0000204
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000205 def _filename_to_abs(self, filename):
206 # Some filenames seem to be unix-like. Convert to Mac names.
207## if '/' in filename and ':' in filename:
208## raise DistutilsPlatformError, 'Filename may be Unix or Mac style: %s'%filename
209## if '/' in filename:
210## filename = macurl2path(filename)
211 filename = distutils.util.convert_path(filename)
212 if not os.path.isabs(filename):
Fred Drakeb94b8492001-12-06 20:51:35 +0000213 curdir = os.getcwd()
214 filename = os.path.join(curdir, filename)
Jack Jansen9020bce2001-06-19 21:23:11 +0000215 # Finally remove .. components
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000216 components = filename.split(':')
Jack Jansen9020bce2001-06-19 21:23:11 +0000217 for i in range(1, len(components)):
Fred Drakeb94b8492001-12-06 20:51:35 +0000218 if components[i] == '..':
219 components[i] = ''
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000220 return ':'.join(components)
Jack Jansenab5320b2002-06-26 15:42:49 +0000221
222 def library_dir_option (self, dir):
223 """Return the compiler option to add 'dir' to the list of
224 directories searched for libraries.
225 """
226 return # XXXX Not correct...
227
228 def runtime_library_dir_option (self, dir):
229 """Return the compiler option to add 'dir' to the list of
230 directories searched for runtime libraries.
231 """
232 # Nothing needed or Mwerks/Mac.
233 return
234
235 def library_option (self, lib):
236 """Return the compiler option to add 'dir' to the list of libraries
237 linked into the shared library or executable.
238 """
239 return
240
241 def find_library_file (self, dirs, lib, debug=0):
242 """Search the specified list of directories for a static or shared
243 library file 'lib' and return the full path to that file. If
244 'debug' true, look for a debugging version (if that makes sense on
245 the current platform). Return None if 'lib' wasn't found in any of
246 the specified directories.
247 """
248 return 0