PageRenderTime 128ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/distutils/distutils/emxccompiler.py

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