| 1 |
"""distutils.extension |
| 2 |
|
| 3 |
Provides the Extension class, used to describe C/C++ extension |
| 4 |
modules in setup scripts.""" |
| 5 |
|
| 6 |
__revision__ = "$Id$" |
| 7 |
|
| 8 |
import os, string, sys |
| 9 |
from types import * |
| 10 |
|
| 11 |
try: |
| 12 |
import warnings |
| 13 |
except ImportError: |
| 14 |
warnings = None |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class Extension: |
| 27 |
"""Just a collection of attributes that describes an extension |
| 28 |
module and everything needed to build it (hopefully in a portable |
| 29 |
way, but there are hooks that let you be as unportable as you need). |
| 30 |
|
| 31 |
Instance attributes: |
| 32 |
name : string |
| 33 |
the full name of the extension, including any packages -- ie. |
| 34 |
*not* a filename or pathname, but Python dotted name |
| 35 |
sources : [string] |
| 36 |
list of source filenames, relative to the distribution root |
| 37 |
(where the setup script lives), in Unix form (slash-separated) |
| 38 |
for portability. Source files may be C, C++, SWIG (.i), |
| 39 |
platform-specific resource files, or whatever else is recognized |
| 40 |
by the "build_ext" command as source for a Python extension. |
| 41 |
include_dirs : [string] |
| 42 |
list of directories to search for C/C++ header files (in Unix |
| 43 |
form for portability) |
| 44 |
define_macros : [(name : string, value : string|None)] |
| 45 |
list of macros to define; each macro is defined using a 2-tuple, |
| 46 |
where 'value' is either the string to define it to or None to |
| 47 |
define it without a particular value (equivalent of "#define |
| 48 |
FOO" in source or -DFOO on Unix C compiler command line) |
| 49 |
undef_macros : [string] |
| 50 |
list of macros to undefine explicitly |
| 51 |
library_dirs : [string] |
| 52 |
list of directories to search for C/C++ libraries at link time |
| 53 |
libraries : [string] |
| 54 |
list of library names (not filenames or paths) to link against |
| 55 |
runtime_library_dirs : [string] |
| 56 |
list of directories to search for C/C++ libraries at run time |
| 57 |
(for shared extensions, this is when the extension is loaded) |
| 58 |
extra_objects : [string] |
| 59 |
list of extra files to link with (eg. object files not implied |
| 60 |
by 'sources', static library that must be explicitly specified, |
| 61 |
binary resource files, etc.) |
| 62 |
extra_compile_args : [string] |
| 63 |
any extra platform- and compiler-specific information to use |
| 64 |
when compiling the source files in 'sources'. For platforms and |
| 65 |
compilers where "command line" makes sense, this is typically a |
| 66 |
list of command-line arguments, but for other platforms it could |
| 67 |
be anything. |
| 68 |
extra_link_args : [string] |
| 69 |
any extra platform- and compiler-specific information to use |
| 70 |
when linking object files together to create the extension (or |
| 71 |
to create a new static Python interpreter). Similar |
| 72 |
interpretation as for 'extra_compile_args'. |
| 73 |
export_symbols : [string] |
| 74 |
list of symbols to be exported from a shared extension. Not |
| 75 |
used on all platforms, and not generally necessary for Python |
| 76 |
extensions, which typically export exactly one symbol: "init" + |
| 77 |
extension_name. |
| 78 |
swig_opts : [string] |
| 79 |
any extra options to pass to SWIG if a source file has the .i |
| 80 |
extension. |
| 81 |
depends : [string] |
| 82 |
list of files that the extension depends on |
| 83 |
language : string |
| 84 |
extension language (i.e. "c", "c++", "objc"). Will be detected |
| 85 |
from the source extensions if not provided. |
| 86 |
""" |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
def __init__ (self, name, sources, |
| 91 |
include_dirs=None, |
| 92 |
define_macros=None, |
| 93 |
undef_macros=None, |
| 94 |
library_dirs=None, |
| 95 |
libraries=None, |
| 96 |
runtime_library_dirs=None, |
| 97 |
extra_objects=None, |
| 98 |
extra_compile_args=None, |
| 99 |
extra_link_args=None, |
| 100 |
export_symbols=None, |
| 101 |
swig_opts = None, |
| 102 |
depends=None, |
| 103 |
language=None, |
| 104 |
**kw |
| 105 |
): |
| 106 |
assert type(name) is StringType, "'name' must be a string" |
| 107 |
assert (type(sources) is ListType and |
| 108 |
map(type, sources) == [StringType]*len(sources)), \ |
| 109 |
"'sources' must be a list of strings" |
| 110 |
|
| 111 |
self.name = name |
| 112 |
self.sources = sources |
| 113 |
self.include_dirs = include_dirs or [] |
| 114 |
self.define_macros = define_macros or [] |
| 115 |
self.undef_macros = undef_macros or [] |
| 116 |
self.library_dirs = library_dirs or [] |
| 117 |
self.libraries = libraries or [] |
| 118 |
self.runtime_library_dirs = runtime_library_dirs or [] |
| 119 |
self.extra_objects = extra_objects or [] |
| 120 |
self.extra_compile_args = extra_compile_args or [] |
| 121 |
self.extra_link_args = extra_link_args or [] |
| 122 |
self.export_symbols = export_symbols or [] |
| 123 |
self.swig_opts = swig_opts or [] |
| 124 |
self.depends = depends or [] |
| 125 |
self.language = language |
| 126 |
|
| 127 |
|
| 128 |
if len(kw): |
| 129 |
L = kw.keys() ; L.sort() |
| 130 |
L = map(repr, L) |
| 131 |
msg = "Unknown Extension options: " + string.join(L, ', ') |
| 132 |
if warnings is not None: |
| 133 |
warnings.warn(msg) |
| 134 |
else: |
| 135 |
sys.stderr.write(msg + '\n') |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
def read_setup_file (filename): |
| 140 |
from distutils.sysconfig import \ |
| 141 |
parse_makefile, expand_makefile_vars, _variable_rx |
| 142 |
from distutils.text_file import TextFile |
| 143 |
from distutils.util import split_quoted |
| 144 |
|
| 145 |
|
| 146 |
vars = parse_makefile(filename) |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
file = TextFile(filename, |
| 151 |
strip_comments=1, skip_blanks=1, join_lines=1, |
| 152 |
lstrip_ws=1, rstrip_ws=1) |
| 153 |
try: |
| 154 |
extensions = [] |
| 155 |
|
| 156 |
while 1: |
| 157 |
line = file.readline() |
| 158 |
if line is None: |
| 159 |
break |
| 160 |
if _variable_rx.match(line): |
| 161 |
continue |
| 162 |
|
| 163 |
if line[0] == line[-1] == "*": |
| 164 |
file.warn("'%s' lines not handled yet" % line) |
| 165 |
continue |
| 166 |
|
| 167 |
|
| 168 |
line = expand_makefile_vars(line, vars) |
| 169 |
words = split_quoted(line) |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
module = words[0] |
| 179 |
ext = Extension(module, []) |
| 180 |
append_next_word = None |
| 181 |
|
| 182 |
for word in words[1:]: |
| 183 |
if append_next_word is not None: |
| 184 |
append_next_word.append(word) |
| 185 |
append_next_word = None |
| 186 |
continue |
| 187 |
|
| 188 |
suffix = os.path.splitext(word)[1] |
| 189 |
switch = word[0:2] ; value = word[2:] |
| 190 |
|
| 191 |
if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"): |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
ext.sources.append(word) |
| 196 |
elif switch == "-I": |
| 197 |
ext.include_dirs.append(value) |
| 198 |
elif switch == "-D": |
| 199 |
equals = string.find(value, "=") |
| 200 |
if equals == -1: |
| 201 |
ext.define_macros.append((value, None)) |
| 202 |
else: |
| 203 |
ext.define_macros.append((value[0:equals], |
| 204 |
value[equals+2:])) |
| 205 |
elif switch == "-U": |
| 206 |
ext.undef_macros.append(value) |
| 207 |
elif switch == "-C": |
| 208 |
ext.extra_compile_args.append(word) |
| 209 |
elif switch == "-l": |
| 210 |
ext.libraries.append(value) |
| 211 |
elif switch == "-L": |
| 212 |
ext.library_dirs.append(value) |
| 213 |
elif switch == "-R": |
| 214 |
ext.runtime_library_dirs.append(value) |
| 215 |
elif word == "-rpath": |
| 216 |
append_next_word = ext.runtime_library_dirs |
| 217 |
elif word == "-Xlinker": |
| 218 |
append_next_word = ext.extra_link_args |
| 219 |
elif word == "-Xcompiler": |
| 220 |
append_next_word = ext.extra_compile_args |
| 221 |
elif switch == "-u": |
| 222 |
ext.extra_link_args.append(word) |
| 223 |
if not value: |
| 224 |
append_next_word = ext.extra_link_args |
| 225 |
elif word == "-Xcompiler": |
| 226 |
append_next_word = ext.extra_compile_args |
| 227 |
elif switch == "-u": |
| 228 |
ext.extra_link_args.append(word) |
| 229 |
if not value: |
| 230 |
append_next_word = ext.extra_link_args |
| 231 |
elif suffix in (".a", ".so", ".sl", ".o", ".dylib"): |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
ext.extra_objects.append(word) |
| 237 |
else: |
| 238 |
file.warn("unrecognized argument '%s'" % word) |
| 239 |
|
| 240 |
extensions.append(ext) |
| 241 |
finally: |
| 242 |
file.close() |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
return extensions |
| 254 |
|
| 255 |
|