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

Python code coverage for Lib/distutils/emxccompiler.py

#countcontent
1n/a"""distutils.emxccompiler
2n/a
3n/aProvides the EMXCCompiler class, a subclass of UnixCCompiler that
4n/ahandles the EMX port of the GNU C compiler to OS/2.
5n/a"""
6n/a
7n/a# issues:
8n/a#
9n/a# * OS/2 insists that DLLs can have names no longer than 8 characters
10n/a# We put export_symbols in a def-file, as though the DLL can have
11n/a# an arbitrary length name, but truncate the output filename.
12n/a#
13n/a# * only use OMF objects and use LINK386 as the linker (-Zomf)
14n/a#
15n/a# * always build for multithreading (-Zmt) as the accompanying OS/2 port
16n/a# of Python is only distributed with threads enabled.
17n/a#
18n/a# tested configurations:
19n/a#
20n/a# * EMX gcc 2.81/EMX 0.9d fix03
21n/a
22n/aimport os,sys,copy
23n/afrom distutils.ccompiler import gen_preprocess_options, gen_lib_options
24n/afrom distutils.unixccompiler import UnixCCompiler
25n/afrom distutils.file_util import write_file
26n/afrom distutils.errors import DistutilsExecError, CompileError, UnknownFileError
27n/afrom distutils import log
28n/a
29n/aclass EMXCCompiler (UnixCCompiler):
30n/a
31n/a compiler_type = 'emx'
32n/a obj_extension = ".obj"
33n/a static_lib_extension = ".lib"
34n/a shared_lib_extension = ".dll"
35n/a static_lib_format = "%s%s"
36n/a shared_lib_format = "%s%s"
37n/a res_extension = ".res" # compiled resource file
38n/a exe_extension = ".exe"
39n/a
40n/a def __init__ (self,
41n/a verbose=0,
42n/a dry_run=0,
43n/a force=0):
44n/a
45n/a UnixCCompiler.__init__ (self, verbose, dry_run, force)
46n/a
47n/a (status, details) = check_config_h()
48n/a self.debug_print("Python's GCC status: %s (details: %s)" %
49n/a (status, details))
50n/a if status is not CONFIG_H_OK:
51n/a self.warn(
52n/a "Python's pyconfig.h doesn't seem to support your compiler. " +
53n/a ("Reason: %s." % details) +
54n/a "Compiling may fail because of undefined preprocessor macros.")
55n/a
56n/a (self.gcc_version, self.ld_version) = \
57n/a get_versions()
58n/a self.debug_print(self.compiler_type + ": gcc %s, ld %s\n" %
59n/a (self.gcc_version,
60n/a self.ld_version) )
61n/a
62n/a # Hard-code GCC because that's what this is all about.
63n/a # XXX optimization, warnings etc. should be customizable.
64n/a self.set_executables(compiler='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall',
65n/a compiler_so='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall',
66n/a linker_exe='gcc -Zomf -Zmt -Zcrtdll',
67n/a linker_so='gcc -Zomf -Zmt -Zcrtdll -Zdll')
68n/a
69n/a # want the gcc library statically linked (so that we don't have
70n/a # to distribute a version dependent on the compiler we have)
71n/a self.dll_libraries=["gcc"]
72n/a
73n/a # __init__ ()
74n/a
75n/a def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
76n/a if ext == '.rc':
77n/a # gcc requires '.rc' compiled to binary ('.res') files !!!
78n/a try:
79n/a self.spawn(["rc", "-r", src])
80n/a except DistutilsExecError as msg:
81n/a raise CompileError(msg)
82n/a else: # for other files use the C-compiler
83n/a try:
84n/a self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
85n/a extra_postargs)
86n/a except DistutilsExecError as msg:
87n/a raise CompileError(msg)
88n/a
89n/a def link (self,
90n/a target_desc,
91n/a objects,
92n/a output_filename,
93n/a output_dir=None,
94n/a libraries=None,
95n/a library_dirs=None,
96n/a runtime_library_dirs=None,
97n/a export_symbols=None,
98n/a debug=0,
99n/a extra_preargs=None,
100n/a extra_postargs=None,
101n/a build_temp=None,
102n/a target_lang=None):
103n/a
104n/a # use separate copies, so we can modify the lists
105n/a extra_preargs = copy.copy(extra_preargs or [])
106n/a libraries = copy.copy(libraries or [])
107n/a objects = copy.copy(objects or [])
108n/a
109n/a # Additional libraries
110n/a libraries.extend(self.dll_libraries)
111n/a
112n/a # handle export symbols by creating a def-file
113n/a # with executables this only works with gcc/ld as linker
114n/a if ((export_symbols is not None) and
115n/a (target_desc != self.EXECUTABLE)):
116n/a # (The linker doesn't do anything if output is up-to-date.
117n/a # So it would probably better to check if we really need this,
118n/a # but for this we had to insert some unchanged parts of
119n/a # UnixCCompiler, and this is not what we want.)
120n/a
121n/a # we want to put some files in the same directory as the
122n/a # object files are, build_temp doesn't help much
123n/a # where are the object files
124n/a temp_dir = os.path.dirname(objects[0])
125n/a # name of dll to give the helper files the same base name
126n/a (dll_name, dll_extension) = os.path.splitext(
127n/a os.path.basename(output_filename))
128n/a
129n/a # generate the filenames for these files
130n/a def_file = os.path.join(temp_dir, dll_name + ".def")
131n/a
132n/a # Generate .def file
133n/a contents = [
134n/a "LIBRARY %s INITINSTANCE TERMINSTANCE" % \
135n/a os.path.splitext(os.path.basename(output_filename))[0],
136n/a "DATA MULTIPLE NONSHARED",
137n/a "EXPORTS"]
138n/a for sym in export_symbols:
139n/a contents.append(' "%s"' % sym)
140n/a self.execute(write_file, (def_file, contents),
141n/a "writing %s" % def_file)
142n/a
143n/a # next add options for def-file and to creating import libraries
144n/a # for gcc/ld the def-file is specified as any other object files
145n/a objects.append(def_file)
146n/a
147n/a #end: if ((export_symbols is not None) and
148n/a # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
149n/a
150n/a # who wants symbols and a many times larger output file
151n/a # should explicitly switch the debug mode on
152n/a # otherwise we let dllwrap/ld strip the output file
153n/a # (On my machine: 10KB < stripped_file < ??100KB
154n/a # unstripped_file = stripped_file + XXX KB
155n/a # ( XXX=254 for a typical python extension))
156n/a if not debug:
157n/a extra_preargs.append("-s")
158n/a
159n/a UnixCCompiler.link(self,
160n/a target_desc,
161n/a objects,
162n/a output_filename,
163n/a output_dir,
164n/a libraries,
165n/a library_dirs,
166n/a runtime_library_dirs,
167n/a None, # export_symbols, we do this in our def-file
168n/a debug,
169n/a extra_preargs,
170n/a extra_postargs,
171n/a build_temp,
172n/a target_lang)
173n/a
174n/a # link ()
175n/a
176n/a # -- Miscellaneous methods -----------------------------------------
177n/a
178n/a # override the object_filenames method from CCompiler to
179n/a # support rc and res-files
180n/a def object_filenames (self,
181n/a source_filenames,
182n/a strip_dir=0,
183n/a output_dir=''):
184n/a if output_dir is None: output_dir = ''
185n/a obj_names = []
186n/a for src_name in source_filenames:
187n/a # use normcase to make sure '.rc' is really '.rc' and not '.RC'
188n/a (base, ext) = os.path.splitext (os.path.normcase(src_name))
189n/a if ext not in (self.src_extensions + ['.rc']):
190n/a raise UnknownFileError("unknown file type '%s' (from '%s')" % \
191n/a (ext, src_name))
192n/a if strip_dir:
193n/a base = os.path.basename (base)
194n/a if ext == '.rc':
195n/a # these need to be compiled to object files
196n/a obj_names.append (os.path.join (output_dir,
197n/a base + self.res_extension))
198n/a else:
199n/a obj_names.append (os.path.join (output_dir,
200n/a base + self.obj_extension))
201n/a return obj_names
202n/a
203n/a # object_filenames ()
204n/a
205n/a # override the find_library_file method from UnixCCompiler
206n/a # to deal with file naming/searching differences
207n/a def find_library_file(self, dirs, lib, debug=0):
208n/a shortlib = '%s.lib' % lib
209n/a longlib = 'lib%s.lib' % lib # this form very rare
210n/a
211n/a # get EMX's default library directory search path
212n/a try:
213n/a emx_dirs = os.environ['LIBRARY_PATH'].split(';')
214n/a except KeyError:
215n/a emx_dirs = []
216n/a
217n/a for dir in dirs + emx_dirs:
218n/a shortlibp = os.path.join(dir, shortlib)
219n/a longlibp = os.path.join(dir, longlib)
220n/a if os.path.exists(shortlibp):
221n/a return shortlibp
222n/a elif os.path.exists(longlibp):
223n/a return longlibp
224n/a
225n/a # Oops, didn't find it in *any* of 'dirs'
226n/a return None
227n/a
228n/a# class EMXCCompiler
229n/a
230n/a
231n/a# Because these compilers aren't configured in Python's pyconfig.h file by
232n/a# default, we should at least warn the user if he is using a unmodified
233n/a# version.
234n/a
235n/aCONFIG_H_OK = "ok"
236n/aCONFIG_H_NOTOK = "not ok"
237n/aCONFIG_H_UNCERTAIN = "uncertain"
238n/a
239n/adef check_config_h():
240n/a
241n/a """Check if the current Python installation (specifically, pyconfig.h)
242n/a appears amenable to building extensions with GCC. Returns a tuple
243n/a (status, details), where 'status' is one of the following constants:
244n/a CONFIG_H_OK
245n/a all is well, go ahead and compile
246n/a CONFIG_H_NOTOK
247n/a doesn't look good
248n/a CONFIG_H_UNCERTAIN
249n/a not sure -- unable to read pyconfig.h
250n/a 'details' is a human-readable string explaining the situation.
251n/a
252n/a Note there are two ways to conclude "OK": either 'sys.version' contains
253n/a the string "GCC" (implying that this Python was built with GCC), or the
254n/a installed "pyconfig.h" contains the string "__GNUC__".
255n/a """
256n/a
257n/a # XXX since this function also checks sys.version, it's not strictly a
258n/a # "pyconfig.h" check -- should probably be renamed...
259n/a
260n/a from distutils import sysconfig
261n/a # if sys.version contains GCC then python was compiled with
262n/a # GCC, and the pyconfig.h file should be OK
263n/a if sys.version.find("GCC") >= 0:
264n/a return (CONFIG_H_OK, "sys.version mentions 'GCC'")
265n/a
266n/a fn = sysconfig.get_config_h_filename()
267n/a try:
268n/a # It would probably better to read single lines to search.
269n/a # But we do this only once, and it is fast enough
270n/a f = open(fn)
271n/a try:
272n/a s = f.read()
273n/a finally:
274n/a f.close()
275n/a
276n/a except IOError as exc:
277n/a # if we can't read this file, we cannot say it is wrong
278n/a # the compiler will complain later about this file as missing
279n/a return (CONFIG_H_UNCERTAIN,
280n/a "couldn't read '%s': %s" % (fn, exc.strerror))
281n/a
282n/a else:
283n/a # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar
284n/a if s.find("__GNUC__") >= 0:
285n/a return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)
286n/a else:
287n/a return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)
288n/a
289n/a
290n/adef get_versions():
291n/a """ Try to find out the versions of gcc and ld.
292n/a If not possible it returns None for it.
293n/a """
294n/a from distutils.version import StrictVersion
295n/a from distutils.spawn import find_executable
296n/a import re
297n/a
298n/a gcc_exe = find_executable('gcc')
299n/a if gcc_exe:
300n/a out = os.popen(gcc_exe + ' -dumpversion','r')
301n/a try:
302n/a out_string = out.read()
303n/a finally:
304n/a out.close()
305n/a result = re.search('(\d+\.\d+\.\d+)', out_string, re.ASCII)
306n/a if result:
307n/a gcc_version = StrictVersion(result.group(1))
308n/a else:
309n/a gcc_version = None
310n/a else:
311n/a gcc_version = None
312n/a # EMX ld has no way of reporting version number, and we use GCC
313n/a # anyway - so we can link OMF DLLs
314n/a ld_version = None
315n/a return (gcc_version, ld_version)