Skip to content

Commit 8eaa206

Browse files
authored
gh-109515: When generating deep frozen modules on Windows, use a list file instead of arguments (GH-109516)
1 parent 6dfb8fe commit 8eaa206

File tree

3 files changed

+78
-30
lines changed

3 files changed

+78
-30
lines changed

PCbuild/_freeze_module.vcxproj

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,33 @@
395395
<OutFile>$(PySourcePath)Python\frozen_modules\getpath.h</OutFile>
396396
</GetPath>
397397
</ItemGroup>
398+
<ItemGroup>
399+
<!-- BEGIN freeze mappings -->
400+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\importlib._bootstrap.h" FrozenId="importlib._bootstrap" />
401+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\importlib._bootstrap_external.h" FrozenId="importlib._bootstrap_external" />
402+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\zipimport.h" FrozenId="zipimport" />
403+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\abc.h" FrozenId="abc" />
404+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\codecs.h" FrozenId="codecs" />
405+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\io.h" FrozenId="io" />
406+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\_collections_abc.h" FrozenId="_collections_abc" />
407+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\_sitebuiltins.h" FrozenId="_sitebuiltins" />
408+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\genericpath.h" FrozenId="genericpath" />
409+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\ntpath.h" FrozenId="ntpath" />
410+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\posixpath.h" FrozenId="posixpath" />
411+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\os.h" FrozenId="os" />
412+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\site.h" FrozenId="site" />
413+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\stat.h" FrozenId="stat" />
414+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\importlib.util.h" FrozenId="importlib.util" />
415+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\importlib.machinery.h" FrozenId="importlib.machinery" />
416+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\runpy.h" FrozenId="runpy" />
417+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\__hello__.h" FrozenId="__hello__" />
418+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\__phello__.h" FrozenId="__phello__" />
419+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\__phello__.ham.h" FrozenId="__phello__.ham" />
420+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\__phello__.ham.eggs.h" FrozenId="__phello__.ham.eggs" />
421+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\__phello__.spam.h" FrozenId="__phello__.spam" />
422+
<FrozenModule Include="$(PySourcePath)\Python\frozen_modules\frozen_only.h" FrozenId="frozen_only" />
423+
<!-- END freeze mappings -->
424+
</ItemGroup>
398425
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
399426
<ImportGroup Label="ExtensionTargets">
400427
</ImportGroup>
@@ -426,32 +453,19 @@
426453
AfterTargets="_RebuildFrozen"
427454
DependsOnTargets="FindPythonForBuild"
428455
Condition="$(Configuration) != 'PGUpdate'">
456+
<PropertyGroup>
457+
<FreezeMappingsFile>
458+
<!-- BEGIN freeze mapping file -->
459+
$(IntDir)\deepfreeze_mappings.txt
460+
<!-- END freeze mapping file -->
461+
</FreezeMappingsFile>
462+
</PropertyGroup>
463+
<WriteLinesToFile
464+
File="$(FreezeMappingsFile)"
465+
Overwrite="true"
466+
Lines="@(FrozenModule->'%(FullPath):%(FrozenId)')" />
429467
<!-- BEGIN deepfreeze rule -->
430-
<Exec Command='$(PythonForBuild) "$(PySourcePath)Tools\build\deepfreeze.py" ^
431-
"$(PySourcePath)Python\frozen_modules\importlib._bootstrap.h:importlib._bootstrap" ^
432-
"$(PySourcePath)Python\frozen_modules\importlib._bootstrap_external.h:importlib._bootstrap_external" ^
433-
"$(PySourcePath)Python\frozen_modules\zipimport.h:zipimport" ^
434-
"$(PySourcePath)Python\frozen_modules\abc.h:abc" ^
435-
"$(PySourcePath)Python\frozen_modules\codecs.h:codecs" ^
436-
"$(PySourcePath)Python\frozen_modules\io.h:io" ^
437-
"$(PySourcePath)Python\frozen_modules\_collections_abc.h:_collections_abc" ^
438-
"$(PySourcePath)Python\frozen_modules\_sitebuiltins.h:_sitebuiltins" ^
439-
"$(PySourcePath)Python\frozen_modules\genericpath.h:genericpath" ^
440-
"$(PySourcePath)Python\frozen_modules\ntpath.h:ntpath" ^
441-
"$(PySourcePath)Python\frozen_modules\posixpath.h:posixpath" ^
442-
"$(PySourcePath)Python\frozen_modules\os.h:os" ^
443-
"$(PySourcePath)Python\frozen_modules\site.h:site" ^
444-
"$(PySourcePath)Python\frozen_modules\stat.h:stat" ^
445-
"$(PySourcePath)Python\frozen_modules\importlib.util.h:importlib.util" ^
446-
"$(PySourcePath)Python\frozen_modules\importlib.machinery.h:importlib.machinery" ^
447-
"$(PySourcePath)Python\frozen_modules\runpy.h:runpy" ^
448-
"$(PySourcePath)Python\frozen_modules\__hello__.h:__hello__" ^
449-
"$(PySourcePath)Python\frozen_modules\__phello__.h:__phello__" ^
450-
"$(PySourcePath)Python\frozen_modules\__phello__.ham.h:__phello__.ham" ^
451-
"$(PySourcePath)Python\frozen_modules\__phello__.ham.eggs.h:__phello__.ham.eggs" ^
452-
"$(PySourcePath)Python\frozen_modules\__phello__.spam.h:__phello__.spam" ^
453-
"$(PySourcePath)Python\frozen_modules\frozen_only.h:frozen_only" ^
454-
"-o" "$(PySourcePath)Python\deepfreeze\deepfreeze.c"'/>
468+
<Exec Command='$(PythonForBuild) "$(PySourcePath)Tools\build\deepfreeze.py" -f "$(IntDir)\deepfreeze_mappings.txt" -o "$(PySourcePath)Python\deepfreeze\deepfreeze.c"' />
455469
<!-- END deepfreeze rule -->
456470
</Target>
457471
<Target Name="_CleanFrozen" BeforeTargets="CoreClean" Condition="$(Configuration) != 'PGUpdate'">

Tools/build/deepfreeze.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,10 @@ def generate(args: list[str], output: TextIO) -> None:
488488
parser = argparse.ArgumentParser()
489489
parser.add_argument("-o", "--output", help="Defaults to deepfreeze.c", default="deepfreeze.c")
490490
parser.add_argument("-v", "--verbose", action="store_true", help="Print diagnostics")
491-
parser.add_argument('args', nargs="+", help="Input file and module name (required) in file:modname format")
491+
group = parser.add_mutually_exclusive_group(required=True)
492+
group.add_argument("-f", "--file", help="read rule lines from a file")
493+
group.add_argument('args', nargs="*", default=(),
494+
help="Input file and module name (required) in file:modname format")
492495

493496
@contextlib.contextmanager
494497
def report_time(label: str):
@@ -506,9 +509,18 @@ def main() -> None:
506509
args = parser.parse_args()
507510
verbose = args.verbose
508511
output = args.output
512+
513+
if args.file:
514+
if verbose:
515+
print(f"Reading targets from {args.file}")
516+
with open(args.file, "rt", encoding="utf-8-sig") as fin:
517+
rules = [x.strip() for x in fin]
518+
else:
519+
rules = args.args
520+
509521
with open(output, "w", encoding="utf-8") as file:
510522
with report_time("generate"):
511-
generate(args.args, file)
523+
generate(rules, file)
512524
if verbose:
513525
print(f"Wrote {os.path.getsize(output)} bytes to {output}")
514526

Tools/build/freeze_modules.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# .gitattributes and .gitignore files needs to be updated.
2222
FROZEN_MODULES_DIR = os.path.join(ROOT_DIR, 'Python', 'frozen_modules')
2323
DEEPFROZEN_MODULES_DIR = os.path.join(ROOT_DIR, 'Python', 'deepfreeze')
24+
DEEPFREEZE_MAPPING_FNAME = 'deepfreeze_mappings.txt'
2425

2526
FROZEN_FILE = os.path.join(ROOT_DIR, 'Python', 'frozen.c')
2627
MAKEFILE = os.path.join(ROOT_DIR, 'Makefile.pre.in')
@@ -645,7 +646,9 @@ def regen_pcbuild(modules):
645646
projlines = []
646647
filterlines = []
647648
corelines = []
648-
deepfreezerules = ['\t<Exec Command=\'$(PythonForBuild) "$(PySourcePath)Tools\\build\\deepfreeze.py" ^']
649+
deepfreezemappingsfile = f'$(IntDir)\\{DEEPFREEZE_MAPPING_FNAME}'
650+
deepfreezerules = [f' <Exec Command=\'$(PythonForBuild) "$(PySourcePath)Tools\\build\\deepfreeze.py" -f "{deepfreezemappingsfile}" -o "$(PySourcePath)Python\\deepfreeze\\deepfreeze.c"\' />']
651+
deepfreezemappings = []
649652
for src in _iter_sources(modules):
650653
pyfile = relpath_for_windows_display(src.pyfile, ROOT_DIR)
651654
header = relpath_for_windows_display(src.frozenfile, ROOT_DIR)
@@ -659,8 +662,7 @@ def regen_pcbuild(modules):
659662
filterlines.append(f' <None Include="..\\{pyfile}">')
660663
filterlines.append(' <Filter>Python Files</Filter>')
661664
filterlines.append(' </None>')
662-
deepfreezerules.append(f'\t\t "$(PySourcePath){header}:{src.frozenid}" ^')
663-
deepfreezerules.append('\t\t "-o" "$(PySourcePath)Python\\deepfreeze\\deepfreeze.c"\'/>' )
665+
deepfreezemappings.append(f' <FrozenModule Include="$(PySourcePath)\\{header}" FrozenId="{src.frozenid}" />\n')
664666

665667
corelines.append(f' <ClCompile Include="..\\Python\\deepfreeze\\deepfreeze.c" />')
666668

@@ -675,6 +677,26 @@ def regen_pcbuild(modules):
675677
PCBUILD_PROJECT,
676678
)
677679
outfile.writelines(lines)
680+
with updating_file_with_tmpfile(PCBUILD_PROJECT) as (infile, outfile):
681+
lines = infile.readlines()
682+
lines = replace_block(
683+
lines,
684+
'<!-- BEGIN freeze mappings -->',
685+
'<!-- END freeze mappings -->',
686+
deepfreezemappings,
687+
PCBUILD_PROJECT,
688+
)
689+
outfile.writelines(lines)
690+
with updating_file_with_tmpfile(PCBUILD_PROJECT) as (infile, outfile):
691+
lines = infile.readlines()
692+
lines = replace_block(
693+
lines,
694+
'<!-- BEGIN freeze mapping file -->',
695+
'<!-- END freeze mapping file -->',
696+
[deepfreezemappingsfile, ],
697+
PCBUILD_PROJECT,
698+
)
699+
outfile.writelines(lines)
678700
with updating_file_with_tmpfile(PCBUILD_PROJECT) as (infile, outfile):
679701
lines = infile.readlines()
680702
lines = replace_block(

0 commit comments

Comments
 (0)