blob: 981fd95f01d8ba19a853c95a223ad7e34729e877 [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
16import mkcwproject
17
18class MWerksCompiler (CCompiler) :
19 """Concrete class that implements an interface to Microsoft Visual C++,
20 as defined by the CCompiler abstract class."""
21
22 compiler_type = 'mwerks'
23
24 # Just set this so CCompiler's constructor doesn't barf. We currently
25 # don't use the 'set_executables()' bureaucracy provided by CCompiler,
26 # as it really isn't necessary for this sort of single-compiler class.
27 # Would be nice to have a consistent interface with UnixCCompiler,
28 # though, so it's worth thinking about.
29 executables = {}
30
31 # Private class data (need to distinguish C from C++ source for compiler)
32 _c_extensions = ['.c']
33 _cpp_extensions = ['.cc', '.cpp', '.cxx']
34 _rc_extensions = ['.r']
35 _exp_extension = '.exp'
36
37 # Needed for the filename generation methods provided by the
38 # base class, CCompiler.
39 src_extensions = (_c_extensions + _cpp_extensions +
40 _rc_extensions)
41 res_extension = '.rsrc'
42 obj_extension = '.obj' # Not used, really
43 static_lib_extension = '.lib'
44 shared_lib_extension = '.slb'
45 static_lib_format = shared_lib_format = '%s%s'
46 exe_extension = ''
47
48
49 def __init__ (self,
50 verbose=0,
51 dry_run=0,
52 force=0):
53
54 CCompiler.__init__ (self, verbose, dry_run, force)
55
56
57 def compile (self,
58 sources,
59 output_dir=None,
60 macros=None,
61 include_dirs=None,
62 debug=0,
63 extra_preargs=None,
64 extra_postargs=None):
65 self.__sources = sources
66 self.__macros = macros
67 self.__include_dirs = include_dirs
68 # Don't need extra_preargs and extra_postargs for CW
69
70 def link (self,
71 target_desc,
72 objects,
73 output_filename,
74 output_dir=None,
75 libraries=None,
76 library_dirs=None,
77 runtime_library_dirs=None,
78 export_symbols=None,
79 debug=0,
80 extra_preargs=None,
81 extra_postargs=None,
82 build_temp=None):
83 # First examine a couple of options for things that aren't implemented yet
84 if not target_desc in (self.SHARED_LIBRARY, self.SHARED_OBJECT):
85 raise DistutilsPlatformError, 'Can only make SHARED_LIBRARY or SHARED_OBJECT targets on the Mac'
86 if runtime_library_dirs:
87 raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
88 if extra_preargs or extra_postargs:
89 raise DistutilsPlatformError, 'Runtime library dirs not implemented yet'
90 if len(export_symbols) != 1:
91 raise DistutilsPlatformError, 'Need exactly one export symbol'
92 # Next there are various things for which we need absolute pathnames.
93 # This is because we (usually) create the project in a subdirectory of
94 # where we are now, and keeping the paths relative is too much work right
95 # now.
96 sources = map(self._filename_to_abs, self.__sources)
97 include_dirs = map(self._filename_to_abs, self.__include_dirs)
98 if objects:
99 objects = map(self._filename_to_abs, objects)
100 else:
101 objects = []
102 if build_temp:
103 build_temp = self._filename_to_abs(build_temp)
104 else:
105 build_temp = os.curdir()
106 if output_dir:
107 output_filename = os.path.join(output_dir, output_filename)
108 # The output filename needs special handling: splitting it into dir and
109 # filename part. Actually I'm not sure this is really needed, but it
110 # can't hurt.
111 output_filename = self._filename_to_abs(output_filename)
112 output_dir, output_filename = os.path.split(output_filename)
113 # Now we need the short names of a couple of things for putting them
114 # into the project.
115 if output_filename[-8:] == '.ppc.slb':
116 basename = output_filename[:-8]
Jack Jansendd13a202001-05-17 12:52:01 +0000117 elif output_filename[-11:] == '.carbon.slb':
118 basename = output_filename[:-11]
Andrew M. Kuchling3f819ec2001-01-15 16:09:35 +0000119 else:
120 basename = os.path.strip(output_filename)[0]
121 projectname = basename + '.mcp'
122 targetname = basename
123 xmlname = basename + '.xml'
124 exportname = basename + '.mcp.exp'
125 prefixname = 'mwerks_%s_config.h'%basename
126 # Create the directories we need
127 distutils.dir_util.mkpath(build_temp, self.verbose, self.dry_run)
128 distutils.dir_util.mkpath(output_dir, self.verbose, self.dry_run)
129 # And on to filling in the parameters for the project builder
130 settings = {}
131 settings['mac_exportname'] = exportname
132 settings['mac_outputdir'] = output_dir
133 settings['mac_dllname'] = output_filename
134 settings['mac_targetname'] = targetname
135 settings['sysprefix'] = sys.prefix
136 settings['mac_sysprefixtype'] = 'Absolute'
137 sourcefilenames = []
138 sourcefiledirs = []
139 for filename in sources + objects:
140 dirname, filename = os.path.split(filename)
141 sourcefilenames.append(filename)
142 if not dirname in sourcefiledirs:
143 sourcefiledirs.append(dirname)
144 settings['sources'] = sourcefilenames
145 settings['extrasearchdirs'] = sourcefiledirs + include_dirs + library_dirs
146 if self.dry_run:
147 print 'CALLING LINKER IN', os.getcwd()
148 for key, value in settings.items():
149 print '%20.20s %s'%(key, value)
150 return
151 # Build the export file
152 exportfilename = os.path.join(build_temp, exportname)
153 if self.verbose:
154 print '\tCreate export file', exportfilename
155 fp = open(exportfilename, 'w')
156 fp.write('%s\n'%export_symbols[0])
157 fp.close()
158 # Generate the prefix file, if needed, and put it in the settings
159 if self.__macros:
160 prefixfilename = os.path.join(os.getcwd(), os.path.join(build_temp, prefixname))
161 fp = open(prefixfilename, 'w')
162 fp.write('#include "mwerks_plugin_config.h"\n')
163 for name, value in self.__macros:
164 if value is None:
165 fp.write('#define %s\n'%name)
166 else:
167 fp.write('#define %s "%s"\n'%(name, value))
168 fp.close()
169 settings['prefixname'] = prefixname
170
171 # Build the XML file. We need the full pathname (only lateron, really)
172 # because we pass this pathname to CodeWarrior in an AppleEvent, and CW
173 # doesn't have a clue about our working directory.
174 xmlfilename = os.path.join(os.getcwd(), os.path.join(build_temp, xmlname))
175 if self.verbose:
176 print '\tCreate XML file', xmlfilename
177 xmlbuilder = mkcwproject.cwxmlgen.ProjectBuilder(settings)
178 xmlbuilder.generate()
179 xmldata = settings['tmp_projectxmldata']
180 fp = open(xmlfilename, 'w')
181 fp.write(xmldata)
182 fp.close()
183 # Generate the project. Again a full pathname.
184 projectfilename = os.path.join(os.getcwd(), os.path.join(build_temp, projectname))
185 if self.verbose:
186 print '\tCreate project file', projectfilename
187 mkcwproject.makeproject(xmlfilename, projectfilename)
188 # And build it
189 if self.verbose:
190 print '\tBuild project'
191 mkcwproject.buildproject(projectfilename)
192
193 def _filename_to_abs(self, filename):
194 # Some filenames seem to be unix-like. Convert to Mac names.
195## if '/' in filename and ':' in filename:
196## raise DistutilsPlatformError, 'Filename may be Unix or Mac style: %s'%filename
197## if '/' in filename:
198## filename = macurl2path(filename)
199 filename = distutils.util.convert_path(filename)
200 if not os.path.isabs(filename):
201 curdir = os.getcwd()
202 filename = os.path.join(curdir, filename)
203 return filename
204
205