PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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