blob: 8f62bf7d878af2cf74b1fd7d264b24b8a239beb2 [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
7import sys, os, string
8from types import *
9from distutils.errors import \
10 DistutilsExecError, DistutilsPlatformError, \
11 CompileError, LibError, LinkError
12from distutils.ccompiler import \
13 CCompiler, gen_preprocess_options, gen_lib_options
14import distutils.util
15import distutils.dir_util
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000016from distutils import log
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000017import mkcwproject
18
19class MWerksCompiler (CCompiler) :
Jack Jansen92c2ebf2001-11-10 23:20:22 +000020 """Concrete class that implements an interface to MetroWerks CodeWarrior,
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000021 as defined by the CCompiler abstract class."""
22
23 compiler_type = 'mwerks'
24
25 # Just set this so CCompiler's constructor doesn't barf. We currently
26 # don't use the 'set_executables()' bureaucracy provided by CCompiler,
27 # as it really isn't necessary for this sort of single-compiler class.
28 # Would be nice to have a consistent interface with UnixCCompiler,
29 # though, so it's worth thinking about.
30 executables = {}
31
32 # Private class data (need to distinguish C from C++ source for compiler)
33 _c_extensions = ['.c']
34 _cpp_extensions = ['.cc', '.cpp', '.cxx']
35 _rc_extensions = ['.r']
36 _exp_extension = '.exp'
37
38 # Needed for the filename generation methods provided by the
39 # base class, CCompiler.
40 src_extensions = (_c_extensions + _cpp_extensions +
41 _rc_extensions)
42 res_extension = '.rsrc'
43 obj_extension = '.obj' # Not used, really
44 static_lib_extension = '.lib'
45 shared_lib_extension = '.slb'
46 static_lib_format = shared_lib_format = '%s%s'
47 exe_extension = ''
48
49
50 def __init__ (self,
51 verbose=0,
52 dry_run=0,
53 force=0):
54
55 CCompiler.__init__ (self, verbose, dry_run, force)
Fred Drakeb94b8492001-12-06 20:51:35 +000056
57
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000058 def compile (self,
59 sources,
60 output_dir=None,
61 macros=None,
62 include_dirs=None,
63 debug=0,
64 extra_preargs=None,
Jeremy Hylton6864d302002-06-13 17:27:13 +000065 extra_postargs=None,
66 depends=None):
Fred Drakeb94b8492001-12-06 20:51:35 +000067 (output_dir, macros, include_dirs) = \
68 self._fix_compile_args (output_dir, macros, include_dirs)
69 self.__sources = sources
70 self.__macros = macros
71 self.__include_dirs = include_dirs
72 # Don't need extra_preargs and extra_postargs for CW
73 return []
74
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000075 def link (self,
76 target_desc,
77 objects,
78 output_filename,
79 output_dir=None,
80 libraries=None,
81 library_dirs=None,
82 runtime_library_dirs=None,
83 export_symbols=None,
84 debug=0,
85 extra_preargs=None,
86 extra_postargs=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +000087 build_temp=None,
88 target_lang=None):
Jack Jansen9020bce2001-06-19 21:23:11 +000089 # First fixup.
90 (objects, output_dir) = self._fix_object_args (objects, output_dir)
91 (libraries, library_dirs, runtime_library_dirs) = \
92 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
93
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000094 # First examine a couple of options for things that aren't implemented yet
95 if not target_desc in (self.SHARED_LIBRARY, self.SHARED_OBJECT):
96 raise DistutilsPlatformError, 'Can only make SHARED_LIBRARY or SHARED_OBJECT targets on the Mac'
97 if runtime_library_dirs:
98 raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
99 if extra_preargs or extra_postargs:
100 raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
101 if len(export_symbols) != 1:
102 raise DistutilsPlatformError, 'Need exactly one export symbol'
103 # Next there are various things for which we need absolute pathnames.
104 # This is because we (usually) create the project in a subdirectory of
105 # where we are now, and keeping the paths relative is too much work right
106 # now.
107 sources = map(self._filename_to_abs, self.__sources)
108 include_dirs = map(self._filename_to_abs, self.__include_dirs)
109 if objects:
110 objects = map(self._filename_to_abs, objects)
111 else:
112 objects = []
113 if build_temp:
114 build_temp = self._filename_to_abs(build_temp)
115 else:
116 build_temp = os.curdir()
117 if output_dir:
118 output_filename = os.path.join(output_dir, output_filename)
119 # The output filename needs special handling: splitting it into dir and
120 # filename part. Actually I'm not sure this is really needed, but it
121 # can't hurt.
122 output_filename = self._filename_to_abs(output_filename)
123 output_dir, output_filename = os.path.split(output_filename)
124 # Now we need the short names of a couple of things for putting them
125 # into the project.
126 if output_filename[-8:] == '.ppc.slb':
127 basename = output_filename[:-8]
Jack Jansendd13a202001-05-17 12:52:01 +0000128 elif output_filename[-11:] == '.carbon.slb':
129 basename = output_filename[:-11]
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000130 else:
131 basename = os.path.strip(output_filename)[0]
132 projectname = basename + '.mcp'
133 targetname = basename
134 xmlname = basename + '.xml'
135 exportname = basename + '.mcp.exp'
136 prefixname = 'mwerks_%s_config.h'%basename
137 # Create the directories we need
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000138 distutils.dir_util.mkpath(build_temp, dry_run=self.dry_run)
139 distutils.dir_util.mkpath(output_dir, dry_run=self.dry_run)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000140 # And on to filling in the parameters for the project builder
141 settings = {}
142 settings['mac_exportname'] = exportname
143 settings['mac_outputdir'] = output_dir
144 settings['mac_dllname'] = output_filename
145 settings['mac_targetname'] = targetname
146 settings['sysprefix'] = sys.prefix
147 settings['mac_sysprefixtype'] = 'Absolute'
148 sourcefilenames = []
149 sourcefiledirs = []
150 for filename in sources + objects:
151 dirname, filename = os.path.split(filename)
152 sourcefilenames.append(filename)
153 if not dirname in sourcefiledirs:
154 sourcefiledirs.append(dirname)
155 settings['sources'] = sourcefilenames
Jack Jansen92c2ebf2001-11-10 23:20:22 +0000156 settings['libraries'] = libraries
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000157 settings['extrasearchdirs'] = sourcefiledirs + include_dirs + library_dirs
158 if self.dry_run:
159 print 'CALLING LINKER IN', os.getcwd()
160 for key, value in settings.items():
161 print '%20.20s %s'%(key, value)
162 return
163 # Build the export file
164 exportfilename = os.path.join(build_temp, exportname)
Jack Jansenab5320b2002-06-26 15:42:49 +0000165 log.debug("\tCreate export file %s", exportfilename)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000166 fp = open(exportfilename, 'w')
167 fp.write('%s\n'%export_symbols[0])
168 fp.close()
169 # Generate the prefix file, if needed, and put it in the settings
170 if self.__macros:
171 prefixfilename = os.path.join(os.getcwd(), os.path.join(build_temp, prefixname))
172 fp = open(prefixfilename, 'w')
Jack Jansen2bb59802002-06-27 22:10:19 +0000173 fp.write('#include "mwerks_shcarbon_config.h"\n')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000174 for name, value in self.__macros:
175 if value is None:
176 fp.write('#define %s\n'%name)
177 else:
Just van Rossum92c5bdb2001-06-19 19:44:02 +0000178 fp.write('#define %s %s\n'%(name, value))
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000179 fp.close()
180 settings['prefixname'] = prefixname
181
182 # Build the XML file. We need the full pathname (only lateron, really)
183 # because we pass this pathname to CodeWarrior in an AppleEvent, and CW
184 # doesn't have a clue about our working directory.
185 xmlfilename = os.path.join(os.getcwd(), os.path.join(build_temp, xmlname))
Jack Jansenab5320b2002-06-26 15:42:49 +0000186 log.debug("\tCreate XML file %s", xmlfilename)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000187 xmlbuilder = mkcwproject.cwxmlgen.ProjectBuilder(settings)
188 xmlbuilder.generate()
189 xmldata = settings['tmp_projectxmldata']
190 fp = open(xmlfilename, 'w')
191 fp.write(xmldata)
192 fp.close()
193 # Generate the project. Again a full pathname.
194 projectfilename = os.path.join(os.getcwd(), os.path.join(build_temp, projectname))
Jack Jansenab5320b2002-06-26 15:42:49 +0000195 log.debug('\tCreate project file %s', projectfilename)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000196 mkcwproject.makeproject(xmlfilename, projectfilename)
197 # And build it
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000198 log.debug('\tBuild project')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000199 mkcwproject.buildproject(projectfilename)
Fred Drakeb94b8492001-12-06 20:51:35 +0000200
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000201 def _filename_to_abs(self, filename):
202 # Some filenames seem to be unix-like. Convert to Mac names.
203## if '/' in filename and ':' in filename:
204## raise DistutilsPlatformError, 'Filename may be Unix or Mac style: %s'%filename
205## if '/' in filename:
206## filename = macurl2path(filename)
207 filename = distutils.util.convert_path(filename)
208 if not os.path.isabs(filename):
Fred Drakeb94b8492001-12-06 20:51:35 +0000209 curdir = os.getcwd()
210 filename = os.path.join(curdir, filename)
Jack Jansen9020bce2001-06-19 21:23:11 +0000211 # Finally remove .. components
212 components = string.split(filename, ':')
213 for i in range(1, len(components)):
Fred Drakeb94b8492001-12-06 20:51:35 +0000214 if components[i] == '..':
215 components[i] = ''
Jack Jansen9020bce2001-06-19 21:23:11 +0000216 return string.join(components, ':')
Jack Jansenab5320b2002-06-26 15:42:49 +0000217
218 def library_dir_option (self, dir):
219 """Return the compiler option to add 'dir' to the list of
220 directories searched for libraries.
221 """
222 return # XXXX Not correct...
223
224 def runtime_library_dir_option (self, dir):
225 """Return the compiler option to add 'dir' to the list of
226 directories searched for runtime libraries.
227 """
228 # Nothing needed or Mwerks/Mac.
229 return
230
231 def library_option (self, lib):
232 """Return the compiler option to add 'dir' to the list of libraries
233 linked into the shared library or executable.
234 """
235 return
236
237 def find_library_file (self, dirs, lib, debug=0):
238 """Search the specified list of directories for a static or shared
239 library file 'lib' and return the full path to that file. If
240 'debug' true, look for a debugging version (if that makes sense on
241 the current platform). Return None if 'lib' wasn't found in any of
242 the specified directories.
243 """
244 return 0