Skip to content

Commit 75f7a25

Browse files
authored
[mypyc] Support overriding the group name used in output files (#19272)
By default, when compiling more than one file, a shared library is generated with a file name derived from the sha of compiled modules (i.e. the group name). This is also used in the names of generated .c and .h files. Add experimental `group_name` argument to `mypycify` that allows overriding this. This can be useful when integrating mypyc to a build system, as this makes the names of output files more predictable.
1 parent f209888 commit 75f7a25

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

mypyc/build.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ def construct_groups(
357357
sources: list[BuildSource],
358358
separate: bool | list[tuple[list[str], str | None]],
359359
use_shared_lib: bool,
360+
group_name_override: str | None,
360361
) -> emitmodule.Groups:
361362
"""Compute Groups given the input source list and separate configs.
362363
@@ -386,7 +387,10 @@ def construct_groups(
386387
# Generate missing names
387388
for i, (group, name) in enumerate(groups):
388389
if use_shared_lib and not name:
389-
name = group_name([source.module for source in group])
390+
if group_name_override is not None:
391+
name = group_name_override
392+
else:
393+
name = group_name([source.module for source in group])
390394
groups[i] = (group, name)
391395

392396
return groups
@@ -432,7 +436,10 @@ def mypyc_build(
432436
or always_use_shared_lib
433437
)
434438

435-
groups = construct_groups(mypyc_sources, separate, use_shared_lib)
439+
groups = construct_groups(mypyc_sources, separate, use_shared_lib, compiler_options.group_name)
440+
441+
if compiler_options.group_name is not None:
442+
assert len(groups) == 1, "If using custom group_name, only one group is expected"
436443

437444
# We let the test harness just pass in the c file contents instead
438445
# so that it can do a corner-cutting version without full stubs.
@@ -477,6 +484,7 @@ def mypycify(
477484
target_dir: str | None = None,
478485
include_runtime_files: bool | None = None,
479486
strict_dunder_typing: bool = False,
487+
group_name: str | None = None,
480488
) -> list[Extension]:
481489
"""Main entry point to building using mypyc.
482490
@@ -519,6 +527,10 @@ def mypycify(
519527
strict_dunder_typing: If True, force dunder methods to have the return type
520528
of the method strictly, which can lead to more
521529
optimization opportunities. Defaults to False.
530+
group_name: If set, override the default group name derived from
531+
the hash of module names. This is used for the names of the
532+
output C files and the shared library. This is only supported
533+
if there is a single group. [Experimental]
522534
"""
523535

524536
# Figure out our configuration
@@ -530,6 +542,7 @@ def mypycify(
530542
target_dir=target_dir,
531543
include_runtime_files=include_runtime_files,
532544
strict_dunder_typing=strict_dunder_typing,
545+
group_name=group_name,
533546
)
534547

535548
# Generate all the actual important C code

mypyc/options.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(
1515
capi_version: tuple[int, int] | None = None,
1616
python_version: tuple[int, int] | None = None,
1717
strict_dunder_typing: bool = False,
18+
group_name: str | None = None,
1819
) -> None:
1920
self.strip_asserts = strip_asserts
2021
self.multi_file = multi_file
@@ -38,3 +39,9 @@ def __init__(
3839
# will assume the return type of the method strictly, which can lead to
3940
# more optimization opportunities.
4041
self.strict_dunders_typing = strict_dunder_typing
42+
# Override the automatic group name derived from the hash of module names.
43+
# This affects the names of generated .c, .h and shared library files.
44+
# This is only supported when compiling exactly one group, and a shared
45+
# library is generated (with shims). This can be used to make the output
46+
# file names more predictable.
47+
self.group_name = group_name

mypyc/test/test_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
235235
else False
236236
)
237237

238-
groups = construct_groups(sources, separate, len(module_names) > 1)
238+
groups = construct_groups(sources, separate, len(module_names) > 1, None)
239239

240240
try:
241241
compiler_options = CompilerOptions(

0 commit comments

Comments
 (0)