PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/distutils/bcppcompiler.py

https://bitbucket.org/quangquach/pypy
Python | 394 lines | 293 code | 39 blank | 62 comment | 33 complexity | a211297b514d7c813ebf0ba8239d493b MD5 | raw file
  1. """distutils.bcppcompiler
  2. Contains BorlandCCompiler, an implementation of the abstract CCompiler class
  3. for the Borland C++ compiler.
  4. """
  5. # This implementation by Lyle Johnson, based on the original msvccompiler.py
  6. # module and using the directions originally published by Gordon Williams.
  7. # XXX looks like there's a LOT of overlap between these two classes:
  8. # someone should sit down and factor out the common code as
  9. # WindowsCCompiler! --GPW
  10. __revision__ = "$Id$"
  11. import os
  12. from distutils.errors import (DistutilsExecError, CompileError, LibError,
  13. LinkError, UnknownFileError)
  14. from distutils.ccompiler import CCompiler, gen_preprocess_options
  15. from distutils.file_util import write_file
  16. from distutils.dep_util import newer
  17. from distutils import log
  18. class BCPPCompiler(CCompiler) :
  19. """Concrete class that implements an interface to the Borland C/C++
  20. compiler, as defined by the CCompiler abstract class.
  21. """
  22. compiler_type = 'bcpp'
  23. # Just set this so CCompiler's constructor doesn't barf. We currently
  24. # don't use the 'set_executables()' bureaucracy provided by CCompiler,
  25. # as it really isn't necessary for this sort of single-compiler class.
  26. # Would be nice to have a consistent interface with UnixCCompiler,
  27. # though, so it's worth thinking about.
  28. executables = {}
  29. # Private class data (need to distinguish C from C++ source for compiler)
  30. _c_extensions = ['.c']
  31. _cpp_extensions = ['.cc', '.cpp', '.cxx']
  32. # Needed for the filename generation methods provided by the
  33. # base class, CCompiler.
  34. src_extensions = _c_extensions + _cpp_extensions
  35. obj_extension = '.obj'
  36. static_lib_extension = '.lib'
  37. shared_lib_extension = '.dll'
  38. static_lib_format = shared_lib_format = '%s%s'
  39. exe_extension = '.exe'
  40. def __init__ (self,
  41. verbose=0,
  42. dry_run=0,
  43. force=0):
  44. CCompiler.__init__ (self, verbose, dry_run, force)
  45. # These executables are assumed to all be in the path.
  46. # Borland doesn't seem to use any special registry settings to
  47. # indicate their installation locations.
  48. self.cc = "bcc32.exe"
  49. self.linker = "ilink32.exe"
  50. self.lib = "tlib.exe"
  51. self.preprocess_options = None
  52. self.compile_options = ['/tWM', '/O2', '/q', '/g0']
  53. self.compile_options_debug = ['/tWM', '/Od', '/q', '/g0']
  54. self.ldflags_shared = ['/Tpd', '/Gn', '/q', '/x']
  55. self.ldflags_shared_debug = ['/Tpd', '/Gn', '/q', '/x']
  56. self.ldflags_static = []
  57. self.ldflags_exe = ['/Gn', '/q', '/x']
  58. self.ldflags_exe_debug = ['/Gn', '/q', '/x','/r']
  59. # -- Worker methods ------------------------------------------------
  60. def compile(self, sources,
  61. output_dir=None, macros=None, include_dirs=None, debug=0,
  62. extra_preargs=None, extra_postargs=None, depends=None):
  63. macros, objects, extra_postargs, pp_opts, build = \
  64. self._setup_compile(output_dir, macros, include_dirs, sources,
  65. depends, extra_postargs)
  66. compile_opts = extra_preargs or []
  67. compile_opts.append ('-c')
  68. if debug:
  69. compile_opts.extend (self.compile_options_debug)
  70. else:
  71. compile_opts.extend (self.compile_options)
  72. for obj in objects:
  73. try:
  74. src, ext = build[obj]
  75. except KeyError:
  76. continue
  77. # XXX why do the normpath here?
  78. src = os.path.normpath(src)
  79. obj = os.path.normpath(obj)
  80. # XXX _setup_compile() did a mkpath() too but before the normpath.
  81. # Is it possible to skip the normpath?
  82. self.mkpath(os.path.dirname(obj))
  83. if ext == '.res':
  84. # This is already a binary file -- skip it.
  85. continue # the 'for' loop
  86. if ext == '.rc':
  87. # This needs to be compiled to a .res file -- do it now.
  88. try:
  89. self.spawn (["brcc32", "-fo", obj, src])
  90. except DistutilsExecError, msg:
  91. raise CompileError, msg
  92. continue # the 'for' loop
  93. # The next two are both for the real compiler.
  94. if ext in self._c_extensions:
  95. input_opt = ""
  96. elif ext in self._cpp_extensions:
  97. input_opt = "-P"
  98. else:
  99. # Unknown file type -- no extra options. The compiler
  100. # will probably fail, but let it just in case this is a
  101. # file the compiler recognizes even if we don't.
  102. input_opt = ""
  103. output_opt = "-o" + obj
  104. # Compiler command line syntax is: "bcc32 [options] file(s)".
  105. # Note that the source file names must appear at the end of
  106. # the command line.
  107. try:
  108. self.spawn ([self.cc] + compile_opts + pp_opts +
  109. [input_opt, output_opt] +
  110. extra_postargs + [src])
  111. except DistutilsExecError, msg:
  112. raise CompileError, msg
  113. return objects
  114. # compile ()
  115. def create_static_lib (self,
  116. objects,
  117. output_libname,
  118. output_dir=None,
  119. debug=0,
  120. target_lang=None):
  121. (objects, output_dir) = self._fix_object_args (objects, output_dir)
  122. output_filename = \
  123. self.library_filename (output_libname, output_dir=output_dir)
  124. if self._need_link (objects, output_filename):
  125. lib_args = [output_filename, '/u'] + objects
  126. if debug:
  127. pass # XXX what goes here?
  128. try:
  129. self.spawn ([self.lib] + lib_args)
  130. except DistutilsExecError, msg:
  131. raise LibError, msg
  132. else:
  133. log.debug("skipping %s (up-to-date)", output_filename)
  134. # create_static_lib ()
  135. def link (self,
  136. target_desc,
  137. objects,
  138. output_filename,
  139. output_dir=None,
  140. libraries=None,
  141. library_dirs=None,
  142. runtime_library_dirs=None,
  143. export_symbols=None,
  144. debug=0,
  145. extra_preargs=None,
  146. extra_postargs=None,
  147. build_temp=None,
  148. target_lang=None):
  149. # XXX this ignores 'build_temp'! should follow the lead of
  150. # msvccompiler.py
  151. (objects, output_dir) = self._fix_object_args (objects, output_dir)
  152. (libraries, library_dirs, runtime_library_dirs) = \
  153. self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
  154. if runtime_library_dirs:
  155. log.warn("I don't know what to do with 'runtime_library_dirs': %s",
  156. str(runtime_library_dirs))
  157. if output_dir is not None:
  158. output_filename = os.path.join (output_dir, output_filename)
  159. if self._need_link (objects, output_filename):
  160. # Figure out linker args based on type of target.
  161. if target_desc == CCompiler.EXECUTABLE:
  162. startup_obj = 'c0w32'
  163. if debug:
  164. ld_args = self.ldflags_exe_debug[:]
  165. else:
  166. ld_args = self.ldflags_exe[:]
  167. else:
  168. startup_obj = 'c0d32'
  169. if debug:
  170. ld_args = self.ldflags_shared_debug[:]
  171. else:
  172. ld_args = self.ldflags_shared[:]
  173. # Create a temporary exports file for use by the linker
  174. if export_symbols is None:
  175. def_file = ''
  176. else:
  177. head, tail = os.path.split (output_filename)
  178. modname, ext = os.path.splitext (tail)
  179. temp_dir = os.path.dirname(objects[0]) # preserve tree structure
  180. def_file = os.path.join (temp_dir, '%s.def' % modname)
  181. contents = ['EXPORTS']
  182. for sym in (export_symbols or []):
  183. contents.append(' %s=_%s' % (sym, sym))
  184. self.execute(write_file, (def_file, contents),
  185. "writing %s" % def_file)
  186. # Borland C++ has problems with '/' in paths
  187. objects2 = map(os.path.normpath, objects)
  188. # split objects in .obj and .res files
  189. # Borland C++ needs them at different positions in the command line
  190. objects = [startup_obj]
  191. resources = []
  192. for file in objects2:
  193. (base, ext) = os.path.splitext(os.path.normcase(file))
  194. if ext == '.res':
  195. resources.append(file)
  196. else:
  197. objects.append(file)
  198. for l in library_dirs:
  199. ld_args.append("/L%s" % os.path.normpath(l))
  200. ld_args.append("/L.") # we sometimes use relative paths
  201. # list of object files
  202. ld_args.extend(objects)
  203. # XXX the command-line syntax for Borland C++ is a bit wonky;
  204. # certain filenames are jammed together in one big string, but
  205. # comma-delimited. This doesn't mesh too well with the
  206. # Unix-centric attitude (with a DOS/Windows quoting hack) of
  207. # 'spawn()', so constructing the argument list is a bit
  208. # awkward. Note that doing the obvious thing and jamming all
  209. # the filenames and commas into one argument would be wrong,
  210. # because 'spawn()' would quote any filenames with spaces in
  211. # them. Arghghh!. Apparently it works fine as coded...
  212. # name of dll/exe file
  213. ld_args.extend([',',output_filename])
  214. # no map file and start libraries
  215. ld_args.append(',,')
  216. for lib in libraries:
  217. # see if we find it and if there is a bcpp specific lib
  218. # (xxx_bcpp.lib)
  219. libfile = self.find_library_file(library_dirs, lib, debug)
  220. if libfile is None:
  221. ld_args.append(lib)
  222. # probably a BCPP internal library -- don't warn
  223. else:
  224. # full name which prefers bcpp_xxx.lib over xxx.lib
  225. ld_args.append(libfile)
  226. # some default libraries
  227. ld_args.append ('import32')
  228. ld_args.append ('cw32mt')
  229. # def file for export symbols
  230. ld_args.extend([',',def_file])
  231. # add resource files
  232. ld_args.append(',')
  233. ld_args.extend(resources)
  234. if extra_preargs:
  235. ld_args[:0] = extra_preargs
  236. if extra_postargs:
  237. ld_args.extend(extra_postargs)
  238. self.mkpath (os.path.dirname (output_filename))
  239. try:
  240. self.spawn ([self.linker] + ld_args)
  241. except DistutilsExecError, msg:
  242. raise LinkError, msg
  243. else:
  244. log.debug("skipping %s (up-to-date)", output_filename)
  245. # link ()
  246. # -- Miscellaneous methods -----------------------------------------
  247. def find_library_file (self, dirs, lib, debug=0):
  248. # List of effective library names to try, in order of preference:
  249. # xxx_bcpp.lib is better than xxx.lib
  250. # and xxx_d.lib is better than xxx.lib if debug is set
  251. #
  252. # The "_bcpp" suffix is to handle a Python installation for people
  253. # with multiple compilers (primarily Distutils hackers, I suspect
  254. # ;-). The idea is they'd have one static library for each
  255. # compiler they care about, since (almost?) every Windows compiler
  256. # seems to have a different format for static libraries.
  257. if debug:
  258. dlib = (lib + "_d")
  259. try_names = (dlib + "_bcpp", lib + "_bcpp", dlib, lib)
  260. else:
  261. try_names = (lib + "_bcpp", lib)
  262. for dir in dirs:
  263. for name in try_names:
  264. libfile = os.path.join(dir, self.library_filename(name))
  265. if os.path.exists(libfile):
  266. return libfile
  267. else:
  268. # Oops, didn't find it in *any* of 'dirs'
  269. return None
  270. # overwrite the one from CCompiler to support rc and res-files
  271. def object_filenames (self,
  272. source_filenames,
  273. strip_dir=0,
  274. output_dir=''):
  275. if output_dir is None: output_dir = ''
  276. obj_names = []
  277. for src_name in source_filenames:
  278. # use normcase to make sure '.rc' is really '.rc' and not '.RC'
  279. (base, ext) = os.path.splitext (os.path.normcase(src_name))
  280. if ext not in (self.src_extensions + ['.rc','.res']):
  281. raise UnknownFileError, \
  282. "unknown file type '%s' (from '%s')" % \
  283. (ext, src_name)
  284. if strip_dir:
  285. base = os.path.basename (base)
  286. if ext == '.res':
  287. # these can go unchanged
  288. obj_names.append (os.path.join (output_dir, base + ext))
  289. elif ext == '.rc':
  290. # these need to be compiled to .res-files
  291. obj_names.append (os.path.join (output_dir, base + '.res'))
  292. else:
  293. obj_names.append (os.path.join (output_dir,
  294. base + self.obj_extension))
  295. return obj_names
  296. # object_filenames ()
  297. def preprocess (self,
  298. source,
  299. output_file=None,
  300. macros=None,
  301. include_dirs=None,
  302. extra_preargs=None,
  303. extra_postargs=None):
  304. (_, macros, include_dirs) = \
  305. self._fix_compile_args(None, macros, include_dirs)
  306. pp_opts = gen_preprocess_options(macros, include_dirs)
  307. pp_args = ['cpp32.exe'] + pp_opts
  308. if output_file is not None:
  309. pp_args.append('-o' + output_file)
  310. if extra_preargs:
  311. pp_args[:0] = extra_preargs
  312. if extra_postargs:
  313. pp_args.extend(extra_postargs)
  314. pp_args.append(source)
  315. # We need to preprocess: either we're being forced to, or the
  316. # source file is newer than the target (or the target doesn't
  317. # exist).
  318. if self.force or output_file is None or newer(source, output_file):
  319. if output_file:
  320. self.mkpath(os.path.dirname(output_file))
  321. try:
  322. self.spawn(pp_args)
  323. except DistutilsExecError, msg:
  324. print msg
  325. raise CompileError, msg
  326. # preprocess()