Skip to content

Commit b091bec

Browse files
pitrouvstinner
authored andcommitted
bpo-31536: Avoid wholesale rebuild after make regen-all (#3678)
* bpo-31536: Avoid wholesale rebuild after `make regen-all` * Add NEWS
1 parent aaf6fc0 commit b091bec

File tree

4 files changed

+86
-51
lines changed

4 files changed

+86
-51
lines changed

‎Makefile.pre.in‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ PYTHON= python$(EXE)
230230
BUILDPYTHON= python$(BUILDEXE)
231231

232232
PYTHON_FOR_REGEN=@PYTHON_FOR_REGEN@
233+
UPDATE_FILE=@PYTHON_FOR_REGEN@ $(srcdir)/Tools/scripts/update_file.py
233234
PYTHON_FOR_BUILD=@PYTHON_FOR_BUILD@
234235
_PYTHON_HOST_PLATFORM=@_PYTHON_HOST_PLATFORM@
235236
BUILD_GNU_TYPE= @build@
@@ -691,12 +692,14 @@ regen-importlib: Programs/_freeze_importlib
691692
# from Lib/importlib/_bootstrap_external.py using _freeze_importlib
692693
./Programs/_freeze_importlib \
693694
$(srcdir)/Lib/importlib/_bootstrap_external.py \
694-
$(srcdir)/Python/importlib_external.h
695+
$(srcdir)/Python/importlib_external.h.new
696+
$(UPDATE_FILE) $(srcdir)/Python/importlib_external.h $(srcdir)/Python/importlib_external.h.new
695697
# Regenerate Python/importlib.h from Lib/importlib/_bootstrap.py
696698
# using _freeze_importlib
697699
./Programs/_freeze_importlib \
698700
$(srcdir)/Lib/importlib/_bootstrap.py \
699-
$(srcdir)/Python/importlib.h
701+
$(srcdir)/Python/importlib.h.new
702+
$(UPDATE_FILE) $(srcdir)/Python/importlib.h $(srcdir)/Python/importlib.h.new
700703

701704

702705
############################################################################
@@ -770,8 +773,10 @@ regen-grammar: $(PGEN)
770773
# from Grammar/Grammar using pgen
771774
@$(MKDIR_P) Include
772775
$(PGEN) $(srcdir)/Grammar/Grammar \
773-
$(srcdir)/Include/graminit.h \
774-
$(srcdir)/Python/graminit.c
776+
$(srcdir)/Include/graminit.h.new \
777+
$(srcdir)/Python/graminit.c.new
778+
$(UPDATE_FILE) $(srcdir)/Include/graminit.h $(srcdir)/Include/graminit.h.new
779+
$(UPDATE_FILE) $(srcdir)/Python/graminit.c $(srcdir)/Python/graminit.c.new
775780

776781
Parser/grammar.o: $(srcdir)/Parser/grammar.c \
777782
$(srcdir)/Include/token.h \
@@ -789,21 +794,24 @@ regen-ast:
789794
# Regenerate Include/Python-ast.h using Parser/asdl_c.py -h
790795
$(MKDIR_P) $(srcdir)/Include
791796
$(PYTHON_FOR_REGEN) $(srcdir)/Parser/asdl_c.py \
792-
-h $(srcdir)/Include \
797+
-h $(srcdir)/Include/Python-ast.h.new \
793798
$(srcdir)/Parser/Python.asdl
799+
$(UPDATE_FILE) $(srcdir)/Include/Python-ast.h $(srcdir)/Include/Python-ast.h.new
794800
# Regenerate Python/Python-ast.c using Parser/asdl_c.py -c
795801
$(MKDIR_P) $(srcdir)/Python
796802
$(PYTHON_FOR_REGEN) $(srcdir)/Parser/asdl_c.py \
797-
-c $(srcdir)/Python \
803+
-c $(srcdir)/Python/Python-ast.c.new \
798804
$(srcdir)/Parser/Python.asdl
805+
$(UPDATE_FILE) $(srcdir)/Python/Python-ast.c $(srcdir)/Python/Python-ast.c.new
799806

800807
.PHONY: regen-opcode
801808
regen-opcode:
802809
# Regenerate Include/opcode.h from Lib/opcode.py
803810
# using Tools/scripts/generate_opcode_h.py
804811
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/scripts/generate_opcode_h.py \
805812
$(srcdir)/Lib/opcode.py \
806-
$(srcdir)/Include/opcode.h
813+
$(srcdir)/Include/opcode.h.new
814+
$(UPDATE_FILE) $(srcdir)/Include/opcode.h $(srcdir)/Include/opcode.h.new
807815

808816
Python/compile.o Python/symtable.o Python/ast.o: $(srcdir)/Include/graminit.h $(srcdir)/Include/Python-ast.h
809817

@@ -860,7 +868,8 @@ regen-opcode-targets:
860868
# Regenerate Python/opcode_targets.h from Lib/opcode.py
861869
# using Python/makeopcodetargets.py
862870
$(PYTHON_FOR_REGEN) $(srcdir)/Python/makeopcodetargets.py \
863-
$(srcdir)/Python/opcode_targets.h
871+
$(srcdir)/Python/opcode_targets.h.new
872+
$(UPDATE_FILE) $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/opcode_targets.h.new
864873

865874
Python/ceval.o: $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/ceval_gil.h
866875

@@ -887,7 +896,8 @@ regen-typeslots:
887896
# using Objects/typeslots.py
888897
$(PYTHON_FOR_REGEN) $(srcdir)/Objects/typeslots.py \
889898
< $(srcdir)/Include/typeslots.h \
890-
$(srcdir)/Objects/typeslots.inc
899+
$(srcdir)/Objects/typeslots.inc.new
900+
$(UPDATE_FILE) $(srcdir)/Objects/typeslots.inc $(srcdir)/Objects/typeslots.inc.new
891901

892902
############################################################################
893903
# Header files
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid wholesale rebuild after `make regen-all` if nothing changed.

‎Parser/asdl_c.py‎

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,59 +1285,55 @@ def main(srcfile, dump_module=False):
12851285
print(mod)
12861286
if not asdl.check(mod):
12871287
sys.exit(1)
1288-
if INC_DIR:
1289-
p = "%s/%s-ast.h" % (INC_DIR, mod.name)
1290-
f = open(p, "w")
1291-
f.write(auto_gen_msg)
1292-
f.write('#include "asdl.h"\n\n')
1293-
c = ChainOfVisitors(TypeDefVisitor(f),
1294-
StructVisitor(f),
1295-
PrototypeVisitor(f),
1296-
)
1297-
c.visit(mod)
1298-
f.write("PyObject* PyAST_mod2obj(mod_ty t);\n")
1299-
f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
1300-
f.write("int PyAST_Check(PyObject* obj);\n")
1301-
f.close()
1302-
1303-
if SRC_DIR:
1304-
p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c")
1305-
f = open(p, "w")
1306-
f.write(auto_gen_msg)
1307-
f.write('#include <stddef.h>\n')
1308-
f.write('\n')
1309-
f.write('#include "Python.h"\n')
1310-
f.write('#include "%s-ast.h"\n' % mod.name)
1311-
f.write('\n')
1312-
f.write("static PyTypeObject AST_type;\n")
1313-
v = ChainOfVisitors(
1314-
PyTypesDeclareVisitor(f),
1315-
PyTypesVisitor(f),
1316-
Obj2ModPrototypeVisitor(f),
1317-
FunctionVisitor(f),
1318-
ObjVisitor(f),
1319-
Obj2ModVisitor(f),
1320-
ASTModuleVisitor(f),
1321-
PartingShots(f),
1322-
)
1323-
v.visit(mod)
1324-
f.close()
1288+
if H_FILE:
1289+
with open(H_FILE, "w") as f:
1290+
f.write(auto_gen_msg)
1291+
f.write('#include "asdl.h"\n\n')
1292+
c = ChainOfVisitors(TypeDefVisitor(f),
1293+
StructVisitor(f),
1294+
PrototypeVisitor(f),
1295+
)
1296+
c.visit(mod)
1297+
f.write("PyObject* PyAST_mod2obj(mod_ty t);\n")
1298+
f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
1299+
f.write("int PyAST_Check(PyObject* obj);\n")
1300+
1301+
if C_FILE:
1302+
with open(C_FILE, "w") as f:
1303+
f.write(auto_gen_msg)
1304+
f.write('#include <stddef.h>\n')
1305+
f.write('\n')
1306+
f.write('#include "Python.h"\n')
1307+
f.write('#include "%s-ast.h"\n' % mod.name)
1308+
f.write('\n')
1309+
f.write("static PyTypeObject AST_type;\n")
1310+
v = ChainOfVisitors(
1311+
PyTypesDeclareVisitor(f),
1312+
PyTypesVisitor(f),
1313+
Obj2ModPrototypeVisitor(f),
1314+
FunctionVisitor(f),
1315+
ObjVisitor(f),
1316+
Obj2ModVisitor(f),
1317+
ASTModuleVisitor(f),
1318+
PartingShots(f),
1319+
)
1320+
v.visit(mod)
13251321

13261322
if __name__ == "__main__":
13271323
import getopt
13281324

1329-
INC_DIR = ''
1330-
SRC_DIR = ''
1325+
H_FILE = ''
1326+
C_FILE = ''
13311327
dump_module = False
13321328
opts, args = getopt.getopt(sys.argv[1:], "dh:c:")
13331329
for o, v in opts:
13341330
if o == '-h':
1335-
INC_DIR = v
1331+
H_FILE = v
13361332
if o == '-c':
1337-
SRC_DIR = v
1333+
C_FILE = v
13381334
if o == '-d':
13391335
dump_module = True
1340-
if INC_DIR and SRC_DIR:
1336+
if H_FILE and C_FILE:
13411337
print('Must specify exactly one output file')
13421338
sys.exit(1)
13431339
elif len(args) != 1:

‎Tools/scripts/update_file.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
A script that replaces an old file with a new one, only if the contents
3+
actually changed. If not, the new file is simply deleted.
4+
5+
This avoids wholesale rebuilds when a code (re)generation phase does not
6+
actually change the in-tree generated code.
7+
"""
8+
9+
import os
10+
import sys
11+
12+
13+
def main(old_path, new_path):
14+
with open(old_path, 'rb') as f:
15+
old_contents = f.read()
16+
with open(new_path, 'rb') as f:
17+
new_contents = f.read()
18+
if old_contents != new_contents:
19+
os.replace(new_path, old_path)
20+
else:
21+
os.unlink(new_path)
22+
23+
24+
if __name__ == '__main__':
25+
if len(sys.argv) != 3:
26+
print("Usage: %s <path to be updated> <path with new contents>" % (sys.argv[0],))
27+
sys.exit(1)
28+
main(sys.argv[1], sys.argv[2])

0 commit comments

Comments
 (0)