home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / EXTENSION.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  9.1 KB  |  218 lines

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