Back to index

python3.2  3.2.2
Classes | Functions
distutils.extension Namespace Reference

Classes

class  Extension

Functions

def read_setup_file

Detailed Description

distutils.extension

Provides the Extension class, used to describe C/C++ extension
modules in setup scripts.

Function Documentation

Reads a Setup file and returns Extension instances.

Definition at line 134 of file extension.py.

00134 
00135 def read_setup_file(filename):
00136     """Reads a Setup file and returns Extension instances."""
00137     from distutils.sysconfig import (parse_makefile, expand_makefile_vars,
00138                                      _variable_rx)
00139 
00140     from distutils.text_file import TextFile
00141     from distutils.util import split_quoted
00142 
00143     # First pass over the file to gather "VAR = VALUE" assignments.
00144     vars = parse_makefile(filename)
00145 
00146     # Second pass to gobble up the real content: lines of the form
00147     #   <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
00148     file = TextFile(filename,
00149                     strip_comments=1, skip_blanks=1, join_lines=1,
00150                     lstrip_ws=1, rstrip_ws=1)
00151     try:
00152         extensions = []
00153 
00154         while True:
00155             line = file.readline()
00156             if line is None:                # eof
00157                 break
00158             if _variable_rx.match(line):    # VAR=VALUE, handled in first pass
00159                 continue
00160 
00161             if line[0] == line[-1] == "*":
00162                 file.warn("'%s' lines not handled yet" % line)
00163                 continue
00164 
00165             line = expand_makefile_vars(line, vars)
00166             words = split_quoted(line)
00167 
00168             # NB. this parses a slightly different syntax than the old
00169             # makesetup script: here, there must be exactly one extension per
00170             # line, and it must be the first word of the line.  I have no idea
00171             # why the old syntax supported multiple extensions per line, as
00172             # they all wind up being the same.
00173 
00174             module = words[0]
00175             ext = Extension(module, [])
00176             append_next_word = None
00177 
00178             for word in words[1:]:
00179                 if append_next_word is not None:
00180                     append_next_word.append(word)
00181                     append_next_word = None
00182                     continue
00183 
00184                 suffix = os.path.splitext(word)[1]
00185                 switch = word[0:2] ; value = word[2:]
00186 
00187                 if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
00188                     # hmm, should we do something about C vs. C++ sources?
00189                     # or leave it up to the CCompiler implementation to
00190                     # worry about?
00191                     ext.sources.append(word)
00192                 elif switch == "-I":
00193                     ext.include_dirs.append(value)
00194                 elif switch == "-D":
00195                     equals = value.find("=")
00196                     if equals == -1:        # bare "-DFOO" -- no value
00197                         ext.define_macros.append((value, None))
00198                     else:                   # "-DFOO=blah"
00199                         ext.define_macros.append((value[0:equals],
00200                                                   value[equals+2:]))
00201                 elif switch == "-U":
00202                     ext.undef_macros.append(value)
00203                 elif switch == "-C":        # only here 'cause makesetup has it!
00204                     ext.extra_compile_args.append(word)
00205                 elif switch == "-l":
00206                     ext.libraries.append(value)
00207                 elif switch == "-L":
00208                     ext.library_dirs.append(value)
00209                 elif switch == "-R":
00210                     ext.runtime_library_dirs.append(value)
00211                 elif word == "-rpath":
00212                     append_next_word = ext.runtime_library_dirs
00213                 elif word == "-Xlinker":
00214                     append_next_word = ext.extra_link_args
00215                 elif word == "-Xcompiler":
00216                     append_next_word = ext.extra_compile_args
00217                 elif switch == "-u":
00218                     ext.extra_link_args.append(word)
00219                     if not value:
00220                         append_next_word = ext.extra_link_args
00221                 elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
00222                     # NB. a really faithful emulation of makesetup would
00223                     # append a .o file to extra_objects only if it
00224                     # had a slash in it; otherwise, it would s/.o/.c/
00225                     # and append it to sources.  Hmmmm.
00226                     ext.extra_objects.append(word)
00227                 else:
00228                     file.warn("unrecognized argument '%s'" % word)
00229 
00230             extensions.append(ext)
00231     finally:
00232         file.close()
00233 
00234     return extensions

Here is the call graph for this function:

Here is the caller graph for this function: