1
0

unixccompiler.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. """distutils.unixccompiler
  2. Contains the UnixCCompiler class, a subclass of CCompiler that handles
  3. the "typical" Unix-style command-line C compiler:
  4. * macros defined with -Dname[=value]
  5. * macros undefined with -Uname
  6. * include search directories specified with -Idir
  7. * libraries specified with -lllib
  8. * library search directories specified with -Ldir
  9. * compile handled by 'cc' (or similar) executable with -c option:
  10. compiles .c to .o
  11. * link static library handled by 'ar' command (possibly with 'ranlib')
  12. * link shared library handled by 'cc -shared'
  13. """
  14. __revision__ = "$Id: unixccompiler.py 52231 2006-10-08 17:41:25Z ronald.oussoren $"
  15. import os, sys
  16. from types import StringType, NoneType
  17. from copy import copy
  18. from distutils import sysconfig
  19. from distutils.dep_util import newer
  20. from distutils.ccompiler import \
  21. CCompiler, gen_preprocess_options, gen_lib_options
  22. from distutils.errors import \
  23. DistutilsExecError, CompileError, LibError, LinkError
  24. from distutils import log
  25. # XXX Things not currently handled:
  26. # * optimization/debug/warning flags; we just use whatever's in Python's
  27. # Makefile and live with it. Is this adequate? If not, we might
  28. # have to have a bunch of subclasses GNUCCompiler, SGICCompiler,
  29. # SunCCompiler, and I suspect down that road lies madness.
  30. # * even if we don't know a warning flag from an optimization flag,
  31. # we need some way for outsiders to feed preprocessor/compiler/linker
  32. # flags in to us -- eg. a sysadmin might want to mandate certain flags
  33. # via a site config file, or a user might want to set something for
  34. # compiling this module distribution only via the setup.py command
  35. # line, whatever. As long as these options come from something on the
  36. # current system, they can be as system-dependent as they like, and we
  37. # should just happily stuff them into the preprocessor/compiler/linker
  38. # options and carry on.
  39. def _darwin_compiler_fixup(compiler_so, cc_args):
  40. """
  41. This function will strip '-isysroot PATH' and '-arch ARCH' from the
  42. compile flag if the user has specified one of them in extra_compile_flags.
  43. This is needed because '-arch ARCH' adds another architecture to the
  44. build, without a way to remove an architecture. Furthermore GCC will
  45. barf if multiple '-isysroot' arguments are present.
  46. """
  47. stripArch = stripSysroot = 0
  48. compiler_so = list(compiler_so)
  49. kernel_version = os.uname()[2] # 8.4.3
  50. major_version = int(kernel_version.split('.')[0])
  51. if major_version < 8:
  52. # OSX before 10.4.0, these don't support -arch and -isysroot at
  53. # all.
  54. stripArch = stripSysroot = True
  55. else:
  56. stripArch = '-arch' in cc_args
  57. stripSysroot = '-isysroot' in cc_args
  58. if stripArch:
  59. while 1:
  60. try:
  61. index = compiler_so.index('-arch')
  62. # Strip this argument and the next one:
  63. del compiler_so[index:index+2]
  64. except ValueError:
  65. break
  66. if stripSysroot:
  67. try:
  68. index = compiler_so.index('-isysroot')
  69. # Strip this argument and the next one:
  70. del compiler_so[index:index+2]
  71. except ValueError:
  72. pass
  73. return compiler_so
  74. class UnixCCompiler(CCompiler):
  75. compiler_type = 'unix'
  76. # These are used by CCompiler in two places: the constructor sets
  77. # instance attributes 'preprocessor', 'compiler', etc. from them, and
  78. # 'set_executable()' allows any of these to be set. The defaults here
  79. # are pretty generic; they will probably have to be set by an outsider
  80. # (eg. using information discovered by the sysconfig about building
  81. # Python extensions).
  82. executables = {'preprocessor' : None,
  83. 'compiler' : ["cc"],
  84. 'compiler_so' : ["cc"],
  85. 'compiler_cxx' : ["cc"],
  86. 'linker_so' : ["cc", "-shared"],
  87. 'linker_exe' : ["cc"],
  88. 'archiver' : ["ar", "-cr"],
  89. 'ranlib' : None,
  90. }
  91. if sys.platform[:6] == "darwin":
  92. executables['ranlib'] = ["ranlib"]
  93. # Needed for the filename generation methods provided by the base
  94. # class, CCompiler. NB. whoever instantiates/uses a particular
  95. # UnixCCompiler instance should set 'shared_lib_ext' -- we set a
  96. # reasonable common default here, but it's not necessarily used on all
  97. # Unices!
  98. src_extensions = [".c",".C",".cc",".cxx",".cpp",".m"]
  99. obj_extension = ".o"
  100. static_lib_extension = ".a"
  101. shared_lib_extension = ".so"
  102. dylib_lib_extension = ".dylib"
  103. static_lib_format = shared_lib_format = dylib_lib_format = "lib%s%s"
  104. if sys.platform == "cygwin":
  105. exe_extension = ".exe"
  106. def preprocess(self, source,
  107. output_file=None, macros=None, include_dirs=None,
  108. extra_preargs=None, extra_postargs=None):
  109. ignore, macros, include_dirs = \
  110. self._fix_compile_args(None, macros, include_dirs)
  111. pp_opts = gen_preprocess_options(macros, include_dirs)
  112. pp_args = self.preprocessor + pp_opts
  113. if output_file:
  114. pp_args.extend(['-o', output_file])
  115. if extra_preargs:
  116. pp_args[:0] = extra_preargs
  117. if extra_postargs:
  118. pp_args.extend(extra_postargs)
  119. pp_args.append(source)
  120. # We need to preprocess: either we're being forced to, or we're
  121. # generating output to stdout, or there's a target output file and
  122. # the source file is newer than the target (or the target doesn't
  123. # exist).
  124. if self.force or output_file is None or newer(source, output_file):
  125. if output_file:
  126. self.mkpath(os.path.dirname(output_file))
  127. try:
  128. self.spawn(pp_args)
  129. except DistutilsExecError, msg:
  130. raise CompileError, msg
  131. def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
  132. compiler_so = self.compiler_so
  133. if sys.platform == 'darwin':
  134. compiler_so = _darwin_compiler_fixup(compiler_so, cc_args + extra_postargs)
  135. try:
  136. self.spawn(compiler_so + cc_args + [src, '-o', obj] +
  137. extra_postargs)
  138. except DistutilsExecError, msg:
  139. raise CompileError, msg
  140. def create_static_lib(self, objects, output_libname,
  141. output_dir=None, debug=0, target_lang=None):
  142. objects, output_dir = self._fix_object_args(objects, output_dir)
  143. output_filename = \
  144. self.library_filename(output_libname, output_dir=output_dir)
  145. if self._need_link(objects, output_filename):
  146. self.mkpath(os.path.dirname(output_filename))
  147. self.spawn(self.archiver +
  148. [output_filename] +
  149. objects + self.objects)
  150. # Not many Unices required ranlib anymore -- SunOS 4.x is, I
  151. # think the only major Unix that does. Maybe we need some
  152. # platform intelligence here to skip ranlib if it's not
  153. # needed -- or maybe Python's configure script took care of
  154. # it for us, hence the check for leading colon.
  155. if self.ranlib:
  156. try:
  157. self.spawn(self.ranlib + [output_filename])
  158. except DistutilsExecError, msg:
  159. raise LibError, msg
  160. else:
  161. log.debug("skipping %s (up-to-date)", output_filename)
  162. def link(self, target_desc, objects,
  163. output_filename, output_dir=None, libraries=None,
  164. library_dirs=None, runtime_library_dirs=None,
  165. export_symbols=None, debug=0, extra_preargs=None,
  166. extra_postargs=None, build_temp=None, target_lang=None):
  167. objects, output_dir = self._fix_object_args(objects, output_dir)
  168. libraries, library_dirs, runtime_library_dirs = \
  169. self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
  170. lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs,
  171. libraries)
  172. if type(output_dir) not in (StringType, NoneType):
  173. raise TypeError, "'output_dir' must be a string or None"
  174. if output_dir is not None:
  175. output_filename = os.path.join(output_dir, output_filename)
  176. if self._need_link(objects, output_filename):
  177. ld_args = (objects + self.objects +
  178. lib_opts + ['-o', output_filename])
  179. if debug:
  180. ld_args[:0] = ['-g']
  181. if extra_preargs:
  182. ld_args[:0] = extra_preargs
  183. if extra_postargs:
  184. ld_args.extend(extra_postargs)
  185. self.mkpath(os.path.dirname(output_filename))
  186. try:
  187. if target_desc == CCompiler.EXECUTABLE:
  188. linker = self.linker_exe[:]
  189. else:
  190. linker = self.linker_so[:]
  191. if target_lang == "c++" and self.compiler_cxx:
  192. linker[0] = self.compiler_cxx[0]
  193. if sys.platform == 'darwin':
  194. linker = _darwin_compiler_fixup(linker, ld_args)
  195. self.spawn(linker + ld_args)
  196. except DistutilsExecError, msg:
  197. raise LinkError, msg
  198. else:
  199. log.debug("skipping %s (up-to-date)", output_filename)
  200. # -- Miscellaneous methods -----------------------------------------
  201. # These are all used by the 'gen_lib_options() function, in
  202. # ccompiler.py.
  203. def library_dir_option(self, dir):
  204. return "-L" + dir
  205. def runtime_library_dir_option(self, dir):
  206. # XXX Hackish, at the very least. See Python bug #445902:
  207. # http://sourceforge.net/tracker/index.php
  208. # ?func=detail&aid=445902&group_id=5470&atid=105470
  209. # Linkers on different platforms need different options to
  210. # specify that directories need to be added to the list of
  211. # directories searched for dependencies when a dynamic library
  212. # is sought. GCC has to be told to pass the -R option through
  213. # to the linker, whereas other compilers just know this.
  214. # Other compilers may need something slightly different. At
  215. # this time, there's no way to determine this information from
  216. # the configuration data stored in the Python installation, so
  217. # we use this hack.
  218. compiler = os.path.basename(sysconfig.get_config_var("CC"))
  219. if sys.platform[:6] == "darwin":
  220. # MacOSX's linker doesn't understand the -R flag at all
  221. return "-L" + dir
  222. elif sys.platform[:5] == "hp-ux":
  223. return "+s -L" + dir
  224. elif sys.platform[:7] == "irix646" or sys.platform[:6] == "osf1V5":
  225. return ["-rpath", dir]
  226. elif compiler[:3] == "gcc" or compiler[:3] == "g++":
  227. return "-Wl,-R" + dir
  228. else:
  229. return "-R" + dir
  230. def library_option(self, lib):
  231. return "-l" + lib
  232. def find_library_file(self, dirs, lib, debug=0):
  233. shared_f = self.library_filename(lib, lib_type='shared')
  234. dylib_f = self.library_filename(lib, lib_type='dylib')
  235. static_f = self.library_filename(lib, lib_type='static')
  236. for dir in dirs:
  237. shared = os.path.join(dir, shared_f)
  238. dylib = os.path.join(dir, dylib_f)
  239. static = os.path.join(dir, static_f)
  240. # We're second-guessing the linker here, with not much hard
  241. # data to go on: GCC seems to prefer the shared library, so I'm
  242. # assuming that *all* Unix C compilers do. And of course I'm
  243. # ignoring even GCC's "-static" option. So sue me.
  244. if os.path.exists(dylib):
  245. return dylib
  246. elif os.path.exists(shared):
  247. return shared
  248. elif os.path.exists(static):
  249. return static
  250. # Oops, didn't find it in *any* of 'dirs'
  251. return None