
    k=1e1                        d 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  ej        e          Zi Zd Z ed          d	             Zd
 Zd Z ed          d             Zd Zd Zd Zd Zd Zd Zd Z ed          d             Z ed          d             Z ed          d             Z ed          d             Z dS )z#Detection of CPU microarchitectures    N   )generic_microarchitectureTARGETSTARGETS_JSONc                       fd}|S )zDecorator to mark functions that are meant to return raw info on
    the current cpu.

    Args:
        operating_system (str or tuple): operating system for which the marked
            function is a viable factory of raw info dictionaries.
    c                      t                                                    t          j                    fd            }|S )Nc                  (                } d}d| v sJ |                     d                      d| v sJ |                     d                      d| v sJ |                     d                      d| v sJ |                     d                      | S )Nz/field "{0}" is missing from raw info dictionary	vendor_idflagsmodel
model_name)format)infomsgfactorys     3lib/python3.11/site-packages/archspec/cpu/detect.py_implz+info_dict.<locals>.decorator.<locals>._impl&   s    799D DC$&&&

;(?(?&&&d???CJJw$7$7???d???CJJw$7$7???4'''L)A)A'''K    )INFO_FACTORYappend	functoolswraps)r   r   operating_systems   ` r   	decoratorzinfo_dict.<locals>.decorator#   sS    %&--g666		!	!
	 
	 
	 
	 
"	!
	 r    )r   r   s   ` r   	info_dictr      s$        $ r   Linux)r   c                      i } t          d          5 }|D ]N}|                    d          \  }}}|dk    r| r n*|                                | |                                <   Oddd           n# 1 swxY w Y   | S )zVReturns a raw info dictionary by parsing the first entry of
    ``/proc/cpuinfo``
    z/proc/cpuinfo:N)open	partitionstrip)r   filelinekey	separatorvalues         r   proc_cpuinfor)   8   s    
 D	o		 .$ 	. 	.D$(NN3$7$7!CE CD %D. . . . . . . . . . . . . . . Ks   AA00A47A4c                     t          j        | t           j        |          5 }|                                d         }d d d            n# 1 swxY w Y   t	          |                    d                    S )N)stdoutenvr   zutf-8)
subprocessPopenPIPEcommunicatestrdecode)argsr,   procoutputs       r   _check_outputr6   P   s    		$zC	@	@	@ 'D!!##A&' ' ' ' ' ' ' ' ' ' ' ' ' ' 'v}}W%%&&&s   A		AAc                      t          j                    } | dk    rt          j                    S t          g dt	                                                                }d|v rdS dS )z+ "Return the machine architecture we are onDarwin)sysctl-nmachdep.cpu.brand_stringr,   Appleaarch64x86_64)platformsystemmachiner6   _ensure_bin_usrbin_in_pathr#   )r   r5   s     r   _machinerD   V   sy    (( 8##!!! 444:T:V:V  egg  & y8r   r8   c                     t                      fd} t                      dk    rf | dd                                          dz    | dd                                          z   } | dd          | | dd           | dd	          d
}nFd} | dd	                                          }d|v rd}nd|v rd}nd|v rd}dg |d | dd	          d}|S )z;Returns a raw info dictionary parsing the output of sysctl.c                  l    t          dgt          |           z                                             S )Nr9   r<   )r6   listr#   )r3   child_environments    r   r9   z sysctl_info_dict.<locals>.sysctlt   s0    hZ$t**4:KLLLRRTTTr   r?   r:   zmachdep.cpu.features zmachdep.cpu.leaf7_featureszmachdep.cpu.vendorzmachdep.cpu.modelr;   )r   r   r   
model nameunknownm2m1appler=   )r   r   r   CPU implementerrJ   )rC   rD   lower)r9   r   r   r   	model_strrH   s        @r   sysctl_info_dictrR   o   sX    344U U U U U zzXF4/006688fT788>>@@A 	  &:;;VD"566 &'ABB	
 
 F4!;<<BBDD	9EEYEE	!!E !& &'ABB
 
 Kr   c                  B   t          t          j                                                  } |                     dd                              t          j                  }dD ]}||vr|                    |           t          j                            |          | d<   | S )NPATH )z/sbinz	/usr/sbin)	dictosenvironitemsgetsplitpathsepr   join)rH   search_pathsadditional_paths      r   rC   rC      s     RZ--//00$((44::2:FFL1 1 1,..000 "
 = =fr   c                     |                      dg           }t          d         d         }|                                D ]\  }}||v r| dxx         d|z   z  cc<   dS )zbAdjust the flags detected on the system to homogenize
    slightly different representations.
    r   conversionsdarwin_flagsrI   N)rZ   r   rY   )r   r   d2ldarwin_flag
linux_flags        r   adjust_raw_flagsrf      ss    
 HHWb!!E
}
%n
5C#&99;; . .Z%MMMS:--MMM. .r   c                 j    d| vrdS t           d         d         }| d         }||v r||         | d<   dS dS )z1Adjust the vendor field to make it human readablerO   Nra   arm_vendorsr   )r   rh   arm_codes      r   adjust_raw_vendorrj      sW    $$ }-m<K%&H;"-h"7 r   c                     i } t           t          j                             D ]h}	  |            } n8# t          $ r+}t	          j        t          |                     Y d}~nd}~ww xY w| r t          |            t          |             ni| S )zReturns a dictionary with information on the cpu of the current host.

    This function calls all the viable factories one after the other until
    there's one that is able to produce the requested information.
    N)	r   r@   rA   	Exceptionwarningswarnr1   rf   rj   )r   r   excs      r   raw_info_dictionaryrp      s     D 1 12 	 		$799DD 	$ 	$ 	$M#c((########	$  	T"""d###E	
 Ks   
/
A$!AA$c                      t                      }t                              |d            fdt          j                    D             pt          |          gS )zReturns an unordered list of known micro-architectures that are
    compatible with the info dictionary passed as argument.

    Args:
        info (dict): dictionary containing information on the host cpu
    c                     dS )NFr   )xys     r   <lambda>z/compatible_microarchitectures.<locals>.<lambda>   s     r   c                 ,    g | ]} |          |S r   r   ).0rs   r   testers     r   
<listcomp>z1compatible_microarchitectures.<locals>.<listcomp>   s(    ;;;!66$??;A;;;r   )rD   COMPATIBILITY_CHECKSrZ   r   valuesr   )r   architecture_familyrx   s   ` @r   compatible_microarchitecturesr}      sj     #** "%%&9;M;MNNF;;;;;w~'';;; !"566@ r   c                      t                      } t          |           }d }d |D             }t          ||          fd|D             }|sS t          ||          S )z3Detects the host micro-architecture and returns it.c                 R    t          | j                  t          | j                  fS )N)len	ancestorsfeatures)items    r   
sorting_fnzhost.<locals>.sorting_fn   s!    4>""C$6$666r   c                 (    g | ]}|j         d k    |S )generic)vendor)rw   cs     r   ry   zhost.<locals>.<listcomp>   s$    III18y3H3H!3H3H3Hr   )r&   c                      g | ]
}|k    |S r   r   )rw   r   best_generics     r   ry   zhost.<locals>.<listcomp>   s#    <<<1|+;+;!+;+;+;r   )rp   r}   max)r   
candidatesr   generic_candidatesr   s       @r   hostr      s       D /t44J7 7 7 JIZIII)z:::L
 =<<<Z<<<J   zz****r   c                 B     t           t                    r f  fd}|S )a  Decorator to register a function as a proper compatibility check.

    A compatibility check function takes the raw info dictionary as a first
    argument and an arbitrary target as the second argument. It returns True
    if the target is compatible with the info dictionary, False otherwise.

    Args:
        architecture_family (str or tuple): architecture family for which
            this test can be used, e.g. x86_64 or ppc64le etc.
    c                 V     t                                fdD                         S )Nc                     i | ]}|S r   r   )rw   familyfuncs     r   
<dictcomp>z:compatibility_check.<locals>.decorator.<locals>.<dictcomp>  s    $T$T$TfVT$T$T$Tr   )rz   update)r   r|   s   `r   r   z&compatibility_check.<locals>.decorator  s3    ##$T$T$T$T@S$T$T$TUUUr   )
isinstancer1   )r|   r   s   ` r   compatibility_checkr     sB     %s++ 524     r   )ppc64leppc64)r|   c                 4   t          j                    }t          j        d|                     dd                    }	 t          |                    d                    }n# t          $ r d}Y nw xY wt          |         }||k    p||j	        v o
|j
        |k    S )z8Compatibility check for PPC64 and PPC64LE architectures.z
POWER(\d+)cpurU   r   r   )r@   rB   researchrZ   intgroupAttributeErrorr   r   
generation)r   targetbasenamegeneration_matchr   	arch_roots         r   compatibility_check_for_powerr     s     !!Hy0C0CDD)//2233

    


	 !I)<yF,<<*

z
)*s   "A! !A0/A0r?   c                    d}|                      dd          }t          |                      dd                                                    }t          |         }||k    s	||j        v o$|j        |dfv o|j                            |          S )z-Compatibility check for x86_64 architectures.r?   r   r   r   rU   )rZ   setr[   r   r   r   r   issubset)r   r   r   r   r   r   s         r   compatibility_check_for_x86_64r   0  s     HXXk9--F488GR((..0011H !I	9		=	V-= = 	/Mfi00	/O$$X..r   r>   c                    d}t          |                     dd                                                    }|                     dd          }|j        dk    r|j        dk    rdS t
          |         }||j        k    o
|j        |dfv }t          j                    dk    r4|                     d|          }t
          |         }|o||k    p||j	        v S |o|j
                            |          S )	z.Compatibility check for AARCH64 architectures.r>   FeaturesrU   rO   r   Fr8   r   )r   rZ   r[   r   namer   r   r@   rA   r   r   r   )	r   r   r   r   r   r   arch_root_and_vendor	model_keyr   s	            r   compatibility_check_for_aarch64r   A  s     H488J++113344HXX'33F }	!!fkY&>&>u!I$5 &-L ; H$$HHWh//		"#V5)UFeo<UVFFO$<$<X$F$FFr   riscv64c                     d}|                      d          }|dk    rd}nd}t          |         }||k    s	||j        v o||k    p
|j        dk    S )z.Compatibility check for riscv64 architectures.r   uarchzsifive,u74-mcu74mcr   )rZ   r   r   r   )r   r   r   r   r   s        r   compatibility_check_for_riscv64r   ]  sq     HHHWE  !Ii@90@#@ %56=I5r   )!__doc__collectionsr   rW   r@   r   r-   rm   microarchitecturer   r   schemar   defaultdictrG   r   rz   r   r)   r6   rD   rR   rC   rf   rj   rp   r}   r   r   r   r   r   r   r   r   r   <module>r      s:  
 * )         				  				      A A A A A A A A             '{&t,,    < G$$$  %$.' ' '  2 H%%%$ $ &%$N	 	 		. 	. 	.8 8 8$  ,   + + +<  , )=>>>* * ?>*( 222  32  333G G 43G6 333  43  r   