1 | n/a | """distutils.ccompiler |
---|
2 | n/a | |
---|
3 | n/a | Contains CCompiler, an abstract base class that defines the interface |
---|
4 | n/a | for the Distutils compiler abstraction model.""" |
---|
5 | n/a | |
---|
6 | n/a | import sys, os, re |
---|
7 | n/a | from distutils.errors import * |
---|
8 | n/a | from distutils.spawn import spawn |
---|
9 | n/a | from distutils.file_util import move_file |
---|
10 | n/a | from distutils.dir_util import mkpath |
---|
11 | n/a | from distutils.dep_util import newer_pairwise, newer_group |
---|
12 | n/a | from distutils.util import split_quoted, execute |
---|
13 | n/a | from distutils import log |
---|
14 | n/a | |
---|
15 | n/a | class CCompiler: |
---|
16 | n/a | """Abstract base class to define the interface that must be implemented |
---|
17 | n/a | by real compiler classes. Also has some utility methods used by |
---|
18 | n/a | several compiler classes. |
---|
19 | n/a | |
---|
20 | n/a | The basic idea behind a compiler abstraction class is that each |
---|
21 | n/a | instance can be used for all the compile/link steps in building a |
---|
22 | n/a | single project. Thus, attributes common to all of those compile and |
---|
23 | n/a | link steps -- include directories, macros to define, libraries to link |
---|
24 | n/a | against, etc. -- are attributes of the compiler instance. To allow for |
---|
25 | n/a | variability in how individual files are treated, most of those |
---|
26 | n/a | attributes may be varied on a per-compilation or per-link basis. |
---|
27 | n/a | """ |
---|
28 | n/a | |
---|
29 | n/a | # 'compiler_type' is a class attribute that identifies this class. It |
---|
30 | n/a | # keeps code that wants to know what kind of compiler it's dealing with |
---|
31 | n/a | # from having to import all possible compiler classes just to do an |
---|
32 | n/a | # 'isinstance'. In concrete CCompiler subclasses, 'compiler_type' |
---|
33 | n/a | # should really, really be one of the keys of the 'compiler_class' |
---|
34 | n/a | # dictionary (see below -- used by the 'new_compiler()' factory |
---|
35 | n/a | # function) -- authors of new compiler interface classes are |
---|
36 | n/a | # responsible for updating 'compiler_class'! |
---|
37 | n/a | compiler_type = None |
---|
38 | n/a | |
---|
39 | n/a | # XXX things not handled by this compiler abstraction model: |
---|
40 | n/a | # * client can't provide additional options for a compiler, |
---|
41 | n/a | # e.g. warning, optimization, debugging flags. Perhaps this |
---|
42 | n/a | # should be the domain of concrete compiler abstraction classes |
---|
43 | n/a | # (UnixCCompiler, MSVCCompiler, etc.) -- or perhaps the base |
---|
44 | n/a | # class should have methods for the common ones. |
---|
45 | n/a | # * can't completely override the include or library searchg |
---|
46 | n/a | # path, ie. no "cc -I -Idir1 -Idir2" or "cc -L -Ldir1 -Ldir2". |
---|
47 | n/a | # I'm not sure how widely supported this is even by Unix |
---|
48 | n/a | # compilers, much less on other platforms. And I'm even less |
---|
49 | n/a | # sure how useful it is; maybe for cross-compiling, but |
---|
50 | n/a | # support for that is a ways off. (And anyways, cross |
---|
51 | n/a | # compilers probably have a dedicated binary with the |
---|
52 | n/a | # right paths compiled in. I hope.) |
---|
53 | n/a | # * can't do really freaky things with the library list/library |
---|
54 | n/a | # dirs, e.g. "-Ldir1 -lfoo -Ldir2 -lfoo" to link against |
---|
55 | n/a | # different versions of libfoo.a in different locations. I |
---|
56 | n/a | # think this is useless without the ability to null out the |
---|
57 | n/a | # library search path anyways. |
---|
58 | n/a | |
---|
59 | n/a | |
---|
60 | n/a | # Subclasses that rely on the standard filename generation methods |
---|
61 | n/a | # implemented below should override these; see the comment near |
---|
62 | n/a | # those methods ('object_filenames()' et. al.) for details: |
---|
63 | n/a | src_extensions = None # list of strings |
---|
64 | n/a | obj_extension = None # string |
---|
65 | n/a | static_lib_extension = None |
---|
66 | n/a | shared_lib_extension = None # string |
---|
67 | n/a | static_lib_format = None # format string |
---|
68 | n/a | shared_lib_format = None # prob. same as static_lib_format |
---|
69 | n/a | exe_extension = None # string |
---|
70 | n/a | |
---|
71 | n/a | # Default language settings. language_map is used to detect a source |
---|
72 | n/a | # file or Extension target language, checking source filenames. |
---|
73 | n/a | # language_order is used to detect the language precedence, when deciding |
---|
74 | n/a | # what language to use when mixing source types. For example, if some |
---|
75 | n/a | # extension has two files with ".c" extension, and one with ".cpp", it |
---|
76 | n/a | # is still linked as c++. |
---|
77 | n/a | language_map = {".c" : "c", |
---|
78 | n/a | ".cc" : "c++", |
---|
79 | n/a | ".cpp" : "c++", |
---|
80 | n/a | ".cxx" : "c++", |
---|
81 | n/a | ".m" : "objc", |
---|
82 | n/a | } |
---|
83 | n/a | language_order = ["c++", "objc", "c"] |
---|
84 | n/a | |
---|
85 | n/a | def __init__(self, verbose=0, dry_run=0, force=0): |
---|
86 | n/a | self.dry_run = dry_run |
---|
87 | n/a | self.force = force |
---|
88 | n/a | self.verbose = verbose |
---|
89 | n/a | |
---|
90 | n/a | # 'output_dir': a common output directory for object, library, |
---|
91 | n/a | # shared object, and shared library files |
---|
92 | n/a | self.output_dir = None |
---|
93 | n/a | |
---|
94 | n/a | # 'macros': a list of macro definitions (or undefinitions). A |
---|
95 | n/a | # macro definition is a 2-tuple (name, value), where the value is |
---|
96 | n/a | # either a string or None (no explicit value). A macro |
---|
97 | n/a | # undefinition is a 1-tuple (name,). |
---|
98 | n/a | self.macros = [] |
---|
99 | n/a | |
---|
100 | n/a | # 'include_dirs': a list of directories to search for include files |
---|
101 | n/a | self.include_dirs = [] |
---|
102 | n/a | |
---|
103 | n/a | # 'libraries': a list of libraries to include in any link |
---|
104 | n/a | # (library names, not filenames: eg. "foo" not "libfoo.a") |
---|
105 | n/a | self.libraries = [] |
---|
106 | n/a | |
---|
107 | n/a | # 'library_dirs': a list of directories to search for libraries |
---|
108 | n/a | self.library_dirs = [] |
---|
109 | n/a | |
---|
110 | n/a | # 'runtime_library_dirs': a list of directories to search for |
---|
111 | n/a | # shared libraries/objects at runtime |
---|
112 | n/a | self.runtime_library_dirs = [] |
---|
113 | n/a | |
---|
114 | n/a | # 'objects': a list of object files (or similar, such as explicitly |
---|
115 | n/a | # named library files) to include on any link |
---|
116 | n/a | self.objects = [] |
---|
117 | n/a | |
---|
118 | n/a | for key in self.executables.keys(): |
---|
119 | n/a | self.set_executable(key, self.executables[key]) |
---|
120 | n/a | |
---|
121 | n/a | def set_executables(self, **kwargs): |
---|
122 | n/a | """Define the executables (and options for them) that will be run |
---|
123 | n/a | to perform the various stages of compilation. The exact set of |
---|
124 | n/a | executables that may be specified here depends on the compiler |
---|
125 | n/a | class (via the 'executables' class attribute), but most will have: |
---|
126 | n/a | compiler the C/C++ compiler |
---|
127 | n/a | linker_so linker used to create shared objects and libraries |
---|
128 | n/a | linker_exe linker used to create binary executables |
---|
129 | n/a | archiver static library creator |
---|
130 | n/a | |
---|
131 | n/a | On platforms with a command-line (Unix, DOS/Windows), each of these |
---|
132 | n/a | is a string that will be split into executable name and (optional) |
---|
133 | n/a | list of arguments. (Splitting the string is done similarly to how |
---|
134 | n/a | Unix shells operate: words are delimited by spaces, but quotes and |
---|
135 | n/a | backslashes can override this. See |
---|
136 | n/a | 'distutils.util.split_quoted()'.) |
---|
137 | n/a | """ |
---|
138 | n/a | |
---|
139 | n/a | # Note that some CCompiler implementation classes will define class |
---|
140 | n/a | # attributes 'cpp', 'cc', etc. with hard-coded executable names; |
---|
141 | n/a | # this is appropriate when a compiler class is for exactly one |
---|
142 | n/a | # compiler/OS combination (eg. MSVCCompiler). Other compiler |
---|
143 | n/a | # classes (UnixCCompiler, in particular) are driven by information |
---|
144 | n/a | # discovered at run-time, since there are many different ways to do |
---|
145 | n/a | # basically the same things with Unix C compilers. |
---|
146 | n/a | |
---|
147 | n/a | for key in kwargs: |
---|
148 | n/a | if key not in self.executables: |
---|
149 | n/a | raise ValueError("unknown executable '%s' for class %s" % |
---|
150 | n/a | (key, self.__class__.__name__)) |
---|
151 | n/a | self.set_executable(key, kwargs[key]) |
---|
152 | n/a | |
---|
153 | n/a | def set_executable(self, key, value): |
---|
154 | n/a | if isinstance(value, str): |
---|
155 | n/a | setattr(self, key, split_quoted(value)) |
---|
156 | n/a | else: |
---|
157 | n/a | setattr(self, key, value) |
---|
158 | n/a | |
---|
159 | n/a | def _find_macro(self, name): |
---|
160 | n/a | i = 0 |
---|
161 | n/a | for defn in self.macros: |
---|
162 | n/a | if defn[0] == name: |
---|
163 | n/a | return i |
---|
164 | n/a | i += 1 |
---|
165 | n/a | return None |
---|
166 | n/a | |
---|
167 | n/a | def _check_macro_definitions(self, definitions): |
---|
168 | n/a | """Ensures that every element of 'definitions' is a valid macro |
---|
169 | n/a | definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do |
---|
170 | n/a | nothing if all definitions are OK, raise TypeError otherwise. |
---|
171 | n/a | """ |
---|
172 | n/a | for defn in definitions: |
---|
173 | n/a | if not (isinstance(defn, tuple) and |
---|
174 | n/a | (len(defn) in (1, 2) and |
---|
175 | n/a | (isinstance (defn[1], str) or defn[1] is None)) and |
---|
176 | n/a | isinstance (defn[0], str)): |
---|
177 | n/a | raise TypeError(("invalid macro definition '%s': " % defn) + \ |
---|
178 | n/a | "must be tuple (string,), (string, string), or " + \ |
---|
179 | n/a | "(string, None)") |
---|
180 | n/a | |
---|
181 | n/a | |
---|
182 | n/a | # -- Bookkeeping methods ------------------------------------------- |
---|
183 | n/a | |
---|
184 | n/a | def define_macro(self, name, value=None): |
---|
185 | n/a | """Define a preprocessor macro for all compilations driven by this |
---|
186 | n/a | compiler object. The optional parameter 'value' should be a |
---|
187 | n/a | string; if it is not supplied, then the macro will be defined |
---|
188 | n/a | without an explicit value and the exact outcome depends on the |
---|
189 | n/a | compiler used (XXX true? does ANSI say anything about this?) |
---|
190 | n/a | """ |
---|
191 | n/a | # Delete from the list of macro definitions/undefinitions if |
---|
192 | n/a | # already there (so that this one will take precedence). |
---|
193 | n/a | i = self._find_macro (name) |
---|
194 | n/a | if i is not None: |
---|
195 | n/a | del self.macros[i] |
---|
196 | n/a | |
---|
197 | n/a | self.macros.append((name, value)) |
---|
198 | n/a | |
---|
199 | n/a | def undefine_macro(self, name): |
---|
200 | n/a | """Undefine a preprocessor macro for all compilations driven by |
---|
201 | n/a | this compiler object. If the same macro is defined by |
---|
202 | n/a | 'define_macro()' and undefined by 'undefine_macro()' the last call |
---|
203 | n/a | takes precedence (including multiple redefinitions or |
---|
204 | n/a | undefinitions). If the macro is redefined/undefined on a |
---|
205 | n/a | per-compilation basis (ie. in the call to 'compile()'), then that |
---|
206 | n/a | takes precedence. |
---|
207 | n/a | """ |
---|
208 | n/a | # Delete from the list of macro definitions/undefinitions if |
---|
209 | n/a | # already there (so that this one will take precedence). |
---|
210 | n/a | i = self._find_macro (name) |
---|
211 | n/a | if i is not None: |
---|
212 | n/a | del self.macros[i] |
---|
213 | n/a | |
---|
214 | n/a | undefn = (name,) |
---|
215 | n/a | self.macros.append(undefn) |
---|
216 | n/a | |
---|
217 | n/a | def add_include_dir(self, dir): |
---|
218 | n/a | """Add 'dir' to the list of directories that will be searched for |
---|
219 | n/a | header files. The compiler is instructed to search directories in |
---|
220 | n/a | the order in which they are supplied by successive calls to |
---|
221 | n/a | 'add_include_dir()'. |
---|
222 | n/a | """ |
---|
223 | n/a | self.include_dirs.append(dir) |
---|
224 | n/a | |
---|
225 | n/a | def set_include_dirs(self, dirs): |
---|
226 | n/a | """Set the list of directories that will be searched to 'dirs' (a |
---|
227 | n/a | list of strings). Overrides any preceding calls to |
---|
228 | n/a | 'add_include_dir()'; subsequence calls to 'add_include_dir()' add |
---|
229 | n/a | to the list passed to 'set_include_dirs()'. This does not affect |
---|
230 | n/a | any list of standard include directories that the compiler may |
---|
231 | n/a | search by default. |
---|
232 | n/a | """ |
---|
233 | n/a | self.include_dirs = dirs[:] |
---|
234 | n/a | |
---|
235 | n/a | def add_library(self, libname): |
---|
236 | n/a | """Add 'libname' to the list of libraries that will be included in |
---|
237 | n/a | all links driven by this compiler object. Note that 'libname' |
---|
238 | n/a | should *not* be the name of a file containing a library, but the |
---|
239 | n/a | name of the library itself: the actual filename will be inferred by |
---|
240 | n/a | the linker, the compiler, or the compiler class (depending on the |
---|
241 | n/a | platform). |
---|
242 | n/a | |
---|
243 | n/a | The linker will be instructed to link against libraries in the |
---|
244 | n/a | order they were supplied to 'add_library()' and/or |
---|
245 | n/a | 'set_libraries()'. It is perfectly valid to duplicate library |
---|
246 | n/a | names; the linker will be instructed to link against libraries as |
---|
247 | n/a | many times as they are mentioned. |
---|
248 | n/a | """ |
---|
249 | n/a | self.libraries.append(libname) |
---|
250 | n/a | |
---|
251 | n/a | def set_libraries(self, libnames): |
---|
252 | n/a | """Set the list of libraries to be included in all links driven by |
---|
253 | n/a | this compiler object to 'libnames' (a list of strings). This does |
---|
254 | n/a | not affect any standard system libraries that the linker may |
---|
255 | n/a | include by default. |
---|
256 | n/a | """ |
---|
257 | n/a | self.libraries = libnames[:] |
---|
258 | n/a | |
---|
259 | n/a | def add_library_dir(self, dir): |
---|
260 | n/a | """Add 'dir' to the list of directories that will be searched for |
---|
261 | n/a | libraries specified to 'add_library()' and 'set_libraries()'. The |
---|
262 | n/a | linker will be instructed to search for libraries in the order they |
---|
263 | n/a | are supplied to 'add_library_dir()' and/or 'set_library_dirs()'. |
---|
264 | n/a | """ |
---|
265 | n/a | self.library_dirs.append(dir) |
---|
266 | n/a | |
---|
267 | n/a | def set_library_dirs(self, dirs): |
---|
268 | n/a | """Set the list of library search directories to 'dirs' (a list of |
---|
269 | n/a | strings). This does not affect any standard library search path |
---|
270 | n/a | that the linker may search by default. |
---|
271 | n/a | """ |
---|
272 | n/a | self.library_dirs = dirs[:] |
---|
273 | n/a | |
---|
274 | n/a | def add_runtime_library_dir(self, dir): |
---|
275 | n/a | """Add 'dir' to the list of directories that will be searched for |
---|
276 | n/a | shared libraries at runtime. |
---|
277 | n/a | """ |
---|
278 | n/a | self.runtime_library_dirs.append(dir) |
---|
279 | n/a | |
---|
280 | n/a | def set_runtime_library_dirs(self, dirs): |
---|
281 | n/a | """Set the list of directories to search for shared libraries at |
---|
282 | n/a | runtime to 'dirs' (a list of strings). This does not affect any |
---|
283 | n/a | standard search path that the runtime linker may search by |
---|
284 | n/a | default. |
---|
285 | n/a | """ |
---|
286 | n/a | self.runtime_library_dirs = dirs[:] |
---|
287 | n/a | |
---|
288 | n/a | def add_link_object(self, object): |
---|
289 | n/a | """Add 'object' to the list of object files (or analogues, such as |
---|
290 | n/a | explicitly named library files or the output of "resource |
---|
291 | n/a | compilers") to be included in every link driven by this compiler |
---|
292 | n/a | object. |
---|
293 | n/a | """ |
---|
294 | n/a | self.objects.append(object) |
---|
295 | n/a | |
---|
296 | n/a | def set_link_objects(self, objects): |
---|
297 | n/a | """Set the list of object files (or analogues) to be included in |
---|
298 | n/a | every link to 'objects'. This does not affect any standard object |
---|
299 | n/a | files that the linker may include by default (such as system |
---|
300 | n/a | libraries). |
---|
301 | n/a | """ |
---|
302 | n/a | self.objects = objects[:] |
---|
303 | n/a | |
---|
304 | n/a | |
---|
305 | n/a | # -- Private utility methods -------------------------------------- |
---|
306 | n/a | # (here for the convenience of subclasses) |
---|
307 | n/a | |
---|
308 | n/a | # Helper method to prep compiler in subclass compile() methods |
---|
309 | n/a | |
---|
310 | n/a | def _setup_compile(self, outdir, macros, incdirs, sources, depends, |
---|
311 | n/a | extra): |
---|
312 | n/a | """Process arguments and decide which source files to compile.""" |
---|
313 | n/a | if outdir is None: |
---|
314 | n/a | outdir = self.output_dir |
---|
315 | n/a | elif not isinstance(outdir, str): |
---|
316 | n/a | raise TypeError("'output_dir' must be a string or None") |
---|
317 | n/a | |
---|
318 | n/a | if macros is None: |
---|
319 | n/a | macros = self.macros |
---|
320 | n/a | elif isinstance(macros, list): |
---|
321 | n/a | macros = macros + (self.macros or []) |
---|
322 | n/a | else: |
---|
323 | n/a | raise TypeError("'macros' (if supplied) must be a list of tuples") |
---|
324 | n/a | |
---|
325 | n/a | if incdirs is None: |
---|
326 | n/a | incdirs = self.include_dirs |
---|
327 | n/a | elif isinstance(incdirs, (list, tuple)): |
---|
328 | n/a | incdirs = list(incdirs) + (self.include_dirs or []) |
---|
329 | n/a | else: |
---|
330 | n/a | raise TypeError( |
---|
331 | n/a | "'include_dirs' (if supplied) must be a list of strings") |
---|
332 | n/a | |
---|
333 | n/a | if extra is None: |
---|
334 | n/a | extra = [] |
---|
335 | n/a | |
---|
336 | n/a | # Get the list of expected output (object) files |
---|
337 | n/a | objects = self.object_filenames(sources, strip_dir=0, |
---|
338 | n/a | output_dir=outdir) |
---|
339 | n/a | assert len(objects) == len(sources) |
---|
340 | n/a | |
---|
341 | n/a | pp_opts = gen_preprocess_options(macros, incdirs) |
---|
342 | n/a | |
---|
343 | n/a | build = {} |
---|
344 | n/a | for i in range(len(sources)): |
---|
345 | n/a | src = sources[i] |
---|
346 | n/a | obj = objects[i] |
---|
347 | n/a | ext = os.path.splitext(src)[1] |
---|
348 | n/a | self.mkpath(os.path.dirname(obj)) |
---|
349 | n/a | build[obj] = (src, ext) |
---|
350 | n/a | |
---|
351 | n/a | return macros, objects, extra, pp_opts, build |
---|
352 | n/a | |
---|
353 | n/a | def _get_cc_args(self, pp_opts, debug, before): |
---|
354 | n/a | # works for unixccompiler, cygwinccompiler |
---|
355 | n/a | cc_args = pp_opts + ['-c'] |
---|
356 | n/a | if debug: |
---|
357 | n/a | cc_args[:0] = ['-g'] |
---|
358 | n/a | if before: |
---|
359 | n/a | cc_args[:0] = before |
---|
360 | n/a | return cc_args |
---|
361 | n/a | |
---|
362 | n/a | def _fix_compile_args(self, output_dir, macros, include_dirs): |
---|
363 | n/a | """Typecheck and fix-up some of the arguments to the 'compile()' |
---|
364 | n/a | method, and return fixed-up values. Specifically: if 'output_dir' |
---|
365 | n/a | is None, replaces it with 'self.output_dir'; ensures that 'macros' |
---|
366 | n/a | is a list, and augments it with 'self.macros'; ensures that |
---|
367 | n/a | 'include_dirs' is a list, and augments it with 'self.include_dirs'. |
---|
368 | n/a | Guarantees that the returned values are of the correct type, |
---|
369 | n/a | i.e. for 'output_dir' either string or None, and for 'macros' and |
---|
370 | n/a | 'include_dirs' either list or None. |
---|
371 | n/a | """ |
---|
372 | n/a | if output_dir is None: |
---|
373 | n/a | output_dir = self.output_dir |
---|
374 | n/a | elif not isinstance(output_dir, str): |
---|
375 | n/a | raise TypeError("'output_dir' must be a string or None") |
---|
376 | n/a | |
---|
377 | n/a | if macros is None: |
---|
378 | n/a | macros = self.macros |
---|
379 | n/a | elif isinstance(macros, list): |
---|
380 | n/a | macros = macros + (self.macros or []) |
---|
381 | n/a | else: |
---|
382 | n/a | raise TypeError("'macros' (if supplied) must be a list of tuples") |
---|
383 | n/a | |
---|
384 | n/a | if include_dirs is None: |
---|
385 | n/a | include_dirs = self.include_dirs |
---|
386 | n/a | elif isinstance(include_dirs, (list, tuple)): |
---|
387 | n/a | include_dirs = list(include_dirs) + (self.include_dirs or []) |
---|
388 | n/a | else: |
---|
389 | n/a | raise TypeError( |
---|
390 | n/a | "'include_dirs' (if supplied) must be a list of strings") |
---|
391 | n/a | |
---|
392 | n/a | return output_dir, macros, include_dirs |
---|
393 | n/a | |
---|
394 | n/a | def _prep_compile(self, sources, output_dir, depends=None): |
---|
395 | n/a | """Decide which souce files must be recompiled. |
---|
396 | n/a | |
---|
397 | n/a | Determine the list of object files corresponding to 'sources', |
---|
398 | n/a | and figure out which ones really need to be recompiled. |
---|
399 | n/a | Return a list of all object files and a dictionary telling |
---|
400 | n/a | which source files can be skipped. |
---|
401 | n/a | """ |
---|
402 | n/a | # Get the list of expected output (object) files |
---|
403 | n/a | objects = self.object_filenames(sources, output_dir=output_dir) |
---|
404 | n/a | assert len(objects) == len(sources) |
---|
405 | n/a | |
---|
406 | n/a | # Return an empty dict for the "which source files can be skipped" |
---|
407 | n/a | # return value to preserve API compatibility. |
---|
408 | n/a | return objects, {} |
---|
409 | n/a | |
---|
410 | n/a | def _fix_object_args(self, objects, output_dir): |
---|
411 | n/a | """Typecheck and fix up some arguments supplied to various methods. |
---|
412 | n/a | Specifically: ensure that 'objects' is a list; if output_dir is |
---|
413 | n/a | None, replace with self.output_dir. Return fixed versions of |
---|
414 | n/a | 'objects' and 'output_dir'. |
---|
415 | n/a | """ |
---|
416 | n/a | if not isinstance(objects, (list, tuple)): |
---|
417 | n/a | raise TypeError("'objects' must be a list or tuple of strings") |
---|
418 | n/a | objects = list(objects) |
---|
419 | n/a | |
---|
420 | n/a | if output_dir is None: |
---|
421 | n/a | output_dir = self.output_dir |
---|
422 | n/a | elif not isinstance(output_dir, str): |
---|
423 | n/a | raise TypeError("'output_dir' must be a string or None") |
---|
424 | n/a | |
---|
425 | n/a | return (objects, output_dir) |
---|
426 | n/a | |
---|
427 | n/a | def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs): |
---|
428 | n/a | """Typecheck and fix up some of the arguments supplied to the |
---|
429 | n/a | 'link_*' methods. Specifically: ensure that all arguments are |
---|
430 | n/a | lists, and augment them with their permanent versions |
---|
431 | n/a | (eg. 'self.libraries' augments 'libraries'). Return a tuple with |
---|
432 | n/a | fixed versions of all arguments. |
---|
433 | n/a | """ |
---|
434 | n/a | if libraries is None: |
---|
435 | n/a | libraries = self.libraries |
---|
436 | n/a | elif isinstance(libraries, (list, tuple)): |
---|
437 | n/a | libraries = list (libraries) + (self.libraries or []) |
---|
438 | n/a | else: |
---|
439 | n/a | raise TypeError( |
---|
440 | n/a | "'libraries' (if supplied) must be a list of strings") |
---|
441 | n/a | |
---|
442 | n/a | if library_dirs is None: |
---|
443 | n/a | library_dirs = self.library_dirs |
---|
444 | n/a | elif isinstance(library_dirs, (list, tuple)): |
---|
445 | n/a | library_dirs = list (library_dirs) + (self.library_dirs or []) |
---|
446 | n/a | else: |
---|
447 | n/a | raise TypeError( |
---|
448 | n/a | "'library_dirs' (if supplied) must be a list of strings") |
---|
449 | n/a | |
---|
450 | n/a | if runtime_library_dirs is None: |
---|
451 | n/a | runtime_library_dirs = self.runtime_library_dirs |
---|
452 | n/a | elif isinstance(runtime_library_dirs, (list, tuple)): |
---|
453 | n/a | runtime_library_dirs = (list(runtime_library_dirs) + |
---|
454 | n/a | (self.runtime_library_dirs or [])) |
---|
455 | n/a | else: |
---|
456 | n/a | raise TypeError("'runtime_library_dirs' (if supplied) " |
---|
457 | n/a | "must be a list of strings") |
---|
458 | n/a | |
---|
459 | n/a | return (libraries, library_dirs, runtime_library_dirs) |
---|
460 | n/a | |
---|
461 | n/a | def _need_link(self, objects, output_file): |
---|
462 | n/a | """Return true if we need to relink the files listed in 'objects' |
---|
463 | n/a | to recreate 'output_file'. |
---|
464 | n/a | """ |
---|
465 | n/a | if self.force: |
---|
466 | n/a | return True |
---|
467 | n/a | else: |
---|
468 | n/a | if self.dry_run: |
---|
469 | n/a | newer = newer_group (objects, output_file, missing='newer') |
---|
470 | n/a | else: |
---|
471 | n/a | newer = newer_group (objects, output_file) |
---|
472 | n/a | return newer |
---|
473 | n/a | |
---|
474 | n/a | def detect_language(self, sources): |
---|
475 | n/a | """Detect the language of a given file, or list of files. Uses |
---|
476 | n/a | language_map, and language_order to do the job. |
---|
477 | n/a | """ |
---|
478 | n/a | if not isinstance(sources, list): |
---|
479 | n/a | sources = [sources] |
---|
480 | n/a | lang = None |
---|
481 | n/a | index = len(self.language_order) |
---|
482 | n/a | for source in sources: |
---|
483 | n/a | base, ext = os.path.splitext(source) |
---|
484 | n/a | extlang = self.language_map.get(ext) |
---|
485 | n/a | try: |
---|
486 | n/a | extindex = self.language_order.index(extlang) |
---|
487 | n/a | if extindex < index: |
---|
488 | n/a | lang = extlang |
---|
489 | n/a | index = extindex |
---|
490 | n/a | except ValueError: |
---|
491 | n/a | pass |
---|
492 | n/a | return lang |
---|
493 | n/a | |
---|
494 | n/a | |
---|
495 | n/a | # -- Worker methods ------------------------------------------------ |
---|
496 | n/a | # (must be implemented by subclasses) |
---|
497 | n/a | |
---|
498 | n/a | def preprocess(self, source, output_file=None, macros=None, |
---|
499 | n/a | include_dirs=None, extra_preargs=None, extra_postargs=None): |
---|
500 | n/a | """Preprocess a single C/C++ source file, named in 'source'. |
---|
501 | n/a | Output will be written to file named 'output_file', or stdout if |
---|
502 | n/a | 'output_file' not supplied. 'macros' is a list of macro |
---|
503 | n/a | definitions as for 'compile()', which will augment the macros set |
---|
504 | n/a | with 'define_macro()' and 'undefine_macro()'. 'include_dirs' is a |
---|
505 | n/a | list of directory names that will be added to the default list. |
---|
506 | n/a | |
---|
507 | n/a | Raises PreprocessError on failure. |
---|
508 | n/a | """ |
---|
509 | n/a | pass |
---|
510 | n/a | |
---|
511 | n/a | def compile(self, sources, output_dir=None, macros=None, |
---|
512 | n/a | include_dirs=None, debug=0, extra_preargs=None, |
---|
513 | n/a | extra_postargs=None, depends=None): |
---|
514 | n/a | """Compile one or more source files. |
---|
515 | n/a | |
---|
516 | n/a | 'sources' must be a list of filenames, most likely C/C++ |
---|
517 | n/a | files, but in reality anything that can be handled by a |
---|
518 | n/a | particular compiler and compiler class (eg. MSVCCompiler can |
---|
519 | n/a | handle resource files in 'sources'). Return a list of object |
---|
520 | n/a | filenames, one per source filename in 'sources'. Depending on |
---|
521 | n/a | the implementation, not all source files will necessarily be |
---|
522 | n/a | compiled, but all corresponding object filenames will be |
---|
523 | n/a | returned. |
---|
524 | n/a | |
---|
525 | n/a | If 'output_dir' is given, object files will be put under it, while |
---|
526 | n/a | retaining their original path component. That is, "foo/bar.c" |
---|
527 | n/a | normally compiles to "foo/bar.o" (for a Unix implementation); if |
---|
528 | n/a | 'output_dir' is "build", then it would compile to |
---|
529 | n/a | "build/foo/bar.o". |
---|
530 | n/a | |
---|
531 | n/a | 'macros', if given, must be a list of macro definitions. A macro |
---|
532 | n/a | definition is either a (name, value) 2-tuple or a (name,) 1-tuple. |
---|
533 | n/a | The former defines a macro; if the value is None, the macro is |
---|
534 | n/a | defined without an explicit value. The 1-tuple case undefines a |
---|
535 | n/a | macro. Later definitions/redefinitions/ undefinitions take |
---|
536 | n/a | precedence. |
---|
537 | n/a | |
---|
538 | n/a | 'include_dirs', if given, must be a list of strings, the |
---|
539 | n/a | directories to add to the default include file search path for this |
---|
540 | n/a | compilation only. |
---|
541 | n/a | |
---|
542 | n/a | 'debug' is a boolean; if true, the compiler will be instructed to |
---|
543 | n/a | output debug symbols in (or alongside) the object file(s). |
---|
544 | n/a | |
---|
545 | n/a | 'extra_preargs' and 'extra_postargs' are implementation- dependent. |
---|
546 | n/a | On platforms that have the notion of a command-line (e.g. Unix, |
---|
547 | n/a | DOS/Windows), they are most likely lists of strings: extra |
---|
548 | n/a | command-line arguments to prepand/append to the compiler command |
---|
549 | n/a | line. On other platforms, consult the implementation class |
---|
550 | n/a | documentation. In any event, they are intended as an escape hatch |
---|
551 | n/a | for those occasions when the abstract compiler framework doesn't |
---|
552 | n/a | cut the mustard. |
---|
553 | n/a | |
---|
554 | n/a | 'depends', if given, is a list of filenames that all targets |
---|
555 | n/a | depend on. If a source file is older than any file in |
---|
556 | n/a | depends, then the source file will be recompiled. This |
---|
557 | n/a | supports dependency tracking, but only at a coarse |
---|
558 | n/a | granularity. |
---|
559 | n/a | |
---|
560 | n/a | Raises CompileError on failure. |
---|
561 | n/a | """ |
---|
562 | n/a | # A concrete compiler class can either override this method |
---|
563 | n/a | # entirely or implement _compile(). |
---|
564 | n/a | macros, objects, extra_postargs, pp_opts, build = \ |
---|
565 | n/a | self._setup_compile(output_dir, macros, include_dirs, sources, |
---|
566 | n/a | depends, extra_postargs) |
---|
567 | n/a | cc_args = self._get_cc_args(pp_opts, debug, extra_preargs) |
---|
568 | n/a | |
---|
569 | n/a | for obj in objects: |
---|
570 | n/a | try: |
---|
571 | n/a | src, ext = build[obj] |
---|
572 | n/a | except KeyError: |
---|
573 | n/a | continue |
---|
574 | n/a | self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) |
---|
575 | n/a | |
---|
576 | n/a | # Return *all* object filenames, not just the ones we just built. |
---|
577 | n/a | return objects |
---|
578 | n/a | |
---|
579 | n/a | def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): |
---|
580 | n/a | """Compile 'src' to product 'obj'.""" |
---|
581 | n/a | # A concrete compiler class that does not override compile() |
---|
582 | n/a | # should implement _compile(). |
---|
583 | n/a | pass |
---|
584 | n/a | |
---|
585 | n/a | def create_static_lib(self, objects, output_libname, output_dir=None, |
---|
586 | n/a | debug=0, target_lang=None): |
---|
587 | n/a | """Link a bunch of stuff together to create a static library file. |
---|
588 | n/a | The "bunch of stuff" consists of the list of object files supplied |
---|
589 | n/a | as 'objects', the extra object files supplied to |
---|
590 | n/a | 'add_link_object()' and/or 'set_link_objects()', the libraries |
---|
591 | n/a | supplied to 'add_library()' and/or 'set_libraries()', and the |
---|
592 | n/a | libraries supplied as 'libraries' (if any). |
---|
593 | n/a | |
---|
594 | n/a | 'output_libname' should be a library name, not a filename; the |
---|
595 | n/a | filename will be inferred from the library name. 'output_dir' is |
---|
596 | n/a | the directory where the library file will be put. |
---|
597 | n/a | |
---|
598 | n/a | 'debug' is a boolean; if true, debugging information will be |
---|
599 | n/a | included in the library (note that on most platforms, it is the |
---|
600 | n/a | compile step where this matters: the 'debug' flag is included here |
---|
601 | n/a | just for consistency). |
---|
602 | n/a | |
---|
603 | n/a | 'target_lang' is the target language for which the given objects |
---|
604 | n/a | are being compiled. This allows specific linkage time treatment of |
---|
605 | n/a | certain languages. |
---|
606 | n/a | |
---|
607 | n/a | Raises LibError on failure. |
---|
608 | n/a | """ |
---|
609 | n/a | pass |
---|
610 | n/a | |
---|
611 | n/a | |
---|
612 | n/a | # values for target_desc parameter in link() |
---|
613 | n/a | SHARED_OBJECT = "shared_object" |
---|
614 | n/a | SHARED_LIBRARY = "shared_library" |
---|
615 | n/a | EXECUTABLE = "executable" |
---|
616 | n/a | |
---|
617 | n/a | def link(self, |
---|
618 | n/a | target_desc, |
---|
619 | n/a | objects, |
---|
620 | n/a | output_filename, |
---|
621 | n/a | output_dir=None, |
---|
622 | n/a | libraries=None, |
---|
623 | n/a | library_dirs=None, |
---|
624 | n/a | runtime_library_dirs=None, |
---|
625 | n/a | export_symbols=None, |
---|
626 | n/a | debug=0, |
---|
627 | n/a | extra_preargs=None, |
---|
628 | n/a | extra_postargs=None, |
---|
629 | n/a | build_temp=None, |
---|
630 | n/a | target_lang=None): |
---|
631 | n/a | """Link a bunch of stuff together to create an executable or |
---|
632 | n/a | shared library file. |
---|
633 | n/a | |
---|
634 | n/a | The "bunch of stuff" consists of the list of object files supplied |
---|
635 | n/a | as 'objects'. 'output_filename' should be a filename. If |
---|
636 | n/a | 'output_dir' is supplied, 'output_filename' is relative to it |
---|
637 | n/a | (i.e. 'output_filename' can provide directory components if |
---|
638 | n/a | needed). |
---|
639 | n/a | |
---|
640 | n/a | 'libraries' is a list of libraries to link against. These are |
---|
641 | n/a | library names, not filenames, since they're translated into |
---|
642 | n/a | filenames in a platform-specific way (eg. "foo" becomes "libfoo.a" |
---|
643 | n/a | on Unix and "foo.lib" on DOS/Windows). However, they can include a |
---|
644 | n/a | directory component, which means the linker will look in that |
---|
645 | n/a | specific directory rather than searching all the normal locations. |
---|
646 | n/a | |
---|
647 | n/a | 'library_dirs', if supplied, should be a list of directories to |
---|
648 | n/a | search for libraries that were specified as bare library names |
---|
649 | n/a | (ie. no directory component). These are on top of the system |
---|
650 | n/a | default and those supplied to 'add_library_dir()' and/or |
---|
651 | n/a | 'set_library_dirs()'. 'runtime_library_dirs' is a list of |
---|
652 | n/a | directories that will be embedded into the shared library and used |
---|
653 | n/a | to search for other shared libraries that *it* depends on at |
---|
654 | n/a | run-time. (This may only be relevant on Unix.) |
---|
655 | n/a | |
---|
656 | n/a | 'export_symbols' is a list of symbols that the shared library will |
---|
657 | n/a | export. (This appears to be relevant only on Windows.) |
---|
658 | n/a | |
---|
659 | n/a | 'debug' is as for 'compile()' and 'create_static_lib()', with the |
---|
660 | n/a | slight distinction that it actually matters on most platforms (as |
---|
661 | n/a | opposed to 'create_static_lib()', which includes a 'debug' flag |
---|
662 | n/a | mostly for form's sake). |
---|
663 | n/a | |
---|
664 | n/a | 'extra_preargs' and 'extra_postargs' are as for 'compile()' (except |
---|
665 | n/a | of course that they supply command-line arguments for the |
---|
666 | n/a | particular linker being used). |
---|
667 | n/a | |
---|
668 | n/a | 'target_lang' is the target language for which the given objects |
---|
669 | n/a | are being compiled. This allows specific linkage time treatment of |
---|
670 | n/a | certain languages. |
---|
671 | n/a | |
---|
672 | n/a | Raises LinkError on failure. |
---|
673 | n/a | """ |
---|
674 | n/a | raise NotImplementedError |
---|
675 | n/a | |
---|
676 | n/a | |
---|
677 | n/a | # Old 'link_*()' methods, rewritten to use the new 'link()' method. |
---|
678 | n/a | |
---|
679 | n/a | def link_shared_lib(self, |
---|
680 | n/a | objects, |
---|
681 | n/a | output_libname, |
---|
682 | n/a | output_dir=None, |
---|
683 | n/a | libraries=None, |
---|
684 | n/a | library_dirs=None, |
---|
685 | n/a | runtime_library_dirs=None, |
---|
686 | n/a | export_symbols=None, |
---|
687 | n/a | debug=0, |
---|
688 | n/a | extra_preargs=None, |
---|
689 | n/a | extra_postargs=None, |
---|
690 | n/a | build_temp=None, |
---|
691 | n/a | target_lang=None): |
---|
692 | n/a | self.link(CCompiler.SHARED_LIBRARY, objects, |
---|
693 | n/a | self.library_filename(output_libname, lib_type='shared'), |
---|
694 | n/a | output_dir, |
---|
695 | n/a | libraries, library_dirs, runtime_library_dirs, |
---|
696 | n/a | export_symbols, debug, |
---|
697 | n/a | extra_preargs, extra_postargs, build_temp, target_lang) |
---|
698 | n/a | |
---|
699 | n/a | |
---|
700 | n/a | def link_shared_object(self, |
---|
701 | n/a | objects, |
---|
702 | n/a | output_filename, |
---|
703 | n/a | output_dir=None, |
---|
704 | n/a | libraries=None, |
---|
705 | n/a | library_dirs=None, |
---|
706 | n/a | runtime_library_dirs=None, |
---|
707 | n/a | export_symbols=None, |
---|
708 | n/a | debug=0, |
---|
709 | n/a | extra_preargs=None, |
---|
710 | n/a | extra_postargs=None, |
---|
711 | n/a | build_temp=None, |
---|
712 | n/a | target_lang=None): |
---|
713 | n/a | self.link(CCompiler.SHARED_OBJECT, objects, |
---|
714 | n/a | output_filename, output_dir, |
---|
715 | n/a | libraries, library_dirs, runtime_library_dirs, |
---|
716 | n/a | export_symbols, debug, |
---|
717 | n/a | extra_preargs, extra_postargs, build_temp, target_lang) |
---|
718 | n/a | |
---|
719 | n/a | |
---|
720 | n/a | def link_executable(self, |
---|
721 | n/a | objects, |
---|
722 | n/a | output_progname, |
---|
723 | n/a | output_dir=None, |
---|
724 | n/a | libraries=None, |
---|
725 | n/a | library_dirs=None, |
---|
726 | n/a | runtime_library_dirs=None, |
---|
727 | n/a | debug=0, |
---|
728 | n/a | extra_preargs=None, |
---|
729 | n/a | extra_postargs=None, |
---|
730 | n/a | target_lang=None): |
---|
731 | n/a | self.link(CCompiler.EXECUTABLE, objects, |
---|
732 | n/a | self.executable_filename(output_progname), output_dir, |
---|
733 | n/a | libraries, library_dirs, runtime_library_dirs, None, |
---|
734 | n/a | debug, extra_preargs, extra_postargs, None, target_lang) |
---|
735 | n/a | |
---|
736 | n/a | |
---|
737 | n/a | # -- Miscellaneous methods ----------------------------------------- |
---|
738 | n/a | # These are all used by the 'gen_lib_options() function; there is |
---|
739 | n/a | # no appropriate default implementation so subclasses should |
---|
740 | n/a | # implement all of these. |
---|
741 | n/a | |
---|
742 | n/a | def library_dir_option(self, dir): |
---|
743 | n/a | """Return the compiler option to add 'dir' to the list of |
---|
744 | n/a | directories searched for libraries. |
---|
745 | n/a | """ |
---|
746 | n/a | raise NotImplementedError |
---|
747 | n/a | |
---|
748 | n/a | def runtime_library_dir_option(self, dir): |
---|
749 | n/a | """Return the compiler option to add 'dir' to the list of |
---|
750 | n/a | directories searched for runtime libraries. |
---|
751 | n/a | """ |
---|
752 | n/a | raise NotImplementedError |
---|
753 | n/a | |
---|
754 | n/a | def library_option(self, lib): |
---|
755 | n/a | """Return the compiler option to add 'lib' to the list of libraries |
---|
756 | n/a | linked into the shared library or executable. |
---|
757 | n/a | """ |
---|
758 | n/a | raise NotImplementedError |
---|
759 | n/a | |
---|
760 | n/a | def has_function(self, funcname, includes=None, include_dirs=None, |
---|
761 | n/a | libraries=None, library_dirs=None): |
---|
762 | n/a | """Return a boolean indicating whether funcname is supported on |
---|
763 | n/a | the current platform. The optional arguments can be used to |
---|
764 | n/a | augment the compilation environment. |
---|
765 | n/a | """ |
---|
766 | n/a | # this can't be included at module scope because it tries to |
---|
767 | n/a | # import math which might not be available at that point - maybe |
---|
768 | n/a | # the necessary logic should just be inlined? |
---|
769 | n/a | import tempfile |
---|
770 | n/a | if includes is None: |
---|
771 | n/a | includes = [] |
---|
772 | n/a | if include_dirs is None: |
---|
773 | n/a | include_dirs = [] |
---|
774 | n/a | if libraries is None: |
---|
775 | n/a | libraries = [] |
---|
776 | n/a | if library_dirs is None: |
---|
777 | n/a | library_dirs = [] |
---|
778 | n/a | fd, fname = tempfile.mkstemp(".c", funcname, text=True) |
---|
779 | n/a | f = os.fdopen(fd, "w") |
---|
780 | n/a | try: |
---|
781 | n/a | for incl in includes: |
---|
782 | n/a | f.write("""#include "%s"\n""" % incl) |
---|
783 | n/a | f.write("""\ |
---|
784 | n/a | main (int argc, char **argv) { |
---|
785 | n/a | %s(); |
---|
786 | n/a | } |
---|
787 | n/a | """ % funcname) |
---|
788 | n/a | finally: |
---|
789 | n/a | f.close() |
---|
790 | n/a | try: |
---|
791 | n/a | objects = self.compile([fname], include_dirs=include_dirs) |
---|
792 | n/a | except CompileError: |
---|
793 | n/a | return False |
---|
794 | n/a | |
---|
795 | n/a | try: |
---|
796 | n/a | self.link_executable(objects, "a.out", |
---|
797 | n/a | libraries=libraries, |
---|
798 | n/a | library_dirs=library_dirs) |
---|
799 | n/a | except (LinkError, TypeError): |
---|
800 | n/a | return False |
---|
801 | n/a | return True |
---|
802 | n/a | |
---|
803 | n/a | def find_library_file (self, dirs, lib, debug=0): |
---|
804 | n/a | """Search the specified list of directories for a static or shared |
---|
805 | n/a | library file 'lib' and return the full path to that file. If |
---|
806 | n/a | 'debug' true, look for a debugging version (if that makes sense on |
---|
807 | n/a | the current platform). Return None if 'lib' wasn't found in any of |
---|
808 | n/a | the specified directories. |
---|
809 | n/a | """ |
---|
810 | n/a | raise NotImplementedError |
---|
811 | n/a | |
---|
812 | n/a | # -- Filename generation methods ----------------------------------- |
---|
813 | n/a | |
---|
814 | n/a | # The default implementation of the filename generating methods are |
---|
815 | n/a | # prejudiced towards the Unix/DOS/Windows view of the world: |
---|
816 | n/a | # * object files are named by replacing the source file extension |
---|
817 | n/a | # (eg. .c/.cpp -> .o/.obj) |
---|
818 | n/a | # * library files (shared or static) are named by plugging the |
---|
819 | n/a | # library name and extension into a format string, eg. |
---|
820 | n/a | # "lib%s.%s" % (lib_name, ".a") for Unix static libraries |
---|
821 | n/a | # * executables are named by appending an extension (possibly |
---|
822 | n/a | # empty) to the program name: eg. progname + ".exe" for |
---|
823 | n/a | # Windows |
---|
824 | n/a | # |
---|
825 | n/a | # To reduce redundant code, these methods expect to find |
---|
826 | n/a | # several attributes in the current object (presumably defined |
---|
827 | n/a | # as class attributes): |
---|
828 | n/a | # * src_extensions - |
---|
829 | n/a | # list of C/C++ source file extensions, eg. ['.c', '.cpp'] |
---|
830 | n/a | # * obj_extension - |
---|
831 | n/a | # object file extension, eg. '.o' or '.obj' |
---|
832 | n/a | # * static_lib_extension - |
---|
833 | n/a | # extension for static library files, eg. '.a' or '.lib' |
---|
834 | n/a | # * shared_lib_extension - |
---|
835 | n/a | # extension for shared library/object files, eg. '.so', '.dll' |
---|
836 | n/a | # * static_lib_format - |
---|
837 | n/a | # format string for generating static library filenames, |
---|
838 | n/a | # eg. 'lib%s.%s' or '%s.%s' |
---|
839 | n/a | # * shared_lib_format |
---|
840 | n/a | # format string for generating shared library filenames |
---|
841 | n/a | # (probably same as static_lib_format, since the extension |
---|
842 | n/a | # is one of the intended parameters to the format string) |
---|
843 | n/a | # * exe_extension - |
---|
844 | n/a | # extension for executable files, eg. '' or '.exe' |
---|
845 | n/a | |
---|
846 | n/a | def object_filenames(self, source_filenames, strip_dir=0, output_dir=''): |
---|
847 | n/a | if output_dir is None: |
---|
848 | n/a | output_dir = '' |
---|
849 | n/a | obj_names = [] |
---|
850 | n/a | for src_name in source_filenames: |
---|
851 | n/a | base, ext = os.path.splitext(src_name) |
---|
852 | n/a | base = os.path.splitdrive(base)[1] # Chop off the drive |
---|
853 | n/a | base = base[os.path.isabs(base):] # If abs, chop off leading / |
---|
854 | n/a | if ext not in self.src_extensions: |
---|
855 | n/a | raise UnknownFileError( |
---|
856 | n/a | "unknown file type '%s' (from '%s')" % (ext, src_name)) |
---|
857 | n/a | if strip_dir: |
---|
858 | n/a | base = os.path.basename(base) |
---|
859 | n/a | obj_names.append(os.path.join(output_dir, |
---|
860 | n/a | base + self.obj_extension)) |
---|
861 | n/a | return obj_names |
---|
862 | n/a | |
---|
863 | n/a | def shared_object_filename(self, basename, strip_dir=0, output_dir=''): |
---|
864 | n/a | assert output_dir is not None |
---|
865 | n/a | if strip_dir: |
---|
866 | n/a | basename = os.path.basename(basename) |
---|
867 | n/a | return os.path.join(output_dir, basename + self.shared_lib_extension) |
---|
868 | n/a | |
---|
869 | n/a | def executable_filename(self, basename, strip_dir=0, output_dir=''): |
---|
870 | n/a | assert output_dir is not None |
---|
871 | n/a | if strip_dir: |
---|
872 | n/a | basename = os.path.basename(basename) |
---|
873 | n/a | return os.path.join(output_dir, basename + (self.exe_extension or '')) |
---|
874 | n/a | |
---|
875 | n/a | def library_filename(self, libname, lib_type='static', # or 'shared' |
---|
876 | n/a | strip_dir=0, output_dir=''): |
---|
877 | n/a | assert output_dir is not None |
---|
878 | n/a | if lib_type not in ("static", "shared", "dylib", "xcode_stub"): |
---|
879 | n/a | raise ValueError( |
---|
880 | n/a | "'lib_type' must be \"static\", \"shared\", \"dylib\", or \"xcode_stub\"") |
---|
881 | n/a | fmt = getattr(self, lib_type + "_lib_format") |
---|
882 | n/a | ext = getattr(self, lib_type + "_lib_extension") |
---|
883 | n/a | |
---|
884 | n/a | dir, base = os.path.split(libname) |
---|
885 | n/a | filename = fmt % (base, ext) |
---|
886 | n/a | if strip_dir: |
---|
887 | n/a | dir = '' |
---|
888 | n/a | |
---|
889 | n/a | return os.path.join(output_dir, dir, filename) |
---|
890 | n/a | |
---|
891 | n/a | |
---|
892 | n/a | # -- Utility methods ----------------------------------------------- |
---|
893 | n/a | |
---|
894 | n/a | def announce(self, msg, level=1): |
---|
895 | n/a | log.debug(msg) |
---|
896 | n/a | |
---|
897 | n/a | def debug_print(self, msg): |
---|
898 | n/a | from distutils.debug import DEBUG |
---|
899 | n/a | if DEBUG: |
---|
900 | n/a | print(msg) |
---|
901 | n/a | |
---|
902 | n/a | def warn(self, msg): |
---|
903 | n/a | sys.stderr.write("warning: %s\n" % msg) |
---|
904 | n/a | |
---|
905 | n/a | def execute(self, func, args, msg=None, level=1): |
---|
906 | n/a | execute(func, args, msg, self.dry_run) |
---|
907 | n/a | |
---|
908 | n/a | def spawn(self, cmd): |
---|
909 | n/a | spawn(cmd, dry_run=self.dry_run) |
---|
910 | n/a | |
---|
911 | n/a | def move_file(self, src, dst): |
---|
912 | n/a | return move_file(src, dst, dry_run=self.dry_run) |
---|
913 | n/a | |
---|
914 | n/a | def mkpath (self, name, mode=0o777): |
---|
915 | n/a | mkpath(name, mode, dry_run=self.dry_run) |
---|
916 | n/a | |
---|
917 | n/a | |
---|
918 | n/a | # Map a sys.platform/os.name ('posix', 'nt') to the default compiler |
---|
919 | n/a | # type for that platform. Keys are interpreted as re match |
---|
920 | n/a | # patterns. Order is important; platform mappings are preferred over |
---|
921 | n/a | # OS names. |
---|
922 | n/a | _default_compilers = ( |
---|
923 | n/a | |
---|
924 | n/a | # Platform string mappings |
---|
925 | n/a | |
---|
926 | n/a | # on a cygwin built python we can use gcc like an ordinary UNIXish |
---|
927 | n/a | # compiler |
---|
928 | n/a | ('cygwin.*', 'unix'), |
---|
929 | n/a | |
---|
930 | n/a | # OS name mappings |
---|
931 | n/a | ('posix', 'unix'), |
---|
932 | n/a | ('nt', 'msvc'), |
---|
933 | n/a | |
---|
934 | n/a | ) |
---|
935 | n/a | |
---|
936 | n/a | def get_default_compiler(osname=None, platform=None): |
---|
937 | n/a | """Determine the default compiler to use for the given platform. |
---|
938 | n/a | |
---|
939 | n/a | osname should be one of the standard Python OS names (i.e. the |
---|
940 | n/a | ones returned by os.name) and platform the common value |
---|
941 | n/a | returned by sys.platform for the platform in question. |
---|
942 | n/a | |
---|
943 | n/a | The default values are os.name and sys.platform in case the |
---|
944 | n/a | parameters are not given. |
---|
945 | n/a | """ |
---|
946 | n/a | if osname is None: |
---|
947 | n/a | osname = os.name |
---|
948 | n/a | if platform is None: |
---|
949 | n/a | platform = sys.platform |
---|
950 | n/a | for pattern, compiler in _default_compilers: |
---|
951 | n/a | if re.match(pattern, platform) is not None or \ |
---|
952 | n/a | re.match(pattern, osname) is not None: |
---|
953 | n/a | return compiler |
---|
954 | n/a | # Default to Unix compiler |
---|
955 | n/a | return 'unix' |
---|
956 | n/a | |
---|
957 | n/a | # Map compiler types to (module_name, class_name) pairs -- ie. where to |
---|
958 | n/a | # find the code that implements an interface to this compiler. (The module |
---|
959 | n/a | # is assumed to be in the 'distutils' package.) |
---|
960 | n/a | compiler_class = { 'unix': ('unixccompiler', 'UnixCCompiler', |
---|
961 | n/a | "standard UNIX-style compiler"), |
---|
962 | n/a | 'msvc': ('_msvccompiler', 'MSVCCompiler', |
---|
963 | n/a | "Microsoft Visual C++"), |
---|
964 | n/a | 'cygwin': ('cygwinccompiler', 'CygwinCCompiler', |
---|
965 | n/a | "Cygwin port of GNU C Compiler for Win32"), |
---|
966 | n/a | 'mingw32': ('cygwinccompiler', 'Mingw32CCompiler', |
---|
967 | n/a | "Mingw32 port of GNU C Compiler for Win32"), |
---|
968 | n/a | 'bcpp': ('bcppcompiler', 'BCPPCompiler', |
---|
969 | n/a | "Borland C++ Compiler"), |
---|
970 | n/a | } |
---|
971 | n/a | |
---|
972 | n/a | def show_compilers(): |
---|
973 | n/a | """Print list of available compilers (used by the "--help-compiler" |
---|
974 | n/a | options to "build", "build_ext", "build_clib"). |
---|
975 | n/a | """ |
---|
976 | n/a | # XXX this "knows" that the compiler option it's describing is |
---|
977 | n/a | # "--compiler", which just happens to be the case for the three |
---|
978 | n/a | # commands that use it. |
---|
979 | n/a | from distutils.fancy_getopt import FancyGetopt |
---|
980 | n/a | compilers = [] |
---|
981 | n/a | for compiler in compiler_class.keys(): |
---|
982 | n/a | compilers.append(("compiler="+compiler, None, |
---|
983 | n/a | compiler_class[compiler][2])) |
---|
984 | n/a | compilers.sort() |
---|
985 | n/a | pretty_printer = FancyGetopt(compilers) |
---|
986 | n/a | pretty_printer.print_help("List of available compilers:") |
---|
987 | n/a | |
---|
988 | n/a | |
---|
989 | n/a | def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0): |
---|
990 | n/a | """Generate an instance of some CCompiler subclass for the supplied |
---|
991 | n/a | platform/compiler combination. 'plat' defaults to 'os.name' |
---|
992 | n/a | (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler |
---|
993 | n/a | for that platform. Currently only 'posix' and 'nt' are supported, and |
---|
994 | n/a | the default compilers are "traditional Unix interface" (UnixCCompiler |
---|
995 | n/a | class) and Visual C++ (MSVCCompiler class). Note that it's perfectly |
---|
996 | n/a | possible to ask for a Unix compiler object under Windows, and a |
---|
997 | n/a | Microsoft compiler object under Unix -- if you supply a value for |
---|
998 | n/a | 'compiler', 'plat' is ignored. |
---|
999 | n/a | """ |
---|
1000 | n/a | if plat is None: |
---|
1001 | n/a | plat = os.name |
---|
1002 | n/a | |
---|
1003 | n/a | try: |
---|
1004 | n/a | if compiler is None: |
---|
1005 | n/a | compiler = get_default_compiler(plat) |
---|
1006 | n/a | |
---|
1007 | n/a | (module_name, class_name, long_description) = compiler_class[compiler] |
---|
1008 | n/a | except KeyError: |
---|
1009 | n/a | msg = "don't know how to compile C/C++ code on platform '%s'" % plat |
---|
1010 | n/a | if compiler is not None: |
---|
1011 | n/a | msg = msg + " with '%s' compiler" % compiler |
---|
1012 | n/a | raise DistutilsPlatformError(msg) |
---|
1013 | n/a | |
---|
1014 | n/a | try: |
---|
1015 | n/a | module_name = "distutils." + module_name |
---|
1016 | n/a | __import__ (module_name) |
---|
1017 | n/a | module = sys.modules[module_name] |
---|
1018 | n/a | klass = vars(module)[class_name] |
---|
1019 | n/a | except ImportError: |
---|
1020 | n/a | raise DistutilsModuleError( |
---|
1021 | n/a | "can't compile C/C++ code: unable to load module '%s'" % \ |
---|
1022 | n/a | module_name) |
---|
1023 | n/a | except KeyError: |
---|
1024 | n/a | raise DistutilsModuleError( |
---|
1025 | n/a | "can't compile C/C++ code: unable to find class '%s' " |
---|
1026 | n/a | "in module '%s'" % (class_name, module_name)) |
---|
1027 | n/a | |
---|
1028 | n/a | # XXX The None is necessary to preserve backwards compatibility |
---|
1029 | n/a | # with classes that expect verbose to be the first positional |
---|
1030 | n/a | # argument. |
---|
1031 | n/a | return klass(None, dry_run, force) |
---|
1032 | n/a | |
---|
1033 | n/a | |
---|
1034 | n/a | def gen_preprocess_options(macros, include_dirs): |
---|
1035 | n/a | """Generate C pre-processor options (-D, -U, -I) as used by at least |
---|
1036 | n/a | two types of compilers: the typical Unix compiler and Visual C++. |
---|
1037 | n/a | 'macros' is the usual thing, a list of 1- or 2-tuples, where (name,) |
---|
1038 | n/a | means undefine (-U) macro 'name', and (name,value) means define (-D) |
---|
1039 | n/a | macro 'name' to 'value'. 'include_dirs' is just a list of directory |
---|
1040 | n/a | names to be added to the header file search path (-I). Returns a list |
---|
1041 | n/a | of command-line options suitable for either Unix compilers or Visual |
---|
1042 | n/a | C++. |
---|
1043 | n/a | """ |
---|
1044 | n/a | # XXX it would be nice (mainly aesthetic, and so we don't generate |
---|
1045 | n/a | # stupid-looking command lines) to go over 'macros' and eliminate |
---|
1046 | n/a | # redundant definitions/undefinitions (ie. ensure that only the |
---|
1047 | n/a | # latest mention of a particular macro winds up on the command |
---|
1048 | n/a | # line). I don't think it's essential, though, since most (all?) |
---|
1049 | n/a | # Unix C compilers only pay attention to the latest -D or -U |
---|
1050 | n/a | # mention of a macro on their command line. Similar situation for |
---|
1051 | n/a | # 'include_dirs'. I'm punting on both for now. Anyways, weeding out |
---|
1052 | n/a | # redundancies like this should probably be the province of |
---|
1053 | n/a | # CCompiler, since the data structures used are inherited from it |
---|
1054 | n/a | # and therefore common to all CCompiler classes. |
---|
1055 | n/a | pp_opts = [] |
---|
1056 | n/a | for macro in macros: |
---|
1057 | n/a | if not (isinstance(macro, tuple) and 1 <= len(macro) <= 2): |
---|
1058 | n/a | raise TypeError( |
---|
1059 | n/a | "bad macro definition '%s': " |
---|
1060 | n/a | "each element of 'macros' list must be a 1- or 2-tuple" |
---|
1061 | n/a | % macro) |
---|
1062 | n/a | |
---|
1063 | n/a | if len(macro) == 1: # undefine this macro |
---|
1064 | n/a | pp_opts.append("-U%s" % macro[0]) |
---|
1065 | n/a | elif len(macro) == 2: |
---|
1066 | n/a | if macro[1] is None: # define with no explicit value |
---|
1067 | n/a | pp_opts.append("-D%s" % macro[0]) |
---|
1068 | n/a | else: |
---|
1069 | n/a | # XXX *don't* need to be clever about quoting the |
---|
1070 | n/a | # macro value here, because we're going to avoid the |
---|
1071 | n/a | # shell at all costs when we spawn the command! |
---|
1072 | n/a | pp_opts.append("-D%s=%s" % macro) |
---|
1073 | n/a | |
---|
1074 | n/a | for dir in include_dirs: |
---|
1075 | n/a | pp_opts.append("-I%s" % dir) |
---|
1076 | n/a | return pp_opts |
---|
1077 | n/a | |
---|
1078 | n/a | |
---|
1079 | n/a | def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries): |
---|
1080 | n/a | """Generate linker options for searching library directories and |
---|
1081 | n/a | linking with specific libraries. 'libraries' and 'library_dirs' are, |
---|
1082 | n/a | respectively, lists of library names (not filenames!) and search |
---|
1083 | n/a | directories. Returns a list of command-line options suitable for use |
---|
1084 | n/a | with some compiler (depending on the two format strings passed in). |
---|
1085 | n/a | """ |
---|
1086 | n/a | lib_opts = [] |
---|
1087 | n/a | |
---|
1088 | n/a | for dir in library_dirs: |
---|
1089 | n/a | lib_opts.append(compiler.library_dir_option(dir)) |
---|
1090 | n/a | |
---|
1091 | n/a | for dir in runtime_library_dirs: |
---|
1092 | n/a | opt = compiler.runtime_library_dir_option(dir) |
---|
1093 | n/a | if isinstance(opt, list): |
---|
1094 | n/a | lib_opts = lib_opts + opt |
---|
1095 | n/a | else: |
---|
1096 | n/a | lib_opts.append(opt) |
---|
1097 | n/a | |
---|
1098 | n/a | # XXX it's important that we *not* remove redundant library mentions! |
---|
1099 | n/a | # sometimes you really do have to say "-lfoo -lbar -lfoo" in order to |
---|
1100 | n/a | # resolve all symbols. I just hope we never have to say "-lfoo obj.o |
---|
1101 | n/a | # -lbar" to get things to work -- that's certainly a possibility, but a |
---|
1102 | n/a | # pretty nasty way to arrange your C code. |
---|
1103 | n/a | |
---|
1104 | n/a | for lib in libraries: |
---|
1105 | n/a | (lib_dir, lib_name) = os.path.split(lib) |
---|
1106 | n/a | if lib_dir: |
---|
1107 | n/a | lib_file = compiler.find_library_file([lib_dir], lib_name) |
---|
1108 | n/a | if lib_file: |
---|
1109 | n/a | lib_opts.append(lib_file) |
---|
1110 | n/a | else: |
---|
1111 | n/a | compiler.warn("no library file corresponding to " |
---|
1112 | n/a | "'%s' found (skipping)" % lib) |
---|
1113 | n/a | else: |
---|
1114 | n/a | lib_opts.append(compiler.library_option (lib)) |
---|
1115 | n/a | return lib_opts |
---|