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