Changeset 391 for python/trunk/Lib/distutils/ccompiler.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/distutils/ccompiler.py
r10 r391 4 4 for the Distutils compiler abstraction model.""" 5 5 6 # This module should be kept compatible with Python 2.1. 7 8 __revision__ = "$Id: ccompiler.py 77425 2010-01-11 22:54:57Z tarek.ziade $" 9 10 import sys, os,re11 from types import * 12 from copy import copy13 from distutils.errors import * 6 __revision__ = "$Id$" 7 8 import sys 9 import os 10 import re 11 12 from distutils.errors import (CompileError, LinkError, UnknownFileError, 13 DistutilsPlatformError, DistutilsModuleError) 14 14 from distutils.spawn import spawn 15 15 from distutils.file_util import move_file 16 16 from distutils.dir_util import mkpath 17 from distutils.dep_util import newer_ pairwise, newer_group17 from distutils.dep_util import newer_group 18 18 from distutils.util import split_quoted, execute 19 19 from distutils import log 20 # following import is for backward compatibility 21 from distutils.sysconfig import customize_compiler 20 22 21 23 class CCompiler: … … 89 91 language_order = ["c++", "objc", "c"] 90 92 91 def __init__ (self, 92 verbose=0, 93 dry_run=0, 94 force=0): 95 93 def __init__ (self, verbose=0, dry_run=0, force=0): 96 94 self.dry_run = dry_run 97 95 self.force = force … … 129 127 self.set_executable(key, self.executables[key]) 130 128 131 # __init__ () 132 133 134 def set_executables (self, **args): 135 129 def set_executables(self, **args): 136 130 """Define the executables (and options for them) that will be run 137 131 to perform the various stages of compilation. The exact set of … … 166 160 self.set_executable(key, args[key]) 167 161 168 # set_executables ()169 170 162 def set_executable(self, key, value): 171 if type(value) is StringType:163 if isinstance(value, str): 172 164 setattr(self, key, split_quoted(value)) 173 165 else: 174 166 setattr(self, key, value) 175 167 176 177 def _find_macro (self, name): 168 def _find_macro(self, name): 178 169 i = 0 179 170 for defn in self.macros: … … 181 172 return i 182 173 i = i + 1 183 184 174 return None 185 175 186 187 def _check_macro_definitions (self, definitions): 176 def _check_macro_definitions(self, definitions): 188 177 """Ensures that every element of 'definitions' is a valid macro 189 178 definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do … … 191 180 """ 192 181 for defn in definitions: 193 if not ( type (defn) is TupleTypeand182 if not (isinstance(defn, tuple) and 194 183 (len (defn) == 1 or 195 184 (len (defn) == 2 and 196 ( type (defn[1]) is StringTypeor defn[1] is None))) and197 type (defn[0]) is StringType):185 (isinstance(defn[1], str) or defn[1] is None))) and 186 isinstance(defn[0], str)): 198 187 raise TypeError, \ 199 188 ("invalid macro definition '%s': " % defn) + \ … … 204 193 # -- Bookkeeping methods ------------------------------------------- 205 194 206 def define_macro 195 def define_macro(self, name, value=None): 207 196 """Define a preprocessor macro for all compilations driven by this 208 197 compiler object. The optional parameter 'value' should be a … … 220 209 self.macros.append (defn) 221 210 222 223 def undefine_macro (self, name): 211 def undefine_macro(self, name): 224 212 """Undefine a preprocessor macro for all compilations driven by 225 213 this compiler object. If the same macro is defined by … … 239 227 self.macros.append (undefn) 240 228 241 242 def add_include_dir (self, dir): 229 def add_include_dir(self, dir): 243 230 """Add 'dir' to the list of directories that will be searched for 244 231 header files. The compiler is instructed to search directories in … … 248 235 self.include_dirs.append (dir) 249 236 250 def set_include_dirs 237 def set_include_dirs(self, dirs): 251 238 """Set the list of directories that will be searched to 'dirs' (a 252 239 list of strings). Overrides any preceding calls to … … 256 243 search by default. 257 244 """ 258 self.include_dirs = copy (dirs) 259 260 261 def add_library (self, libname): 245 self.include_dirs = dirs[:] 246 247 def add_library(self, libname): 262 248 """Add 'libname' to the list of libraries that will be included in 263 249 all links driven by this compiler object. Note that 'libname' … … 275 261 self.libraries.append (libname) 276 262 277 def set_libraries 263 def set_libraries(self, libnames): 278 264 """Set the list of libraries to be included in all links driven by 279 265 this compiler object to 'libnames' (a list of strings). This does … … 281 267 include by default. 282 268 """ 283 self.libraries = copy (libnames)284 285 286 def add_library_dir 269 self.libraries = libnames[:] 270 271 272 def add_library_dir(self, dir): 287 273 """Add 'dir' to the list of directories that will be searched for 288 274 libraries specified to 'add_library()' and 'set_libraries()'. The … … 290 276 are supplied to 'add_library_dir()' and/or 'set_library_dirs()'. 291 277 """ 292 self.library_dirs.append 293 294 def set_library_dirs 278 self.library_dirs.append(dir) 279 280 def set_library_dirs(self, dirs): 295 281 """Set the list of library search directories to 'dirs' (a list of 296 282 strings). This does not affect any standard library search path 297 283 that the linker may search by default. 298 284 """ 299 self.library_dirs = copy (dirs) 300 301 302 def add_runtime_library_dir (self, dir): 285 self.library_dirs = dirs[:] 286 287 def add_runtime_library_dir(self, dir): 303 288 """Add 'dir' to the list of directories that will be searched for 304 289 shared libraries at runtime. 305 290 """ 306 self.runtime_library_dirs.append 307 308 def set_runtime_library_dirs 291 self.runtime_library_dirs.append(dir) 292 293 def set_runtime_library_dirs(self, dirs): 309 294 """Set the list of directories to search for shared libraries at 310 295 runtime to 'dirs' (a list of strings). This does not affect any … … 312 297 default. 313 298 """ 314 self.runtime_library_dirs = copy (dirs) 315 316 317 def add_link_object (self, object): 299 self.runtime_library_dirs = dirs[:] 300 301 def add_link_object(self, object): 318 302 """Add 'object' to the list of object files (or analogues, such as 319 303 explicitly named library files or the output of "resource … … 321 305 object. 322 306 """ 323 self.objects.append 324 325 def set_link_objects 307 self.objects.append(object) 308 309 def set_link_objects(self, objects): 326 310 """Set the list of object files (or analogues) to be included in 327 311 every link to 'objects'. This does not affect any standard object … … 329 313 libraries). 330 314 """ 331 self.objects = copy (objects)315 self.objects = objects[:] 332 316 333 317 … … 342 326 if outdir is None: 343 327 outdir = self.output_dir 344 elif type(outdir) is not StringType:328 elif not isinstance(outdir, str): 345 329 raise TypeError, "'output_dir' must be a string or None" 346 330 347 331 if macros is None: 348 332 macros = self.macros 349 elif type(macros) is ListType:333 elif isinstance(macros, list): 350 334 macros = macros + (self.macros or []) 351 335 else: … … 354 338 if incdirs is None: 355 339 incdirs = self.include_dirs 356 elif type(incdirs) in (ListType, TupleType):340 elif isinstance(incdirs, (list, tuple)): 357 341 incdirs = list(incdirs) + (self.include_dirs or []) 358 342 else: … … 390 374 return cc_args 391 375 392 def _fix_compile_args 376 def _fix_compile_args(self, output_dir, macros, include_dirs): 393 377 """Typecheck and fix-up some of the arguments to the 'compile()' 394 378 method, and return fixed-up values. Specifically: if 'output_dir' … … 402 386 if output_dir is None: 403 387 output_dir = self.output_dir 404 elif type (output_dir) is not StringType:388 elif not isinstance(output_dir, str): 405 389 raise TypeError, "'output_dir' must be a string or None" 406 390 407 391 if macros is None: 408 392 macros = self.macros 409 elif type (macros) is ListType:393 elif isinstance(macros, list): 410 394 macros = macros + (self.macros or []) 411 395 else: … … 414 398 if include_dirs is None: 415 399 include_dirs = self.include_dirs 416 elif type (include_dirs) in (ListType, TupleType):400 elif isinstance(include_dirs, (list, tuple)): 417 401 include_dirs = list (include_dirs) + (self.include_dirs or []) 418 402 else: … … 422 406 return output_dir, macros, include_dirs 423 407 424 # _fix_compile_args () 425 426 def _prep_compile(self, sources, output_dir, depends=None): 427 """Decide which souce files must be recompiled. 428 429 Determine the list of object files corresponding to 'sources', 430 and figure out which ones really need to be recompiled. 431 Return a list of all object files and a dictionary telling 432 which source files can be skipped. 433 """ 434 # Get the list of expected output (object) files 435 objects = self.object_filenames(sources, output_dir=output_dir) 436 assert len(objects) == len(sources) 437 438 # Return an empty dict for the "which source files can be skipped" 439 # return value to preserve API compatibility. 440 return objects, {} 441 442 def _fix_object_args (self, objects, output_dir): 408 def _fix_object_args(self, objects, output_dir): 443 409 """Typecheck and fix up some arguments supplied to various methods. 444 410 Specifically: ensure that 'objects' is a list; if output_dir is … … 446 412 'objects' and 'output_dir'. 447 413 """ 448 if type (objects) not in (ListType, TupleType):414 if not isinstance(objects, (list, tuple)): 449 415 raise TypeError, \ 450 416 "'objects' must be a list or tuple of strings" … … 453 419 if output_dir is None: 454 420 output_dir = self.output_dir 455 elif type (output_dir) is not StringType:421 elif not isinstance(output_dir, str): 456 422 raise TypeError, "'output_dir' must be a string or None" 457 423 458 424 return (objects, output_dir) 459 425 460 461 def _fix_lib_args (self, libraries, library_dirs, runtime_library_dirs): 426 def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs): 462 427 """Typecheck and fix up some of the arguments supplied to the 463 428 'link_*' methods. Specifically: ensure that all arguments are … … 468 433 if libraries is None: 469 434 libraries = self.libraries 470 elif type (libraries) in (ListType, TupleType):435 elif isinstance(libraries, (list, tuple)): 471 436 libraries = list (libraries) + (self.libraries or []) 472 437 else: … … 476 441 if library_dirs is None: 477 442 library_dirs = self.library_dirs 478 elif type (library_dirs) in (ListType, TupleType):443 elif isinstance(library_dirs, (list, tuple)): 479 444 library_dirs = list (library_dirs) + (self.library_dirs or []) 480 445 else: … … 484 449 if runtime_library_dirs is None: 485 450 runtime_library_dirs = self.runtime_library_dirs 486 elif type (runtime_library_dirs) in (ListType, TupleType):451 elif isinstance(runtime_library_dirs, (list, tuple)): 487 452 runtime_library_dirs = (list (runtime_library_dirs) + 488 453 (self.runtime_library_dirs or [])) … … 494 459 return (libraries, library_dirs, runtime_library_dirs) 495 460 496 # _fix_lib_args () 497 498 499 def _need_link (self, objects, output_file): 461 def _need_link(self, objects, output_file): 500 462 """Return true if we need to relink the files listed in 'objects' 501 463 to recreate 'output_file'. … … 510 472 return newer 511 473 512 # _need_link () 513 514 def detect_language (self, sources): 474 def detect_language(self, sources): 515 475 """Detect the language of a given file, or list of files. Uses 516 476 language_map, and language_order to do the job. 517 477 """ 518 if type(sources) is not ListType:478 if not isinstance(sources, list): 519 479 sources = [sources] 520 480 lang = None … … 532 492 return lang 533 493 534 # detect_language ()535 536 494 # -- Worker methods ------------------------------------------------ 537 495 # (must be implemented by subclasses) 538 496 539 def preprocess (self, 540 source, 541 output_file=None, 542 macros=None, 543 include_dirs=None, 544 extra_preargs=None, 545 extra_postargs=None): 497 def preprocess(self, source, output_file=None, macros=None, 498 include_dirs=None, extra_preargs=None, extra_postargs=None): 546 499 """Preprocess a single C/C++ source file, named in 'source'. 547 500 Output will be written to file named 'output_file', or stdout if … … 631 584 pass 632 585 633 def create_static_lib (self, 634 objects, 635 output_libname, 636 output_dir=None, 637 debug=0, 638 target_lang=None): 586 def create_static_lib(self, objects, output_libname, output_dir=None, 587 debug=0, target_lang=None): 639 588 """Link a bunch of stuff together to create a static library file. 640 589 The "bunch of stuff" consists of the list of object files supplied … … 661 610 pass 662 611 663 664 612 # values for target_desc parameter in link() 665 613 SHARED_OBJECT = "shared_object" … … 667 615 EXECUTABLE = "executable" 668 616 669 def link (self, 670 target_desc, 671 objects, 672 output_filename, 673 output_dir=None, 674 libraries=None, 675 library_dirs=None, 676 runtime_library_dirs=None, 677 export_symbols=None, 678 debug=0, 679 extra_preargs=None, 680 extra_postargs=None, 681 build_temp=None, 682 target_lang=None): 617 def link(self, target_desc, objects, output_filename, output_dir=None, 618 libraries=None, library_dirs=None, runtime_library_dirs=None, 619 export_symbols=None, debug=0, extra_preargs=None, 620 extra_postargs=None, build_temp=None, target_lang=None): 683 621 """Link a bunch of stuff together to create an executable or 684 622 shared library file. … … 729 667 # Old 'link_*()' methods, rewritten to use the new 'link()' method. 730 668 731 def link_shared_lib (self, 732 objects, 733 output_libname, 734 output_dir=None, 735 libraries=None, 736 library_dirs=None, 737 runtime_library_dirs=None, 738 export_symbols=None, 739 debug=0, 740 extra_preargs=None, 741 extra_postargs=None, 742 build_temp=None, 743 target_lang=None): 669 def link_shared_lib(self, objects, output_libname, output_dir=None, 670 libraries=None, library_dirs=None, 671 runtime_library_dirs=None, export_symbols=None, 672 debug=0, extra_preargs=None, extra_postargs=None, 673 build_temp=None, target_lang=None): 744 674 self.link(CCompiler.SHARED_LIBRARY, objects, 745 675 self.library_filename(output_libname, lib_type='shared'), … … 750 680 751 681 752 def link_shared_object (self, 753 objects, 754 output_filename, 755 output_dir=None, 756 libraries=None, 757 library_dirs=None, 758 runtime_library_dirs=None, 759 export_symbols=None, 760 debug=0, 761 extra_preargs=None, 762 extra_postargs=None, 763 build_temp=None, 764 target_lang=None): 682 def link_shared_object(self, objects, output_filename, output_dir=None, 683 libraries=None, library_dirs=None, 684 runtime_library_dirs=None, export_symbols=None, 685 debug=0, extra_preargs=None, extra_postargs=None, 686 build_temp=None, target_lang=None): 765 687 self.link(CCompiler.SHARED_OBJECT, objects, 766 688 output_filename, output_dir, … … 769 691 extra_preargs, extra_postargs, build_temp, target_lang) 770 692 771 772 def link_executable (self, 773 objects, 774 output_progname, 775 output_dir=None, 776 libraries=None, 777 library_dirs=None, 778 runtime_library_dirs=None, 779 debug=0, 780 extra_preargs=None, 781 extra_postargs=None, 782 target_lang=None): 693 def link_executable(self, objects, output_progname, output_dir=None, 694 libraries=None, library_dirs=None, 695 runtime_library_dirs=None, debug=0, extra_preargs=None, 696 extra_postargs=None, target_lang=None): 783 697 self.link(CCompiler.EXECUTABLE, objects, 784 698 self.executable_filename(output_progname), output_dir, … … 792 706 # implement all of these. 793 707 794 def library_dir_option 708 def library_dir_option(self, dir): 795 709 """Return the compiler option to add 'dir' to the list of 796 710 directories searched for libraries. … … 798 712 raise NotImplementedError 799 713 800 def runtime_library_dir_option 714 def runtime_library_dir_option(self, dir): 801 715 """Return the compiler option to add 'dir' to the list of 802 716 directories searched for runtime libraries. … … 804 718 raise NotImplementedError 805 719 806 def library_option 720 def library_option(self, lib): 807 721 """Return the compiler option to add 'dir' to the list of libraries 808 722 linked into the shared library or executable. … … 810 724 raise NotImplementedError 811 725 812 def has_function(self, funcname, 813 includes=None, 814 include_dirs=None, 815 libraries=None, 816 library_dirs=None): 726 def has_function(self, funcname, includes=None, include_dirs=None, 727 libraries=None, library_dirs=None): 817 728 """Return a boolean indicating whether funcname is supported on 818 729 the current platform. The optional arguments can be used to … … 834 745 fd, fname = tempfile.mkstemp(".c", funcname, text=True) 835 746 f = os.fdopen(fd, "w") 836 for incl in includes: 837 f.write("""#include "%s"\n""" % incl) 838 f.write("""\ 747 try: 748 for incl in includes: 749 f.write("""#include "%s"\n""" % incl) 750 f.write("""\ 839 751 main (int argc, char **argv) { 840 752 %s(); 841 753 } 842 754 """ % funcname) 843 f.close() 755 finally: 756 f.close() 844 757 try: 845 758 objects = self.compile([fname], include_dirs=include_dirs) … … 945 858 # -- Utility methods ----------------------------------------------- 946 859 947 def announce 860 def announce(self, msg, level=1): 948 861 log.debug(msg) 949 862 950 def debug_print 863 def debug_print(self, msg): 951 864 from distutils.debug import DEBUG 952 865 if DEBUG: 953 866 print msg 954 867 955 def warn 956 sys.stderr.write 957 958 def execute 868 def warn(self, msg): 869 sys.stderr.write("warning: %s\n" % msg) 870 871 def execute(self, func, args, msg=None, level=1): 959 872 execute(func, args, msg, self.dry_run) 960 873 961 def spawn 962 spawn 963 964 def move_file 965 return move_file 966 967 def mkpath 968 mkpath 874 def spawn(self, cmd): 875 spawn(cmd, dry_run=self.dry_run) 876 877 def move_file(self, src, dst): 878 return move_file(src, dst, dry_run=self.dry_run) 879 880 def mkpath(self, name, mode=0777): 881 mkpath(name, mode, dry_run=self.dry_run) 969 882 970 883 … … 989 902 ('posix', 'unix'), 990 903 ('nt', 'msvc'), 991 ('mac', 'mwerks'),992 904 993 905 ) 994 906 995 907 def get_default_compiler(osname=None, platform=None): 996 997 908 """ Determine the default compiler to use for the given platform. 998 909 … … 1029 940 'bcpp': ('bcppcompiler', 'BCPPCompiler', 1030 941 "Borland C++ Compiler"), 1031 'mwerks': ('mwerkscompiler', 'MWerksCompiler',1032 "MetroWerks CodeWarrior"),1033 942 'emx': ('emxccompiler', 'EMXCCompiler', 1034 943 "EMX port of GNU C Compiler for OS/2"), … … 1052 961 1053 962 1054 def new_compiler (plat=None, 1055 compiler=None, 1056 verbose=0, 1057 dry_run=0, 1058 force=0): 963 def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0): 1059 964 """Generate an instance of some CCompiler subclass for the supplied 1060 965 platform/compiler combination. 'plat' defaults to 'os.name' … … 1098 1003 # with classes that expect verbose to be the first positional 1099 1004 # argument. 1100 return klass 1101 1102 1103 def gen_preprocess_options 1005 return klass(None, dry_run, force) 1006 1007 1008 def gen_preprocess_options(macros, include_dirs): 1104 1009 """Generate C pre-processor options (-D, -U, -I) as used by at least 1105 1010 two types of compilers: the typical Unix compiler and Visual C++. … … 1126 1031 for macro in macros: 1127 1032 1128 if not ( type (macro) is TupleTypeand1033 if not (isinstance(macro, tuple) and 1129 1034 1 <= len (macro) <= 2): 1130 1035 raise TypeError, \ … … 1149 1054 return pp_opts 1150 1055 1151 # gen_preprocess_options () 1152 1153 1154 def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries): 1056 1057 def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries): 1155 1058 """Generate linker options for searching library directories and 1156 linking with specific libraries. 'libraries' and 'library_dirs' are, 1157 respectively, lists of library names (not filenames!) and search 1158 directories. Returns a list of command-line options suitable for use 1159 with some compiler (depending on the two format strings passed in). 1059 linking with specific libraries. 1060 1061 'libraries' and 'library_dirs' are, respectively, lists of library names 1062 (not filenames!) and search directories. Returns a list of command-line 1063 options suitable for use with some compiler (depending on the two format 1064 strings passed in). 1160 1065 """ 1161 1066 lib_opts = [] 1162 1067 1163 1068 for dir in library_dirs: 1164 lib_opts.append (compiler.library_dir_option(dir))1069 lib_opts.append(compiler.library_dir_option(dir)) 1165 1070 1166 1071 for dir in runtime_library_dirs: 1167 opt = compiler.runtime_library_dir_option 1168 if type(opt) is ListType:1169 lib_opts = lib_opts + opt1072 opt = compiler.runtime_library_dir_option(dir) 1073 if isinstance(opt, list): 1074 lib_opts.extend(opt) 1170 1075 else: 1171 lib_opts.append 1076 lib_opts.append(opt) 1172 1077 1173 1078 # XXX it's important that we *not* remove redundant library mentions! … … 1178 1083 1179 1084 for lib in libraries: 1180 (lib_dir, lib_name) = os.path.split(lib)1181 if lib_dir :1182 lib_file = compiler.find_library_file 1183 if lib_file :1184 lib_opts.append 1085 lib_dir, lib_name = os.path.split(lib) 1086 if lib_dir != '': 1087 lib_file = compiler.find_library_file([lib_dir], lib_name) 1088 if lib_file is not None: 1089 lib_opts.append(lib_file) 1185 1090 else: 1186 compiler.warn 1187 1091 compiler.warn("no library file corresponding to " 1092 "'%s' found (skipping)" % lib) 1188 1093 else: 1189 lib_opts.append (compiler.library_option(lib))1094 lib_opts.append(compiler.library_option(lib)) 1190 1095 1191 1096 return lib_opts 1192 1193 # gen_lib_options ()
Note:
See TracChangeset
for help on using the changeset viewer.