extension.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. """distutils.extension
  2. Provides the Extension class, used to describe C/C++ extension
  3. modules in setup scripts."""
  4. __revision__ = "$Id$"
  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. try:
  140. extensions = []
  141. while 1:
  142. line = file.readline()
  143. if line is None: # eof
  144. break
  145. if _variable_rx.match(line): # VAR=VALUE, handled in first pass
  146. continue
  147. if line[0] == line[-1] == "*":
  148. file.warn("'%s' lines not handled yet" % line)
  149. continue
  150. #print "original line: " + line
  151. line = expand_makefile_vars(line, vars)
  152. words = split_quoted(line)
  153. #print "expanded line: " + line
  154. # NB. this parses a slightly different syntax than the old
  155. # makesetup script: here, there must be exactly one extension per
  156. # line, and it must be the first word of the line. I have no idea
  157. # why the old syntax supported multiple extensions per line, as
  158. # they all wind up being the same.
  159. module = words[0]
  160. ext = Extension(module, [])
  161. append_next_word = None
  162. for word in words[1:]:
  163. if append_next_word is not None:
  164. append_next_word.append(word)
  165. append_next_word = None
  166. continue
  167. suffix = os.path.splitext(word)[1]
  168. switch = word[0:2] ; value = word[2:]
  169. if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
  170. # hmm, should we do something about C vs. C++ sources?
  171. # or leave it up to the CCompiler implementation to
  172. # worry about?
  173. ext.sources.append(word)
  174. elif switch == "-I":
  175. ext.include_dirs.append(value)
  176. elif switch == "-D":
  177. equals = string.find(value, "=")
  178. if equals == -1: # bare "-DFOO" -- no value
  179. ext.define_macros.append((value, None))
  180. else: # "-DFOO=blah"
  181. ext.define_macros.append((value[0:equals],
  182. value[equals+2:]))
  183. elif switch == "-U":
  184. ext.undef_macros.append(value)
  185. elif switch == "-C": # only here 'cause makesetup has it!
  186. ext.extra_compile_args.append(word)
  187. elif switch == "-l":
  188. ext.libraries.append(value)
  189. elif switch == "-L":
  190. ext.library_dirs.append(value)
  191. elif switch == "-R":
  192. ext.runtime_library_dirs.append(value)
  193. elif word == "-rpath":
  194. append_next_word = ext.runtime_library_dirs
  195. elif word == "-Xlinker":
  196. append_next_word = ext.extra_link_args
  197. elif word == "-Xcompiler":
  198. append_next_word = ext.extra_compile_args
  199. elif switch == "-u":
  200. ext.extra_link_args.append(word)
  201. if not value:
  202. append_next_word = ext.extra_link_args
  203. elif word == "-Xcompiler":
  204. append_next_word = ext.extra_compile_args
  205. elif switch == "-u":
  206. ext.extra_link_args.append(word)
  207. if not value:
  208. append_next_word = ext.extra_link_args
  209. elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
  210. # NB. a really faithful emulation of makesetup would
  211. # append a .o file to extra_objects only if it
  212. # had a slash in it; otherwise, it would s/.o/.c/
  213. # and append it to sources. Hmmmm.
  214. ext.extra_objects.append(word)
  215. else:
  216. file.warn("unrecognized argument '%s'" % word)
  217. extensions.append(ext)
  218. finally:
  219. file.close()
  220. #print "module:", module
  221. #print "source files:", source_files
  222. #print "cpp args:", cpp_args
  223. #print "lib args:", library_args
  224. #extensions[module] = { 'sources': source_files,
  225. # 'cpp_args': cpp_args,
  226. # 'lib_args': library_args }
  227. return extensions
  228. # read_setup_file ()