MCPcopy Index your code
hub / github.com/python/mypy / _generate_setup_py

Function _generate_setup_py

mypyc/test/librt_cache.py:77–137  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

75
76
77def _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"""\
96import os
97from setuptools import setup, Extension
98import build_setup # noqa: F401 # Monkey-patches compiler for per-file SIMD flags
99
100build_dir = {build_dir!r}
101lib_rt_dir = {lib_rt_dir!r}
102
103RUNTIME_C_FILES = {runtime_files_repr}
104LIBRT_MODULES = {librt_modules_repr}
105CFLAGS = {cflags_repr}
106
107def 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
113for 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
120extensions = []
121for 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 ))

Callers 1

get_librt_pathFunction · 0.85

Calls 3

include_dirFunction · 0.90
get_cflagsFunction · 0.90
reprFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…