PyXR

c:\python24\lib \ distutils \ extension.py



0001 """distutils.extension
0002 
0003 Provides the Extension class, used to describe C/C++ extension
0004 modules in setup scripts."""
0005 
0006 __revision__ = "$Id: extension.py,v 1.19 2004/10/14 10:02:08 anthonybaxter Exp $"
0007 
0008 import os, string, sys
0009 from types import *
0010 
0011 try:
0012     import warnings
0013 except ImportError:
0014     warnings = None
0015 
0016 # This class is really only used by the "build_ext" command, so it might
0017 # make sense to put it in distutils.command.build_ext.  However, that
0018 # module is already big enough, and I want to make this class a bit more
0019 # complex to simplify some common cases ("foo" module in "foo.c") and do
0020 # better error-checking ("foo.c" actually exists).
0021 #
0022 # Also, putting this in build_ext.py means every setup script would have to
0023 # import that large-ish module (indirectly, through distutils.core) in
0024 # order to do anything.
0025 
0026 class Extension:
0027     """Just a collection of attributes that describes an extension
0028     module and everything needed to build it (hopefully in a portable
0029     way, but there are hooks that let you be as unportable as you need).
0030 
0031     Instance attributes:
0032       name : string
0033         the full name of the extension, including any packages -- ie.
0034         *not* a filename or pathname, but Python dotted name
0035       sources : [string]
0036         list of source filenames, relative to the distribution root
0037         (where the setup script lives), in Unix form (slash-separated)
0038         for portability.  Source files may be C, C++, SWIG (.i),
0039         platform-specific resource files, or whatever else is recognized
0040         by the "build_ext" command as source for a Python extension.
0041       include_dirs : [string]
0042         list of directories to search for C/C++ header files (in Unix
0043         form for portability)
0044       define_macros : [(name : string, value : string|None)]
0045         list of macros to define; each macro is defined using a 2-tuple,
0046         where 'value' is either the string to define it to or None to
0047         define it without a particular value (equivalent of "#define
0048         FOO" in source or -DFOO on Unix C compiler command line)
0049       undef_macros : [string]
0050         list of macros to undefine explicitly
0051       library_dirs : [string]
0052         list of directories to search for C/C++ libraries at link time
0053       libraries : [string]
0054         list of library names (not filenames or paths) to link against
0055       runtime_library_dirs : [string]
0056         list of directories to search for C/C++ libraries at run time
0057         (for shared extensions, this is when the extension is loaded)
0058       extra_objects : [string]
0059         list of extra files to link with (eg. object files not implied
0060         by 'sources', static library that must be explicitly specified,
0061         binary resource files, etc.)
0062       extra_compile_args : [string]
0063         any extra platform- and compiler-specific information to use
0064         when compiling the source files in 'sources'.  For platforms and
0065         compilers where "command line" makes sense, this is typically a
0066         list of command-line arguments, but for other platforms it could
0067         be anything.
0068       extra_link_args : [string]
0069         any extra platform- and compiler-specific information to use
0070         when linking object files together to create the extension (or
0071         to create a new static Python interpreter).  Similar
0072         interpretation as for 'extra_compile_args'.
0073       export_symbols : [string]
0074         list of symbols to be exported from a shared extension.  Not
0075         used on all platforms, and not generally necessary for Python
0076         extensions, which typically export exactly one symbol: "init" +
0077         extension_name.
0078       swig_opts : [string]
0079         any extra options to pass to SWIG if a source file has the .i
0080         extension.
0081       depends : [string]
0082         list of files that the extension depends on
0083       language : string
0084         extension language (i.e. "c", "c++", "objc"). Will be detected
0085         from the source extensions if not provided.
0086     """
0087 
0088     # When adding arguments to this constructor, be sure to update
0089     # setup_keywords in core.py.
0090     def __init__ (self, name, sources,
0091                   include_dirs=None,
0092                   define_macros=None,
0093                   undef_macros=None,
0094                   library_dirs=None,
0095                   libraries=None,
0096                   runtime_library_dirs=None,
0097                   extra_objects=None,
0098                   extra_compile_args=None,
0099                   extra_link_args=None,
0100                   export_symbols=None,
0101                   swig_opts = None,
0102                   depends=None,
0103                   language=None,
0104                   **kw                      # To catch unknown keywords
0105                  ):
0106         assert type(name) is StringType, "'name' must be a string"
0107         assert (type(sources) is ListType and
0108                 map(type, sources) == [StringType]*len(sources)), \
0109                 "'sources' must be a list of strings"
0110 
0111         self.name = name
0112         self.sources = sources
0113         self.include_dirs = include_dirs or []
0114         self.define_macros = define_macros or []
0115         self.undef_macros = undef_macros or []
0116         self.library_dirs = library_dirs or []
0117         self.libraries = libraries or []
0118         self.runtime_library_dirs = runtime_library_dirs or []
0119         self.extra_objects = extra_objects or []
0120         self.extra_compile_args = extra_compile_args or []
0121         self.extra_link_args = extra_link_args or []
0122         self.export_symbols = export_symbols or []
0123         self.swig_opts = swig_opts or []
0124         self.depends = depends or []
0125         self.language = language
0126 
0127         # If there are unknown keyword options, warn about them
0128         if len(kw):
0129             L = kw.keys() ; L.sort()
0130             L = map(repr, L)
0131             msg = "Unknown Extension options: " + string.join(L, ', ')
0132             if warnings is not None:
0133                 warnings.warn(msg)
0134             else:
0135                 sys.stderr.write(msg + '\n')
0136 # class Extension
0137 
0138 
0139 def read_setup_file (filename):
0140     from distutils.sysconfig import \
0141          parse_makefile, expand_makefile_vars, _variable_rx
0142     from distutils.text_file import TextFile
0143     from distutils.util import split_quoted
0144 
0145     # First pass over the file to gather "VAR = VALUE" assignments.
0146     vars = parse_makefile(filename)
0147 
0148     # Second pass to gobble up the real content: lines of the form
0149     #   <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
0150     file = TextFile(filename,
0151                     strip_comments=1, skip_blanks=1, join_lines=1,
0152                     lstrip_ws=1, rstrip_ws=1)
0153     extensions = []
0154 
0155     while 1:
0156         line = file.readline()
0157         if line is None:                # eof
0158             break
0159         if _variable_rx.match(line):    # VAR=VALUE, handled in first pass
0160             continue
0161 
0162         if line[0] == line[-1] == "*":
0163             file.warn("'%s' lines not handled yet" % line)
0164             continue
0165 
0166         #print "original line: " + line
0167         line = expand_makefile_vars(line, vars)
0168         words = split_quoted(line)
0169         #print "expanded line: " + line
0170 
0171         # NB. this parses a slightly different syntax than the old
0172         # makesetup script: here, there must be exactly one extension per
0173         # line, and it must be the first word of the line.  I have no idea
0174         # why the old syntax supported multiple extensions per line, as
0175         # they all wind up being the same.
0176 
0177         module = words[0]
0178         ext = Extension(module, [])
0179         append_next_word = None
0180 
0181         for word in words[1:]:
0182             if append_next_word is not None:
0183                 append_next_word.append(word)
0184                 append_next_word = None
0185                 continue
0186 
0187             suffix = os.path.splitext(word)[1]
0188             switch = word[0:2] ; value = word[2:]
0189 
0190             if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
0191                 # hmm, should we do something about C vs. C++ sources?
0192                 # or leave it up to the CCompiler implementation to
0193                 # worry about?
0194                 ext.sources.append(word)
0195             elif switch == "-I":
0196                 ext.include_dirs.append(value)
0197             elif switch == "-D":
0198                 equals = string.find(value, "=")
0199                 if equals == -1:        # bare "-DFOO" -- no value
0200                     ext.define_macros.append((value, None))
0201                 else:                   # "-DFOO=blah"
0202                     ext.define_macros.append((value[0:equals],
0203                                               value[equals+2:]))
0204             elif switch == "-U":
0205                 ext.undef_macros.append(value)
0206             elif switch == "-C":        # only here 'cause makesetup has it!
0207                 ext.extra_compile_args.append(word)
0208             elif switch == "-l":
0209                 ext.libraries.append(value)
0210             elif switch == "-L":
0211                 ext.library_dirs.append(value)
0212             elif switch == "-R":
0213                 ext.runtime_library_dirs.append(value)
0214             elif word == "-rpath":
0215                 append_next_word = ext.runtime_library_dirs
0216             elif word == "-Xlinker":
0217                 append_next_word = ext.extra_link_args
0218             elif word == "-Xcompiler":
0219                 append_next_word = ext.extra_compile_args
0220             elif switch == "-u":
0221                 ext.extra_link_args.append(word)
0222                 if not value:
0223                     append_next_word = ext.extra_link_args
0224             elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
0225                 # NB. a really faithful emulation of makesetup would
0226                 # append a .o file to extra_objects only if it
0227                 # had a slash in it; otherwise, it would s/.o/.c/
0228                 # and append it to sources.  Hmmmm.
0229                 ext.extra_objects.append(word)
0230             else:
0231                 file.warn("unrecognized argument '%s'" % word)
0232 
0233         extensions.append(ext)
0234 
0235         #print "module:", module
0236         #print "source files:", source_files
0237         #print "cpp args:", cpp_args
0238         #print "lib args:", library_args
0239 
0240         #extensions[module] = { 'sources': source_files,
0241         #                       'cpp_args': cpp_args,
0242         #                       'lib_args': library_args }
0243 
0244     return extensions
0245 
0246 # read_setup_file ()
0247 

Generated by PyXR 0.9.4
SourceForge.net Logo