ó
9­\c           @` sà  d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 d  d l m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m Z m Z e e j d+ k rd n d	 ƒ Z e j  d
 k r'd Z! n7 e j  d k r?d Z! n e" j# d j$ e j  ƒ ƒ d Z! d d d e& d d „ Z' d e& d d „ Z( d e& d d e& e& d „ Z) d d d e& e& d „ Z* d d d „ Z+ i
 e d f d 6e d f d 6e d f d 6e d f d 6e d f d 6e d f d 6e d f d 6e d f d 6e d f d 6e d f d  6Z, d d d e& d! „ Z- d d d d d d d" „ Z. d# „  Z/ d$ „  Z0 d% „  Z1 d d& d d d' „ Z2 d( „  Z3 d d) „ Z4 d e& d d d* „ Z5 d S(,   i    (   t   absolute_importt   divisiont   print_functionN(   t   get_config_vart   get_config_varsi   (   t   get_abspatht	   make_dirst   copyt   Globt   ArbitraryDepthGlobt   glob_at_deptht   CompileErrort   import_module_from_filet   pyx_is_cplust   sha256_of_stringt   sha256_of_file(   t   CCompilerRunnert   CppCompilerRunnert   FortranCompilerRunneri   t
   EXT_SUFFIXt   SOt   posixs   .ot   nts   .objs   Unknown os.name: {}c         K` sæ  i  } | d k	 r® x™ | j ƒ  D]ˆ \ } }	 t | t ƒ rd xj t j | j ƒ D] }
 |	 | |
 <qM Wq t | t ƒ r x1 t | j | ƒ D] }
 |	 | |
 <q† Wq |	 | | <q Wn  | p· d } t	 j
 j | ƒ st	 j
 j | ƒ rö t d j | ƒ ƒ ‚ qt | ƒ n  | d k rBd } x* |  D] } t | | d t d t ƒqWn  g  } x— |  D] } | rvt	 j
 j | ƒ \ } } n$ t	 j
 j t	 j
 j | ƒ ƒ \ } } | j ƒ  } | j | j | i  ƒ ƒ | j t | | d | | ƒ qOW| S(   sL   Compile source code files to object files.

    Parameters
    ==========

    files : iterable of str
        Paths to source files, if ``cwd`` is given, the paths are taken as relative.
    Runner: CompilerRunner subclass (optional)
        Could be e.g. ``FortranCompilerRunner``. Will be inferred from filename
        extensions if missing.
    destdir: str
        Output directory, if cwd is given, the path is taken as relative.
    cwd: str
        Working directory. Specify to have compiler run in other directory.
        also used as root of relative paths.
    keep_dir_struct: bool
        Reproduce directory structure in `destdir`. default: ``False``
    per_file_kwargs: dict
        Dict mapping instances in ``files`` to keyword arguments.
    \*\*kwargs: dict
        Default keyword arguments to pass to ``Runner``.

    t   .s   {} is not a directoryt   only_updatet   dest_is_dirt   cwdN(   t   Nonet   itemst
   isinstanceR   t   globt   pathnameR	   R
   t   filenamet   ost   patht   isdirt   existst   IOErrort   formatR   R   t   Truet   splitextt   basenamet   updatet   gett   appendt   src2obj(   t   filest   Runnert   destdirR   t   keep_dir_structt   per_file_kwargst   kwargst   _per_file_kwargst   kt   vR"   t   ft   dstpathst   namet   extt   file_kwargs(    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   compile_sources&   s8     $#c         C` s¼   |  p t  j j d d ƒ }  |  j ƒ  d k rg | rM t i d d g d 6|  f St i d g d 6|  f SnQ |  j ƒ  d k s d r¬ | rœ t i d g d	 6|  f St i  |  f Sn t d
 ƒ ‚ d  S(   Nt   SYMPY_COMPILER_VENDORt   gnut   intels   -nofor_mains   -cxxlibt   flagst   llvmt   fortrant   lib_optionss   No vendor found.(   R!   t   environR+   t   lowerR   R   t
   ValueError(   t   vendort   cplusR   (    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   get_mixed_fort_c_linkere   s    c         K` s  | d	 k rJ t j j t j j |  d ƒ ƒ \ } } | rJ | t 7} qJ n  | sè | rÐ t d | j d d	 ƒ d | d | ƒ \ } }	 }
 x\ |	 j ƒ  D]6 \ } } | | k r¿ | | j	 | ƒ q“ | | | <q“ Wqè | rß t
 } qè t } n  | j d g  ƒ } | rd | k r| j d ƒ qn  | j d t ƒ } | sFt d ƒ ‚ n  t | d | ƒ} | |  | | d | | } | j ƒ  | S(
   sK   Link object files.

    Parameters
    ==========

    obj_files: iterable of str
        Paths to object files.
    out_file: str (optional)
        Path to executable/shared library, if ``None`` it will be
        deduced from the last item in obj_files.
    shared: bool
        Generate a shared library?
    Runner: CompilerRunner subclass (optional)
        If not given the ``cplus`` and ``fort`` flags will be inspected
        (fallback is the C compiler).
    cwd: str
        Path to the root of relative paths and working directory for compiler.
    cplus: bool
        C++ objects? default: ``False``.
    fort: bool
        Fortran objects? default: ``False``.
    \*\*kwargs: dict
        Keyword arguments passed to ``Runner``.

    Returns
    =======

    The absolute path to the generated shared object / executable.

    iÿÿÿÿRG   RH   R   R@   s   -sharedt
   run_linkers*   run_linker was set to False (nonsensical).N(   R   R!   R"   R(   R)   t	   sharedextRI   R+   R   t   expandR   R   t   popR,   R'   RF   R   t   run(   t	   obj_filest   out_filet   sharedR/   R   RH   t   fortR3   R:   t   extra_kwargsRG   R5   R6   R@   RJ   t   runner(    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   linkz   s8     (		
c         K` sd  | p	 g  } | j  d g  ƒ } | j  d g  ƒ } t j d k rO t j d ƒ n“ t j d k ra n t j d  d k rw nk d d	 l m }	 |	 j d
 ƒ râ |	 j d ƒ }
 d j t j	 d ?t j	 d ?d @|
 pÌ d ƒ } | | g 7} n  | j  d g  ƒ } d } x* | D]" } | | k r| j
 | ƒ qqWt |  d t d | d | d | d | d | d | d | | S(   s,   Link python extension module (shared object) for importing

    Parameters
    ==========

    obj_files: iterable of str
        Paths to object files to be linked.
    so_file: str
        Name (path) of shared object file to create. If not specified it will
        have the basname of the last object file in `obj_files` but with the
        extension '.so' (Unix).
    cwd: path string
        Root of relative paths and working directory of linker.
    libraries: iterable of strings
        Libraries to link against, e.g. ['m'].
    cplus: bool
        Any C++ objects? default: ``False``.
    fort: bool
        Any Fortran objects? default: ``False``.
    kwargs**: dict
        Keyword arguments passed to ``link(...)``.

    Returns
    =======

    Absolute path to the generate shared object.
    t   include_dirst   library_dirst   win32s   Windows not yet supported.t   darwini   t   aixi    (   t	   sysconfigt   Py_ENABLE_SHAREDt   ABIFLAGSs   python{}.{}{}i   i   iÿ   t    R@   s   -pthreadRQ   R   RH   RR   t	   libraries(   s   -pthread(   RM   t   syst   platformt   warningst   warnt	   distutilsR[   R   R&   t
   hexversionR,   RU   R'   (   RO   t   so_fileR   R_   RH   RR   R3   RV   RW   R[   R]   t	   pythonlibR@   t   needed_flagst   flag(    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt
   link_py_soÀ   s0    c         K` sÔ  d d l  m } m } d d l  m } |  j ƒ  j d ƒ sV |  j ƒ  j d ƒ sV t ‚ | p_ d } | pk d } | j d t ƒ r† d n d	 } t	 j
 j t	 j
 j |  ƒ ƒ d | } t	 j
 j | | ƒ }	 | rÜ t	 j ƒ  }
 n d }
 t	 j | ƒ zÐ | | ƒ } | j j | ƒ | |  g | ƒ } | j d k r>t d
 ƒ ‚ n  t	 j
 j t	 j
 j |  ƒ ƒ t	 j
 j | ƒ k r¾t	 j
 j |	 ƒ rt	 j |	 ƒ n  t j t	 j
 j t	 j
 j |  ƒ | ƒ | ƒ n  Wd t	 j |
 ƒ X|	 S(   s¬   Generates a C file from a Cython source file.

    Parameters
    ==========

    src: str
        Path to Cython source.
    destdir: str (optional)
        Path to output directory (default: '.').
    cwd: path string (optional)
        Root of relative paths (default: '.').
    **cy_kwargs:
        Second argument passed to cy_compile. Generates a .cpp file if ``cplus=True`` in ``cy_kwargs``,
        else a .c file.
    i    (   t   default_optionst   CompilationOptions(   t   compiles   .pyxs   .pyR   RH   s   .cpps   .cs   Cython compilation failed.N(   t   Cython.Compiler.MainRk   Rl   Rm   RE   t   endswitht   AssertionErrorR+   t   FalseR!   R"   R(   R)   t   joint   getcwdt   chdirt   __dict__R*   t
   num_errorsRF   t   abspatht   dirnameR$   t   unlinkt   shutilt   move(   t   srcR0   R   t	   cy_kwargsRk   Rl   t
   cy_compileR:   t   c_namet   dstfilet   ori_dirt
   cy_optionst	   cy_result(    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   simple_cythonize  s0    0&02s   .cs   .cpps   .cxxs   .fs   .fors   .ftns   .f90t   f95s   .f95t   f2003s   .f03t   f2008s   .f08c         K` s   t  j j t  j j |  ƒ ƒ \ } } | d k rl t  j j |  ƒ rK d } ql t  j j |  ƒ } | pf d } n  t  j j | ƒ rš t  j j | | t	 ƒ } n  | j
 d g  ƒ } | rê d d l m }	 |	 ƒ  }
 |
 | k rê | j |
 ƒ qê n  | j ƒ  d k rt |  d | d | d | | S| d k rYt | j ƒ  \ } } d | k rY| | d <qYn  | j
 d	 g  ƒ } d } x* | D]" } | | k rx| j | ƒ qxqxW| j
 d t ƒ } | rÅt d ƒ ‚ n  | |  g | d | d | d | d	 | | } | j ƒ  | S(   s¸   Compiles a source code file to an object file.

    Files ending with '.pyx' assumed to be cython files and
    are dispatched to pyx2obj.

    Parameters
    ==========

    srcpath: str
        Path to source file.
    Runner: CompilerRunner subclass (optional)
        If ``None``: deduced from extension of srcpath.
    objpath : str (optional)
        Path to generated object. If ``None``: deduced from ``srcpath``.
    cwd: str (optional)
        Working directory and root of relative paths. If ``None``: current dir.
    inc_py: bool
        Add Python include path to kwarg "include_dirs". Default: False
    \*\*kwargs: dict
        keyword arguments passed to Runner or pyx2obj

    R   RV   i    (   t   get_python_incs   .pyxt   objpathR   t   stdR@   s   -fPICRJ   s#   src2obj called with run_linker=TrueN(   s   -fPIC(   R!   R"   R(   R)   R   t   isabsRx   R#   Rr   t   objextRM   t   distutils.sysconfigRˆ   R,   RE   t   pyx2objt   extension_mappingRq   R   RN   (   t   srcpathR/   R‰   R   t   inc_pyR3   R9   R:   RV   Rˆ   t
   py_inc_dirRŠ   R@   Rh   Ri   RJ   RT   (    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyR-   A  sB    $		
c         K` só  |  j  d ƒ s t ‚ | p d } | p* d } | pB t j j | ƒ } t | d | ƒ} t j j | ƒ r¯ t j j |  ƒ }	 t j j |	 ƒ \ }
 } t j j	 | |
 t
 ƒ } n  | p¸ i  } | | d <| d k rà t |  ƒ } n  | | d <t |  d | d | | } | pg  } | j d g  ƒ } d } x* | D]" } | | k r0| j | ƒ q0q0W| j d g  ƒ } | j d t ƒ r‰t d ƒ ‚ n  | r¤| j d d ƒ } n | j d d ƒ } t | d | d | d | d | d | d | d t d t | S(   s9  
    Convenience function

    If cwd is specified, pyxpath and dst are taken to be relative
    If only_update is set to `True` the modification time is checked
    and compilation is only run if the source is newer than the
    destination

    Parameters
    ==========

    pyxpath: str
        Path to Cython source file.
    objpath: str (optional)
        Path to object file to generate.
    destdir: str (optional)
        Directory to put generated C file. When ``None``: directory of ``objpath``.
    cwd: str (optional)
        Working directory and root of relative paths.
    include_dirs: iterable of path strings (optional)
        Passed onto src2obj and via cy_kwargs['include_path']
        to simple_cythonize.
    cy_kwargs: dict (optional)
        Keyword arguments passed onto `simple_cythonize`
    cplus: bool (optional)
        Indicate whether C++ is used. default: auto-detect using ``.util.pyx_is_cplus``.
    compile_kwargs: dict
        keyword arguments passed onto src2obj

    Returns
    =======

    Absolute path of generated object file.

    s   .pyxR   R   t
   output_dirRH   R0   R@   s   -fwrapvs   -pthreads   -fPICt   optionst   strict_aliasings/   Cython requires strict aliasing to be disabled.RŠ   s   c++98t   c99R‰   RV   R‘   N(   s   -fwrapvs   -pthreads   -fPIC(   Ro   Rp   R!   R"   Rx   R   R#   R)   R(   Rr   RŒ   R   R   R„   RM   R,   Rq   R   R-   R'   (   t   pyxpathR‰   R0   R   RV   R}   RH   R3   t   abs_objpatht	   pyx_fnameR9   R:   t   interm_c_fileR@   Rh   Ri   R”   RŠ   (    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyRŽ   „  s>    %

c         C` s`   xY |  D]Q } t  j j | ƒ \ } } | j ƒ  } | t k r t | d | k rX t Sq q Wt S(   Ni    (   R!   R"   R(   RE   R   R'   Rq   (   t   srcst   clsR|   R9   R:   t   key(    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   _any_XÕ  s    c         C` s   t  |  t ƒ S(   N(   Rž   R   (   R›   (    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   any_fortran_srcß  s    c         C` s   t  |  t ƒ S(   N(   Rž   R   (   R›   (    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   any_cplus_srcã  s    R   c      	   C` sê   | d k r5 t j j t j j |  d ƒ ƒ d } n  | p> i  } | pJ i  } y" t t j j | | ƒ |  ƒ } Wnt t k
 rå t t	 t
 t |  ƒ ƒ d | d | | } t | d | d t |  ƒ d t |  ƒ | } t | ƒ } n X| S(   sµ   Compiles sources to a shared object (python extension) and imports it

    Sources in ``sources`` which is imported. If shared object is newer than the sources, they
    are not recompiled but instead it is imported.

    Parameters
    ==========

    sources : string
        List of paths to sources.
    extname : string
        Name of extension (default: ``None``).
        If ``None``: taken from the last file in ``sources`` without extension.
    build_dir: str
        Path to directory in which objects files etc. are generated.
    compile_kwargs: dict
        keyword arguments passed to ``compile_sources``
    link_kwargs: dict
        keyword arguments passed to ``link_py_so``

    Returns
    =======

    The imported module from of the python extension.

    Examples
    ========

    >>> mod = compile_link_import_py_ext(['fft.f90', 'conv.cpp', '_fft.pyx'])  # doctest: +SKIP
    >>> Aprim = mod.fft(A)  # doctest: +SKIP

    iÿÿÿÿi    R0   R   RR   RH   N(   R   R!   R"   R(   R)   R   Rr   t   ImportErrorR<   t   listt   mapR   Rj   RŸ   R    (   t   sourcest   extnamet	   build_dirt   compile_kwargst   link_kwargst   modt   objst   so(    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   compile_link_import_py_extç  s    ")"c   
      C` sM  | p t  j ƒ  } t j j | ƒ s6 t d | ƒ ‚ n  g  } x|  D]ü \ } } t j j | | ƒ } t } t | j	 d ƒ ƒ j
 ƒ  } t j j | ƒ rê t j j | d ƒ rÉ t | d d ƒ j ƒ  } n t | ƒ j
 ƒ  } | | k } n  | r2t | d ƒ . }	 |	 j | ƒ t | d d ƒ j | ƒ Wd  QXn  | j | ƒ qC W| | f S(   Ns   Non-existent directory: s   utf-8s   .sha256t   rtt   wt(   t   tempfilet   mkdtempR!   R"   R#   t   OSErrorRr   R'   R   t   encodet	   hexdigestR$   t   opent   readR   t   writeR,   (
   R¤   R¦   t   source_filesR9   R|   t   destt   differst   sha256_in_memt   sha256_on_diskt   fh(    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   _write_sources_to_build_dir  s&    #c         K` sC   t  |  | ƒ \ } } t | d | | } t d | ƒ } | | f S(   så   Compiles, links and imports extension module from source.

    Parameters
    ==========

    sources : iterable of name/source pair tuples
    build_dir : string (default: None)
        Path. ``None`` implies use a temporary directory.
    **kwargs:
        Keyword arguments passed onto `compile_link_import_py_ext`.

    Returns
    =======

    mod : module
        The compiled and imported extension module.
    info : dict
        Containing ``build_dir`` as 'build_dir'.

    R¦   (   R½   R¬   t   dict(   R¤   R¦   R3   R·   R©   t   info(    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   compile_link_import_strings2  s    c      	   C` sR  | r! | d	 k	 r! t d ƒ ‚ n  zÚ t |  | ƒ \ } } t t t t | ƒ ƒ d | d | | pc i  } t | d | d t | ƒ d t	 | ƒ | p– i  } t
 j | g d t
 j d t
 j ƒ} | j ƒ  }	 g  | j ƒ  D] }
 |
 j d ƒ ^ qÙ \ } } Wd	 | r,t j j | ƒ r,t j | ƒ d	 } n  Xt d
 |	 d | ƒ } | | f | f S(   sy   Compiles, links and runs a program built from sources.

    Parameters
    ==========

    sources : iterable of name/source pair tuples
    build_dir : string (default: None)
        Path. ``None`` implies use a temporary directory.
    clean : bool
        Whether to remove build_dir after use. This will only have an
        effect if ``build_dir`` is ``None`` (which creates a temporary directory).
        Passing ``clean == True`` and ``build_dir != None`` raises a ``ValueError``.
        This will also set ``build_dir`` in returned info dictionary to ``None``.
    compile_kwargs: dict
        Keyword arguments passed onto ``compile_sources``
    link_kwargs: dict
        Keyword arguments passed onto ``link``

    Returns
    =======

    (stdout, stderr): pair of strings
    info: dict
        Containing exit status as 'exit_status' and ``build_dir`` as 'build_dir'

    sI   Automatic removal of build_dir is only available for temporary directory.R0   R   RR   RH   t   stdoutt   stderrs   utf-8Nt   exit_statusR¦   (   R   RF   R½   R<   R¢   R£   R   RU   RŸ   R    t
   subprocesst   Popent   PIPEt   waitt   communicatet   decodeR!   R"   R#   Rz   t   rmtreeR¾   (   R¤   R¦   t   cleanR§   R¨   R·   Rª   t   progt   pRÃ   t   txtRÁ   RÂ   R¿   (    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   compile_run_stringsM  s"    $2
(   i   i   (6   t
   __future__R    R   R   R!   t   reRz   RÄ   R`   R¯   Rb   R   R   R   t   utilR   R   R   R   R	   R
   R   R   R   R   R   t   runnersR   R   R   t   version_infoRK   R9   RŒ   t   warningt   warngR&   R   Rq   R<   RI   RU   Rj   R„   R   R-   RŽ   Rž   RŸ   R    R¬   R½   RÀ   RÏ   (    (    (    sG   lib/python2.7/site-packages/sympy/utilities/_compilation/compilation.pyt   <module>   s^   L!		>	E	@2C	P	
			1	