o
    uJAff                     @  sJ  d dl 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	Z	d dl
Zd dlmZ ddlmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZ d
dlmZ d
dlmZ ddlmZ ejrddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ne'Z$e(dej)Z*G dd dej+Z,G dd de$Z-dS )    )annotationsNPath   )arglist)mesonlib)mlog)GnuLikeDynamicLinkerMixinSolarisDynamicLinkerCompCertDynamicLinker)LibType	OptionKey   )	compilers)CompileCheckMode   )VisualStudioLikeCompiler)
Dependency)ImmutableListProtocol)Environment)Compiler)ExternalProgramz\.so (?:\.[0-9]+)? (?:\.[0-9]+)? (?:\.[0-9]+)?$ |
                             ^(?:-Wl,)?-l |
                             \.a$c                   @  sP   e Zd ZdZdZdZdZdZddddZe	e
jdddddZdddZdS )CLikeCompilerArgs)-I-L)r   -isystemr   z-Dz-U)-lz-Wl,-lz-Wl,--export-dynamicz
-Wl,-rpath)z.libz.dll.soz.dylibz.a)-cz-S-Ez-pipe-pthreadFcopyboolreturnT.List[str]c           
        s  t  jtjsJ d   |r  }n }t  jjttt	frOd}d}t
|D ]\}}t|s4q*|}|dk r<|}q*|dkrO||d d ||d  j }|r fdd|D }g }	t
|D ]P\}}|d	soqe|d	kr|t|d k r ||d  |v r|	||d g7 }	qe|d
r |dd  |v r|	|g7 }	qe |dd  |v r|	|g7 }	qet|	D ]}|| q j|jS )NzHow did you get herer   r   z-Wl,--end-groupz-Wl,--start-groupc                   s   g | ]}  |qS  )_cached_realpath.0iselfr&   F/usr/lib/python3.10/site-packages/mesonbuild/compilers/mixins/clike.py
<listcomp>h   s    z/CLikeCompilerArgs.to_native.<locals>.<listcomp>r   z	-isystem=	      )
isinstancecompilerr   r   flush_pre_postr!   linkerr	   r
   r   	enumerateGROUP_FLAGSsearchinsertget_default_include_dirs
startswithlenr'   reversedpopunix_args_to_native
_container)
r,   r!   newZgroup_startZ	group_endr*   eachZdefault_dirsZreal_default_dirsZbad_idx_listr&   r+   r-   	to_nativeG   sJ   



 
zCLikeCompilerArgs.to_nativeNmaxsizeargstrc                 C  s   t j| S N)ospathrealpath)rE   r&   r&   r-   r'   {   s   z"CLikeCompilerArgs._cached_realpathc                 C  s   |    d| jd| jdS )NzCLikeCompilerArgs(z, ))r3   r2   r?   r+   r&   r&   r-   __repr__   s   zCLikeCompilerArgs.__repr__F)r!   r"   r#   r$   )rE   rF   r#   rF   r#   rF   )__name__
__module____qualname__prepend_prefixesdedup2_prefixesdedup1_prefixesdedup1_suffixesdedup1_argsrB   staticmethod	functools	lru_cacher'   rL   r&   r&   r&   r-   r   =   s    4
r   c                      s  e Zd ZU dZejri Zded< i Zded< i Z	ded< e
jZddddZddddZd	ddZd
ddZd
ddZd
ddZddd Zd
d!d"Zdd#d$Zd
d%d&Zd
d'd(Zd
d)d*Zdd,d-Zd
d.d/Zdd2d3Zdd7d8Ze 	ddd<d=Z	ddd>d?Z e dd@dAZ!ddBdCZ"d
dDdEZ#ddHdIZ$ddJdKZ%d
dLdMZ&ddNdOZ'ddQdRZ(ddXdYZ)ddZd[Z*ddd\ddddeZ+dddfdgddidjZ,ddd\ddmdnZ-ddrdsZ.e/j0fddvdwZ1ddd\dd{d|Z2dd~dZ3		d d!ddZ4ddd!ddZ5ddd\d"ddZ6ddd\d#ddZ7ddd\d$ddZ8ddd\d%ddZ9	fd&d'ddZ:d(ddZ;e<d)ddZ=e<d)ddZ>ddd\d*ddZ?ddd\d+ddZ@ddd,ddZAd-ddZBd.ddZCd.ddZDd-ddZEd&d/ddZFd&d0ddZGe<d1ddZHeId2ddǄZJe<d3dd̄ZKe d-dd΄ZLd4dd҄ZMd4ddԄZNeOjPdfd5ddׄZQdddلZRd6dd܄ZSd6ddބZT	Րd7d6ddZUd8ddZVd8ddZWdddZXd9ddZYd:ddZZd;ddZ[d<ddZ\d;ddZ]d<ddZ^e<d=ddZ_d>ddZ`d?ddZad@ddZbejdddA fddZcdBddZd  ZeS (C  CLikeCompilerz(Shared bits for the C and CPP Compilers.zT.Dict[str, T.List[str]]	warn_argszaT.Dict[T.Tuple[T.Tuple[str, ...], str, T.Tuple[str, ...], str, LibType], T.Optional[T.List[str]]]find_library_cachezYT.Dict[T.Tuple[T.Tuple[str, ...], str, T.Tuple[str, ...], bool], T.Optional[T.List[str]]]find_framework_cacheNexe_wrapperT.Optional['ExternalProgram']c                 C  s8   | j d |r| r| sd | _n|| _d | _d S )Nh)can_compile_suffixesaddfoundget_commandr^   preprocessor)r,   r^   r&   r&   r-   __init__   s
   
zCLikeCompiler.__init__argsT.Optional[T.Iterable[str]]r#   r   c                 C  s
   t | |S rG   )r   r,   rg   r&   r&   r-   compiler_args      
zCLikeCompiler.compiler_argsr"   c                 C     dS )NTr&   r+   r&   r&   r-   needs_static_linker      z!CLikeCompiler.needs_static_linkerr$   c                 C  s   |   S )zM
        Args that are always-on for all C compilers other than MSVC
        )get_largefile_argsr+   r&   r&   r-   get_always_args   s   zCLikeCompiler.get_always_argsc                 C     dgS )Nz	-nostdincr&   r+   r&   r&   r-   get_no_stdinc_args      z CLikeCompiler.get_no_stdinc_argsc                 C  rq   )Nz	-nostdlibr&   r+   r&   r&   r-   get_no_stdlib_link_args   rs   z%CLikeCompiler.get_no_stdlib_link_argslevelrF   c                 C  s
   | j | S rG   )r[   )r,   ru   r&   r&   r-   get_warn_args   rk   zCLikeCompiler.get_warn_argsc                 C  rq   )Nz-wr&   r+   r&   r&   r-   get_no_warn_args   s   zCLikeCompiler.get_no_warn_argsc                 C  rl   )Ndr&   r+   r&   r&   r-   get_depfile_suffix   rn   z CLikeCompiler.get_depfile_suffixc                 C  s   ddgS )Nr   z-Pr&   r+   r&   r&   r-   get_preprocess_only_args      z&CLikeCompiler.get_preprocess_only_argsc                 C  rq   )Nr   r&   r+   r&   r&   r-   get_compile_only_args   rs   z#CLikeCompiler.get_compile_only_argsc                 C  rq   )Nz-O0r&   r+   r&   r&   r-   get_no_optimization_args   rs   z&CLikeCompiler.get_no_optimization_args
outputnamec                 C  s   d|gS )Nz-or&   )r,   r~   r&   r&   r-   get_output_args   r{   zCLikeCompiler.get_output_argsc                 C  rq   Nz-Werrorr&   r+   r&   r&   r-   get_werror_args   rs   zCLikeCompiler.get_werror_argsrI   	is_systemc                 C  s"   |dkrd}|rd|gS d| gS )N .r   r   r&   )r,   rI   r   r&   r&   r-   get_include_args   s
   
zCLikeCompiler.get_include_argsenv'Environment'namec                 C     g S )zP
        Get dirs from the compiler, either `libraries:` or `programs:`
        r&   )r,   r   r   r&   r&   r-   get_compiler_dirs   s   zCLikeCompiler.get_compiler_dirs	elf_classT.Optional[int]'ImmutableListProtocol[str]'c           
   
     s   |  |d}|d u s|dkr|S g }|D ]]  fddt D }|s*|  q|D ]D}tj |}z2t|d"}|d}	|	dd d	ksQt|	d |krV|  W d    n1 s`w   Y  W  n	 t	yp   Y q,w q|S )
N	librariesr   c                   s0   g | ]}| d rtjtj |r|qS )r   )endswithrH   rI   isfilejoin)r)   frx   r&   r-   r.      s   0 z3CLikeCompiler._get_library_dirs.<locals>.<listcomp>rb   r      s   ELF)
r   rH   listdirappendrI   r   openreadintOSError)
r,   r   r   dirsretvalfilesr   Zfile_to_checkfdheaderr&   r   r-   _get_library_dirs   s0   

 
	zCLikeCompiler._get_library_dirsc                 C  s   |  || S )znWrap the lru_cache so that we return a new copy and don't allow
        mutation of the cached value.
        )r   r!   )r,   r   r   r&   r&   r-   get_library_dirs   s   zCLikeCompiler.get_library_dirsc                 C  s   |  |dS )z
        Programs used by the compiler. Also where toolchain DLLs such as
        libstdc++-6.dll are found with MinGW.
        programs)r   r,   r   r&   r&   r-   _get_program_dirs  s   zCLikeCompiler._get_program_dirsc                 C  s   |  | S rG   )r   r!   r   r&   r&   r-   get_program_dirs     zCLikeCompiler.get_program_dirsc                 C  rq   )Nz-fPICr&   r+   r&   r&   r-   get_pic_args  rs   zCLikeCompiler.get_pic_argspch_dirr   c                 C  s   dt j|gS )Nz-include)rH   rI   basename)r,   r   r   r&   r&   r-   get_pch_use_args  s   zCLikeCompiler.get_pch_use_argsc                 C  s   t j|d |   S )Nr   )rH   rI   r   get_pch_suffixr,   r   r&   r&   r-   get_pch_name  s   zCLikeCompiler.get_pch_namec                 C  r   rG   r&   r+   r&   r&   r-   r9     rn   z&CLikeCompiler.get_default_include_dirsc                 C     | j |S rG   )r4   Zexport_dynamic_argsr   r&   r&   r-   gen_export_dynamic_link_args     z*CLikeCompiler.gen_export_dynamic_link_args
implibnamec                 C  r   rG   )r4   Zimport_library_args)r,   r   r&   r&   r-   gen_import_library_args   r   z%CLikeCompiler.gen_import_library_argswork_direnvironmentsnamecodeNonec              
   C  s$  t d|   d t| j t d| jd tj	||}|
ddd }tj}| jr;|d7 }| jd u r;tj}| ||\}}	|| |	 }
|d7 }tj	||}t|d	d
d}|| W d    n1 slw   Y  | j|g | | |
 }tj||d\}}}t dt| t d t | t d t | t d |jdkrtd|   d| jr| jd u rd S | j |g }n|g}t dt| ztj|tjtjd}W n ty } z	td|dd }~ww |jdkrtd| j d|   dd S )NzSanity testing z
 compiler:zIs cross compiler: r   r   r   Z_crossz.exewzutf-8)encoding)cwdz#Sanity check compiler command line:zSanity check compile stdout:z"-----
Sanity check compile stderr:z-----z	Compiler z cannot compile programs.zRunning test binary command: )stdoutstderrz)Could not invoke sanity test executable: zExecutables created by z
 compiler z are not runnable.)r   debugget_display_languager   	join_argsexelistis_crossrH   rI   r   rsplitr   LINKr^   COMPILE_get_basic_compiler_argslinker_to_compiler_argsr   writer   
Popen_safe
returncodeEnvironmentExceptionname_stringrd   
subprocessrunDEVNULL	Exceptionlanguage)r,   r   r   r   r   source_namebinnamemodecargslargsextra_flagsZbinary_nameofilecmdlistpcstdostdepeer&   r&   r-   _sanity_check_impl#  sR    







z CLikeCompiler._sanity_check_implc                 C  s   d}|  ||d|S )Nz.int main(void) { int class=0; return class; }
zsanitycheckc.c)r   )r,   r   r   r   r&   r&   r-   sanity_check[  s   zCLikeCompiler.sanity_check
extra_argsdependencieshnameprefixr   IT.Union[None, T.List[str], T.Callable[['CompileCheckMode'], T.List[str]]]r    T.Optional[T.List['Dependency']]T.Tuple[bool, bool]c                C  "   | d| d}| j ||||dS )N
        #include <>r   compiles)r,   r   r   r   r   r   r   r&   r&   r-   check_header_  s   
zCLikeCompiler.check_headerF)r   r   disable_cacher   c                C  s4   | d| d| d| d}| j ||||tj|dS )Nz;
        #ifdef __has_include
         #if !__has_include("z")
          #error "Header 'zG' could not be found"
         #endif
        #else
         #include <z>
        #endif)r   r   r   r   )r   r   
PREPROCESS)r,   r   r   r   r   r   r   r   r&   r&   r-   
has_headerg  s   
zCLikeCompiler.has_headersymbolGT.Union[None, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]]c                C  s.   | d| d| d| d}| j ||||dS )Nr   zx>
        int main(void) {
            /* If it's not defined as a macro, try to use as a symbol */
            #ifndef z
                z4;
            #endif
            return 0;
        }r   r   )r,   r   r   r   r   r   r   tr&   r&   r-   has_header_symbolv  s   
	zCLikeCompiler.has_header_symbolr   r   !T.Tuple[T.List[str], T.List[str]]c           
   	     s  g }g }|t ju r1z|jjtd j}|jjtd j}|| ||7 }W n ttfy0   Y nw |j	| j
| j t trB g |  }||7 }|t ju r}|| j
| jd }|d uri|| |d | j7 }|j| j
| j}	| fdd|	D 7 }|| |7 }||fS )Nb_vscrt	buildtype_ldr   c                      g | ]}| vr|qS r&   r&   r)   xZsys_argsr&   r-   r.         z:CLikeCompiler._get_basic_compiler_args.<locals>.<listcomp>)r   r   coredataoptionsr   valueget_crt_compile_argsKeyErrorAttributeErrorget_external_argsfor_machiner   r1   rF   remove_linkerlike_argslookup_binary_entryuse_linker_argsversionget_external_link_argsget_compiler_args_for_mode)
r,   r   r   r   r   crt_valr   Zcleaned_sys_argsZld_valueZsys_ld_argsr&   r   r-   r     s.   



z&CLikeCompiler._get_basic_compiler_args]T.Union[None, arglist.CompilerArgs, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]]arglist.CompilerArgsc              	     s8  |d u rg }nt |}t  fdd|D }|d u rg }n
t|tjjs)|g}|  }g }|D ]6}|| 7 }| dk}|	 D ]}	|	
| | D ]}
|| |
| qOqC tju rg|| 7 }q1| | \}}||7 }||7 }||  7 }| g dgkr|g krd|vr|dg7 }|| | }|S )Nc                   s"   g | ]}t |r| jn|qS r&   )callabler  )r)   r   r   r&   r-   r.     s   " z4CLikeCompiler.build_wrapper_args.<locals>.<listcomp>systemz/link)r   listifyr1   collectionsabcIterablerj   get_compile_argsget_include_typeget_include_dirsto_string_listget_source_dirget_build_dirextendr   r   r   get_link_argsr   get_compiler_check_argsr   )r,   r   r   r   r   r   r   rx   system_incdirr*   idircaZlarg   r&   r  r-   build_wrapper_args  s8   

 
z CLikeCompiler.build_wrapper_args'mesonlib.FileOrString'GT.Union[T.List[str], T.Callable[[CompileCheckMode], T.List[str]], None]compilers.RunResultc                C  s^  | | j}|r| jd u rtd| j||||tjddl}|jdkr>t	
d|j d|j d tdW  d    S |rJ| j |jg }n|jg}z
t|\}}	}
W n* ty } zt	
d	| d
| d tdW  Y d }~W  d    S d }~ww W d    n1 sw   Y  t	
d t	
|	 t	
d t	
|
 td|j|	|
S )Nz8Can not run test applications in this cross environment.Tr   want_outputr   zCould not compile test file z: 
FzCould not run: z	 (error: z)
zProgram stdout:
zProgram stderr:
)need_exe_wrapperr  r^   r   CrossNoRunException_build_wrapperr   r   r   r   r   
input_name	RunResultrd   output_namer   r   r   )r,   r   r   r   r   r+  pr   r   soZser   r&   r&   r-   r     s4   






zCLikeCompiler.run
expressionc                 C  s&   | d| d}| j ||||dd S )NzI
        #include <stddef.h>
        int main(void) { static int a[1-2*!(z)]; a[0]=0; return 0; }r   r   r   )r,   r3  r   r   r   r   r   r&   r&   r-   _compile_int  s   
zCLikeCompiler._compile_intlowhighguessr   c	                 C  s  t |tr| | d| ||||r|S d}	d}
t |tr#t |ts| | d||||rhd }}| | d| ||||re|d }||	krNtdt|d	 d |	}| | d| ||||sA|}n[d
 }}| | d| ||||r|d }||
k rtdt|d	 |
}| | d| ||||sz|}n$||k rtd| d| d| d| }| |||||std||kr|t|| d	  }| | d| ||||r|}n|d }||ks|S )Nz == ii   z >= 0r   z > r   zCross-compile check overflowedr   r%   z < z!high limit smaller than low limitz <= z && z >= zValue out of given range)r1   r   r4  r   r   minmax)r,   r3  r5  r6  r7  r   r   r   r   ZmaxintZminintcur	conditionr&   r&   r-   cross_compute_int  sF   




zCLikeCompiler.cross_compute_int)r   c             
   C  st   |d u rg }| j r| ||||||||S | d| d}	| j|	|||d}
|
js+dS |
jdkr5tdt|
jS )Nzr
        #include<stddef.h>
        #include<stdio.h>
        int main(void) {
            printf("%ld\n", (long)(z#));
            return 0;
        }r   r%   r   z&Could not run compute_int test binary.)	r   r<  r   compiledr   r   r   r   r   )r,   r3  r5  r6  r7  r   r   r   r   r   resr&   r&   r-   compute_int-  s   



zCLikeCompiler.compute_inttypenamec             
   C  sV   |d u rg }| d| d}| j ||||dd sdS | d| dd d d ||||S )NB
        #include <stddef.h>
        int main(void) {
            + something;
            return 0;
        }r   r   r%   zsizeof(rK   r   r<  r,   r@  r   r   r   r   r   r&   r&   r-   cross_sizeofD  s   
 zCLikeCompiler.cross_sizeofT.Tuple[int, bool]c          	      C  s~   |d u rg }| j r| j|||||d}|dfS | d| d}| j||||d}|js-dS |jdkr7tdt|j|j	fS )Nr   Fzy
        #include<stddef.h>
        #include<stdio.h>
        int main(void) {
            printf("%ld\n", (long)(sizeof(z$)));
            return 0;
        })r%   Fr   z!Could not run sizeof test binary.)
r   rE  
cached_runr=  r   r   r   r   r   cached)	r,   r@  r   r   r   r   rr   r>  r&   r&   r-   sizeofT  s$   


zCLikeCompiler.sizeofT.Optional[T.List[str]]c             
   C  s^   |d u rg }| d| d}| j ||||dd sdS | d| d}| dd d d ||||S )	NrA  rB  r   r   r%   zR
        #include <stddef.h>
        struct tmp {
            char c;
            z target;
        };zoffsetof(struct tmp, target)rC  rD  r&   r&   r-   cross_alignmentl  s   
zCLikeCompiler.cross_alignmentc          
      C  s   |d u rg }| j r| j|||||d}|dfS | d| d}| j||||d}|js0td|jdkr:tdt|j}	|	dkrLtd| d	|	|j	fS )
Nr   Fzm
        #include <stdio.h>
        #include <stddef.h>
        struct tmp {
            char c;
            z target;
        };
        int main(void) {
            printf("%d", (int)offsetof(struct tmp, target));
            return 0;
        }z!Could not compile alignment test.r   z$Could not run alignment test binary.z!Could not determine alignment of z&. Sorry. You might want to file a bug.)
r   rL  rG  r=  r   r   r   r   r   rH  )
r,   r@  r   r   r   r   rI  r   r>  Zalignr&   r&   r-   	alignment  s*   





zCLikeCompiler.alignmentdnameAT.Union[T.List[str], T.Callable[[CompileCheckMode], T.List[str]]]T.Tuple[str, bool]c                 C  sB  d}d}d}	d| d| d| d|	 d| | | }
| j |||tjd	 }tj| j|
|j|tjd
}|rAtj| j|
|tjd
}| }|j	}|j
dkrUtd|W d    n1 s_w   Y  |j|}|j|}|dks||dks|||krtd|j|t| | }||	krd }||fS | | }||fS )Nz#"MESON_GET_DEFINE_DELIMITER_START"
z!
"MESON_GET_DEFINE_DELIMITER_END"z%"MESON_GET_DEFINE_UNDEFINED_SENTINEL"z	
        z
        #ifndef z
        # define  z
        #endif
        r  r   r   r   zCould not get define r%   z,Delimiters not found in preprocessor output.)r$  r   r   rB   rX   partialcached_compiler   compilerH  r   r   r   r   findrfindMesonBugExceptionr;   _concatenate_string_literalsstrip)r,   rN  r   r   r   r   r   Zdelim_startZ	delim_endZsentinel_undefr   rg   funcr1  rH  Zstar_idxZend_idxdefine_valuer&   r&   r-   
get_define  sT   


zCLikeCompiler.get_definefnamertypeT.Union[str, int]c                 C  s   |dkr	d}d}n|dkrd}d}nt d|| d| d	| d
| d}	| j|	|||d}
|
js<td| d|dkrC|
jS |dkr`zt|
j W S  ty_   td| dw J d)Nstringz%sz(char*)r   z%lliz(long long int)zBUG: Unknown return type zJ
        #include <stdio.h>
        int main(void) {
            printf ("z", rQ  z$());
            return 0;
        }r   zCould not get return value of z()zReturn value of z() is not an intFZUnreachable)	AssertionErrorr   r=  r   r   r   r   rZ  
ValueError)r,   r^  r_  r   r   r   r   fmtcastr   r>  r&   r&   r-   get_return_value  s4   zCLikeCompiler.get_return_valueT.Tuple[str, str]c                  C  s   d} | d7 } d}| |fS )a0  
        Try to find the function without a prototype from a header by defining
        our own dummy prototype and trying to link with the C library (and
        whatever else the compiler links in by default). This is very similar
        to the check performed by Autoconf for AC_CHECK_FUNCS.
        z
        #define {func} meson_disable_define_of_{func}
        {prefix}
        #include <limits.h>
        #undef {func}
        zb
        #ifdef __cplusplus
        extern "C"
        #endif
        char {func} (void);
        zA
        int main(void) {{
          return {func} ();
        }}r&   headmainr&   r&   r-   _no_prototype_templ  s   	z!CLikeCompiler._no_prototype_templc                  C  s   d} d}| |fS )z
        Returns a head-er and main() call that uses the headers listed by the
        user for the function prototype while checking if a function exists.
        z{prefix}
#include <limits.h>
z
int main(void) {{
            void *a = (void*) &{func};
            long long b = (long long) a;
            return (int) b;
        }}r&   rh  r&   r&   r-   _have_prototype_templ	  s   	z#CLikeCompiler._have_prototype_templfuncnamec                C  s,  |du rg }d| }| dd}| jr1|jj|d}|dur1t|tr(|dfS td| d||d}d	}	d
|v rC| 	 \}
}n| 
 \}
}|
|	 | }| j|jdi ||||d\}}|rfd|fS |  dv rndS d
|v|d< |d}||d< |rdnd|d< d}| j|jdi ||||dS )a  Determine if a function exists.

        First, this function looks for the symbol in the default libraries
        provided by the compiler (stdlib + a few others usually). If that
        fails, it checks if any of the headers specified in the prefix provide
        an implementation of the function, and if that fails, it checks if it's
        implemented as a compiler-builtin.
        Nzhas function rQ  _FzCross variable z is not a boolean.)r   r[  z
        #if defined __stub_{func} || defined __stub___{func}
        fail fail fail this function is not going to work
        #endif
        z#includer   T>   msvczintel-clFFZno_includesZ
__builtin_
is_builtinr   a  {prefix}
        int main(void) {{

        /* With some toolchains (MSYS2/mingw for example) the compiler
         * provides various builtins which are not really implemented and
         * fall back to the stdlib where they aren't provided and fail at
         * build/link time. In case the user provides a header, including
         * the header didn't lead to the function being defined, and the
         * function we are checking isn't a builtin itself we assume the
         * builtin is not functional and we just error out. */
        #if !{no_includes:d} && !defined({func}) && !{is_builtin:d}
            #error "No definition for {__builtin_}{func} found in the prefix"
        #endif

        #ifdef __has_builtin
            #if !__has_builtin({__builtin_}{func})
                #error "{__builtin_}{func} not found"
            #endif
        #elif ! defined({func})
            {__builtin_}{func};
        #endif
        return 0;
        }}r&   )replacer   
propertieshostgetr1   r"   r   r   rl  rk  linksformatget_idr:   )r,   rm  r   r   r   r   varnamevalfargsZ
stubs_failri  rj  Ztemplr>  rH  rq  r   r&   r&   r-   has_function  s>   



zCLikeCompiler.has_functionmembernamesc          	      C  sH   |d u rg }d dd |D }| d| d| d}| j||||dS )Nr   c                 s  s    | ]	}d | dV  qdS )zfoo.z;
Nr&   )r)   memberr&   r&   r-   	<genexpr>  s    z,CLikeCompiler.has_members.<locals>.<genexpr>z&
        void bar(void) {
            z foo;
            z

        }r   )r   r   )	r,   r@  r}  r   r   r   r   membersr   r&   r&   r-   has_members  s   
zCLikeCompiler.has_membersc                C  r   )Nz-
        void bar(void) {
            sizeof(z);
        }r   r   rD  r&   r&   r-   has_type  s   
zCLikeCompiler.has_typec           	   	   C  sB  d}d|   d }| tj}d}| j|||tjddt}|jdkr.td|d	|j tj	
|js=td
|dt|jd?}|D ]4}d| |v rbtd  W d   W d   dS ||v rztd  W d   W d   dS qFW d   n1 sw   Y  W d   n1 sw   Y  td|d)z
        Check if symbols have underscore prefix by compiling a small test binary
        and then searching the binary for the string,
        s   meson_uscore_prefixzD#ifdef __cplusplus
        extern "C" {
        #endif
        void zG (void) {}
        #ifdef __cplusplus
        }
        #endif
        )_symbols_have_underscore_prefix_searchbinT)r   r   r)  r   zBUG: Unable to compile z check: z'BUG: Can't find compiled test code for z checkr      _z9Underscore prefix check found prefixed function in binaryNz=Underscore prefix check found non-prefixed function in binaryFzBUG: z+ check did not find symbol string in binary)decoder   r   r   r-  r   RuntimeErrorr   rH   rI   r   r0  r   r   r   )	r,   r   Zsymbol_namer   rg   nr1  oliner&   r&   r-   r    s>   


z7CLikeCompiler._symbols_have_underscore_prefix_searchbinT.Optional[bool]c                 C  s   d}d| d}| j ||tjddD}|jdkr td|j |j|d  }t	d	|d
 |dkr@	 W d   dS |dkrM	 W d   dS 	 W d   dS 1 sYw   Y  dS )aZ  
        Check if symbols have underscore prefix by querying the
        __USER_LABEL_PREFIX__ define that most compilers provide
        for this. Return if functions have underscore prefix or None
        if it was not possible to determine, like when the compiler
        does not set the define or the define has an unexpected value.
        z""MESON_HAVE_UNDERSCORE_DELIMITER" z
        #ifndef __USER_LABEL_PREFIX__
        #define MESON_UNDERSCORE_PREFIX unsupported
        #else
        #define MESON_UNDERSCORE_PREFIX __USER_LABEL_PREFIX__
        #endif
        z MESON_UNDERSCORE_PREFIX
        Fr(  r   zHBUG: Unable to preprocess _symbols_have_underscore_prefix_define check: r%   z@Queried compiler for function prefix: __USER_LABEL_PREFIX__ is ""rn  NTr   )
r-  r   r   r   r  r   	partitionrstripr   r   )r,   r   delimr   r1  Zsymbol_prefixr&   r&   r-   &_symbols_have_underscore_prefix_define  s"   
$z4CLikeCompiler._symbols_have_underscore_prefix_definec                 C  s6   |j | j }| rdS | s| r|jdkS dS )z
        Check if symbols have underscore prefix by consulting a hardcoded
        list of cases where we know the results.
        Return if functions have underscore prefix or None if unknown.
        Tx86N)machinesr  	is_darwin
is_windows	is_cygwin
cpu_family)r,   r   mr&   r&   r-   $_symbols_have_underscore_prefix_list  s   
z2CLikeCompiler._symbols_have_underscore_prefix_listc                 C  s6   |  |}|dur|S | |}|dur|S | |S )zR
        Check if the compiler prefixes an underscore to global C symbols
        N)r  r  r  )r,   r   resultr&   r&   r-   symbols_have_underscore_prefix  s   


z,CLikeCompiler.symbols_have_underscore_prefixprefixessuffixessharedc                 C  sZ   g }|D ]}|D ]}| |d |  qq|r+|j| j  r+|D ]	}| |d  q!|S )Nz{}.z{}.so.[0-9]*.[0-9]*)r   r  r  
is_openbsd)r,   r   r  r  r  patternsr1  sr&   r&   r-   _get_patterns  s   zCLikeCompiler._get_patternslibtyper   strictT.Tuple[str, ...]c              	     sr  dg}|rt | tsdg}nddg}|j| j  rddg}n0|j| j  r:t | tr0dg}ng d}|dg7 }n|j| j  rLddg}d	g| }ndg}|tju rr| 	|||d
  
 fdd| 	|||dD  t S |tju r| 	|||d  
 fdd| 	|||d
D  t S |tju r| 	|||d
 t S |tju sJ | 	|||d t S )zg
        Get library prefixes and suffixes for the target platform ordered by
        priority
        alibr   dylibr2  )dll.ar  dllr  r  cygTc                   r   r&   r&   r   r  r&   r-   r.   2  r   z4CLikeCompiler.get_library_naming.<locals>.<listcomp>Fc                   r   r&   r&   r   r  r&   r-   r.   5  r   )r1   r   r  r  r  r  r  r   PREFER_SHAREDr  r  PREFER_STATICSHAREDSTATICtuple)r,   r   r  r  Zstlibextr  Zshlibextr&   r  r-   get_library_naming  s:   


$	
$
z CLikeCompiler.get_library_naminglibsc              	   C  s   ddd}g }| D ]/}|j ddd	}t|d
krq	ztdd |d dD  W n	 ty2   Y q	w || q	t||ddS )Nr   rF   r#   T.Tuple[int, ...]c                 S  s*   | j dddd }tdd |dD S )N.so.r   maxsplitc                 s      | ]}t |V  qd S rG   r   r(   r&   r&   r-   r  A      zHCLikeCompiler._sort_shlibs_openbsd.<locals>.tuple_key.<locals>.<genexpr>r   )r   r  split)r   verr&   r&   r-   	tuple_key?  s   z5CLikeCompiler._sort_shlibs_openbsd.<locals>.tuple_keyr  r   r  r   c                 s  r  rG   r  r(   r&   r&   r-   r  J  r  z5CLikeCompiler._sort_shlibs_openbsd.<locals>.<genexpr>r   T)keyreverse)r   rF   r#   r  )r   r;   r  r  rc  r   sorted)r  r  Zfilteredr  retr&   r&   r-   _sort_shlibs_openbsd=  s   
 z"CLikeCompiler._sort_shlibs_openbsdpattern	directorylibnameT.List[Path]c                 C  s>   t ||| }d|v rdd | tt|D S |gS )N*c                 S  s   g | ]}t |qS r&   r   r   r&   r&   r-   r.   W  s    z:CLikeCompiler._get_trials_from_pattern.<locals>.<listcomp>)r   rw  r  globrF   )clsr  r  r  r   r&   r&   r-   _get_trials_from_patternP  s   z&CLikeCompiler._get_trials_from_patternr   pathsT.Optional[Path]c              	   C  sz   |D ]8}|  r:| jj r6| jj r6tt|}|r$| jjj|vr6t	
d| d| d| jjj  q|  S qdS )z
        We just check whether the library exists. We can't do a link check
        because the library might have unresolved symbols that require other
        libraries. On macOS we check if the library matches our target
        architecture.
        z	Rejected z, supports z
 but need N)is_filer  rt  r  buildr   darwin_get_object_archsrF   r  r   r   )r   r  r1  Zarchsr&   r&   r-   _get_file_from_listZ  s   "z!CLikeCompiler._get_file_from_listc                 C  s   |  dd|d dkS )zP
        returns true if the output produced is 64-bit, false if 32-bit
        zvoid *r   r   r0   )rJ  r   r&   r&   r-   output_is_64bitp  s   zCLikeCompiler.output_is_64bit
extra_dirslib_prefix_warningc              	   C  s4  |s|t ju s|| jv r4d| g}|  |   }|| | }	| j|||	ddd r-|S || jv r4d S | ||}
z| |rCd}nd}W n t	j
tfyT   d}Y nw t|| ||D ]8}|
D ]3}| |||}|soqc| ||}|sxqc|dr|j|r|rtd|d	 | g    S q_d S )
Nr   Tr   r   r   r   r   r  zfind_library(z>) starting in "lib" only works by accident and is not portable)r   r  internal_libsget_linker_always_argsget_allow_undefined_link_argsr   rv  r  r  r   MesonExceptionr  	itertoolschainr   r  r  r:   r   r   warningas_posix)r,   r  r   r  r   r  r  r   r   r   r  r   rx   r1  trialstrialr&   r&   r-   _find_library_realw  s>   




z CLikeCompiler._find_library_realc           	      C  s~   || j v rg S t|tr|g}t| j|t|||f}|| jvr0| ||||||}|| j|< n| j| }|d u r;d S | S rG   )ignore_libsr1   rF   r  r   r\   r  r!   )	r,   r  r   r  r   r  r  r  r  r&   r&   r-   _find_library_impl  s   



z CLikeCompiler._find_library_implTc                 C  s   d}|  ||||||S Nzint main(void) { return 0; }
)r  )r,   r  r   r  r  r  r   r&   r&   r-   find_library  s   zCLikeCompiler.find_libraryc                 C  s   | j dkr
td| jddg d }||  7 }||j| j| j7 }t	
dd|d tj }d	|d
< tj||tjd\}}}g }|dD ]}d|vrRqK||dd   qK|S )z
        These are usually /Library/Frameworks and /System/Library/Frameworks,
        unless you select a particular macOS SDK with the -isysroot flag.
        You can also add to this by setting -F in CFLAGS.
        clangz2Cannot find framework path with non-clang compilerF)ccache)z-vr   -z#Finding framework path by running: rQ  r*  CLC_ALL)r   stdinz(framework directory)Ni)idr   r  get_exelistrp   r   r  r  r   r   r   r   rH   environr!   r   r   PIPEr  r   rZ  )r,   r   commandsos_envrn  r   r  r  r&   r&   r-   find_framework_paths  s   


z"CLikeCompiler.find_framework_pathsallow_systemc           	      C  s^   d}g }|D ]	}|d| g7 }q|rg nddg}|d|g7 }| j |||| ddd r-|S d S )	Nzint main(void) { return 0; }z-Fz-Zz
-L/usr/libz
-frameworkTr  r   )rv  )	r,   r   r   r  r  r   	link_argsrx   r   r&   r&   r-   _find_framework_real  s   z"CLikeCompiler._find_framework_realc                 C  sj   t |tr|g}t| j|t||f}|| jv r| j| }n| ||||}|| j|< |d u r1d S | S rG   )r1   rF   r  r   r]   r  r!   )r,   r   r   r  r  r  r  r&   r&   r-   _find_framework_impl  s   


z"CLikeCompiler._find_framework_implc                 C  s   |  ||||S )z
        Finds the framework with the specified name, and returns link args for
        the same or returns None when the framework is not found.
        )r  )r,   r   r   r  r  r&   r&   r-   find_framework  s   zCLikeCompiler.find_frameworkr  r   c                 C  r   rG   r&   r,   r  r   r&   r&   r-   r       z"CLikeCompiler.get_crt_compile_argsc                 C  r   rG   r&   r  r&   r&   r-   get_crt_link_args  r  zCLikeCompiler.get_crt_link_argsc                 C  s&   |j | j }| s| rg S dgS )Nr    )r  r  is_haikur  )r,   r   Zhost_mr&   r&   r-   thread_flags   s   zCLikeCompiler.thread_flagsc                 C  s   |  S rG   )r!   ri   r&   r&   r-   r     r{   z%CLikeCompiler.linker_to_compiler_argsc                 C  s   | j ||||dS )NrR  r   )r,   rg   r   r   r   r&   r&   r-   has_arguments
  s   zCLikeCompiler.has_argumentsc                 C  sf   g }|D ]$}| dr|d|dd    | dr#t| d || q| j|||tjdS )Nz-Wno-z-Wr   z-Wl,a9   looks like a linker argument, but has_argument and other similar methods only support checking compiler arguments. Using them to check linker arguments are never supported, and results are likely to be wrong regardless of the compiler you are using. has_link_argument or other similar method can be used instead.r  )r:   r   r   r  r  r   r   )r,   rg   r   r   new_argsrE   r&   r&   r-   _has_multi_arguments  s   

z"CLikeCompiler._has_multi_argumentsc                 C     |  ||dS )Nzextern int i;
int i;
)r  r,   rg   r   r&   r&   r-   has_multi_arguments!  r   z!CLikeCompiler.has_multi_argumentsc                 C  s,   | j  | }| |}| j|||tjdS )Nr  )r4   fatal_warningsr   r  r   r   )r,   rg   r   r   r&   r&   r-   _has_multi_link_arguments$  s   
z'CLikeCompiler._has_multi_link_argumentsc                 C  r  r  )r   r  r&   r&   r-   has_multi_link_arguments,  r   z&CLikeCompiler.has_multi_link_argumentsr  c                 C  sD   t d}| }||}|r d|dddd}||}|s|S )NzV(?P<pre>.*([^\\]")|^")(?P<str1>([^\\"]|\\.)*)"\s+"(?P<str2>([^\\"]|\\.)*)(?P<post>".*)r   preZstr1Zstr2post)rerU  matchr   group)r  r  r  r  r&   r&   r-   rY  /  s   


z*CLikeCompiler._concatenate_string_literalsc                 C  rq   r   r&   r   r&   r&   r-   !get_has_func_attribute_extra_args9  s   z/CLikeCompiler.get_has_func_attribute_extra_argsc                 C  sD   |j | j }| s| s|dv rdS | j| ||| |dS )N>   	dllexport	dllimportrp  )r   )r  r  r  r  r   attribute_check_funcr  )r,   r   r   r  r&   r&   r-   has_func_attribute?  s   z CLikeCompiler.has_func_attributedisablec                 C  s   |rdgS g S )Nz-DNDEBUGr&   )r,   r  r&   r&   r-   get_assert_argsJ  s   zCLikeCompiler.get_assert_argsrC   srcc                   s   | j dkrdS t |S )NPREPROCESSORT)r   supercan_compile)r,   r  	__class__r&   r-   r  O  s   
zCLikeCompiler.can_compiler   c                 C  s@   | j st| | _ | j|   | j _d| j _| j| j  | j S )Nr  )re   r!   r   get_preprocess_to_file_argsr   modesr   r+   r&   r&   r-   get_preprocessorV  s   zCLikeCompiler.get_preprocessorrG   )r^   r_   )rg   rh   r#   r   )r#   r"   )r#   r$   )ru   rF   r#   r$   rN   )r~   rF   r#   r$   )rI   rF   r   r"   r#   r$   )r   r   r   rF   r#   r$   )r   r   r   r   r#   r   )r   r   r   r   r#   r$   )r   r   r#   r   )r   r   r#   r$   )r   rF   r   rF   r#   r$   )r   rF   r#   rF   )r   rF   r#   r$   )
r   rF   r   r   r   rF   r   rF   r#   r   )r   rF   r   r   r#   r   )r   rF   r   rF   r   r   r   r   r   r   r#   r   )r   rF   r   rF   r   r   r   r   r   r   r   r"   r#   r   )r   rF   r   rF   r   rF   r   r   r   r   r   r   r#   r   )r   r   r   r   r#   r   )
r   r   r   r  r   r   r   r   r#   r  )
r   r%  r   r   r   r&  r   r   r#   r'  )r3  rF   r   rF   r   r   r   r   r   r   r#   r"   )NN)r3  rF   r5  r   r6  r   r7  r   r   rF   r   r   r   r   r   r   r#   r   )r@  rF   r   rF   r   r   r   r   r   r   r#   r   )r@  rF   r   rF   r   r   r   r   r   r   r#   rF  )r@  rF   r   rF   r   r   r   rK  r   r   r#   r   )r@  rF   r   rF   r   r   r   rK  r   r   r#   rF  rM   )rN  rF   r   rF   r   r   r   rO  r   r   r   r"   r#   rP  )r^  rF   r_  rF   r   rF   r   r   r   rK  r   r   r#   r`  )r#   rg  )rm  rF   r   rF   r   r   r   rK  r   r   r#   r   )r@  rF   r}  r$   r   rF   r   r   r   r   r   r   r#   r   )r@  rF   r   rF   r   r   r   rO  r   r   r#   r   )r   r   r#   r"   )r   r   r#   r  )
r   r   r  r$   r  r$   r  r"   r#   r$   )r   r   r  r   r  r"   r#   r  )r  r$   r#   r$   )r  rF   r  rF   r  rF   r#   r  )r   r   r  r  r#   r  )r  rF   r   r   r  r$   r   rF   r  r   r  r"   r#   rK  )r  rF   r   r   r  r$   r  r   r  r"   r#   rK  )
r   rF   r   r   r  r$   r  r"   r#   rK  )T)r  rF   r   rF   r#   r$   )rg   r$   r#   r$   )
rg   r$   r   r   r   rF   r   r   r#   r   )rg   r$   r   r   r   rF   r#   r   )rg   r$   r   r   r#   r   )r  rF   r#   rF   )r   rF   r#   r$   )r   rF   r   r   r#   r   )r  r"   r#   r$   )r  r%  r#   r"   )r#   r   )frO   rP   rQ   __doc__TTYPE_CHECKINGr[   __annotations__r\   r]   r   UNIXY_COMPILER_INTERNAL_LIBSr  rf   rj   rm   rp   rr   rt   rv   rw   ry   rz   r|   r}   r   r   r   r   rX   rY   r   r   r   r   r   r   r   r9   r   r   r   r   r   r   r   r   r   r   r$  r   r4  r<  r?  rE  rJ  rL  rM  r]  rf  rW   rk  rl  r|  r  r  r  r  r  r  r  r  r  classmethodr  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r   r  r  r  r   r  rY  r  r  r  r  r  __classcell__r&   r&   r  r-   rZ      s   
 (8	+.2"&"f
".	,		
rZ   ).
__future__r   r  rX   r  r  rH   r  r   r!   typingr  pathlibr   r   r   r   r   linkers.linkersr	   r
   r   r   r   r   r   Zvisualstudior   r  r   r   Z_typingr   r   r   Zcompilers.compilersr   r   r   objectrU  Xr6   CompilerArgsr   rZ   r&   r&   r&   r-   <module>   s@   H