diff --git a/mypyc/build.py b/mypyc/build.py index 3bc38cb4dd90..ab7ba5393614 100644 --- a/mypyc/build.py +++ b/mypyc/build.py @@ -357,6 +357,7 @@ def construct_groups( sources: list[BuildSource], separate: bool | list[tuple[list[str], str | None]], use_shared_lib: bool, + group_name_override: str | None, ) -> emitmodule.Groups: """Compute Groups given the input source list and separate configs. @@ -386,7 +387,10 @@ def construct_groups( # Generate missing names for i, (group, name) in enumerate(groups): if use_shared_lib and not name: - name = group_name([source.module for source in group]) + if group_name_override is not None: + name = group_name_override + else: + name = group_name([source.module for source in group]) groups[i] = (group, name) return groups @@ -432,7 +436,10 @@ def mypyc_build( or always_use_shared_lib ) - groups = construct_groups(mypyc_sources, separate, use_shared_lib) + groups = construct_groups(mypyc_sources, separate, use_shared_lib, compiler_options.group_name) + + if compiler_options.group_name is not None: + assert len(groups) == 1, "If using custom group_name, only one group is expected" # We let the test harness just pass in the c file contents instead # so that it can do a corner-cutting version without full stubs. @@ -477,6 +484,7 @@ def mypycify( target_dir: str | None = None, include_runtime_files: bool | None = None, strict_dunder_typing: bool = False, + group_name: str | None = None, ) -> list[Extension]: """Main entry point to building using mypyc. @@ -519,6 +527,10 @@ def mypycify( strict_dunder_typing: If True, force dunder methods to have the return type of the method strictly, which can lead to more optimization opportunities. Defaults to False. + group_name: If set, override the default group name derived from + the hash of module names. This is used for the names of the + output C files and the shared library. This is only supported + if there is a single group. [Experimental] """ # Figure out our configuration @@ -530,6 +542,7 @@ def mypycify( target_dir=target_dir, include_runtime_files=include_runtime_files, strict_dunder_typing=strict_dunder_typing, + group_name=group_name, ) # Generate all the actual important C code diff --git a/mypyc/options.py b/mypyc/options.py index 24e68163bb11..51114926f6b2 100644 --- a/mypyc/options.py +++ b/mypyc/options.py @@ -15,6 +15,7 @@ def __init__( capi_version: tuple[int, int] | None = None, python_version: tuple[int, int] | None = None, strict_dunder_typing: bool = False, + group_name: str | None = None, ) -> None: self.strip_asserts = strip_asserts self.multi_file = multi_file @@ -38,3 +39,9 @@ def __init__( # will assume the return type of the method strictly, which can lead to # more optimization opportunities. self.strict_dunders_typing = strict_dunder_typing + # Override the automatic group name derived from the hash of module names. + # This affects the names of generated .c, .h and shared library files. + # This is only supported when compiling exactly one group, and a shared + # library is generated (with shims). This can be used to make the output + # file names more predictable. + self.group_name = group_name diff --git a/mypyc/test/test_run.py b/mypyc/test/test_run.py index e5b7e2421433..b96c4241f30d 100644 --- a/mypyc/test/test_run.py +++ b/mypyc/test/test_run.py @@ -235,7 +235,7 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) -> else False ) - groups = construct_groups(sources, separate, len(module_names) > 1) + groups = construct_groups(sources, separate, len(module_names) > 1, None) try: compiler_options = CompilerOptions(