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.
(build_dir: str, experimental: bool, opt_level: str)
| 75 | |
| 76 | |
| 77 | def _generate_setup_py(build_dir: str, experimental: bool, opt_level: str) -> str: |
| 78 | """Generate setup.py content for building librt directly. |
| 79 | |
| 80 | We inline LIBRT_MODULES/RUNTIME_C_FILES/include_dir/cflags values to avoid |
| 81 | importing mypyc.build, which recursively imports lots of things. |
| 82 | """ |
| 83 | lib_rt_dir = include_dir() |
| 84 | |
| 85 | # Get compiler flags using the shared helper |
| 86 | cflags = get_cflags(opt_level=opt_level, experimental_features=experimental) |
| 87 | |
| 88 | # Serialize values to inline in generated setup.py |
| 89 | librt_modules_repr = repr( |
| 90 | [(m.module, m.c_files, m.other_files, m.include_dirs) for m in LIBRT_MODULES] |
| 91 | ) |
| 92 | runtime_files_repr = repr(RUNTIME_C_FILES) |
| 93 | cflags_repr = repr(cflags) |
| 94 | |
| 95 | return f"""\ |
| 96 | import os |
| 97 | from setuptools import setup, Extension |
| 98 | import build_setup # noqa: F401 # Monkey-patches compiler for per-file SIMD flags |
| 99 | |
| 100 | build_dir = {build_dir!r} |
| 101 | lib_rt_dir = {lib_rt_dir!r} |
| 102 | |
| 103 | RUNTIME_C_FILES = {runtime_files_repr} |
| 104 | LIBRT_MODULES = {librt_modules_repr} |
| 105 | CFLAGS = {cflags_repr} |
| 106 | |
| 107 | def write_file(path, contents): |
| 108 | os.makedirs(os.path.dirname(path), exist_ok=True) |
| 109 | with open(path, "wb") as f: |
| 110 | f.write(contents) |
| 111 | |
| 112 | # Copy runtime C files |
| 113 | for name in RUNTIME_C_FILES: |
| 114 | src = os.path.join(lib_rt_dir, name) |
| 115 | dst = os.path.join(build_dir, name) |
| 116 | with open(src, "rb") as f: |
| 117 | write_file(dst, f.read()) |
| 118 | |
| 119 | # Build extensions for each librt module |
| 120 | extensions = [] |
| 121 | for mod, file_names, extra_files, includes in LIBRT_MODULES: |
| 122 | # Copy source files |
| 123 | for fname in file_names + extra_files: |
| 124 | src = os.path.join(lib_rt_dir, fname) |
| 125 | dst = os.path.join(build_dir, fname) |
| 126 | with open(src, "rb") as f: |
| 127 | write_file(dst, f.read()) |
| 128 | |
| 129 | extensions.append(Extension( |
| 130 | mod, |
| 131 | sources=[os.path.join(build_dir, f) for f in file_names + RUNTIME_C_FILES], |
| 132 | include_dirs=[lib_rt_dir] + [os.path.join(lib_rt_dir, d) for d in includes], |
| 133 | extra_compile_args=CFLAGS, |
| 134 | )) |
no test coverage detected
searching dependent graphs…