MCPcopy Create free account
hub / github.com/python/mypy / mypyc_build

Function mypyc_build

mypyc/build.py:585–657  ·  view source on GitHub ↗

Do the front and middle end of mypyc building, producing and writing out C source.

(
    paths: list[str],
    compiler_options: CompilerOptions,
    *,
    separate: bool | list[tuple[list[str], str | None]] = False,
    only_compile_paths: Iterable[str] | None = None,
    skip_cgen_input: (
        tuple[list[list[tuple[str, str]]], list[tuple[str, list[str], bool]]] | None
    ) = None,
    always_use_shared_lib: bool = False,
)

Source from the content-addressed store, hash-verified

583
584
585def mypyc_build(
586 paths: list[str],
587 compiler_options: CompilerOptions,
588 *,
589 separate: bool | list[tuple[list[str], str | None]] = False,
590 only_compile_paths: Iterable[str] | None = None,
591 skip_cgen_input: (
592 tuple[list[list[tuple[str, str]]], list[tuple[str, list[str], bool]]] | None
593 ) = None,
594 always_use_shared_lib: bool = False,
595) -> tuple[emitmodule.Groups, list[tuple[list[str], list[str]]], list[SourceDep]]:
596 """Do the front and middle end of mypyc building, producing and writing out C source."""
597 fscache = FileSystemCache()
598 mypyc_sources, all_sources, options = get_mypy_config(
599 paths, only_compile_paths, compiler_options, fscache
600 )
601
602 # We generate a shared lib if there are multiple modules or if any
603 # of the modules are in package. (Because I didn't want to fuss
604 # around with making the single module code handle packages.)
605 use_shared_lib = (
606 len(mypyc_sources) > 1
607 or any("." in x.module for x in mypyc_sources)
608 or any(is_package_source(x) for x in mypyc_sources)
609 or always_use_shared_lib
610 )
611
612 groups = construct_groups(mypyc_sources, separate, use_shared_lib, compiler_options.group_name)
613
614 if compiler_options.group_name is not None:
615 assert len(groups) == 1, "If using custom group_name, only one group is expected"
616
617 # We let the test harness just pass in the c file contents instead
618 # so that it can do a corner-cutting version without full stubs.
619 source_deps: list[SourceDep] = []
620 if not skip_cgen_input:
621 group_cfiles, ops_text, source_deps = generate_c(
622 all_sources, options, groups, fscache, compiler_options=compiler_options
623 )
624 # TODO: unique names?
625 write_file(os.path.join(compiler_options.target_dir, "ops.txt"), ops_text)
626 else:
627 group_cfiles = skip_cgen_input[0]
628 source_deps = [
629 SourceDep(path, include_dirs=dirs, internal=internal)
630 for (path, dirs, internal) in skip_cgen_input[1]
631 ]
632
633 # Write out the generated C and collect the files for each group
634 # Should this be here??
635 group_cfilenames: list[tuple[list[str], list[str]]] = []
636 for cfiles in group_cfiles:
637 cfilenames = []
638 for cfile, ctext in cfiles:
639 cfile = os.path.join(compiler_options.target_dir, cfile)
640 # Empty contents marks a file the previous run already wrote
641 # (fully-cached group): skip the rewrite and just reuse it.
642 if ctext and not options.mypyc_skip_c_generation:

Callers 1

mypycifyFunction · 0.85

Calls 13

FileSystemCacheClass · 0.90
SourceDepClass · 0.90
get_mypy_configFunction · 0.85
lenFunction · 0.85
anyFunction · 0.85
is_package_sourceFunction · 0.85
construct_groupsFunction · 0.85
generate_cFunction · 0.85
write_fileFunction · 0.85
get_header_depsFunction · 0.85
appendMethod · 0.80
existsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…