Ë
    pPêj#  ã                  ó¼   — d Z 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	m
Z
 ddlZddlmZmZmZ ddlmZ ddlmZ dd„Zdd	„Zddd
„Z	 d	 	 	 	 	 	 	 	 	 dd„Zy)a¦  Build and cache librt for use in tests.

This module provides a way to build librt extension modules once and cache
them across test runs, and across different test cases in a single run. The
cache is invalidated when source files or details of the build environment change.

Note: Tests must run in a subprocess to use the cached librt, since importing
this module also triggers the import of the regular installed librt.

Usage:
    from mypyc.test.librt_cache import get_librt_path, run_with_librt

    # Get path to built librt (builds if needed)
    path = get_librt_path()

    # Run a test file in subprocess with built librt
    result = run_with_librt("test_librt.py")
é    )ÚannotationsN)ÚAny)ÚLIBRT_MODULESÚ
get_cflagsÚinclude_dir)ÚRUNTIME_C_FILES)ÚPREFIXc                óP  — ddl m} t        j                  «       }|j	                  | rdnd«       |j	                  d|› �j                  «       «       |j	                  t        j                  j                  «       «       t        t        d«      }|j	                  |rdnd«       t        t        j                  d	«      «      }|j	                  |rd
nd«       |j                  «       }|j	                  |j                  j                  «       «       dD ]F  }t        j                  j!                  |d«      }|j	                  |› d|› �j                  «       «       ŒH t"        D ]€  }	t        j$                  j'                  t)        «       |	«      }
|j	                  |	j                  «       dz   «       t+        |
d«      5 }|j	                  |j-                  «       «       ddd«       Œ‚ t.        D ]�  \  }}}}||z   D ]€  }t        j$                  j'                  t)        «       |«      }
|j	                  |j                  «       dz   «       t+        |
d«      5 }|j	                  |j-                  «       «       ddd«       Œ‚ Œ‘ |j1                  «       dd S # 1 sw Y   �Œ8xY w# 1 sw Y   Œ®xY w)zFCompute hash for librt build, including sources and build environment.r   )Ú	ccompilers   exps   noexpzopt=Úgettotalrefcounts   debugs   releaseÚPy_GIL_DISABLEDs   freethreadeds   gil)ÚCCÚCXXÚCFLAGSÚCPPFLAGSÚLDFLAGSÚ ú=ó   |ÚrbNé   )Ú	distutilsr   ÚhashlibÚsha256ÚupdateÚencodeÚsysÚversionÚhasattrÚboolÚ	sysconfigÚget_config_varÚnew_compilerÚcompiler_typeÚosÚenvironÚgetr   ÚpathÚjoinr   ÚopenÚreadr   Ú	hexdigest)ÚexperimentalÚ	opt_levelr   ÚhÚis_debugÚis_free_threadedÚcompilerÚvarÚvalÚnamer(   ÚfÚmodÚfilesÚextraÚincludesÚfnames                    ú[/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/mypyc/test/librt_cache.pyÚ_librt_build_hashr=   %   s%  € õ $ä�‰Ó€Aà‡H�H‘|‰V¨Ô2Ø‡H�Hˆt�I�;Ð×&Ñ&Ó(Ô)à‡H�HŒS�[‰[×ÑÓ!Ô"ä”sÐ.Ó/€HØ‡H�H™‰X zÔ2äœI×4Ñ4Ð5FÓGÓHÐØ‡H�HÑ 0‰_°fÔ=à×*Ñ*Ó,€HØ‡H�HˆX×#Ñ#×*Ñ*Ó,Ô-à=ò *ˆÜ�j‰j�n‰n˜S "Ó%ˆØ	�‰�C�5˜˜#˜�×&Ñ&Ó(Õ)ð*ô  ò ˆÜ�w‰w�|‰|œK›M¨4Ó0ˆØ	�‰�—‘“ Ñ%Ô&Ü�$˜Óð 	 Ø�H‰H�Q—V‘V“XÔ÷	ð 	ðô (5ò #Ñ#ˆˆU�E˜8Ø˜U‘]ò 	#ˆEÜ—7‘7—<‘<¤£¨uÓ5ˆDØ�H‰H�U—\‘\“^ dÑ*Ô+Ü�d˜DÓ!ð # QØ—‘˜Ÿ™›Ô"÷#ð #ñ	#ð#ð �;‰;‹=˜˜"ÐÐ÷	ñ 	ú÷#ð #ús   Æ: JÉ JÊJ	ÊJ%c                ó$  — t        «       }t        ||¬«      }t        t        D �cg c]0  }|j                  |j
                  |j                  |j                  f‘Œ2 c}«      }t        t        «      }t        |«      }d| ›d|›d|› d|› d|› d�S c c}w )zÐGenerate setup.py content for building librt directly.

    We inline LIBRT_MODULES/RUNTIME_C_FILES/include_dir/cflags values to avoid
    importing mypyc.build, which recursively imports lots of things.
    )r.   Úexperimental_featuresz“import os
from setuptools import setup, Extension
import build_setup  # noqa: F401  # Monkey-patches compiler for per-file SIMD flags

build_dir = z
lib_rt_dir = z

RUNTIME_C_FILES = z
LIBRT_MODULES = z

CFLAGS = aí  

def write_file(path, contents):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "wb") as f:
        f.write(contents)

# Copy runtime C files
for name in RUNTIME_C_FILES:
    src = os.path.join(lib_rt_dir, name)
    dst = os.path.join(build_dir, name)
    with open(src, "rb") as f:
        write_file(dst, f.read())

# Build extensions for each librt module
extensions = []
for mod, file_names, extra_files, includes in LIBRT_MODULES:
    # Copy source files
    for fname in file_names + extra_files:
        src = os.path.join(lib_rt_dir, fname)
        dst = os.path.join(build_dir, fname)
        with open(src, "rb") as f:
            write_file(dst, f.read())

    extensions.append(Extension(
        mod,
        sources=[os.path.join(build_dir, f) for f in file_names + RUNTIME_C_FILES],
        include_dirs=[lib_rt_dir] + [os.path.join(lib_rt_dir, d) for d in includes],
        extra_compile_args=CFLAGS,
    ))

setup(name='librt_cached', ext_modules=extensions)
)	r   r   Úreprr   ÚmoduleÚc_filesÚother_filesÚinclude_dirsr   )	Ú	build_dirr-   r.   Ú
lib_rt_dirÚcflagsÚmÚlibrt_modules_reprÚruntime_files_reprÚcflags_reprs	            r<   Ú_generate_setup_pyrL   M   s®   € ô “€Jô  )À<ÔP€Fô ÜGTÖUÀ!ˆ!�(‰(�A—I‘I˜qŸ}™}¨a¯n©nÒ	=ÒUóÐô œoÓ.ÐÜ�v“,€Kðð
 ˆMð Øˆ^ð à%Ð&ð 'Ø#Ð$ð %
Ø
ˆð  ð*ð *ùò 	Vs   ¥5Bc           
     óˆ  ‡— t         j                  j                  t        dd«      }t	        | |«      }t         j                  j                  |d|› �«      }t         j                  j                  |d|› d�«      }t         j                  j                  |d«      }t        j
                  |d¬«       t        j                  dk(  rd	nd
Št        j                  |d¬«      5  t         j                  j                  |«      rPt        ˆfd„t        j                  t         j                  j                  |d«      «      D «       «      r|cddd«       S t         j                  j                  |«      rt        j                  |«       t        j
                  |«       t         j                  j                  |d«      }t        j
                  |«       t        t         j                  j                  |d«      d«      5 }	 ddd«       t         j                  j                  t         j                  j!                  t         j                  j!                  t         j                  j#                  t$        «      «      «      d«      }	t         j                  j                  |d«      }
t        j&                  |	|
«       t         j                  j                  |d«      }t        |d«      5 }|j)                  t+        || |«      «       ddd«       t-        j.                  t        j0                  |ddg|dd¬«      }|j2                  dk7  r%t5        d|j6                  › d|j8                  › �«      ‚t        |d«      5 }|j)                  d«       ddd«       ddd«       |S # 1 sw Y   �Œ›xY w# 1 sw Y   Œ¥xY w# 1 sw Y   Œ,xY w# 1 sw Y   |S xY w)a9  Get path to librt built from the repository, building and caching if necessary.

    Uses build/librt-cache/ under the repo root (gitignored). The cache is
    keyed by a hash of sources and build environment, so it auto-invalidates
    when relevant factors change.

    Safe to call from multiple parallel pytest workers - uses file locking.

    Args:
        experimental: Whether to enable experimental features.
        opt_level: Optimization level ("0".."3") used when building librt.

    Returns:
        Path to directory containing built librt modules.
    Úbuildzlibrt-cachezlibrt-z.lockz	.completeT)Úexist_okÚwin32z.pydz.soi,  )Útimeoutc              3  ó@   •K  — | ]  }|j                  ‰«      –— Œ y ­w)N)Úendswith)Ú.0r6   Úbinary_suffixs     €r<   ú	<genexpr>z!get_librt_path.<locals>.<genexpr>«   s   øè ø€ ò *
Ø*+ˆA�J‰J�}×%ñ*
ùs   ƒÚlibrtNz__init__.pyÚwzbuild_setup.pyzsetup.pyÚ	build_extz	--inplace)ÚcwdÚcapture_outputÚtextr   zlibrt build failed:
ú
Úok)r%   r(   r)   r	   r=   Úmakedirsr   ÚplatformÚfilelockÚFileLockÚexistsÚanyÚlistdirÚshutilÚrmtreer*   ÚdirnameÚabspathÚ__file__ÚcopyÚwriterL   Ú
subprocessÚrunÚ
executableÚ
returncodeÚRuntimeErrorÚstdoutÚstderr)r-   r.   Ú
cache_rootÚ
build_hashrE   Ú	lock_fileÚmarkerÚ	librt_pkgr6   Úbuild_setup_srcÚbuild_setup_dstÚsetup_pyÚresultrU   s                @r<   Úget_librt_pathr}   Œ   sÓ  ø€ ô" —‘—‘œf g¨}Ó=€JÜ" <°Ó;€JÜ—‘—‘˜Z¨6°*°Ð)>Ó?€IÜ—‘—‘˜Z¨6°*°¸UÐ)CÓD€IÜ�W‰W�\‰\˜) [Ó1€Fä‡K�K�
 TÕ*ä!Ÿl™l¨gÒ5‘F¸5€Mä	×	Ñ	˜9¨cÔ	2ñ .ô �7‰7�>‰>˜&Ô!¤có *
Ü/1¯z©z¼"¿'¹'¿,¹,ÀyÐRYÓ:ZÓ/[ô*
ô '
ð ÷.ñ .ô �7‰7�>‰>˜)Ô$Ü�M‰M˜)Ô$ä
�‰�IÔô —G‘G—L‘L ¨GÓ4ˆ	Ü
�‰�IÔÜ”"—'‘'—,‘,˜y¨-Ó8¸#Ó>ð 	À!Ø÷	ô Ÿ'™'Ÿ,™,Ü�G‰G�O‰OœBŸG™GŸO™O¬B¯G©G¯O©O¼HÓ,EÓFÓGÐIYó
ˆô Ÿ'™'Ÿ,™, yÐ2BÓCˆÜ�‰�O _Ô5ô —7‘7—<‘< 	¨:Ó6ˆÜ�(˜CÓ ð 	L AØ�G‰GÔ& y°,À	ÓJÔK÷	Lô
 —‘Ü�^‰^˜X {°KÐ@ØØØô	
ˆð ×Ñ Ò!ÜÐ!6°v·}±}°oÀRÈÏÉÀÐWÓXÐXô �&˜#Óð 	 !Ø�G‰G�DŒM÷	÷[.ð` Ð÷=	ñ 	ú÷	Lð 	Lú÷	ð 	ú÷[.ð` Ðús^   ÃA&N7ÅB(N7Ç7NÇ9C%N7ËNË;A3N7Í.N+Î N7ÎN	ÎN7ÎN(	Î$N7Î+N4	Î0N7Î7Oc                ó
  — t        ||¬«      }t        j                  j                  «       }|j	                  dd«      }||rt        j
                  |z   ndz   |d<   t        j                  t        j                  | gdd||¬«      S )a)  Run a Python file in a subprocess with built librt available.

    This runs the file in a fresh Python process where the built librt
    is at the front of sys.path, avoiding conflicts with any system librt.

    Args:
        file_path: Path to Python file to execute.
        experimental: Whether to use experimental features.
        check: If True, raise CalledProcessError on non-zero exit.
        opt_level: Optimization level ("0".."3") used when building librt.

    Returns:
        CompletedProcess with stdout, stderr, and returncode.
    )r.   Ú
PYTHONPATHr   T)r[   r\   ÚcheckÚenv)
r}   r%   r&   rk   r'   Úpathseprm   rn   r   ro   )Ú	file_pathr-   r€   r.   Ú
librt_pathr�   Úexistings          r<   Úrun_with_librtr†   Ú   sr   € ô"   ¸	ÔB€Jä
�*‰*�/‰/Ó
€CØ�w‰w�| RÓ(€HØ"¹x¤b§j¡j°8Ò&;ÈRÑP€CˆÑä�>‰>Ü	�‰˜Ð#°D¸tÈ5ÐVYôð ó    )r-   r    r.   ÚstrÚreturnrˆ   )rE   rˆ   r-   r    r.   rˆ   r‰   rˆ   )TÚ0)TTrŠ   )
rƒ   rˆ   r-   r    r€   r    r.   rˆ   r‰   z subprocess.CompletedProcess[str])Ú__doc__Ú
__future__r   r   r%   rf   rm   r   r!   Útypingr   ra   Úmypyc.buildr   r   r   Úmypyc.commonr   Úmypyc.test.configr	   r=   rL   r}   r†   © r‡   r<   ú<module>r’      sz   ðñõ& #ã Û 	Û Û Û 
Û Ý ã ç >Ñ >Ý (Ý $ó%óP<ô~Kð^ UXðØðØ"&ðØ6:ðØNQðà%ôr‡   