ยปCore Development>Code coverage>Lib/distutils/extension.py

Python code coverage for Lib/distutils/extension.py

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