blob: f2f77e52d1edea0ba8165fdbfebc4f937c8a3adc [file] [log] [blame]
Marc-André Lemburg9273ec72002-02-06 18:22:48 +00001"""distutils.emxccompiler
2
3Provides the EMXCCompiler class, a subclass of UnixCCompiler that
4handles the EMX port of the GNU C compiler to OS/2.
5"""
6
7# issues:
8#
9# * OS/2 insists that DLLs can have names no longer than 8 characters
10# We put export_symbols in a def-file, as though the DLL can have
11# an arbitrary length name, but truncate the output filename.
12#
13# * only use OMF objects and use LINK386 as the linker (-Zomf)
14#
15# * always build for multithreading (-Zmt) as the accompanying OS/2 port
16# of Python is only distributed with threads enabled.
17#
18# tested configurations:
Guido van Rossumbffa52f2002-09-29 00:25:51 +000019#
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000020# * EMX gcc 2.81/EMX 0.9d fix03
21
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000022__revision__ = "$Id$"
23
Tarek Ziadéa99dedf2009-07-16 15:35:45 +000024import os, sys, copy
25from warnings import warn
26
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000027from distutils.unixccompiler import UnixCCompiler
28from distutils.file_util import write_file
29from distutils.errors import DistutilsExecError, CompileError, UnknownFileError
Tarek Ziadéa99dedf2009-07-16 15:35:45 +000030from distutils.util import get_compiler_versions
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000031
32class EMXCCompiler (UnixCCompiler):
33
34 compiler_type = 'emx'
35 obj_extension = ".obj"
36 static_lib_extension = ".lib"
37 shared_lib_extension = ".dll"
38 static_lib_format = "%s%s"
39 shared_lib_format = "%s%s"
40 res_extension = ".res" # compiled resource file
41 exe_extension = ".exe"
Guido van Rossumbffa52f2002-09-29 00:25:51 +000042
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000043 def __init__ (self,
44 verbose=0,
45 dry_run=0,
46 force=0):
47
48 UnixCCompiler.__init__ (self, verbose, dry_run, force)
49
50 (status, details) = check_config_h()
51 self.debug_print("Python's GCC status: %s (details: %s)" %
52 (status, details))
53 if status is not CONFIG_H_OK:
54 self.warn(
55 "Python's pyconfig.h doesn't seem to support your compiler. " +
56 ("Reason: %s." % details) +
57 "Compiling may fail because of undefined preprocessor macros.")
Guido van Rossumbffa52f2002-09-29 00:25:51 +000058
Tarek Ziadéa99dedf2009-07-16 15:35:45 +000059 gcc_version, ld_version, dllwrap_version = get_compiler_versions()
60 self.gcc_version, self.ld_version = gcc_version, ld_version
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000061 self.debug_print(self.compiler_type + ": gcc %s, ld %s\n" %
Guido van Rossumbffa52f2002-09-29 00:25:51 +000062 (self.gcc_version,
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000063 self.ld_version) )
64
65 # Hard-code GCC because that's what this is all about.
66 # XXX optimization, warnings etc. should be customizable.
Andrew MacIntyre63ee1102003-12-02 12:17:59 +000067 self.set_executables(compiler='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall',
68 compiler_so='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall',
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000069 linker_exe='gcc -Zomf -Zmt -Zcrtdll',
70 linker_so='gcc -Zomf -Zmt -Zcrtdll -Zdll')
71
72 # want the gcc library statically linked (so that we don't have
73 # to distribute a version dependent on the compiler we have)
74 self.dll_libraries=["gcc"]
Guido van Rossumbffa52f2002-09-29 00:25:51 +000075
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000076 # __init__ ()
77
Andrew MacIntyre428a38c2002-08-04 06:17:08 +000078 def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
Jeremy Hylton1b046e42002-06-18 18:48:55 +000079 if ext == '.rc':
80 # gcc requires '.rc' compiled to binary ('.res') files !!!
81 try:
82 self.spawn(["rc", "-r", src])
83 except DistutilsExecError, msg:
84 raise CompileError, msg
Guido van Rossumbffa52f2002-09-29 00:25:51 +000085 else: # for other files use the C-compiler
Jeremy Hylton1b046e42002-06-18 18:48:55 +000086 try:
87 self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
88 extra_postargs)
89 except DistutilsExecError, msg:
90 raise CompileError, msg
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000091
92 def link (self,
93 target_desc,
94 objects,
95 output_filename,
96 output_dir=None,
97 libraries=None,
98 library_dirs=None,
99 runtime_library_dirs=None,
100 export_symbols=None,
101 debug=0,
102 extra_preargs=None,
103 extra_postargs=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000104 build_temp=None,
105 target_lang=None):
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000106
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000107 # use separate copies, so we can modify the lists
108 extra_preargs = copy.copy(extra_preargs or [])
109 libraries = copy.copy(libraries or [])
110 objects = copy.copy(objects or [])
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000111
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000112 # Additional libraries
113 libraries.extend(self.dll_libraries)
114
115 # handle export symbols by creating a def-file
116 # with executables this only works with gcc/ld as linker
117 if ((export_symbols is not None) and
118 (target_desc != self.EXECUTABLE)):
119 # (The linker doesn't do anything if output is up-to-date.
120 # So it would probably better to check if we really need this,
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000121 # but for this we had to insert some unchanged parts of
122 # UnixCCompiler, and this is not what we want.)
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000123
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000124 # we want to put some files in the same directory as the
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000125 # object files are, build_temp doesn't help much
126 # where are the object files
127 temp_dir = os.path.dirname(objects[0])
128 # name of dll to give the helper files the same base name
129 (dll_name, dll_extension) = os.path.splitext(
130 os.path.basename(output_filename))
131
132 # generate the filenames for these files
133 def_file = os.path.join(temp_dir, dll_name + ".def")
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000134
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000135 # Generate .def file
136 contents = [
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000137 "LIBRARY %s INITINSTANCE TERMINSTANCE" % \
138 os.path.splitext(os.path.basename(output_filename))[0],
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000139 "DATA MULTIPLE NONSHARED",
140 "EXPORTS"]
141 for sym in export_symbols:
142 contents.append(' "%s"' % sym)
143 self.execute(write_file, (def_file, contents),
144 "writing %s" % def_file)
145
146 # next add options for def-file and to creating import libraries
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000147 # for gcc/ld the def-file is specified as any other object files
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000148 objects.append(def_file)
149
150 #end: if ((export_symbols is not None) and
151 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000152
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000153 # who wants symbols and a many times larger output file
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000154 # should explicitly switch the debug mode on
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000155 # otherwise we let dllwrap/ld strip the output file
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000156 # (On my machine: 10KB < stripped_file < ??100KB
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000157 # unstripped_file = stripped_file + XXX KB
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000158 # ( XXX=254 for a typical python extension))
159 if not debug:
160 extra_preargs.append("-s")
161
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000162 UnixCCompiler.link(self,
163 target_desc,
164 objects,
165 output_filename,
166 output_dir,
167 libraries,
168 library_dirs,
169 runtime_library_dirs,
170 None, # export_symbols, we do this in our def-file
171 debug,
172 extra_preargs,
173 extra_postargs,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000174 build_temp,
175 target_lang)
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000176
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000177 # link ()
178
179 # -- Miscellaneous methods -----------------------------------------
180
Andrew MacIntyre4104db32002-08-04 06:21:25 +0000181 # override the object_filenames method from CCompiler to
182 # support rc and res-files
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000183 def object_filenames (self,
184 source_filenames,
185 strip_dir=0,
186 output_dir=''):
187 if output_dir is None: output_dir = ''
188 obj_names = []
189 for src_name in source_filenames:
190 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
191 (base, ext) = os.path.splitext (os.path.normcase(src_name))
192 if ext not in (self.src_extensions + ['.rc']):
193 raise UnknownFileError, \
194 "unknown file type '%s' (from '%s')" % \
195 (ext, src_name)
196 if strip_dir:
197 base = os.path.basename (base)
198 if ext == '.rc':
199 # these need to be compiled to object files
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000200 obj_names.append (os.path.join (output_dir,
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000201 base + self.res_extension))
202 else:
203 obj_names.append (os.path.join (output_dir,
204 base + self.obj_extension))
205 return obj_names
206
207 # object_filenames ()
208
Andrew MacIntyre4104db32002-08-04 06:21:25 +0000209 # override the find_library_file method from UnixCCompiler
210 # to deal with file naming/searching differences
211 def find_library_file(self, dirs, lib, debug=0):
212 shortlib = '%s.lib' % lib
213 longlib = 'lib%s.lib' % lib # this form very rare
214
215 # get EMX's default library directory search path
216 try:
217 emx_dirs = os.environ['LIBRARY_PATH'].split(';')
218 except KeyError:
219 emx_dirs = []
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000220
Andrew MacIntyre4104db32002-08-04 06:21:25 +0000221 for dir in dirs + emx_dirs:
222 shortlibp = os.path.join(dir, shortlib)
223 longlibp = os.path.join(dir, longlib)
224 if os.path.exists(shortlibp):
225 return shortlibp
226 elif os.path.exists(longlibp):
227 return longlibp
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000228
Andrew MacIntyre4104db32002-08-04 06:21:25 +0000229 # Oops, didn't find it in *any* of 'dirs'
230 return None
231
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000232# class EMXCCompiler
233
234
235# Because these compilers aren't configured in Python's pyconfig.h file by
236# default, we should at least warn the user if he is using a unmodified
237# version.
238
239CONFIG_H_OK = "ok"
240CONFIG_H_NOTOK = "not ok"
241CONFIG_H_UNCERTAIN = "uncertain"
242
243def check_config_h():
244
245 """Check if the current Python installation (specifically, pyconfig.h)
246 appears amenable to building extensions with GCC. Returns a tuple
247 (status, details), where 'status' is one of the following constants:
248 CONFIG_H_OK
249 all is well, go ahead and compile
250 CONFIG_H_NOTOK
251 doesn't look good
252 CONFIG_H_UNCERTAIN
253 not sure -- unable to read pyconfig.h
254 'details' is a human-readable string explaining the situation.
255
256 Note there are two ways to conclude "OK": either 'sys.version' contains
257 the string "GCC" (implying that this Python was built with GCC), or the
258 installed "pyconfig.h" contains the string "__GNUC__".
259 """
260
261 # XXX since this function also checks sys.version, it's not strictly a
262 # "pyconfig.h" check -- should probably be renamed...
263
264 from distutils import sysconfig
265 import string
266 # if sys.version contains GCC then python was compiled with
267 # GCC, and the pyconfig.h file should be OK
268 if string.find(sys.version,"GCC") >= 0:
269 return (CONFIG_H_OK, "sys.version mentions 'GCC'")
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000270
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000271 fn = sysconfig.get_config_h_filename()
272 try:
273 # It would probably better to read single lines to search.
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000274 # But we do this only once, and it is fast enough
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000275 f = open(fn)
276 s = f.read()
277 f.close()
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000278
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000279 except IOError, exc:
280 # if we can't read this file, we cannot say it is wrong
281 # the compiler will complain later about this file as missing
282 return (CONFIG_H_UNCERTAIN,
283 "couldn't read '%s': %s" % (fn, exc.strerror))
284
285 else:
286 # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar
287 if string.find(s,"__GNUC__") >= 0:
288 return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)
289 else:
290 return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)
291
292
293def get_versions():
294 """ Try to find out the versions of gcc and ld.
295 If not possible it returns None for it.
296 """
Tarek Ziadéa99dedf2009-07-16 15:35:45 +0000297 warn("'distutils.emxccompiler.get_versions' is deprecated "
298 "use 'distutils.util.get_compiler_versions' instead",
299 DeprecationWarning)
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000300
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000301 # EMX ld has no way of reporting version number, and we use GCC
302 # anyway - so we can link OMF DLLs
Tarek Ziadéa99dedf2009-07-16 15:35:45 +0000303 gcc_version, ld_version, dllwrap_version = get_compiler_versions()
304 return gcc_version, None