+++ /dev/null
-"""distutils.emxccompiler\r
-\r
-Provides the EMXCCompiler class, a subclass of UnixCCompiler that\r
-handles the EMX port of the GNU C compiler to OS/2.\r
-"""\r
-\r
-# issues:\r
-#\r
-# * OS/2 insists that DLLs can have names no longer than 8 characters\r
-# We put export_symbols in a def-file, as though the DLL can have\r
-# an arbitrary length name, but truncate the output filename.\r
-#\r
-# * only use OMF objects and use LINK386 as the linker (-Zomf)\r
-#\r
-# * always build for multithreading (-Zmt) as the accompanying OS/2 port\r
-# of Python is only distributed with threads enabled.\r
-#\r
-# tested configurations:\r
-#\r
-# * EMX gcc 2.81/EMX 0.9d fix03\r
-\r
-__revision__ = "$Id$"\r
-\r
-import os,sys,copy\r
-from distutils.ccompiler import gen_preprocess_options, gen_lib_options\r
-from distutils.unixccompiler import UnixCCompiler\r
-from distutils.file_util import write_file\r
-from distutils.errors import DistutilsExecError, CompileError, UnknownFileError\r
-from distutils import log\r
-\r
-class EMXCCompiler (UnixCCompiler):\r
-\r
- compiler_type = 'emx'\r
- obj_extension = ".obj"\r
- static_lib_extension = ".lib"\r
- shared_lib_extension = ".dll"\r
- static_lib_format = "%s%s"\r
- shared_lib_format = "%s%s"\r
- res_extension = ".res" # compiled resource file\r
- exe_extension = ".exe"\r
-\r
- def __init__ (self,\r
- verbose=0,\r
- dry_run=0,\r
- force=0):\r
-\r
- UnixCCompiler.__init__ (self, verbose, dry_run, force)\r
-\r
- (status, details) = check_config_h()\r
- self.debug_print("Python's GCC status: %s (details: %s)" %\r
- (status, details))\r
- if status is not CONFIG_H_OK:\r
- self.warn(\r
- "Python's pyconfig.h doesn't seem to support your compiler. " +\r
- ("Reason: %s." % details) +\r
- "Compiling may fail because of undefined preprocessor macros.")\r
-\r
- (self.gcc_version, self.ld_version) = \\r
- get_versions()\r
- self.debug_print(self.compiler_type + ": gcc %s, ld %s\n" %\r
- (self.gcc_version,\r
- self.ld_version) )\r
-\r
- # Hard-code GCC because that's what this is all about.\r
- # XXX optimization, warnings etc. should be customizable.\r
- self.set_executables(compiler='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall',\r
- compiler_so='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall',\r
- linker_exe='gcc -Zomf -Zmt -Zcrtdll',\r
- linker_so='gcc -Zomf -Zmt -Zcrtdll -Zdll')\r
-\r
- # want the gcc library statically linked (so that we don't have\r
- # to distribute a version dependent on the compiler we have)\r
- self.dll_libraries=["gcc"]\r
-\r
- # __init__ ()\r
-\r
- def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):\r
- if ext == '.rc':\r
- # gcc requires '.rc' compiled to binary ('.res') files !!!\r
- try:\r
- self.spawn(["rc", "-r", src])\r
- except DistutilsExecError, msg:\r
- raise CompileError, msg\r
- else: # for other files use the C-compiler\r
- try:\r
- self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +\r
- extra_postargs)\r
- except DistutilsExecError, msg:\r
- raise CompileError, msg\r
-\r
- def link (self,\r
- target_desc,\r
- objects,\r
- output_filename,\r
- output_dir=None,\r
- libraries=None,\r
- library_dirs=None,\r
- runtime_library_dirs=None,\r
- export_symbols=None,\r
- debug=0,\r
- extra_preargs=None,\r
- extra_postargs=None,\r
- build_temp=None,\r
- target_lang=None):\r
-\r
- # use separate copies, so we can modify the lists\r
- extra_preargs = copy.copy(extra_preargs or [])\r
- libraries = copy.copy(libraries or [])\r
- objects = copy.copy(objects or [])\r
-\r
- # Additional libraries\r
- libraries.extend(self.dll_libraries)\r
-\r
- # handle export symbols by creating a def-file\r
- # with executables this only works with gcc/ld as linker\r
- if ((export_symbols is not None) and\r
- (target_desc != self.EXECUTABLE)):\r
- # (The linker doesn't do anything if output is up-to-date.\r
- # So it would probably better to check if we really need this,\r
- # but for this we had to insert some unchanged parts of\r
- # UnixCCompiler, and this is not what we want.)\r
-\r
- # we want to put some files in the same directory as the\r
- # object files are, build_temp doesn't help much\r
- # where are the object files\r
- temp_dir = os.path.dirname(objects[0])\r
- # name of dll to give the helper files the same base name\r
- (dll_name, dll_extension) = os.path.splitext(\r
- os.path.basename(output_filename))\r
-\r
- # generate the filenames for these files\r
- def_file = os.path.join(temp_dir, dll_name + ".def")\r
-\r
- # Generate .def file\r
- contents = [\r
- "LIBRARY %s INITINSTANCE TERMINSTANCE" % \\r
- os.path.splitext(os.path.basename(output_filename))[0],\r
- "DATA MULTIPLE NONSHARED",\r
- "EXPORTS"]\r
- for sym in export_symbols:\r
- contents.append(' "%s"' % sym)\r
- self.execute(write_file, (def_file, contents),\r
- "writing %s" % def_file)\r
-\r
- # next add options for def-file and to creating import libraries\r
- # for gcc/ld the def-file is specified as any other object files\r
- objects.append(def_file)\r
-\r
- #end: if ((export_symbols is not None) and\r
- # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):\r
-\r
- # who wants symbols and a many times larger output file\r
- # should explicitly switch the debug mode on\r
- # otherwise we let dllwrap/ld strip the output file\r
- # (On my machine: 10KB < stripped_file < ??100KB\r
- # unstripped_file = stripped_file + XXX KB\r
- # ( XXX=254 for a typical python extension))\r
- if not debug:\r
- extra_preargs.append("-s")\r
-\r
- UnixCCompiler.link(self,\r
- target_desc,\r
- objects,\r
- output_filename,\r
- output_dir,\r
- libraries,\r
- library_dirs,\r
- runtime_library_dirs,\r
- None, # export_symbols, we do this in our def-file\r
- debug,\r
- extra_preargs,\r
- extra_postargs,\r
- build_temp,\r
- target_lang)\r
-\r
- # link ()\r
-\r
- # -- Miscellaneous methods -----------------------------------------\r
-\r
- # override the object_filenames method from CCompiler to\r
- # support rc and res-files\r
- def object_filenames (self,\r
- source_filenames,\r
- strip_dir=0,\r
- output_dir=''):\r
- if output_dir is None: output_dir = ''\r
- obj_names = []\r
- for src_name in source_filenames:\r
- # use normcase to make sure '.rc' is really '.rc' and not '.RC'\r
- (base, ext) = os.path.splitext (os.path.normcase(src_name))\r
- if ext not in (self.src_extensions + ['.rc']):\r
- raise UnknownFileError, \\r
- "unknown file type '%s' (from '%s')" % \\r
- (ext, src_name)\r
- if strip_dir:\r
- base = os.path.basename (base)\r
- if ext == '.rc':\r
- # these need to be compiled to object files\r
- obj_names.append (os.path.join (output_dir,\r
- base + self.res_extension))\r
- else:\r
- obj_names.append (os.path.join (output_dir,\r
- base + self.obj_extension))\r
- return obj_names\r
-\r
- # object_filenames ()\r
-\r
- # override the find_library_file method from UnixCCompiler\r
- # to deal with file naming/searching differences\r
- def find_library_file(self, dirs, lib, debug=0):\r
- shortlib = '%s.lib' % lib\r
- longlib = 'lib%s.lib' % lib # this form very rare\r
-\r
- # get EMX's default library directory search path\r
- try:\r
- emx_dirs = os.environ['LIBRARY_PATH'].split(';')\r
- except KeyError:\r
- emx_dirs = []\r
-\r
- for dir in dirs + emx_dirs:\r
- shortlibp = os.path.join(dir, shortlib)\r
- longlibp = os.path.join(dir, longlib)\r
- if os.path.exists(shortlibp):\r
- return shortlibp\r
- elif os.path.exists(longlibp):\r
- return longlibp\r
-\r
- # Oops, didn't find it in *any* of 'dirs'\r
- return None\r
-\r
-# class EMXCCompiler\r
-\r
-\r
-# Because these compilers aren't configured in Python's pyconfig.h file by\r
-# default, we should at least warn the user if he is using a unmodified\r
-# version.\r
-\r
-CONFIG_H_OK = "ok"\r
-CONFIG_H_NOTOK = "not ok"\r
-CONFIG_H_UNCERTAIN = "uncertain"\r
-\r
-def check_config_h():\r
-\r
- """Check if the current Python installation (specifically, pyconfig.h)\r
- appears amenable to building extensions with GCC. Returns a tuple\r
- (status, details), where 'status' is one of the following constants:\r
- CONFIG_H_OK\r
- all is well, go ahead and compile\r
- CONFIG_H_NOTOK\r
- doesn't look good\r
- CONFIG_H_UNCERTAIN\r
- not sure -- unable to read pyconfig.h\r
- 'details' is a human-readable string explaining the situation.\r
-\r
- Note there are two ways to conclude "OK": either 'sys.version' contains\r
- the string "GCC" (implying that this Python was built with GCC), or the\r
- installed "pyconfig.h" contains the string "__GNUC__".\r
- """\r
-\r
- # XXX since this function also checks sys.version, it's not strictly a\r
- # "pyconfig.h" check -- should probably be renamed...\r
-\r
- from distutils import sysconfig\r
- import string\r
- # if sys.version contains GCC then python was compiled with\r
- # GCC, and the pyconfig.h file should be OK\r
- if string.find(sys.version,"GCC") >= 0:\r
- return (CONFIG_H_OK, "sys.version mentions 'GCC'")\r
-\r
- fn = sysconfig.get_config_h_filename()\r
- try:\r
- # It would probably better to read single lines to search.\r
- # But we do this only once, and it is fast enough\r
- f = open(fn)\r
- try:\r
- s = f.read()\r
- finally:\r
- f.close()\r
-\r
- except IOError, exc:\r
- # if we can't read this file, we cannot say it is wrong\r
- # the compiler will complain later about this file as missing\r
- return (CONFIG_H_UNCERTAIN,\r
- "couldn't read '%s': %s" % (fn, exc.strerror))\r
-\r
- else:\r
- # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar\r
- if string.find(s,"__GNUC__") >= 0:\r
- return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)\r
- else:\r
- return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)\r
-\r
-\r
-def get_versions():\r
- """ Try to find out the versions of gcc and ld.\r
- If not possible it returns None for it.\r
- """\r
- from distutils.version import StrictVersion\r
- from distutils.spawn import find_executable\r
- import re\r
-\r
- gcc_exe = find_executable('gcc')\r
- if gcc_exe:\r
- out = os.popen(gcc_exe + ' -dumpversion','r')\r
- try:\r
- out_string = out.read()\r
- finally:\r
- out.close()\r
- result = re.search('(\d+\.\d+\.\d+)',out_string)\r
- if result:\r
- gcc_version = StrictVersion(result.group(1))\r
- else:\r
- gcc_version = None\r
- else:\r
- gcc_version = None\r
- # EMX ld has no way of reporting version number, and we use GCC\r
- # anyway - so we can link OMF DLLs\r
- ld_version = None\r
- return (gcc_version, ld_version)\r