3333from mypyc .common import RUNTIME_C_FILES
3434
3535
36- def _librt_build_hash (experimental : bool ) -> str :
36+ def _librt_build_hash (experimental : bool , opt_level : str ) -> str :
3737 """Compute hash for librt build, including sources and build environment."""
3838 # Import lazily to ensure mypyc.build has ensured that distutils is correctly set up
3939 from distutils import ccompiler
4040
4141 h = hashlib .sha256 ()
4242 # Include experimental flag
4343 h .update (b"exp" if experimental else b"noexp" )
44+ h .update (f"opt={ opt_level } " .encode ())
4445 # Include full Python version string (includes git hash for dev builds)
4546 h .update (sys .version .encode ())
4647 # Include debug build status (gettotalrefcount only exists in debug builds)
@@ -72,16 +73,16 @@ def _librt_build_hash(experimental: bool) -> str:
7273 return h .hexdigest ()[:16 ]
7374
7475
75- def _generate_setup_py (build_dir : str , experimental : bool ) -> str :
76+ def _generate_setup_py (build_dir : str , experimental : bool , opt_level : str ) -> str :
7677 """Generate setup.py content for building librt directly.
7778
7879 We inline LIBRT_MODULES/RUNTIME_C_FILES/include_dir/cflags values to avoid
7980 importing mypyc.build, which recursively imports lots of things.
8081 """
8182 lib_rt_dir = include_dir ()
8283
83- # Get compiler flags using the shared helper (with -O0 for faster builds)
84- cflags = get_cflags (opt_level = "0" , experimental_features = experimental )
84+ # Get compiler flags using the shared helper
85+ cflags = get_cflags (opt_level = opt_level , experimental_features = experimental )
8586
8687 # Serialize values to inline in generated setup.py
8788 librt_modules_repr = repr (
@@ -135,7 +136,7 @@ def write_file(path, contents):
135136"""
136137
137138
138- def get_librt_path (experimental : bool = True ) -> str :
139+ def get_librt_path (experimental : bool = True , opt_level : str = "0" ) -> str :
139140 """Get path to librt built from the repository, building and caching if necessary.
140141
141142 Uses build/librt-cache/ under the repo root (gitignored). The cache is
@@ -146,14 +147,15 @@ def get_librt_path(experimental: bool = True) -> str:
146147
147148 Args:
148149 experimental: Whether to enable experimental features.
150+ opt_level: Optimization level ("0".."3") used when building librt.
149151
150152 Returns:
151153 Path to directory containing built librt modules.
152154 """
153155 # Use build/librt-cache/ under the repo root (gitignored)
154156 repo_root = os .path .dirname (os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))
155157 cache_root = os .path .join (repo_root , "build" , "librt-cache" )
156- build_hash = _librt_build_hash (experimental )
158+ build_hash = _librt_build_hash (experimental , opt_level )
157159 build_dir = os .path .join (cache_root , f"librt-{ build_hash } " )
158160 lock_file = os .path .join (cache_root , f"librt-{ build_hash } .lock" )
159161 marker = os .path .join (build_dir , ".complete" )
@@ -186,7 +188,7 @@ def get_librt_path(experimental: bool = True) -> str:
186188 # Write setup.py
187189 setup_py = os .path .join (build_dir , "setup.py" )
188190 with open (setup_py , "w" ) as f :
189- f .write (_generate_setup_py (build_dir , experimental ))
191+ f .write (_generate_setup_py (build_dir , experimental , opt_level ))
190192
191193 # Build (parallel builds don't work well because multiple extensions
192194 # share the same runtime C files, causing race conditions)
@@ -207,7 +209,7 @@ def get_librt_path(experimental: bool = True) -> str:
207209
208210
209211def run_with_librt (
210- file_path : str , experimental : bool = True , check : bool = True
212+ file_path : str , experimental : bool = True , check : bool = True , opt_level : str = "0"
211213) -> subprocess .CompletedProcess [str ]:
212214 """Run a Python file in a subprocess with built librt available.
213215
@@ -218,11 +220,12 @@ def run_with_librt(
218220 file_path: Path to Python file to execute.
219221 experimental: Whether to use experimental features.
220222 check: If True, raise CalledProcessError on non-zero exit.
223+ opt_level: Optimization level ("0".."3") used when building librt.
221224
222225 Returns:
223226 CompletedProcess with stdout, stderr, and returncode.
224227 """
225- librt_path = get_librt_path (experimental )
228+ librt_path = get_librt_path (experimental , opt_level = opt_level )
226229 # Prepend librt path to PYTHONPATH
227230 env = os .environ .copy ()
228231 existing = env .get ("PYTHONPATH" , "" )
0 commit comments