ó
¹å½\c           @   s\   d  d l  Z  d  d l Z d  d l Z d d l m Z d „  Z e  j ƒ  d „ Z d „  Z d S(   iÿÿÿÿNi   (   t   PkgConfigErrorc         C   s™   x’ | j  ƒ  D]„ \ } } | |  k r2 | |  | <q t |  | t ƒ s[ t d | f ƒ ‚ n  t | t ƒ s€ t d | f ƒ ‚ n  |  | j | ƒ q W|  S(   s¬   Merge values from cffi config flags cfg2 to cf1

    Example:
        merge_flags({"libraries": ["one"]}, {"libraries": ["two"]})
        {"libraries": ["one", "two"]}
    s$   cfg1[%r] should be a list of stringss$   cfg2[%r] should be a list of strings(   t   itemst
   isinstancet   listt	   TypeErrort   extend(   t   cfg1t   cfg2t   keyt   value(    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   merge_flags   s    c         C   s}  d d g } | j  | ƒ | j  |  ƒ y% t j | d t j d t j ƒ} Wn2 t k
 r } t d t | ƒ j ƒ  f ƒ ‚ n X| j ƒ  \ } } | j	 d k rÝ y | j
 | ƒ } Wn t k
 rÇ n Xt | j ƒ  ƒ ‚ n  t j d k rBt | t ƒ rBy | j
 | ƒ } WqBt k
 r>t d | |  | | f ƒ ‚ qBXn  t j d	 k ryd	 | k ryt d
 | |  | f ƒ ‚ n  | S(   s5   Calls pkg-config and returns the output if found
    s
   pkg-configs   --print-errorst   stdoutt   stderrs   cannot run pkg-config: %si    i   sK   pkg-config %s %s returned bytes that cannot be decoded with encoding %r:
%rs   \sE   pkg-config %s %s returned an unsupported backslash-escaped output:
%r(   i   (   t   appendt
   subprocesst   Popent   PIPEt   EnvironmentErrorR    t   strt   stript   communicatet
   returncodet   decodet	   Exceptiont   syst   version_infoR   t   UnicodeDecodeErrort   ost   altsep(   t   libnamet   flagt   encodingt   at   pct   et   boutt   berr(    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   call   s0    %#c            sˆ   d „  ‰  d „  ‰ d „  ‰ d „  ‰ d „  ‰ d „  ‰ ‡  ‡ ‡ ‡ ‡ ‡ f d †  } i  } x' |  D] } | | ƒ } t  | | ƒ qa W| S(   sÞ  Return compiler line flags for FFI.set_source based on pkg-config output

    Usage
        ...
        ffibuilder.set_source("_foo", pkgconfig = ["libfoo", "libbar >= 1.8.3"])

    If pkg-config is installed on build machine, then arguments include_dirs,
    library_dirs, libraries, define_macros, extra_compile_args and
    extra_link_args are extended with an output of pkg-config for libfoo and
    libbar.

    Raises PkgConfigError in case the pkg-config call fails.
    c         S   s0   g  |  j  ƒ  D] } | j d ƒ r | d ^ q S(   Ns   -Ii   (   t   splitt
   startswith(   t   stringt   x(    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   get_include_dirsK   s    c         S   s0   g  |  j  ƒ  D] } | j d ƒ r | d ^ q S(   Ns   -Li   (   R&   R'   (   R(   R)   (    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   get_library_dirsN   s    c         S   s0   g  |  j  ƒ  D] } | j d ƒ r | d ^ q S(   Ns   -li   (   R&   R'   (   R(   R)   (    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   get_librariesQ   s    c         S   s;   d „  } g  |  j  ƒ  D]! } | j d ƒ r | | ƒ ^ q S(   Nc         S   s:   |  d }  d |  k r, t  |  j d d ƒ ƒ S|  d  f Sd  S(   Ni   t   =i   (   t   tupleR&   t   None(   R)   (    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   _macroV   s    
s   -D(   R&   R'   (   R(   R0   R)   (    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt
   get_macrosU   s    	c         S   s=   g  |  j  ƒ  D], } | j d ƒ r | j d ƒ r | ^ q S(   Ns   -Is   -D(   R&   R'   (   R(   R)   (    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   get_other_cflags^   s    #c         S   s=   g  |  j  ƒ  D], } | j d ƒ r | j d ƒ r | ^ q S(   Ns   -Ls   -l(   R&   R'   (   R(   R)   (    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   get_other_libsb   s    #c            s|   t  j ƒ  } t |  d ƒ } t |  d ƒ } i ˆ  | ƒ d 6ˆ | ƒ d 6ˆ | ƒ d 6ˆ | ƒ d 6ˆ | ƒ d 6ˆ | ƒ d 6S(	   Ns   --cflagss   --libst   include_dirst   library_dirst	   librariest   define_macrost   extra_compile_argst   extra_link_args(   R   t   getfilesystemencodingR%   (   R   t   fset
   all_cflagst   all_libs(   R*   R,   R+   R1   R2   R3   (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   kwargsg   s    (   R
   (   t   libsR>   t   retR   t	   lib_flags(    (   R*   R,   R+   R1   R2   R3   s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   flags_from_pkgconfig<   s    							(	   R   R   R   t   errorR    R
   R:   R%   RB   (    (    (    s-   lib/python2.7/site-packages/cffi/pkgconfig.pyt   <module>   s   $	"