blob: 3675f8df9c8255076685d9e60e937c17d7f38b4a [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
Tarek Ziadé36797272010-07-22 12:50:05 +000022import os,sys,copy
23from distutils.ccompiler import gen_preprocess_options, gen_lib_options
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000024from distutils.unixccompiler import UnixCCompiler
25from distutils.file_util import write_file
26from distutils.errors import DistutilsExecError, CompileError, UnknownFileError
Tarek Ziadé36797272010-07-22 12:50:05 +000027from distutils import log
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000028
29class EMXCCompiler (UnixCCompiler):
30
31 compiler_type = 'emx'
32 obj_extension = ".obj"
33 static_lib_extension = ".lib"
34 shared_lib_extension = ".dll"
35 static_lib_format = "%s%s"
36 shared_lib_format = "%s%s"
37 res_extension = ".res" # compiled resource file
38 exe_extension = ".exe"
Guido van Rossumbffa52f2002-09-29 00:25:51 +000039
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000040 def __init__ (self,
41 verbose=0,
42 dry_run=0,
43 force=0):
44
45 UnixCCompiler.__init__ (self, verbose, dry_run, force)
46
47 (status, details) = check_config_h()
48 self.debug_print("Python's GCC status: %s (details: %s)" %
49 (status, details))
50 if status is not CONFIG_H_OK:
51 self.warn(
52 "Python's pyconfig.h doesn't seem to support your compiler. " +
53 ("Reason: %s." % details) +
54 "Compiling may fail because of undefined preprocessor macros.")
Guido van Rossumbffa52f2002-09-29 00:25:51 +000055
Tarek Ziadé36797272010-07-22 12:50:05 +000056 (self.gcc_version, self.ld_version) = \
57 get_versions()
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000058 self.debug_print(self.compiler_type + ": gcc %s, ld %s\n" %
Guido van Rossumbffa52f2002-09-29 00:25:51 +000059 (self.gcc_version,
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000060 self.ld_version) )
61
62 # Hard-code GCC because that's what this is all about.
63 # XXX optimization, warnings etc. should be customizable.
Andrew MacIntyre63ee1102003-12-02 12:17:59 +000064 self.set_executables(compiler='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall',
65 compiler_so='gcc -Zomf -Zmt -O3 -fomit-frame-pointer -mprobe -Wall',
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000066 linker_exe='gcc -Zomf -Zmt -Zcrtdll',
67 linker_so='gcc -Zomf -Zmt -Zcrtdll -Zdll')
68
69 # want the gcc library statically linked (so that we don't have
70 # to distribute a version dependent on the compiler we have)
71 self.dll_libraries=["gcc"]
Guido van Rossumbffa52f2002-09-29 00:25:51 +000072
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000073 # __init__ ()
74
Andrew MacIntyre428a38c2002-08-04 06:17:08 +000075 def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
Jeremy Hylton1b046e42002-06-18 18:48:55 +000076 if ext == '.rc':
77 # gcc requires '.rc' compiled to binary ('.res') files !!!
78 try:
79 self.spawn(["rc", "-r", src])
Guido van Rossumb940e112007-01-10 16:19:56 +000080 except DistutilsExecError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +000081 raise CompileError(msg)
Guido van Rossumbffa52f2002-09-29 00:25:51 +000082 else: # for other files use the C-compiler
Jeremy Hylton1b046e42002-06-18 18:48:55 +000083 try:
84 self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
85 extra_postargs)
Guido van Rossumb940e112007-01-10 16:19:56 +000086 except DistutilsExecError as msg:
Collin Winter5b7e9d72007-08-30 03:52:21 +000087 raise CompileError(msg)
Marc-André Lemburg9273ec72002-02-06 18:22:48 +000088
89 def link (self,
90 target_desc,
91 objects,
92 output_filename,
93 output_dir=None,
94 libraries=None,
95 library_dirs=None,
96 runtime_library_dirs=None,
97 export_symbols=None,
98 debug=0,
99 extra_preargs=None,
100 extra_postargs=None,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000101 build_temp=None,
102 target_lang=None):
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000103
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000104 # use separate copies, so we can modify the lists
105 extra_preargs = copy.copy(extra_preargs or [])
106 libraries = copy.copy(libraries or [])
107 objects = copy.copy(objects or [])
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000108
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000109 # Additional libraries
110 libraries.extend(self.dll_libraries)
111
112 # handle export symbols by creating a def-file
113 # with executables this only works with gcc/ld as linker
114 if ((export_symbols is not None) and
115 (target_desc != self.EXECUTABLE)):
116 # (The linker doesn't do anything if output is up-to-date.
117 # So it would probably better to check if we really need this,
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000118 # but for this we had to insert some unchanged parts of
119 # UnixCCompiler, and this is not what we want.)
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000120
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000121 # we want to put some files in the same directory as the
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000122 # object files are, build_temp doesn't help much
123 # where are the object files
124 temp_dir = os.path.dirname(objects[0])
125 # name of dll to give the helper files the same base name
126 (dll_name, dll_extension) = os.path.splitext(
127 os.path.basename(output_filename))
128
129 # generate the filenames for these files
130 def_file = os.path.join(temp_dir, dll_name + ".def")
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000131
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000132 # Generate .def file
133 contents = [
Jeremy Hyltona2f99892002-06-04 20:26:44 +0000134 "LIBRARY %s INITINSTANCE TERMINSTANCE" % \
135 os.path.splitext(os.path.basename(output_filename))[0],
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000136 "DATA MULTIPLE NONSHARED",
137 "EXPORTS"]
138 for sym in export_symbols:
139 contents.append(' "%s"' % sym)
140 self.execute(write_file, (def_file, contents),
141 "writing %s" % def_file)
142
143 # next add options for def-file and to creating import libraries
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000144 # for gcc/ld the def-file is specified as any other object files
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000145 objects.append(def_file)
146
147 #end: if ((export_symbols is not None) and
148 # (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000149
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000150 # who wants symbols and a many times larger output file
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000151 # should explicitly switch the debug mode on
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000152 # otherwise we let dllwrap/ld strip the output file
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000153 # (On my machine: 10KB < stripped_file < ??100KB
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000154 # unstripped_file = stripped_file + XXX KB
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000155 # ( XXX=254 for a typical python extension))
156 if not debug:
157 extra_preargs.append("-s")
158
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000159 UnixCCompiler.link(self,
160 target_desc,
161 objects,
162 output_filename,
163 output_dir,
164 libraries,
165 library_dirs,
166 runtime_library_dirs,
167 None, # export_symbols, we do this in our def-file
168 debug,
169 extra_preargs,
170 extra_postargs,
Gustavo Niemeyer6b016852002-11-05 16:12:02 +0000171 build_temp,
172 target_lang)
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000173
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000174 # link ()
175
176 # -- Miscellaneous methods -----------------------------------------
177
Andrew MacIntyre4104db32002-08-04 06:21:25 +0000178 # override the object_filenames method from CCompiler to
179 # support rc and res-files
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000180 def object_filenames (self,
181 source_filenames,
182 strip_dir=0,
183 output_dir=''):
184 if output_dir is None: output_dir = ''
185 obj_names = []
186 for src_name in source_filenames:
187 # use normcase to make sure '.rc' is really '.rc' and not '.RC'
188 (base, ext) = os.path.splitext (os.path.normcase(src_name))
189 if ext not in (self.src_extensions + ['.rc']):
Collin Winter5b7e9d72007-08-30 03:52:21 +0000190 raise UnknownFileError("unknown file type '%s' (from '%s')" % \
191 (ext, src_name))
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000192 if strip_dir:
193 base = os.path.basename (base)
194 if ext == '.rc':
195 # these need to be compiled to object files
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000196 obj_names.append (os.path.join (output_dir,
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000197 base + self.res_extension))
198 else:
199 obj_names.append (os.path.join (output_dir,
200 base + self.obj_extension))
201 return obj_names
202
203 # object_filenames ()
204
Andrew MacIntyre4104db32002-08-04 06:21:25 +0000205 # override the find_library_file method from UnixCCompiler
206 # to deal with file naming/searching differences
207 def find_library_file(self, dirs, lib, debug=0):
208 shortlib = '%s.lib' % lib
209 longlib = 'lib%s.lib' % lib # this form very rare
210
211 # get EMX's default library directory search path
212 try:
213 emx_dirs = os.environ['LIBRARY_PATH'].split(';')
214 except KeyError:
215 emx_dirs = []
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000216
Andrew MacIntyre4104db32002-08-04 06:21:25 +0000217 for dir in dirs + emx_dirs:
218 shortlibp = os.path.join(dir, shortlib)
219 longlibp = os.path.join(dir, longlib)
220 if os.path.exists(shortlibp):
221 return shortlibp
222 elif os.path.exists(longlibp):
223 return longlibp
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000224
Andrew MacIntyre4104db32002-08-04 06:21:25 +0000225 # Oops, didn't find it in *any* of 'dirs'
226 return None
227
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000228# class EMXCCompiler
229
230
231# Because these compilers aren't configured in Python's pyconfig.h file by
232# default, we should at least warn the user if he is using a unmodified
233# version.
234
235CONFIG_H_OK = "ok"
236CONFIG_H_NOTOK = "not ok"
237CONFIG_H_UNCERTAIN = "uncertain"
238
239def check_config_h():
240
241 """Check if the current Python installation (specifically, pyconfig.h)
242 appears amenable to building extensions with GCC. Returns a tuple
243 (status, details), where 'status' is one of the following constants:
244 CONFIG_H_OK
245 all is well, go ahead and compile
246 CONFIG_H_NOTOK
247 doesn't look good
248 CONFIG_H_UNCERTAIN
249 not sure -- unable to read pyconfig.h
250 'details' is a human-readable string explaining the situation.
251
252 Note there are two ways to conclude "OK": either 'sys.version' contains
253 the string "GCC" (implying that this Python was built with GCC), or the
254 installed "pyconfig.h" contains the string "__GNUC__".
255 """
256
257 # XXX since this function also checks sys.version, it's not strictly a
258 # "pyconfig.h" check -- should probably be renamed...
259
260 from distutils import sysconfig
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000261 # if sys.version contains GCC then python was compiled with
262 # GCC, and the pyconfig.h file should be OK
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000263 if sys.version.find("GCC") >= 0:
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000264 return (CONFIG_H_OK, "sys.version mentions 'GCC'")
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000265
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000266 fn = sysconfig.get_config_h_filename()
267 try:
268 # It would probably better to read single lines to search.
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000269 # But we do this only once, and it is fast enough
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000270 f = open(fn)
Éric Araujobee5cef2010-11-05 23:51:56 +0000271 try:
272 s = f.read()
273 finally:
274 f.close()
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000275
Guido van Rossumb940e112007-01-10 16:19:56 +0000276 except IOError as exc:
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000277 # if we can't read this file, we cannot say it is wrong
278 # the compiler will complain later about this file as missing
279 return (CONFIG_H_UNCERTAIN,
280 "couldn't read '%s': %s" % (fn, exc.strerror))
281
282 else:
283 # "pyconfig.h" contains an "#ifdef __GNUC__" or something similar
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000284 if s.find("__GNUC__") >= 0:
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000285 return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn)
286 else:
287 return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn)
288
289
290def get_versions():
291 """ Try to find out the versions of gcc and ld.
292 If not possible it returns None for it.
293 """
Tarek Ziadé36797272010-07-22 12:50:05 +0000294 from distutils.version import StrictVersion
295 from distutils.spawn import find_executable
296 import re
Guido van Rossumbffa52f2002-09-29 00:25:51 +0000297
Tarek Ziadé36797272010-07-22 12:50:05 +0000298 gcc_exe = find_executable('gcc')
299 if gcc_exe:
300 out = os.popen(gcc_exe + ' -dumpversion','r')
Éric Araujobee5cef2010-11-05 23:51:56 +0000301 try:
302 out_string = out.read()
303 finally:
304 out.close()
Tarek Ziadé36797272010-07-22 12:50:05 +0000305 result = re.search('(\d+\.\d+\.\d+)', out_string, re.ASCII)
306 if result:
307 gcc_version = StrictVersion(result.group(1))
308 else:
309 gcc_version = None
310 else:
311 gcc_version = None
Marc-André Lemburg9273ec72002-02-06 18:22:48 +0000312 # EMX ld has no way of reporting version number, and we use GCC
313 # anyway - so we can link OMF DLLs
Tarek Ziadé36797272010-07-22 12:50:05 +0000314 ld_version = None
315 return (gcc_version, ld_version)