[@sdZddlZddlZddlZddlTddlmZddlmZddl m Z ddl m Z m Z ddlmZmZdd lmZGd d d Zd d fdd fddffZddddZidddfd 6dddfd6dddfd6dddfd6d d!d"fd#6Zd$d%Zdddddd&d'Zd(d)Zd*d+ZdS),zdistutils.ccompiler Contains CCompiler, an abstract base class that defines the interface for the Distutils compiler abstraction model.N)*)spawn) move_file)mkpath)newer_pairwise newer_group) split_quotedexecute)logc @seZdZdZdZdZdZdZdZdZ dZ dZ idd6dd6dd6dd6d d 6Z dd dgZ d d d d d ZddZddZddZddZdddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Z d2d3Z!dd4d5Z"d6d7Z#d8d9Z$d:d;Z%d<d=Z&dddddd>d?Z'dddd dddd@dAZ(dBdCZ)dd ddDdEZ*dFZ+dGZ,dHZ-dddddd dddddIdJ Z.dddddd dddddKdL Z/dddddd dddddMdN Z0ddddd ddddOdPZ1dQdRZ2dSdTZ3dUdVZ4dddddWdXZ5d dYdZZ6d d[d\d]Z7d d[d^d_Z8d d[d`daZ9dbd d[dcddZ:dedfdgZ;dhdiZ<djdkZ=ddedldmZ>dndoZ?dpdqZ@drdsdtZAdS)u CCompileraAbstract base class to define the interface that must be implemented by real compiler classes. Also has some utility methods used by several compiler classes. The basic idea behind a compiler abstraction class is that each instance can be used for all the compile/link steps in building a single project. Thus, attributes common to all of those compile and link steps -- include directories, macros to define, libraries to link against, etc. -- are attributes of the compiler instance. To allow for variability in how individual files are treated, most of those attributes may be varied on a per-compilation or per-link basis. Ncz.czc++z.ccz.cppz.cxxZobjcz.mrcCs||_||_||_d|_g|_g|_g|_g|_g|_g|_ x.|j j D]}|j ||j |qjWdS)N) dry_runforceverbose output_dirmacros include_dirs libraries library_dirsruntime_library_dirsobjects executableskeysset_executable)selfrr rkeyr)/usr/lib/python3.4/distutils/ccompiler.py__init__Us          zCCompiler.__init__cKsWxP|D]H}||jkr;td||jjfn|j|||qWdS)aDefine the executables (and options for them) that will be run to perform the various stages of compilation. The exact set of executables that may be specified here depends on the compiler class (via the 'executables' class attribute), but most will have: compiler the C/C++ compiler linker_so linker used to create shared objects and libraries linker_exe linker used to create binary executables archiver static library creator On platforms with a command-line (Unix, DOS/Windows), each of these is a string that will be split into executable name and (optional) list of arguments. (Splitting the string is done similarly to how Unix shells operate: words are delimited by spaces, but quotes and backslashes can override this. See 'distutils.util.split_quoted()'.) z$unknown executable '%s' for class %sN)r ValueError __class____name__r)rkwargsrrrrset_executablesys  zCCompiler.set_executablescCs<t|tr(t||t|nt|||dS)N) isinstancestrsetattrr)rrvaluerrrrszCCompiler.set_executablecCs<d}x/|jD]$}|d|kr*|S|d7}qWdS)Nr)r)rnameidefnrrr _find_macros zCCompiler._find_macrocCsx|D]x}t|toat|dkoat|dtsQ|ddkoat|dtstd|ddqqWdS) zEnsures that every element of 'definitions' is a valid macro definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do nothing if all definitions are OK, raise TypeError otherwise. r(Nrzinvalid macro definition '%s': z.must be tuple (string,), (string, string), or z(string, None))r(r-)r$tuplelenr% TypeError)rZ definitionsr+rrr_check_macro_definitionss # z"CCompiler._check_macro_definitionscCsB|j|}|dk r(|j|=n|jj||fdS)a_Define a preprocessor macro for all compilations driven by this compiler object. The optional parameter 'value' should be a string; if it is not supplied, then the macro will be defined without an explicit value and the exact outcome depends on the compiler used (XXX true? does ANSI say anything about this?) N)r,rappend)rr)r'r*rrr define_macros   zCCompiler.define_macrocCsE|j|}|dk r(|j|=n|f}|jj|dS)aUndefine a preprocessor macro for all compilations driven by this compiler object. If the same macro is defined by 'define_macro()' and undefined by 'undefine_macro()' the last call takes precedence (including multiple redefinitions or undefinitions). If the macro is redefined/undefined on a per-compilation basis (ie. in the call to 'compile()'), then that takes precedence. N)r,rr2)rr)r*Zundefnrrrundefine_macros    zCCompiler.undefine_macrocCs|jj|dS)zAdd 'dir' to the list of directories that will be searched for header files. The compiler is instructed to search directories in the order in which they are supplied by successive calls to 'add_include_dir()'. N)rr2)rdirrrradd_include_dirszCCompiler.add_include_dircCs|dd|_dS)aySet the list of directories that will be searched to 'dirs' (a list of strings). Overrides any preceding calls to 'add_include_dir()'; subsequence calls to 'add_include_dir()' add to the list passed to 'set_include_dirs()'. This does not affect any list of standard include directories that the compiler may search by default. N)r)rdirsrrrset_include_dirsszCCompiler.set_include_dirscCs|jj|dS)aAdd 'libname' to the list of libraries that will be included in all links driven by this compiler object. Note that 'libname' should *not* be the name of a file containing a library, but the name of the library itself: the actual filename will be inferred by the linker, the compiler, or the compiler class (depending on the platform). The linker will be instructed to link against libraries in the order they were supplied to 'add_library()' and/or 'set_libraries()'. It is perfectly valid to duplicate library names; the linker will be instructed to link against libraries as many times as they are mentioned. N)rr2)rlibnamerrr add_libraryszCCompiler.add_librarycCs|dd|_dS)zSet the list of libraries to be included in all links driven by this compiler object to 'libnames' (a list of strings). This does not affect any standard system libraries that the linker may include by default. N)r)rZlibnamesrrr set_librariesszCCompiler.set_librariescCs|jj|dS)a'Add 'dir' to the list of directories that will be searched for libraries specified to 'add_library()' and 'set_libraries()'. The linker will be instructed to search for libraries in the order they are supplied to 'add_library_dir()' and/or 'set_library_dirs()'. N)rr2)rr5rrradd_library_dirszCCompiler.add_library_dircCs|dd|_dS)zSet the list of library search directories to 'dirs' (a list of strings). This does not affect any standard library search path that the linker may search by default. N)r)rr7rrrset_library_dirs szCCompiler.set_library_dirscCs|jj|dS)zlAdd 'dir' to the list of directories that will be searched for shared libraries at runtime. N)rr2)rr5rrradd_runtime_library_dirsz!CCompiler.add_runtime_library_dircCs|dd|_dS)zSet the list of directories to search for shared libraries at runtime to 'dirs' (a list of strings). This does not affect any standard search path that the runtime linker may search by default. N)r)rr7rrrset_runtime_library_dirssz"CCompiler.set_runtime_library_dirscCs|jj|dS)zAdd 'object' to the list of object files (or analogues, such as explicitly named library files or the output of "resource compilers") to be included in every link driven by this compiler object. N)rr2)robjectrrradd_link_object szCCompiler.add_link_objectcCs|dd|_dS)zSet the list of object files (or analogues) to be included in every link to 'objects'. This does not affect any standard object files that the linker may include by default (such as system libraries). N)r)rrrrrset_link_objects(szCCompiler.set_link_objectscCs|dkr|j}nt|ts6tdn|dkrN|j}n1t|trs||jplg}n td|dkr|j}n=t|ttfrt||jpg}n td|dkrg}n|j|ddd|}t |t |ks"t t ||}i} xmt t |D]Y} || } || } t jj| d} |jt jj| | | f| | 'runtime_library_dirs' (if supplied) must be a list of strings)rr$rDr.r0rr)rrrrrrr _fix_lib_argss&          zCCompiler._fix_lib_argscCsE|jr dS|jr.t||dd}nt||}|SdS)zjReturn true if we need to relink the files listed in 'objects' to recreate 'output_file'. TZmissingnewerN)rr r)rr output_filer\rrr _need_links   zCCompiler._need_linkc Cst|ts|g}nd}t|j}x~|D]v}tjj|\}}|jj|}y1|jj |}||kr|}|}nWq7t k rYq7Xq7W|S)z|Detect the language of a given file, or list of files. Uses language_map, and language_order to do the job. N) r$rDr/language_orderrIrJrK language_mapgetindexr) rrMZlangrbsourcebaserSZextlangZextindexrrrdetect_languages      zCCompiler.detect_languagecCsdS)aPreprocess a single C/C++ source file, named in 'source'. Output will be written to file named 'output_file', or stdout if 'output_file' not supplied. 'macros' is a list of macro definitions as for 'compile()', which will augment the macros set with 'define_macro()' and 'undefine_macro()'. 'include_dirs' is a list of directory names that will be added to the default list. Raises PreprocessError on failure. Nr)rrcr]rr extra_preargsextra_postargsrrr preprocesss zCCompiler.preprocessc Cs|j||||||\}} }} } |j| ||} xV| D]N} y| | \}}Wntk rzwIYnX|j| ||| || qIW| S)aK Compile one or more source files. 'sources' must be a list of filenames, most likely C/C++ files, but in reality anything that can be handled by a particular compiler and compiler class (eg. MSVCCompiler can handle resource files in 'sources'). Return a list of object filenames, one per source filename in 'sources'. Depending on the implementation, not all source files will necessarily be compiled, but all corresponding object filenames will be returned. If 'output_dir' is given, object files will be put under it, while retaining their original path component. That is, "foo/bar.c" normally compiles to "foo/bar.o" (for a Unix implementation); if 'output_dir' is "build", then it would compile to "build/foo/bar.o". 'macros', if given, must be a list of macro definitions. A macro definition is either a (name, value) 2-tuple or a (name,) 1-tuple. The former defines a macro; if the value is None, the macro is defined without an explicit value. The 1-tuple case undefines a macro. Later definitions/redefinitions/ undefinitions take precedence. 'include_dirs', if given, must be a list of strings, the directories to add to the default include file search path for this compilation only. 'debug' is a boolean; if true, the compiler will be instructed to output debug symbols in (or alongside) the object file(s). 'extra_preargs' and 'extra_postargs' are implementation- dependent. On platforms that have the notion of a command-line (e.g. Unix, DOS/Windows), they are most likely lists of strings: extra command-line arguments to prepand/append to the compiler command line. On other platforms, consult the implementation class documentation. In any event, they are intended as an escape hatch for those occasions when the abstract compiler framework doesn't cut the mustard. 'depends', if given, is a list of filenames that all targets depend on. If a source file is older than any file in depends, then the source file will be recompiled. This supports dependency tracking, but only at a coarse granularity. Raises CompileError on failure. )rTrWKeyError_compile)rrMrrrrUrfrgrNrrOrPrVrRrQrSrrrcompiles6   zCCompiler.compilecCsdS)zCompile 'src' to product 'obj'.Nr)rrRrQrSrVrgrOrrrrjCszCCompiler._compilecCsdS)a&Link a bunch of stuff together to create a static library file. The "bunch of stuff" consists of the list of object files supplied as 'objects', the extra object files supplied to 'add_link_object()' and/or 'set_link_objects()', the libraries supplied to 'add_library()' and/or 'set_libraries()', and the libraries supplied as 'libraries' (if any). 'output_libname' should be a library name, not a filename; the filename will be inferred from the library name. 'output_dir' is the directory where the library file will be put. 'debug' is a boolean; if true, debugging information will be included in the library (note that on most platforms, it is the compile step where this matters: the 'debug' flag is included here just for consistency). 'target_lang' is the target language for which the given objects are being compiled. This allows specific linkage time treatment of certain languages. Raises LibError on failure. Nr)rroutput_libnamerrU target_langrrrcreate_static_libIszCCompiler.create_static_libZ shared_objectZshared_library executablecCs tdS)auLink a bunch of stuff together to create an executable or shared library file. The "bunch of stuff" consists of the list of object files supplied as 'objects'. 'output_filename' should be a filename. If 'output_dir' is supplied, 'output_filename' is relative to it (i.e. 'output_filename' can provide directory components if needed). 'libraries' is a list of libraries to link against. These are library names, not filenames, since they're translated into filenames in a platform-specific way (eg. "foo" becomes "libfoo.a" on Unix and "foo.lib" on DOS/Windows). However, they can include a directory component, which means the linker will look in that specific directory rather than searching all the normal locations. 'library_dirs', if supplied, should be a list of directories to search for libraries that were specified as bare library names (ie. no directory component). These are on top of the system default and those supplied to 'add_library_dir()' and/or 'set_library_dirs()'. 'runtime_library_dirs' is a list of directories that will be embedded into the shared library and used to search for other shared libraries that *it* depends on at run-time. (This may only be relevant on Unix.) 'export_symbols' is a list of symbols that the shared library will export. (This appears to be relevant only on Windows.) 'debug' is as for 'compile()' and 'create_static_lib()', with the slight distinction that it actually matters on most platforms (as opposed to 'create_static_lib()', which includes a 'debug' flag mostly for form's sake). 'extra_preargs' and 'extra_postargs' are as for 'compile()' (except of course that they supply command-line arguments for the particular linker being used). 'target_lang' is the target language for which the given objects are being compiled. This allows specific linkage time treatment of certain languages. Raises LinkError on failure. N)NotImplementedError)rZ target_descroutput_filenamerrrrexport_symbolsrUrfrg build_temprmrrrlinkis9zCCompiler.linkc CsG|jtj||j|dd||||||| | | | dS)Nlib_typeshared)rtr SHARED_LIBRARYlibrary_filename) rrrlrrrrrrrUrfrgrsrmrrrlink_shared_libs  zCCompiler.link_shared_libc Cs8|jtj||||||||| | | | dS)N)rtr SHARED_OBJECT) rrrqrrrrrrrUrfrgrsrmrrrlink_shared_objects  zCCompiler.link_shared_objectc CsA|jtj||j|||||d||| d| dS)N)rtr EXECUTABLEexecutable_filename) rrZoutput_prognamerrrrrUrfrgrmrrrlink_executables  zCCompiler.link_executablecCs tdS)zkReturn the compiler option to add 'dir' to the list of directories searched for libraries. N)rp)rr5rrrlibrary_dir_optionszCCompiler.library_dir_optioncCs tdS)zsReturn the compiler option to add 'dir' to the list of directories searched for runtime libraries. N)rp)rr5rrrruntime_library_dir_optionsz$CCompiler.runtime_library_dir_optioncCs tdS)zReturn the compiler option to add 'dir' to the list of libraries linked into the shared library or executable. N)rp)rlibrrrlibrary_optionszCCompiler.library_optionc CsMddl}|dkr!g}n|dkr6g}n|dkrKg}n|dkr`g}n|jd|dd\}}tj|d} z7x|D]} | jd| qW| jd|Wd| jXy|j|gd |} Wntk r d SYnXy |j| d d |d |Wnt t fk rHd SYnXdS)zReturn a boolean indicating whether funcname is supported on the current platform. The optional arguments can be used to augment the compilation environment. rNz.ctextTwz#include "%s" z+main (int argc, char **argv) { %s(); } rFza.outrr) tempfileZmkstemprIfdopenwritecloserkZ CompileErrorr~Z LinkErrorr0) rZfuncnameZincludesrrrrfdZfnamefZinclrrrr has_functions8               zCCompiler.has_functioncCs tdS)aHSearch the specified list of directories for a static or shared library file 'lib' and return the full path to that file. If 'debug' true, look for a debugging version (if that makes sense on the current platform). Return None if 'lib' wasn't found in any of the specified directories. N)rp)rr7rrUrrrfind_library_file#szCCompiler.find_library_filecCs|dkrd}ng}x|D]}tjj|\}}tjj|d}|tjj|d}||jkrtd||fn|rtjj|}n|jtjj |||j q"W|S)Nrr(z"unknown file type '%s' (from '%s')) rIrJrK splitdriveisabssrc_extensionsZUnknownFileErrorbasenamer2join obj_extension)rZsource_filenamesrCrZ obj_namesZsrc_namerdrSrrrrENs   zCCompiler.object_filenamescCsG|dk st|r-tjj|}ntjj|||jS)N)rFrIrJrrshared_lib_extension)rrrCrrrrshared_object_filename_sz CCompiler.shared_object_filenamecCsM|dk st|r-tjj|}ntjj|||jpHdS)Nr)rFrIrJrr exe_extension)rrrCrrrrr}eszCCompiler.executable_filenamestaticc Cs|dk st|dkr-tdnt||d}t||d}tjj|\}}|||f} |rd}ntjj||| S) Nrrvdylibz0'lib_type' must be "static", "shared" or "dylib"Z _lib_formatZ_lib_extensionr)rzsharedr)rFrgetattrrIrJsplitr) rr9rurCrZfmtrSr5rdfilenamerrrrxks   zCCompiler.library_filenamer(cCstj|dS)N)r rU)rmsglevelrrrannounce~szCCompiler.announcecCs'ddlm}|r#t|ndS)Nr)DEBUG)Zdistutils.debugrprint)rrrrrr debug_printszCCompiler.debug_printcCstjjd|dS)Nz warning: %s )sysstderrr)rrrrrwarnszCCompiler.warncCst||||jdS)N)r r )rfuncargsrrrrrr szCCompiler.executecCst|d|jdS)Nr )rr )rcmdrrrrszCCompiler.spawncCst||d|jS)Nr )rr )rrQZdstrrrrszCCompiler.move_fileicCst||d|jdS)Nr )rr )rr)moderrrrszCCompiler.mkpath)Br! __module__ __qualname____doc__Z compiler_typerrZstatic_lib_extensionrZstatic_lib_formatZshared_lib_formatrr`r_rr#rr,r1r3r4r6r8r:r;r<r=r>r?rArBrTrWrXrYrZr[r^rerhrkrjrnrzrwr|rtryr{r~rrrrrrErr}rxrrrr rrrrrrrr s    $              +   "   B 4     *+    r zcygwin.*unixposixntZmsvccCs|dkrtj}n|dkr0tj}nxHtD]@\}}tj||dk sstj||dk r7|Sq7WdS)akDetermine the default compiler to use for the given platform. osname should be one of the standard Python OS names (i.e. the ones returned by os.name) and platform the common value returned by sys.platform for the platform in question. The default values are os.name and sys.platform in case the parameters are not given. Nr)rIr)rplatform_default_compilersrematch)Zosnamerpatterncompilerrrrget_default_compilers    rZ unixccompilerZ UnixCCompilerzstandard UNIX-style compilerZ msvccompilerZ MSVCCompilerzMicrosoft Visual C++ZcygwinccompilerZCygwinCCompilerz'Cygwin port of GNU C Compiler for Win32cygwinZMingw32CCompilerz(Mingw32 port of GNU C Compiler for Win32Zmingw32Z bcppcompilerZ BCPPCompilerzBorland C++ CompilerZbcppcCsvddlm}g}x6tjD](}|jd|dt|dfq#W|j||}|jddS)zyPrint list of available compilers (used by the "--help-compiler" options to "build", "build_ext", "build_clib"). r) FancyGetoptz compiler=Nr-zList of available compilers:)Zdistutils.fancy_getoptrcompiler_classrr2sortZ print_help)rZ compilersrZpretty_printerrrrshow_compilerss  rc Cs#|dkrtj}ny2|dkr6t|}nt|\}}}WnEtk rd|}|dk r|d|}nt|YnXy5d|}t|tj|} t | |} WnIt k rt d|Yn(tk rt d||fYnX| d||S)a[Generate an instance of some CCompiler subclass for the supplied platform/compiler combination. 'plat' defaults to 'os.name' (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler for that platform. Currently only 'posix' and 'nt' are supported, and the default compilers are "traditional Unix interface" (UnixCCompiler class) and Visual C++ (MSVCCompiler class). Note that it's perfectly possible to ask for a Unix compiler object under Windows, and a Microsoft compiler object under Unix -- if you supply a value for 'compiler', 'plat' is ignored. Nz5don't know how to compile C/C++ code on platform '%s'z with '%s' compilerz distutils.z4can't compile C/C++ code: unable to load module '%s'zBcan't compile C/C++ code: unable to find class '%s' in module '%s') rIr)rrriZDistutilsPlatformError __import__rmodulesvars ImportErrorZDistutilsModuleError) Zplatrrr rZ module_nameZ class_nameZlong_descriptionrmoduleklassrrr new_compilers2           rcCsg}x|D]}t|toAdt|ko?dknsWtd|nt|dkr|jd|dq t|dkr |ddkr|jd|dq|jd|q q Wx|D]}|jd |qW|S) aGenerate C pre-processor options (-D, -U, -I) as used by at least two types of compilers: the typical Unix compiler and Visual C++. 'macros' is the usual thing, a list of 1- or 2-tuples, where (name,) means undefine (-U) macro 'name', and (name,value) means define (-D) macro 'name' to 'value'. 'include_dirs' is just a list of directory names to be added to the header file search path (-I). Returns a list of command-line options suitable for either Unix compilers or Visual C++. r(r-zPbad macro definition '%s': each element of 'macros' list must be a 1- or 2-tuplez-U%srNz-D%sz-D%s=%sz-I%s)r$r.r/r0r2)rrrOZmacror5rrrrG s 1  rGc Csg}x$|D]}|j|j|q WxF|D]>}|j|}t|tre||}q4|j|q4Wx|D]y}tjj|\}} |r|j|g| } | r|j| q|j d|q}|j|j |q}W|S)acGenerate linker options for searching library directories and linking with specific libraries. 'libraries' and 'library_dirs' are, respectively, lists of library names (not filenames!) and search directories. Returns a list of command-line options suitable for use with some compiler (depending on the two format strings passed in). z6no library file corresponding to '%s' found (skipping)) r2rrr$rDrIrJrrrr) rrrrZlib_optsr5ZoptrZlib_dirZlib_nameZlib_filerrrgen_lib_options7s$      r)rrrIrZdistutils.errorsZdistutils.spawnrZdistutils.file_utilrZdistutils.dir_utilrZdistutils.dep_utilrrZdistutils.utilrr Z distutilsr r rrrrrrGrrrrrs<$          - -