/Lib/distutils/extension.py

http://unladen-swallow.googlecode.com/ · Python · 246 lines · 166 code · 25 blank · 55 comment · 40 complexity · bc5942353c7e60791600f50a2e4a95f6 MD5 · raw file

  1. """distutils.extension
  2. Provides the Extension class, used to describe C/C++ extension
  3. modules in setup scripts."""
  4. __revision__ = "$Id: extension.py 37623 2004-10-14 10:02:08Z anthonybaxter $"
  5. import os, string, sys
  6. from types import *
  7. try:
  8. import warnings
  9. except ImportError:
  10. warnings = None
  11. # This class is really only used by the "build_ext" command, so it might
  12. # make sense to put it in distutils.command.build_ext. However, that
  13. # module is already big enough, and I want to make this class a bit more
  14. # complex to simplify some common cases ("foo" module in "foo.c") and do
  15. # better error-checking ("foo.c" actually exists).
  16. #
  17. # Also, putting this in build_ext.py means every setup script would have to
  18. # import that large-ish module (indirectly, through distutils.core) in
  19. # order to do anything.
  20. class Extension:
  21. """Just a collection of attributes that describes an extension
  22. module and everything needed to build it (hopefully in a portable
  23. way, but there are hooks that let you be as unportable as you need).
  24. Instance attributes:
  25. name : string
  26. the full name of the extension, including any packages -- ie.
  27. *not* a filename or pathname, but Python dotted name
  28. sources : [string]
  29. list of source filenames, relative to the distribution root
  30. (where the setup script lives), in Unix form (slash-separated)
  31. for portability. Source files may be C, C++, SWIG (.i),
  32. platform-specific resource files, or whatever else is recognized
  33. by the "build_ext" command as source for a Python extension.
  34. include_dirs : [string]
  35. list of directories to search for C/C++ header files (in Unix
  36. form for portability)
  37. define_macros : [(name : string, value : string|None)]
  38. list of macros to define; each macro is defined using a 2-tuple,
  39. where 'value' is either the string to define it to or None to
  40. define it without a particular value (equivalent of "#define
  41. FOO" in source or -DFOO on Unix C compiler command line)
  42. undef_macros : [string]
  43. list of macros to undefine explicitly
  44. library_dirs : [string]
  45. list of directories to search for C/C++ libraries at link time
  46. libraries : [string]
  47. list of library names (not filenames or paths) to link against
  48. runtime_library_dirs : [string]
  49. list of directories to search for C/C++ libraries at run time
  50. (for shared extensions, this is when the extension is loaded)
  51. extra_objects : [string]
  52. list of extra files to link with (eg. object files not implied
  53. by 'sources', static library that must be explicitly specified,
  54. binary resource files, etc.)
  55. extra_compile_args : [string]
  56. any extra platform- and compiler-specific information to use
  57. when compiling the source files in 'sources'. For platforms and
  58. compilers where "command line" makes sense, this is typically a
  59. list of command-line arguments, but for other platforms it could
  60. be anything.
  61. extra_link_args : [string]
  62. any extra platform- and compiler-specific information to use
  63. when linking object files together to create the extension (or
  64. to create a new static Python interpreter). Similar
  65. interpretation as for 'extra_compile_args'.
  66. export_symbols : [string]
  67. list of symbols to be exported from a shared extension. Not
  68. used on all platforms, and not generally necessary for Python
  69. extensions, which typically export exactly one symbol: "init" +
  70. extension_name.
  71. swig_opts : [string]
  72. any extra options to pass to SWIG if a source file has the .i
  73. extension.
  74. depends : [string]
  75. list of files that the extension depends on
  76. language : string
  77. extension language (i.e. "c", "c++", "objc"). Will be detected
  78. from the source extensions if not provided.
  79. """
  80. # When adding arguments to this constructor, be sure to update
  81. # setup_keywords in core.py.
  82. def __init__ (self, name, sources,
  83. include_dirs=None,
  84. define_macros=None,
  85. undef_macros=None,
  86. library_dirs=None,
  87. libraries=None,
  88. runtime_library_dirs=None,
  89. extra_objects=None,
  90. extra_compile_args=None,
  91. extra_link_args=None,
  92. export_symbols=None,
  93. swig_opts = None,
  94. depends=None,
  95. language=None,
  96. **kw # To catch unknown keywords
  97. ):
  98. assert type(name) is StringType, "'name' must be a string"
  99. assert (type(sources) is ListType and
  100. map(type, sources) == [StringType]*len(sources)), \
  101. "'sources' must be a list of strings"
  102. self.name = name
  103. self.sources = sources
  104. self.include_dirs = include_dirs or []
  105. self.define_macros = define_macros or []
  106. self.undef_macros = undef_macros or []
  107. self.library_dirs = library_dirs or []
  108. self.libraries = libraries or []
  109. self.runtime_library_dirs = runtime_library_dirs or []
  110. self.extra_objects = extra_objects or []
  111. self.extra_compile_args = extra_compile_args or []
  112. self.extra_link_args = extra_link_args or []
  113. self.export_symbols = export_symbols or []
  114. self.swig_opts = swig_opts or []
  115. self.depends = depends or []
  116. self.language = language
  117. # If there are unknown keyword options, warn about them
  118. if len(kw):
  119. L = kw.keys() ; L.sort()
  120. L = map(repr, L)
  121. msg = "Unknown Extension options: " + string.join(L, ', ')
  122. if warnings is not None:
  123. warnings.warn(msg)
  124. else:
  125. sys.stderr.write(msg + '\n')
  126. # class Extension
  127. def read_setup_file (filename):
  128. from distutils.sysconfig import \
  129. parse_makefile, expand_makefile_vars, _variable_rx
  130. from distutils.text_file import TextFile
  131. from distutils.util import split_quoted
  132. # First pass over the file to gather "VAR = VALUE" assignments.
  133. vars = parse_makefile(filename)
  134. # Second pass to gobble up the real content: lines of the form
  135. # <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
  136. file = TextFile(filename,
  137. strip_comments=1, skip_blanks=1, join_lines=1,
  138. lstrip_ws=1, rstrip_ws=1)
  139. extensions = []
  140. while 1:
  141. line = file.readline()
  142. if line is None: # eof
  143. break
  144. if _variable_rx.match(line): # VAR=VALUE, handled in first pass
  145. continue
  146. if line[0] == line[-1] == "*":
  147. file.warn("'%s' lines not handled yet" % line)
  148. continue
  149. #print "original line: " + line
  150. line = expand_makefile_vars(line, vars)
  151. words = split_quoted(line)
  152. #print "expanded line: " + line
  153. # NB. this parses a slightly different syntax than the old
  154. # makesetup script: here, there must be exactly one extension per
  155. # line, and it must be the first word of the line. I have no idea
  156. # why the old syntax supported multiple extensions per line, as
  157. # they all wind up being the same.
  158. module = words[0]
  159. ext = Extension(module, [])
  160. append_next_word = None
  161. for word in words[1:]:
  162. if append_next_word is not None:
  163. append_next_word.append(word)
  164. append_next_word = None
  165. continue
  166. suffix = os.path.splitext(word)[1]
  167. switch = word[0:2] ; value = word[2:]
  168. if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
  169. # hmm, should we do something about C vs. C++ sources?
  170. # or leave it up to the CCompiler implementation to
  171. # worry about?
  172. ext.sources.append(word)
  173. elif switch == "-I":
  174. ext.include_dirs.append(value)
  175. elif switch == "-D":
  176. equals = string.find(value, "=")
  177. if equals == -1: # bare "-DFOO" -- no value
  178. ext.define_macros.append((value, None))
  179. else: # "-DFOO=blah"
  180. ext.define_macros.append((value[0:equals],
  181. value[equals+2:]))
  182. elif switch == "-U":
  183. ext.undef_macros.append(value)
  184. elif switch == "-C": # only here 'cause makesetup has it!
  185. ext.extra_compile_args.append(word)
  186. elif switch == "-l":
  187. ext.libraries.append(value)
  188. elif switch == "-L":
  189. ext.library_dirs.append(value)
  190. elif switch == "-R":
  191. ext.runtime_library_dirs.append(value)
  192. elif word == "-rpath":
  193. append_next_word = ext.runtime_library_dirs
  194. elif word == "-Xlinker":
  195. append_next_word = ext.extra_link_args
  196. elif word == "-Xcompiler":
  197. append_next_word = ext.extra_compile_args
  198. elif switch == "-u":
  199. ext.extra_link_args.append(word)
  200. if not value:
  201. append_next_word = ext.extra_link_args
  202. elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
  203. # NB. a really faithful emulation of makesetup would
  204. # append a .o file to extra_objects only if it
  205. # had a slash in it; otherwise, it would s/.o/.c/
  206. # and append it to sources. Hmmmm.
  207. ext.extra_objects.append(word)
  208. else:
  209. file.warn("unrecognized argument '%s'" % word)
  210. extensions.append(ext)
  211. #print "module:", module
  212. #print "source files:", source_files
  213. #print "cpp args:", cpp_args
  214. #print "lib args:", library_args
  215. #extensions[module] = { 'sources': source_files,
  216. # 'cpp_args': cpp_args,
  217. # 'lib_args': library_args }
  218. return extensions
  219. # read_setup_file ()