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