blob: 130cd6147b118df3fd2c128df8423d5896ac3bd3 [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
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00007__revision__ = "$Id$"
8
Neal Norwitz9d72bb42007-04-17 08:48:32 +00009import sys, os
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000010from distutils.errors import \
11 DistutilsExecError, DistutilsPlatformError, \
12 CompileError, LibError, LinkError
13from distutils.ccompiler import \
14 CCompiler, gen_preprocess_options, gen_lib_options
15import distutils.util
16import distutils.dir_util
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000017from distutils import log
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000018
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):
Collin Winter5b7e9d72007-08-30 03:52:21 +000096 raise DistutilsPlatformError('Can only make SHARED_LIBRARY or SHARED_OBJECT targets on the Mac')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000097 if runtime_library_dirs:
Collin Winter5b7e9d72007-08-30 03:52:21 +000098 raise DistutilsPlatformError('Runtime library dirs not implemented yet')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000099 if extra_preargs or extra_postargs:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000100 raise DistutilsPlatformError('Runtime library dirs not implemented yet')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000101 if len(export_symbols) != 1:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000102 raise DistutilsPlatformError('Need exactly one export symbol')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000103 # 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.
Amaury Forgeot d'Arc61cb0872008-07-26 20:09:45 +0000107 sources = [self._filename_to_abs(s) for s in self.__sources]
108 include_dirs = [self._filename_to_abs(d) for d in self.__include_dirs]
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000109 if objects:
Amaury Forgeot d'Arc61cb0872008-07-26 20:09:45 +0000110 objects = [self._filename_to_abs(o) for o in objects]
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000111 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:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000159 print('CALLING LINKER IN', os.getcwd())
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000160 for key, value in settings.items():
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000161 print('%20.20s %s'%(key, value))
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000162 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)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000187 import mkcwproject
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000188 xmlbuilder = mkcwproject.cwxmlgen.ProjectBuilder(settings)
189 xmlbuilder.generate()
190 xmldata = settings['tmp_projectxmldata']
191 fp = open(xmlfilename, 'w')
192 fp.write(xmldata)
193 fp.close()
194 # Generate the project. Again a full pathname.
195 projectfilename = os.path.join(os.getcwd(), os.path.join(build_temp, projectname))
Jack Jansenab5320b2002-06-26 15:42:49 +0000196 log.debug('\tCreate project file %s', projectfilename)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000197 mkcwproject.makeproject(xmlfilename, projectfilename)
198 # And build it
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000199 log.debug('\tBuild project')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000200 mkcwproject.buildproject(projectfilename)
Fred Drakeb94b8492001-12-06 20:51:35 +0000201
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000202 def _filename_to_abs(self, filename):
203 # Some filenames seem to be unix-like. Convert to Mac names.
204## if '/' in filename and ':' in filename:
205## raise DistutilsPlatformError, 'Filename may be Unix or Mac style: %s'%filename
206## if '/' in filename:
207## filename = macurl2path(filename)
208 filename = distutils.util.convert_path(filename)
209 if not os.path.isabs(filename):
Fred Drakeb94b8492001-12-06 20:51:35 +0000210 curdir = os.getcwd()
211 filename = os.path.join(curdir, filename)
Jack Jansen9020bce2001-06-19 21:23:11 +0000212 # Finally remove .. components
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000213 components = filename.split(':')
Jack Jansen9020bce2001-06-19 21:23:11 +0000214 for i in range(1, len(components)):
Fred Drakeb94b8492001-12-06 20:51:35 +0000215 if components[i] == '..':
216 components[i] = ''
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000217 return ':'.join(components)
Jack Jansenab5320b2002-06-26 15:42:49 +0000218
219 def library_dir_option (self, dir):
220 """Return the compiler option to add 'dir' to the list of
221 directories searched for libraries.
222 """
223 return # XXXX Not correct...
224
225 def runtime_library_dir_option (self, dir):
226 """Return the compiler option to add 'dir' to the list of
227 directories searched for runtime libraries.
228 """
229 # Nothing needed or Mwerks/Mac.
230 return
231
232 def library_option (self, lib):
233 """Return the compiler option to add 'dir' to the list of libraries
234 linked into the shared library or executable.
235 """
236 return
237
238 def find_library_file (self, dirs, lib, debug=0):
239 """Search the specified list of directories for a static or shared
240 library file 'lib' and return the full path to that file. If
241 'debug' true, look for a debugging version (if that makes sense on
242 the current platform). Return None if 'lib' wasn't found in any of
243 the specified directories.
244 """
245 return 0