o
    儸`E#                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
 G dd deZdd Zd	d
 Zdd Zd-ddZdd Zedd Zedd Zedd Zdd Zdd Zd-ddZdd Zd d! Zd"d#d$d%d&Zd-d'd(Zd-d)d*Zd+d, ZdS ).zPpkgconfig is a Python module to interface with the pkg-config command line
tool.    Nwraps)callPIPEPopenc                       s    e Zd ZdZ fddZ  ZS )PackageNotFoundErrorz,
    Raised if a package was not found.
    c                    s   d| }t t| | d S )Nz%s not found)superr   __init__)selfpackagemessage	__class__ 7usr/lib/python3.10/site-packages/pkgconfig/pkgconfig.pyr	   %   s   zPackageNotFoundError.__init__)__name__
__module____qualname____doc__r	   __classcell__r   r   r   r   r   !   s    r   c                 C   s(   dd }|| }||}||k||k  S )z
    Compare two version strings and return -1, 0 or 1 depending on the equality
    of the subset of matching version numbers.

    The implementation is inspired by the top answer at
    http://stackoverflow.com/a/1714190/997768.
    c                 S   s   t dd| } g }| dD ]U}t d|}|r#|t|d qt d|}|r>|t|d ||d qt d|}|rc|t|d ||d |t|d	 qqt|S )
Nz(\.0+)*$ .z^(\d+)$   z^(\d+)([a-zA-Z]+)$   z^(\d+)([a-zA-Z]+)(\d+)$   )resubsplitmatchappendintgrouptuple)vresultpartmr   r   r   	normalize2   s(   z$_compare_versions.<locals>.normalizer   )Zv1Zv2r'   Zn1Zn2r   r   r   _compare_versions*   s   r(   c                 C   s    t d| }|d|dfS )zESplits version specifiers in the form ">= 0.1.2" into ('0.1.2', '>=')z([<>=]?=?)?\s*([0-9.a-zA-Z]+)r   r   )r   searchr!   )specr&   r   r   r   _split_version_specifierQ   s   r+   c                    s   t   fdd}|S )Nc               
      s6   z | i |W S  t y } ztd| d }~ww )Nz%pkg-config probably not installed: %r)OSErrorEnvironmentError)argskwargsefuncr   r   _wrapperX   s   z _convert_error.<locals>._wrapperr   )r2   r3   r   r1   r   _convert_errorW   s   r4   Fc                 C   s   |r| dfS | fS )Nz--staticr   )optionstaticr   r   r   _build_optionsa   s   r7   c                 C   s   t | st| d S N)existsr   r   r   r   r   _raise_if_not_existse   s   r;   c                 G   sT   t jdd pd}d|d|| }tt|ttd}|	 \}}|
 dS )N
PKG_CONFIG
pkg-configz{0} {1} {2} )stdoutstderrzutf-8)osenvirongetformatjoinr   shlexr   r   communicaterstripdecode)r   optionspkg_config_execmdprocouterrr   r   r   _queryj   s
   rP   c                 C   s.   t jddpd}d||  }t|dkS )z{
    Return True if package information is available.

    If ``pkg-config`` not on path, raises ``EnvironmentError``.
    r<   Nr=   z{0} --exists {1}r   )rA   rB   rC   rD   r   r   )r   rK   rL   r   r   r   r9   t   s   r9   c                 C   s   t | ddS )z
    Return a list of package names that is required by the package.

    If ``pkg-config`` not on path, raises ``EnvironmentError``.
    z--print-requires
rP   r   r:   r   r   r   requires   s   rS   c                 C      t |  t| dS )z~
    Return the CFLAGS string returned by pkg-config.

    If ``pkg-config`` is not on path, raises ``EnvironmentError``.
    --cflagsr;   rP   r:   r   r   r   cflags      
rW   c                 C   rT   )zz
    Return the version returned by pkg-config.

    If `pkg-config` is not in the path, raises ``EnvironmentError``.
    --modversionrV   r:   r   r   r   
modversion   rX   rZ   c                 C   s    t |  t| gtd|dR  S )z
    Return the LDFLAGS string returned by pkg-config.

    The static specifier will also include libraries for static linking (i.e.,
    includes any private libraries).
    --libsr6   )r;   rP   r7   )r   r6   r   r   r   libs   s   r]   c                    s<   t   t d}dd |dD }t fdd|D S )zg
    Return a dictionary of all the variables defined in the .pc pkg-config file
    of 'package'.
    z--print-variablesc                 s   s     | ]}|d kr|  V  qdS )r   N)strip.0xr   r   r   	<genexpr>       zvariables.<locals>.<genexpr>rQ   c                 3   s(    | ]}|t  d | fV  qdS )z--variable={0}N)rP   rD   r^   r_   r:   r   r   rb      s   & )r;   rP   r   dict)r   r$   namesr   r:   r   	variables   s   
rf   c                 C   s   t | sdS t|\}}t| d}zt||}W n ty(   d|}t|w |dv r1|dkS |dkr9|dkS |dkrA|dkS |dkrI|dk S |d	krQ|dkS d
S )a  
    Check if the package meets the required version.

    The version specifier consists of an optional comparator (one of =, ==, >,
    <, >=, <=) and an arbitrarily long version number separated by dots. The
    should be as you would expect, e.g. for an installed version '0.1.2' of
    package 'foo':

    >>> installed('foo', '==0.1.2')
    True
    >>> installed('foo', '<0.1')
    False
    >>> installed('foo', '>= 0.0.4')
    True

    If ``pkg-config`` not on path, raises ``EnvironmentError``.
    FrY   z&{0} is not a correct version specifier)r   =z==r   >z>=<z<=N)r9   r+   rP   r(   
ValueErrorrD   )r   versionnumberZ
comparatorrZ   r$   msgr   r   r   	installed   s*   

rn   define_macrosinclude_dirslibrary_dirs	libraries)z-Dz-Iz-Lz-lc                    s   |   D ]}t| qt| gtd|dR  }|dd}tt}t d|D ]}t	
|dd }|rB|| |dd   q(dd	   fd
d|d D |d< ttdd | D S )a  
    Parse the output from pkg-config about the passed package or packages.

    Builds a dictionary containing the 'libraries', the 'library_dirs', the
    'include_dirs', and the 'define_macros' that are presented by pkg-config.
    *package* is a string with space-delimited package names.

    The static specifier will also include libraries for static linking (i.e.,
    includes any private libraries).

    If ``pkg-config`` is not on path, raises ``EnvironmentError``.
    z--cflags --libsr\   \"r   (?<!\\) Nr   c                 S   s*   t | d}t|dkr|S |d d fS )Nrg   r   r   )r"   r   len)r&   tr   r   r   r     s   zparse.<locals>.splitc                    s   g | ]} |qS r   r   )r`   r&   r   r   r   
<listcomp>  s    zparse.<locals>.<listcomp>ro   c                 s   s     | ]\}}|r||fV  qd S r8   r   )r`   kr#   r   r   r   rb     rc   zparse.<locals>.<genexpr>)r   r;   rP   r7   replacecollectionsdefaultdictlistr   
_PARSE_MAPrC   r   r^   items)packagesr6   r   rN   r$   tokenkeyr   rw   r   parse   s   

r   c                    s@      D ]}t| q fdd}|d| j |d| j dS )z
    Append the ``--cflags`` and ``--libs`` of a space-separated list of
    *packages* to the ``extra_compile_args`` and ``extra_link_args`` of a
    distutils/setuptools ``Extension``.
    c              	      sN   t jdkrdgng }t g|t| dR  }|td|dd d S )Nntz--msvc-syntaxr\   rt   rs   r   )rA   namerP   r7   extendr   r   rz   )r5   targetZos_optsflagsr   r6   r   r   query_and_extend  s   z-configure_extension.<locals>.query_and_extendrU   r[   N)r   r;   extra_compile_argsextra_link_args)extr   r6   r   r   r   r   r   configure_extension  s
   
r   c                  C   s   dd t dddD } | S )z2Return a list of all packages found by pkg-config.c                 S   s   g | ]}|  d  qS )r   rw   )r`   liner   r   r   rx   "  s    zlist_all.<locals>.<listcomp>r   z
--list-allrQ   rR   )r   r   r   r   list_all   s   r   )F)r   rA   rF   r   r{   	functoolsr   
subprocessr   r   r   	Exceptionr   r(   r+   r4   r7   r;   rP   r9   rS   rW   rZ   r]   rf   rn   r~   r   r   r   r   r   r   r   <module>   s@   	'


	

	


/

$