Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 1 | """distutils.emxccompiler |
| 2 | |
| 3 | Provides the EMXCCompiler class, a subclass of UnixCCompiler that |
| 4 | handles the EMX port of the GNU C compiler to OS/2. |
| 5 | """ |
| 6 | |
| 7 | # issues: |
| 8 | # |
| 9 | # * OS/2 insists that DLLs can have names no longer than 8 characters |
| 10 | # We put export_symbols in a def-file, as though the DLL can have |
| 11 | # an arbitrary length name, but truncate the output filename. |
| 12 | # |
| 13 | # * only use OMF objects and use LINK386 as the linker (-Zomf) |
| 14 | # |
| 15 | # * always build for multithreading (-Zmt) as the accompanying OS/2 port |
| 16 | # of Python is only distributed with threads enabled. |
| 17 | # |
| 18 | # tested configurations: |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 19 | # |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 20 | # * EMX gcc 2.81/EMX 0.9d fix03 |
| 21 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 22 | __revision__ = "$Id$" |
| 23 | |
Tarek Ziadé | f8926b2 | 2009-07-16 16:18:19 +0000 | [diff] [blame] | 24 | import os, sys, copy |
| 25 | from warnings import warn |
| 26 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 27 | from distutils.ccompiler import gen_preprocess_options, gen_lib_options |
| 28 | from distutils.unixccompiler import UnixCCompiler |
| 29 | from distutils.file_util import write_file |
| 30 | from distutils.errors import DistutilsExecError, CompileError, UnknownFileError |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 31 | from distutils import log |
Tarek Ziadé | f8926b2 | 2009-07-16 16:18:19 +0000 | [diff] [blame] | 32 | from distutils.util import get_compiler_versions |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 33 | |
| 34 | class EMXCCompiler (UnixCCompiler): |
| 35 | |
| 36 | compiler_type = 'emx' |
| 37 | obj_extension = ".obj" |
| 38 | static_lib_extension = ".lib" |
| 39 | shared_lib_extension = ".dll" |
| 40 | static_lib_format = "%s%s" |
| 41 | shared_lib_format = "%s%s" |
| 42 | res_extension = ".res" # compiled resource file |
| 43 | exe_extension = ".exe" |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 44 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 45 | def __init__ (self, |
| 46 | verbose=0, |
| 47 | dry_run=0, |
| 48 | force=0): |
| 49 | |
| 50 | UnixCCompiler.__init__ (self, verbose, dry_run, force) |
| 51 | |
| 52 | (status, details) = check_config_h() |
| 53 | self.debug_print("Python's GCC status: %s (details: %s)" % |
| 54 | (status, details)) |
| 55 | if status is not CONFIG_H_OK: |
| 56 | self.warn( |
| 57 | "Python's pyconfig.h doesn't seem to support your compiler. " + |
| 58 | ("Reason: %s." % details) + |
| 59 | "Compiling may fail because of undefined preprocessor macros.") |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 60 | |
Tarek Ziadé | f8926b2 | 2009-07-16 16:18:19 +0000 | [diff] [blame] | 61 | gcc_version, ld_version, dllwrap_version = get_compiler_versions() |
| 62 | self.gcc_version, self.ld_version = gcc_version, ld_version |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 63 | self.debug_print(self.compiler_type + ": gcc %s, ld %s\n" % |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 64 | (self.gcc_version, |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 65 | self.ld_version) ) |
| 66 | |
| 67 | # Hard-code GCC because that's what this is all about. |
| 68 | # XXX optimization, warnings etc. should be customizable. |
Andrew MacIntyre | 63ee110 | 2003-12-02 12:17:59 +0000 | [diff] [blame] | 69 | self.set_executables(compiler='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall', |
| 70 | compiler_so='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall', |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 71 | linker_exe='gcc -Zomf -Zmt -Zcrtdll', |
| 72 | linker_so='gcc -Zomf -Zmt -Zcrtdll -Zdll') |
| 73 | |
| 74 | # want the gcc library statically linked (so that we don't have |
| 75 | # to distribute a version dependent on the compiler we have) |
| 76 | self.dll_libraries=["gcc"] |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 77 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 78 | # __init__ () |
| 79 | |
Andrew MacIntyre | 428a38c | 2002-08-04 06:17:08 +0000 | [diff] [blame] | 80 | def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): |
Jeremy Hylton | 1b046e4 | 2002-06-18 18:48:55 +0000 | [diff] [blame] | 81 | if ext == '.rc': |
| 82 | # gcc requires '.rc' compiled to binary ('.res') files !!! |
| 83 | try: |
| 84 | self.spawn(["rc", "-r", src]) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 85 | except DistutilsExecError as msg: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 86 | raise CompileError(msg) |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 87 | else: # for other files use the C-compiler |
Jeremy Hylton | 1b046e4 | 2002-06-18 18:48:55 +0000 | [diff] [blame] | 88 | try: |
| 89 | self.spawn(self.compiler_so + cc_args + [src, '-o', obj] + |
| 90 | extra_postargs) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 91 | except DistutilsExecError as msg: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 92 | raise CompileError(msg) |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 93 | |
| 94 | def link (self, |
| 95 | target_desc, |
| 96 | objects, |
| 97 | output_filename, |
| 98 | output_dir=None, |
| 99 | libraries=None, |
| 100 | library_dirs=None, |
| 101 | runtime_library_dirs=None, |
| 102 | export_symbols=None, |
| 103 | debug=0, |
| 104 | extra_preargs=None, |
| 105 | extra_postargs=None, |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 106 | build_temp=None, |
| 107 | target_lang=None): |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 108 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 109 | # use separate copies, so we can modify the lists |
| 110 | extra_preargs = copy.copy(extra_preargs or []) |
| 111 | libraries = copy.copy(libraries or []) |
| 112 | objects = copy.copy(objects or []) |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 113 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 114 | # Additional libraries |
| 115 | libraries.extend(self.dll_libraries) |
| 116 | |
| 117 | # handle export symbols by creating a def-file |
| 118 | # with executables this only works with gcc/ld as linker |
| 119 | if ((export_symbols is not None) and |
| 120 | (target_desc != self.EXECUTABLE)): |
| 121 | # (The linker doesn't do anything if output is up-to-date. |
| 122 | # So it would probably better to check if we really need this, |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 123 | # but for this we had to insert some unchanged parts of |
| 124 | # UnixCCompiler, and this is not what we want.) |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 126 | # we want to put some files in the same directory as the |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 127 | # object files are, build_temp doesn't help much |
| 128 | # where are the object files |
| 129 | temp_dir = os.path.dirname(objects[0]) |
| 130 | # name of dll to give the helper files the same base name |
| 131 | (dll_name, dll_extension) = os.path.splitext( |
| 132 | os.path.basename(output_filename)) |
| 133 | |
| 134 | # generate the filenames for these files |
| 135 | def_file = os.path.join(temp_dir, dll_name + ".def") |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 136 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 137 | # Generate .def file |
| 138 | contents = [ |
Jeremy Hylton | a2f9989 | 2002-06-04 20:26:44 +0000 | [diff] [blame] | 139 | "LIBRARY %s INITINSTANCE TERMINSTANCE" % \ |
| 140 | os.path.splitext(os.path.basename(output_filename))[0], |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 141 | "DATA MULTIPLE NONSHARED", |
| 142 | "EXPORTS"] |
| 143 | for sym in export_symbols: |
| 144 | contents.append(' "%s"' % sym) |
| 145 | self.execute(write_file, (def_file, contents), |
| 146 | "writing %s" % def_file) |
| 147 | |
| 148 | # next add options for def-file and to creating import libraries |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 149 | # for gcc/ld the def-file is specified as any other object files |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 150 | objects.append(def_file) |
| 151 | |
| 152 | #end: if ((export_symbols is not None) and |
| 153 | # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")): |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 154 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 155 | # who wants symbols and a many times larger output file |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 156 | # should explicitly switch the debug mode on |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 157 | # otherwise we let dllwrap/ld strip the output file |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 158 | # (On my machine: 10KB < stripped_file < ??100KB |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 159 | # unstripped_file = stripped_file + XXX KB |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 160 | # ( XXX=254 for a typical python extension)) |
| 161 | if not debug: |
| 162 | extra_preargs.append("-s") |
| 163 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 164 | UnixCCompiler.link(self, |
| 165 | target_desc, |
| 166 | objects, |
| 167 | output_filename, |
| 168 | output_dir, |
| 169 | libraries, |
| 170 | library_dirs, |
| 171 | runtime_library_dirs, |
| 172 | None, # export_symbols, we do this in our def-file |
| 173 | debug, |
| 174 | extra_preargs, |
| 175 | extra_postargs, |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 176 | build_temp, |
| 177 | target_lang) |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 178 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 179 | # link () |
| 180 | |
| 181 | # -- Miscellaneous methods ----------------------------------------- |
| 182 | |
Andrew MacIntyre | 4104db3 | 2002-08-04 06:21:25 +0000 | [diff] [blame] | 183 | # override the object_filenames method from CCompiler to |
| 184 | # support rc and res-files |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 185 | def object_filenames (self, |
| 186 | source_filenames, |
| 187 | strip_dir=0, |
| 188 | output_dir=''): |
| 189 | if output_dir is None: output_dir = '' |
| 190 | obj_names = [] |
| 191 | for src_name in source_filenames: |
| 192 | # use normcase to make sure '.rc' is really '.rc' and not '.RC' |
| 193 | (base, ext) = os.path.splitext (os.path.normcase(src_name)) |
| 194 | if ext not in (self.src_extensions + ['.rc']): |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 195 | raise UnknownFileError("unknown file type '%s' (from '%s')" % \ |
| 196 | (ext, src_name)) |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 197 | if strip_dir: |
| 198 | base = os.path.basename (base) |
| 199 | if ext == '.rc': |
| 200 | # these need to be compiled to object files |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 201 | obj_names.append (os.path.join (output_dir, |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 202 | base + self.res_extension)) |
| 203 | else: |
| 204 | obj_names.append (os.path.join (output_dir, |
| 205 | base + self.obj_extension)) |
| 206 | return obj_names |
| 207 | |
| 208 | # object_filenames () |
| 209 | |
Andrew MacIntyre | 4104db3 | 2002-08-04 06:21:25 +0000 | [diff] [blame] | 210 | # override the find_library_file method from UnixCCompiler |
| 211 | # to deal with file naming/searching differences |
| 212 | def find_library_file(self, dirs, lib, debug=0): |
| 213 | shortlib = '%s.lib' % lib |
| 214 | longlib = 'lib%s.lib' % lib # this form very rare |
| 215 | |
| 216 | # get EMX's default library directory search path |
| 217 | try: |
| 218 | emx_dirs = os.environ['LIBRARY_PATH'].split(';') |
| 219 | except KeyError: |
| 220 | emx_dirs = [] |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 221 | |
Andrew MacIntyre | 4104db3 | 2002-08-04 06:21:25 +0000 | [diff] [blame] | 222 | for dir in dirs + emx_dirs: |
| 223 | shortlibp = os.path.join(dir, shortlib) |
| 224 | longlibp = os.path.join(dir, longlib) |
| 225 | if os.path.exists(shortlibp): |
| 226 | return shortlibp |
| 227 | elif os.path.exists(longlibp): |
| 228 | return longlibp |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 229 | |
Andrew MacIntyre | 4104db3 | 2002-08-04 06:21:25 +0000 | [diff] [blame] | 230 | # Oops, didn't find it in *any* of 'dirs' |
| 231 | return None |
| 232 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 233 | # class EMXCCompiler |
| 234 | |
| 235 | |
| 236 | # Because these compilers aren't configured in Python's pyconfig.h file by |
| 237 | # default, we should at least warn the user if he is using a unmodified |
| 238 | # version. |
| 239 | |
| 240 | CONFIG_H_OK = "ok" |
| 241 | CONFIG_H_NOTOK = "not ok" |
| 242 | CONFIG_H_UNCERTAIN = "uncertain" |
| 243 | |
| 244 | def check_config_h(): |
| 245 | |
| 246 | """Check if the current Python installation (specifically, pyconfig.h) |
| 247 | appears amenable to building extensions with GCC. Returns a tuple |
| 248 | (status, details), where 'status' is one of the following constants: |
| 249 | CONFIG_H_OK |
| 250 | all is well, go ahead and compile |
| 251 | CONFIG_H_NOTOK |
| 252 | doesn't look good |
| 253 | CONFIG_H_UNCERTAIN |
| 254 | not sure -- unable to read pyconfig.h |
| 255 | 'details' is a human-readable string explaining the situation. |
| 256 | |
| 257 | Note there are two ways to conclude "OK": either 'sys.version' contains |
| 258 | the string "GCC" (implying that this Python was built with GCC), or the |
| 259 | installed "pyconfig.h" contains the string "__GNUC__". |
| 260 | """ |
| 261 | |
| 262 | # XXX since this function also checks sys.version, it's not strictly a |
| 263 | # "pyconfig.h" check -- should probably be renamed... |
| 264 | |
| 265 | from distutils import sysconfig |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 266 | # if sys.version contains GCC then python was compiled with |
| 267 | # GCC, and the pyconfig.h file should be OK |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 268 | if sys.version.find("GCC") >= 0: |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 269 | return (CONFIG_H_OK, "sys.version mentions 'GCC'") |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 270 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 271 | fn = sysconfig.get_config_h_filename() |
| 272 | try: |
| 273 | # It would probably better to read single lines to search. |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 274 | # But we do this only once, and it is fast enough |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 275 | f = open(fn) |
| 276 | s = f.read() |
| 277 | f.close() |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 279 | except IOError as exc: |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 280 | # if we can't read this file, we cannot say it is wrong |
| 281 | # the compiler will complain later about this file as missing |
| 282 | return (CONFIG_H_UNCERTAIN, |
| 283 | "couldn't read '%s': %s" % (fn, exc.strerror)) |
| 284 | |
| 285 | else: |
| 286 | # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 287 | if s.find("__GNUC__") >= 0: |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 288 | return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn) |
| 289 | else: |
| 290 | return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn) |
| 291 | |
| 292 | |
| 293 | def get_versions(): |
| 294 | """ Try to find out the versions of gcc and ld. |
| 295 | If not possible it returns None for it. |
| 296 | """ |
Tarek Ziadé | f8926b2 | 2009-07-16 16:18:19 +0000 | [diff] [blame] | 297 | warn("'distutils.emxccompiler.get_versions' is deprecated " |
| 298 | "use 'distutils.util.get_compiler_versions' instead", |
| 299 | DeprecationWarning) |
Guido van Rossum | bffa52f | 2002-09-29 00:25:51 +0000 | [diff] [blame] | 300 | |
Marc-André Lemburg | 9273ec7 | 2002-02-06 18:22:48 +0000 | [diff] [blame] | 301 | # EMX ld has no way of reporting version number, and we use GCC |
| 302 | # anyway - so we can link OMF DLLs |
Tarek Ziadé | f8926b2 | 2009-07-16 16:18:19 +0000 | [diff] [blame] | 303 | gcc_version, ld_version, dllwrap_version = get_compiler_versions() |
| 304 | return gcc_version, None |