| 1 |
"""distutils.cygwinccompiler |
| 2 |
|
| 3 |
Provides the CygwinCCompiler class, a subclass of UnixCCompiler that |
| 4 |
handles the Cygwin port of the GNU C compiler to Windows. It also contains |
| 5 |
the Mingw32CCompiler class which handles the mingw32 port of GCC (same as |
| 6 |
cygwin in no-cygwin mode). |
| 7 |
""" |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
__revision__ = "$Id$" |
| 51 |
|
| 52 |
import os,sys,copy |
| 53 |
from distutils.ccompiler import gen_preprocess_options, gen_lib_options |
| 54 |
from distutils.unixccompiler import UnixCCompiler |
| 55 |
from distutils.file_util import write_file |
| 56 |
from distutils.errors import DistutilsExecError, CompileError, UnknownFileError |
| 57 |
from distutils import log |
| 58 |
|
| 59 |
def get_msvcr(): |
| 60 |
"""Include the appropriate MSVC runtime library if Python was built |
| 61 |
with MSVC 7.0 or later. |
| 62 |
""" |
| 63 |
msc_pos = sys.version.find('MSC v.') |
| 64 |
if msc_pos != -1: |
| 65 |
msc_ver = sys.version[msc_pos+6:msc_pos+10] |
| 66 |
if msc_ver == '1300': |
| 67 |
|
| 68 |
return ['msvcr70'] |
| 69 |
elif msc_ver == '1310': |
| 70 |
|
| 71 |
return ['msvcr71'] |
| 72 |
elif msc_ver == '1400': |
| 73 |
|
| 74 |
return ['msvcr80'] |
| 75 |
elif msc_ver == '1500': |
| 76 |
|
| 77 |
return ['msvcr90'] |
| 78 |
else: |
| 79 |
raise ValueError("Unknown MS Compiler version %s " % msc_ver) |
| 80 |
|
| 81 |
|
| 82 |
class CygwinCCompiler (UnixCCompiler): |
| 83 |
|
| 84 |
compiler_type = 'cygwin' |
| 85 |
obj_extension = ".o" |
| 86 |
static_lib_extension = ".a" |
| 87 |
shared_lib_extension = ".dll" |
| 88 |
static_lib_format = "lib%s%s" |
| 89 |
shared_lib_format = "%s%s" |
| 90 |
exe_extension = ".exe" |
| 91 |
|
| 92 |
def __init__ (self, verbose=0, dry_run=0, force=0): |
| 93 |
|
| 94 |
UnixCCompiler.__init__ (self, verbose, dry_run, force) |
| 95 |
|
| 96 |
(status, details) = check_config_h() |
| 97 |
self.debug_print("Python's GCC status: %s (details: %s)" % |
| 98 |
(status, details)) |
| 99 |
if status is not CONFIG_H_OK: |
| 100 |
self.warn( |
| 101 |
"Python's pyconfig.h doesn't seem to support your compiler. " |
| 102 |
"Reason: %s. " |
| 103 |
"Compiling may fail because of undefined preprocessor macros." |
| 104 |
% details) |
| 105 |
|
| 106 |
self.gcc_version, self.ld_version, self.dllwrap_version = \ |
| 107 |
get_versions() |
| 108 |
self.debug_print(self.compiler_type + ": gcc %s, ld %s, dllwrap %s\n" % |
| 109 |
(self.gcc_version, |
| 110 |
self.ld_version, |
| 111 |
self.dllwrap_version) ) |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
if self.ld_version >= "2.10.90": |
| 119 |
self.linker_dll = "gcc" |
| 120 |
else: |
| 121 |
self.linker_dll = "dllwrap" |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
if self.ld_version >= "2.13": |
| 126 |
shared_option = "-shared" |
| 127 |
else: |
| 128 |
shared_option = "-mdll -static" |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
self.set_executables(compiler='gcc -mcygwin -O -Wall', |
| 133 |
compiler_so='gcc -mcygwin -mdll -O -Wall', |
| 134 |
compiler_cxx='g++ -mcygwin -O -Wall', |
| 135 |
linker_exe='gcc -mcygwin', |
| 136 |
linker_so=('%s -mcygwin %s' % |
| 137 |
(self.linker_dll, shared_option))) |
| 138 |
|
| 139 |
|
| 140 |
if self.gcc_version == "2.91.57": |
| 141 |
|
| 142 |
|
| 143 |
self.dll_libraries=["msvcrt"] |
| 144 |
self.warn( |
| 145 |
"Consider upgrading to a newer version of gcc") |
| 146 |
else: |
| 147 |
|
| 148 |
|
| 149 |
self.dll_libraries = get_msvcr() |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts): |
| 155 |
if ext == '.rc' or ext == '.res': |
| 156 |
|
| 157 |
try: |
| 158 |
self.spawn(["windres", "-i", src, "-o", obj]) |
| 159 |
except DistutilsExecError, msg: |
| 160 |
raise CompileError, msg |
| 161 |
else: |
| 162 |
try: |
| 163 |
self.spawn(self.compiler_so + cc_args + [src, '-o', obj] + |
| 164 |
extra_postargs) |
| 165 |
except DistutilsExecError, msg: |
| 166 |
raise CompileError, msg |
| 167 |
|
| 168 |
def link (self, |
| 169 |
target_desc, |
| 170 |
objects, |
| 171 |
output_filename, |
| 172 |
output_dir=None, |
| 173 |
libraries=None, |
| 174 |
library_dirs=None, |
| 175 |
runtime_library_dirs=None, |
| 176 |
export_symbols=None, |
| 177 |
debug=0, |
| 178 |
extra_preargs=None, |
| 179 |
extra_postargs=None, |
| 180 |
build_temp=None, |
| 181 |
target_lang=None): |
| 182 |
|
| 183 |
|
| 184 |
extra_preargs = copy.copy(extra_preargs or []) |
| 185 |
libraries = copy.copy(libraries or []) |
| 186 |
objects = copy.copy(objects or []) |
| 187 |
|
| 188 |
|
| 189 |
libraries.extend(self.dll_libraries) |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if ((export_symbols is not None) and |
| 194 |
(target_desc != self.EXECUTABLE or self.linker_dll == "gcc")): |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
temp_dir = os.path.dirname(objects[0]) |
| 204 |
|
| 205 |
(dll_name, dll_extension) = os.path.splitext( |
| 206 |
os.path.basename(output_filename)) |
| 207 |
|
| 208 |
|
| 209 |
def_file = os.path.join(temp_dir, dll_name + ".def") |
| 210 |
lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a") |
| 211 |
|
| 212 |
|
| 213 |
contents = [ |
| 214 |
"LIBRARY %s" % os.path.basename(output_filename), |
| 215 |
"EXPORTS"] |
| 216 |
for sym in export_symbols: |
| 217 |
contents.append(sym) |
| 218 |
self.execute(write_file, (def_file, contents), |
| 219 |
"writing %s" % def_file) |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
if self.linker_dll == "dllwrap": |
| 225 |
extra_preargs.extend(["--output-lib", lib_file]) |
| 226 |
|
| 227 |
extra_preargs.extend(["--def", def_file]) |
| 228 |
|
| 229 |
else: |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
objects.append(def_file) |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
if not debug: |
| 245 |
extra_preargs.append("-s") |
| 246 |
|
| 247 |
UnixCCompiler.link(self, |
| 248 |
target_desc, |
| 249 |
objects, |
| 250 |
output_filename, |
| 251 |
output_dir, |
| 252 |
libraries, |
| 253 |
library_dirs, |
| 254 |
runtime_library_dirs, |
| 255 |
None, |
| 256 |
debug, |
| 257 |
extra_preargs, |
| 258 |
extra_postargs, |
| 259 |
build_temp, |
| 260 |
target_lang) |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
def object_filenames (self, |
| 268 |
source_filenames, |
| 269 |
strip_dir=0, |
| 270 |
output_dir=''): |
| 271 |
if output_dir is None: output_dir = '' |
| 272 |
obj_names = [] |
| 273 |
for src_name in source_filenames: |
| 274 |
|
| 275 |
(base, ext) = os.path.splitext (os.path.normcase(src_name)) |
| 276 |
if ext not in (self.src_extensions + ['.rc','.res']): |
| 277 |
raise UnknownFileError, \ |
| 278 |
"unknown file type '%s' (from '%s')" % \ |
| 279 |
(ext, src_name) |
| 280 |
if strip_dir: |
| 281 |
base = os.path.basename (base) |
| 282 |
if ext == '.res' or ext == '.rc': |
| 283 |
|
| 284 |
obj_names.append (os.path.join (output_dir, |
| 285 |
base + ext + self.obj_extension)) |
| 286 |
else: |
| 287 |
obj_names.append (os.path.join (output_dir, |
| 288 |
base + self.obj_extension)) |
| 289 |
return obj_names |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
class Mingw32CCompiler (CygwinCCompiler): |
| 298 |
|
| 299 |
compiler_type = 'mingw32' |
| 300 |
|
| 301 |
def __init__ (self, |
| 302 |
verbose=0, |
| 303 |
dry_run=0, |
| 304 |
force=0): |
| 305 |
|
| 306 |
CygwinCCompiler.__init__ (self, verbose, dry_run, force) |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if self.ld_version >= "2.13": |
| 311 |
shared_option = "-shared" |
| 312 |
else: |
| 313 |
shared_option = "-mdll -static" |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if self.gcc_version <= "2.91.57": |
| 318 |
entry_point = '--entry _DllMain@12' |
| 319 |
else: |
| 320 |
entry_point = '' |
| 321 |
|
| 322 |
if self.gcc_version < '4' or is_cygwingcc(): |
| 323 |
no_cygwin = ' -mno-cygwin' |
| 324 |
else: |
| 325 |
no_cygwin = '' |
| 326 |
|
| 327 |
self.set_executables(compiler='gcc%s -O -Wall' % no_cygwin, |
| 328 |
compiler_so='gcc%s -mdll -O -Wall' % no_cygwin, |
| 329 |
compiler_cxx='g++%s -O -Wall' % no_cygwin, |
| 330 |
linker_exe='gcc%s' % no_cygwin, |
| 331 |
linker_so='%s%s %s %s' |
| 332 |
% (self.linker_dll, no_cygwin, |
| 333 |
shared_option, entry_point)) |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
self.dll_libraries=[] |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
self.dll_libraries = get_msvcr() |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
CONFIG_H_OK = "ok" |
| 354 |
CONFIG_H_NOTOK = "not ok" |
| 355 |
CONFIG_H_UNCERTAIN = "uncertain" |
| 356 |
|
| 357 |
def check_config_h(): |
| 358 |
|
| 359 |
"""Check if the current Python installation (specifically, pyconfig.h) |
| 360 |
appears amenable to building extensions with GCC. Returns a tuple |
| 361 |
(status, details), where 'status' is one of the following constants: |
| 362 |
CONFIG_H_OK |
| 363 |
all is well, go ahead and compile |
| 364 |
CONFIG_H_NOTOK |
| 365 |
doesn't look good |
| 366 |
CONFIG_H_UNCERTAIN |
| 367 |
not sure -- unable to read pyconfig.h |
| 368 |
'details' is a human-readable string explaining the situation. |
| 369 |
|
| 370 |
Note there are two ways to conclude "OK": either 'sys.version' contains |
| 371 |
the string "GCC" (implying that this Python was built with GCC), or the |
| 372 |
installed "pyconfig.h" contains the string "__GNUC__". |
| 373 |
""" |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
from distutils import sysconfig |
| 379 |
import string |
| 380 |
|
| 381 |
|
| 382 |
if string.find(sys.version,"GCC") >= 0: |
| 383 |
return (CONFIG_H_OK, "sys.version mentions 'GCC'") |
| 384 |
|
| 385 |
fn = sysconfig.get_config_h_filename() |
| 386 |
try: |
| 387 |
|
| 388 |
|
| 389 |
f = open(fn) |
| 390 |
try: |
| 391 |
s = f.read() |
| 392 |
finally: |
| 393 |
f.close() |
| 394 |
|
| 395 |
except IOError, exc: |
| 396 |
|
| 397 |
|
| 398 |
return (CONFIG_H_UNCERTAIN, |
| 399 |
"couldn't read '%s': %s" % (fn, exc.strerror)) |
| 400 |
|
| 401 |
else: |
| 402 |
|
| 403 |
if string.find(s,"__GNUC__") >= 0: |
| 404 |
return (CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn) |
| 405 |
else: |
| 406 |
return (CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn) |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
def get_versions(): |
| 411 |
""" Try to find out the versions of gcc, ld and dllwrap. |
| 412 |
If not possible it returns None for it. |
| 413 |
""" |
| 414 |
from distutils.version import LooseVersion |
| 415 |
from distutils.spawn import find_executable |
| 416 |
import re |
| 417 |
|
| 418 |
gcc_exe = find_executable('gcc') |
| 419 |
if gcc_exe: |
| 420 |
out = os.popen(gcc_exe + ' -dumpversion','r') |
| 421 |
out_string = out.read() |
| 422 |
out.close() |
| 423 |
result = re.search('(\d+\.\d+(\.\d+)*)',out_string) |
| 424 |
if result: |
| 425 |
gcc_version = LooseVersion(result.group(1)) |
| 426 |
else: |
| 427 |
gcc_version = None |
| 428 |
else: |
| 429 |
gcc_version = None |
| 430 |
ld_exe = find_executable('ld') |
| 431 |
if ld_exe: |
| 432 |
out = os.popen(ld_exe + ' -v','r') |
| 433 |
out_string = out.read() |
| 434 |
out.close() |
| 435 |
result = re.search('(\d+\.\d+(\.\d+)*)',out_string) |
| 436 |
if result: |
| 437 |
ld_version = LooseVersion(result.group(1)) |
| 438 |
else: |
| 439 |
ld_version = None |
| 440 |
else: |
| 441 |
ld_version = None |
| 442 |
dllwrap_exe = find_executable('dllwrap') |
| 443 |
if dllwrap_exe: |
| 444 |
out = os.popen(dllwrap_exe + ' --version','r') |
| 445 |
out_string = out.read() |
| 446 |
out.close() |
| 447 |
result = re.search(' (\d+\.\d+(\.\d+)*)',out_string) |
| 448 |
if result: |
| 449 |
dllwrap_version = LooseVersion(result.group(1)) |
| 450 |
else: |
| 451 |
dllwrap_version = None |
| 452 |
else: |
| 453 |
dllwrap_version = None |
| 454 |
return (gcc_version, ld_version, dllwrap_version) |
| 455 |
|
| 456 |
def is_cygwingcc(): |
| 457 |
'''Try to determine if the gcc that would be used is from cygwin.''' |
| 458 |
out = os.popen('gcc -dumpmachine', 'r') |
| 459 |
out_string = out.read() |
| 460 |
out.close() |
| 461 |
|
| 462 |
|
| 463 |
return out_string.strip().endswith('cygwin') |