Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 1 | """distutils.mwerkscompiler |
| 2 | |
| 3 | Contains MWerksCompiler, an implementation of the abstract CCompiler class |
| 4 | for MetroWerks CodeWarrior on the Macintosh. Needs work to support CW on |
| 5 | Windows.""" |
| 6 | |
Andrew M. Kuchling | d448f66 | 2002-11-19 13:12:28 +0000 | [diff] [blame] | 7 | __revision__ = "$Id$" |
| 8 | |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 9 | import sys, os |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 10 | from distutils.errors import \ |
| 11 | DistutilsExecError, DistutilsPlatformError, \ |
| 12 | CompileError, LibError, LinkError |
| 13 | from distutils.ccompiler import \ |
| 14 | CCompiler, gen_preprocess_options, gen_lib_options |
| 15 | import distutils.util |
| 16 | import distutils.dir_util |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 17 | from distutils import log |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 18 | |
| 19 | class MWerksCompiler (CCompiler) : |
Jack Jansen | 92c2ebf | 2001-11-10 23:20:22 +0000 | [diff] [blame] | 20 | """Concrete class that implements an interface to MetroWerks CodeWarrior, |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 21 | 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 Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 56 | |
| 57 | |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 58 | def compile (self, |
| 59 | sources, |
| 60 | output_dir=None, |
| 61 | macros=None, |
| 62 | include_dirs=None, |
| 63 | debug=0, |
| 64 | extra_preargs=None, |
Jeremy Hylton | 6864d30 | 2002-06-13 17:27:13 +0000 | [diff] [blame] | 65 | extra_postargs=None, |
| 66 | depends=None): |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 67 | (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. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 75 | 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 Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 87 | build_temp=None, |
| 88 | target_lang=None): |
Jack Jansen | 9020bce | 2001-06-19 21:23:11 +0000 | [diff] [blame] | 89 | # 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. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 94 | # 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 Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 96 | raise DistutilsPlatformError('Can only make SHARED_LIBRARY or SHARED_OBJECT targets on the Mac') |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 97 | if runtime_library_dirs: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 98 | raise DistutilsPlatformError('Runtime library dirs not implemented yet') |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 99 | if extra_preargs or extra_postargs: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 100 | raise DistutilsPlatformError('Runtime library dirs not implemented yet') |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 101 | if len(export_symbols) != 1: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 102 | raise DistutilsPlatformError('Need exactly one export symbol') |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 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. |
Amaury Forgeot d'Arc | 61cb087 | 2008-07-26 20:09:45 +0000 | [diff] [blame] | 107 | 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. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 109 | if objects: |
Amaury Forgeot d'Arc | 61cb087 | 2008-07-26 20:09:45 +0000 | [diff] [blame] | 110 | objects = [self._filename_to_abs(o) for o in objects] |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 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 Jansen | dd13a20 | 2001-05-17 12:52:01 +0000 | [diff] [blame] | 128 | elif output_filename[-11:] == '.carbon.slb': |
| 129 | basename = output_filename[:-11] |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 130 | 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 Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 138 | 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. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 140 | # 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 Jansen | 92c2ebf | 2001-11-10 23:20:22 +0000 | [diff] [blame] | 156 | settings['libraries'] = libraries |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 157 | settings['extrasearchdirs'] = sourcefiledirs + include_dirs + library_dirs |
| 158 | if self.dry_run: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 159 | print('CALLING LINKER IN', os.getcwd()) |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 160 | for key, value in settings.items(): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 161 | print('%20.20s %s'%(key, value)) |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 162 | return |
| 163 | # Build the export file |
| 164 | exportfilename = os.path.join(build_temp, exportname) |
Jack Jansen | ab5320b | 2002-06-26 15:42:49 +0000 | [diff] [blame] | 165 | log.debug("\tCreate export file %s", exportfilename) |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 166 | 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 Jansen | 2bb5980 | 2002-06-27 22:10:19 +0000 | [diff] [blame] | 173 | fp.write('#include "mwerks_shcarbon_config.h"\n') |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 174 | for name, value in self.__macros: |
| 175 | if value is None: |
| 176 | fp.write('#define %s\n'%name) |
| 177 | else: |
Just van Rossum | 92c5bdb | 2001-06-19 19:44:02 +0000 | [diff] [blame] | 178 | fp.write('#define %s %s\n'%(name, value)) |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 179 | 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 Jansen | ab5320b | 2002-06-26 15:42:49 +0000 | [diff] [blame] | 186 | log.debug("\tCreate XML file %s", xmlfilename) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 187 | import mkcwproject |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 188 | 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 Jansen | ab5320b | 2002-06-26 15:42:49 +0000 | [diff] [blame] | 196 | log.debug('\tCreate project file %s', projectfilename) |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 197 | mkcwproject.makeproject(xmlfilename, projectfilename) |
| 198 | # And build it |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 199 | log.debug('\tBuild project') |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 200 | mkcwproject.buildproject(projectfilename) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 201 | |
Andrew M. Kuchling | 3f819ec | 2001-01-15 16:09:35 +0000 | [diff] [blame] | 202 | 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 Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 210 | curdir = os.getcwd() |
| 211 | filename = os.path.join(curdir, filename) |
Jack Jansen | 9020bce | 2001-06-19 21:23:11 +0000 | [diff] [blame] | 212 | # Finally remove .. components |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 213 | components = filename.split(':') |
Jack Jansen | 9020bce | 2001-06-19 21:23:11 +0000 | [diff] [blame] | 214 | for i in range(1, len(components)): |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 215 | if components[i] == '..': |
| 216 | components[i] = '' |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 217 | return ':'.join(components) |
Jack Jansen | ab5320b | 2002-06-26 15:42:49 +0000 | [diff] [blame] | 218 | |
| 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 |