blob: 6242f12aa11a9f4abb0a05a9a12c3d955a707c76 [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,
65 extra_postargs=None):
Fred Drakeb94b8492001-12-06 20:51:35 +000066 (output_dir, macros, include_dirs) = \
67 self._fix_compile_args (output_dir, macros, include_dirs)
68 self.__sources = sources
69 self.__macros = macros
70 self.__include_dirs = include_dirs
71 # Don't need extra_preargs and extra_postargs for CW
72 return []
73
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000074 def link (self,
75 target_desc,
76 objects,
77 output_filename,
78 output_dir=None,
79 libraries=None,
80 library_dirs=None,
81 runtime_library_dirs=None,
82 export_symbols=None,
83 debug=0,
84 extra_preargs=None,
85 extra_postargs=None,
86 build_temp=None):
Jack Jansen9020bce2001-06-19 21:23:11 +000087 # First fixup.
88 (objects, output_dir) = self._fix_object_args (objects, output_dir)
89 (libraries, library_dirs, runtime_library_dirs) = \
90 self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
91
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +000092 # First examine a couple of options for things that aren't implemented yet
93 if not target_desc in (self.SHARED_LIBRARY, self.SHARED_OBJECT):
94 raise DistutilsPlatformError, 'Can only make SHARED_LIBRARY or SHARED_OBJECT targets on the Mac'
95 if runtime_library_dirs:
96 raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
97 if extra_preargs or extra_postargs:
98 raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
99 if len(export_symbols) != 1:
100 raise DistutilsPlatformError, 'Need exactly one export symbol'
101 # Next there are various things for which we need absolute pathnames.
102 # This is because we (usually) create the project in a subdirectory of
103 # where we are now, and keeping the paths relative is too much work right
104 # now.
105 sources = map(self._filename_to_abs, self.__sources)
106 include_dirs = map(self._filename_to_abs, self.__include_dirs)
107 if objects:
108 objects = map(self._filename_to_abs, objects)
109 else:
110 objects = []
111 if build_temp:
112 build_temp = self._filename_to_abs(build_temp)
113 else:
114 build_temp = os.curdir()
115 if output_dir:
116 output_filename = os.path.join(output_dir, output_filename)
117 # The output filename needs special handling: splitting it into dir and
118 # filename part. Actually I'm not sure this is really needed, but it
119 # can't hurt.
120 output_filename = self._filename_to_abs(output_filename)
121 output_dir, output_filename = os.path.split(output_filename)
122 # Now we need the short names of a couple of things for putting them
123 # into the project.
124 if output_filename[-8:] == '.ppc.slb':
125 basename = output_filename[:-8]
Jack Jansendd13a202001-05-17 12:52:01 +0000126 elif output_filename[-11:] == '.carbon.slb':
127 basename = output_filename[:-11]
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000128 else:
129 basename = os.path.strip(output_filename)[0]
130 projectname = basename + '.mcp'
131 targetname = basename
132 xmlname = basename + '.xml'
133 exportname = basename + '.mcp.exp'
134 prefixname = 'mwerks_%s_config.h'%basename
135 # Create the directories we need
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000136 distutils.dir_util.mkpath(build_temp, dry_run=self.dry_run)
137 distutils.dir_util.mkpath(output_dir, dry_run=self.dry_run)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000138 # And on to filling in the parameters for the project builder
139 settings = {}
140 settings['mac_exportname'] = exportname
141 settings['mac_outputdir'] = output_dir
142 settings['mac_dllname'] = output_filename
143 settings['mac_targetname'] = targetname
144 settings['sysprefix'] = sys.prefix
145 settings['mac_sysprefixtype'] = 'Absolute'
146 sourcefilenames = []
147 sourcefiledirs = []
148 for filename in sources + objects:
149 dirname, filename = os.path.split(filename)
150 sourcefilenames.append(filename)
151 if not dirname in sourcefiledirs:
152 sourcefiledirs.append(dirname)
153 settings['sources'] = sourcefilenames
Jack Jansen92c2ebf2001-11-10 23:20:22 +0000154 settings['libraries'] = libraries
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000155 settings['extrasearchdirs'] = sourcefiledirs + include_dirs + library_dirs
156 if self.dry_run:
157 print 'CALLING LINKER IN', os.getcwd()
158 for key, value in settings.items():
159 print '%20.20s %s'%(key, value)
160 return
161 # Build the export file
162 exportfilename = os.path.join(build_temp, exportname)
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000163 log.debug("\tCreate export file", exportfilename)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000164 fp = open(exportfilename, 'w')
165 fp.write('%s\n'%export_symbols[0])
166 fp.close()
167 # Generate the prefix file, if needed, and put it in the settings
168 if self.__macros:
169 prefixfilename = os.path.join(os.getcwd(), os.path.join(build_temp, prefixname))
170 fp = open(prefixfilename, 'w')
171 fp.write('#include "mwerks_plugin_config.h"\n')
172 for name, value in self.__macros:
173 if value is None:
174 fp.write('#define %s\n'%name)
175 else:
Just van Rossum92c5bdb2001-06-19 19:44:02 +0000176 fp.write('#define %s %s\n'%(name, value))
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000177 fp.close()
178 settings['prefixname'] = prefixname
179
180 # Build the XML file. We need the full pathname (only lateron, really)
181 # because we pass this pathname to CodeWarrior in an AppleEvent, and CW
182 # doesn't have a clue about our working directory.
183 xmlfilename = os.path.join(os.getcwd(), os.path.join(build_temp, xmlname))
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000184 log.debug("\tCreate XML file", xmlfilename)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000185 xmlbuilder = mkcwproject.cwxmlgen.ProjectBuilder(settings)
186 xmlbuilder.generate()
187 xmldata = settings['tmp_projectxmldata']
188 fp = open(xmlfilename, 'w')
189 fp.write(xmldata)
190 fp.close()
191 # Generate the project. Again a full pathname.
192 projectfilename = os.path.join(os.getcwd(), os.path.join(build_temp, projectname))
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000193 log.debug('\tCreate project file', projectfilename)
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000194 mkcwproject.makeproject(xmlfilename, projectfilename)
195 # And build it
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000196 log.debug('\tBuild project')
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000197 mkcwproject.buildproject(projectfilename)
Fred Drakeb94b8492001-12-06 20:51:35 +0000198
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000199 def _filename_to_abs(self, filename):
200 # Some filenames seem to be unix-like. Convert to Mac names.
201## if '/' in filename and ':' in filename:
202## raise DistutilsPlatformError, 'Filename may be Unix or Mac style: %s'%filename
203## if '/' in filename:
204## filename = macurl2path(filename)
205 filename = distutils.util.convert_path(filename)
206 if not os.path.isabs(filename):
Fred Drakeb94b8492001-12-06 20:51:35 +0000207 curdir = os.getcwd()
208 filename = os.path.join(curdir, filename)
Jack Jansen9020bce2001-06-19 21:23:11 +0000209 # Finally remove .. components
210 components = string.split(filename, ':')
211 for i in range(1, len(components)):
Fred Drakeb94b8492001-12-06 20:51:35 +0000212 if components[i] == '..':
213 components[i] = ''
Jack Jansen9020bce2001-06-19 21:23:11 +0000214 return string.join(components, ':')