
    eCi                        d 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	 ddl
m
Z
  ej        d          Zdadadadad Zd	 Zd
 Zej        d             Zd Zd Zd Zd Zd Zd Zd ZddZd Zd Zd Z ddZ!d Z"d Z#dS )z0Permanent cache for system libraries and ports.
    N)Path   )configfilelockutils)settingscachec                 @    t          j        | t           j                  S N)osaccessW_OK)paths    //b/s/w/ir/x/w/install/emscripten/tools/cache.pyis_writabler      s    	4	!	!!    c           
         t           j        rt          d          t          t                    s t          j        dt           d|  d           t          dk    rt          	                    dt          j                     dt                      dt          j        vsJ d	|  d
            	 t                              d           n\# t          j        $ rJ t                              dt           d|  dt$           d           t                                           Y nw xY wdt          j        d<   t          	                    d           t          dz  ad S )Nz1Attempt to lock the cache but FROZEN_CACHE is setzcache directory "z-" is not writable while accessing cache for: d (see https://emscripten.org/docs/tools_reference/emcc.html for info on setting the cache directory)r   PID z9 acquiring multiprocess file lock to Emscripten cache at EM_CACHE_IS_LOCKEDzFattempt to lock the cache while a parent process is holding the lock ()iX  z#Accessing the Emscripten cache at "z" (for "z") is taking a long time, another process should be writing to it. If there are none and you suspect this process has deadlocked, try deleting the lock file "zI" and try again. If this occurs deterministically, consider filing a bug.1doner   )r   FROZEN_CACHE	Exceptionr   cachedirr   exit_with_erroracquired_countloggerdebugr   getpidenviron	cachelockacquirer   Timeoutwarningcachelock_namereasons    r   acquire_cache_lockr*      s    I G
H
HH	X		 S	  Rh  R  Rek  R  R  R  S  S  Sq
LLh	hh^fhhiiirz111  4F  }C  4F  4F  4F111       nn  M8  M  MV  M  M  tB  M  M  M  N  N  N (+BJ#$
LLA...s   .C	 	AD"!D"c                  F   t           dz  a t           dk    s
J d            t           dk    rvt          j        d         dk    sJ t          j        d= t                                           t
                              dt          j                     dt                      d S d S )Nr   r   z&Called release more times than acquirer   r   r   z8 released multiprocess file lock to Emscripten cache at )	r   r   r"   r#   releaser   r    r!   r    r   r   release_cache_lockr.   6   s    A.	1			F			q:*+s2222

'(
LLg	gg]egghhhhh	 r   c              #   t   K   t          |            	 dV  t                       dS # t                       w xY w)z?A context manager that performs actions in the given directory.N)r*   r.   r(   s    r   lockr0   A   sI       V	EEEs   ' 7c                     t                       t          j                            t                    sT	 t          j        t                     d S # t          $ r+} t          j        dt           d|  d           Y d } ~ d S d } ~ ww xY wd S )Nz"unable to create cache directory "z": r   )	ensure_setupr   r   isdirr   r   safe_ensure_dirsr   r   )es    r   ensurer6   K   s    ...	x	 	  wwX&&&&& w w w  v  v  va  v  v  v  w  w  w  w  w  w  w  w  www ws   A 
B A??Bc                     t                       t          j        r
J d            t          d          5  t	          j        t          t          j        	                    t                    g           d d d            d S # 1 swxY w Y   d S )Nz/Cache cannot be erased when FROZEN_CACHE is seterase)exclude)r2   r   r   r0   r   delete_contentsr   r   r   basenamer'   r-   r   r   r8   r8   T   s    ... SS"SSS	 G}} P P	(RW-=-=n-M-M,NOOOOP P P P P P P P P P P P P P P P P Ps   ?B  BBc                 H    t                       t          t          |           S r   )r2   r   r   names    r   get_pathr?   ]   s    ...	h		r   c                 p    t                       | r%t          j                            t          d          S dS )Nsysroot)r2   r   r   joinr   absolutes    r   get_sysrootrE   b   s.    ... -7<<),,,	r   c                  6    t          t          dg| R            S )Ninclude)strget_sysroot_dirpartss    r   get_include_dirrL   i   s     	_Y////	0	00r   c                  R    t          t          t          d          g| R            S )NTrC   )rH   r   rE   rJ   s    r   rI   rI   m   s+    	T+t,,,5u555	6	66r   c                    t                       t          t          |           d          }t          j        rt          |d          }nt          |d          }g }t          j        r;t          j        dk    r|                    d           n|                    d           t          j        st          j        r|                    d           |r#t          |d		                    |                    }|S )
NrC   libzwasm64-emscriptenzwasm32-emscriptenthinthinltoltopic-)
r2   r   rE   r   MEMORY64LTOappendRELOCATABLEMAIN_MODULErB   )rD   r   subdirs      r   get_lib_dirr[   q   s    ...	k8,,,e	4	4$ +)**DD)**D&\ |vmmImmE X1 
MM% (chhv&&''D	+r   Fc                 b    t          t          |                              |                     S )NrC   )rH   r[   joinpath)r>   rD   s     r   get_lib_namer^      s)    	[(+++44T::	;	;;r   c                 >    t          t          |                      d S r   )
erase_filer^   r=   s    r   	erase_libra      s    \$     r   c                    t          d| z             5  t          t          |           }|                                r1t                              d|            t          j        |           d d d            d S # 1 swxY w Y   d S )Nzerase: zdeleting cached file: )r0   r   r   existsr   infor   delete_file)	shortnamer>   s     r   r`   r`      s    I	!""  )$$D{{}} kk1411222	                 s   AA;;A?A?c                 @    t          |           }t          |g|R i |S r   )r^   get)libnameargskwargsr>   s       r   get_librl      s-    	g		$	T	#D	#	#	#F	#	##r   c                     t                       t          t          |           }|                                r|st	          |          S t
          j        rt          d|  dt           d          t          |           5  |                                r|st	          |          cd d d            S || 	                    d          rd}nd}d| d|  d	| d
}t                              |           t          j        |j                    |t	          |                     t          j        d          s|                                sJ |st                              d           d d d            n# 1 swxY w Y   t	          |          S )Nz1FROZEN_CACHE is set, but cache file is missing: "z" (in cache root path "z"))z.bcz.soz.azsystem libraryzsystem assetzgenerating z: z... (this will be cached in "z" for subsequent builds)EMBUILDER_PORT_BUILD_DEFERREDz - ok)r2   r   r   rc   rH   r   r   r   r0   endswithr   rd   r   r4   parentr   getenvis_file)rf   creatorwhatforcequiet	cachenamemessages          r   rh   rh      s   ...8Y'')   y>> x v	vvjrvvv
w
wwI   % ^^        |			0	1	1 oDooIooIoooG
KK	9+,,,GC	NN
 9455 !      kk''              * 
Ys   ?%E61B9E66E:=E:c                      t          t          j                  at	                       t          t          d          at          j        t
                    ad S )Nz
cache.lock)	r   r   CACHEr   r6   r'   r   FileLockr#   r-   r   r   setupr|      sA     &,( 	(((,//.//)))r   c                  4    t           st                       d S d S r   )r   r|   r-   r   r   r2   r2      s!    	 	GGGGG r   )F)NFF)$__doc__
contextlibloggingr   pathlibr    r   r   r   r   	getLoggerr   r   r   r#   r'   r   r*   r.   contextmanagerr0   r6   r8   r?   rE   rL   rI   r[   r^   ra   r`   rl   rh   r|   r2   r-   r   r   <module>r      s         				       % % % % % % % % % %      		7	#	# 	" " "  0i i i   w w wP P P  
  1 1 17 7 7  *< < < <! ! !  $ $ $" " " "J	0 	0 	0    r   