Skip to content

Commit 2b7ac0c

Browse files
committed
tools: ynl-gen: don't touch the output file if content is the same
I often regenerate all YNL files in the tree to make sure they are in sync with the codegen and specs. Generator rewrites the files unconditionally, so since make looks at file modification time to decide what to rebuild - my next build takes longer. We already generate the code to a tempfile most of the time, only overwrite the target when we have to. Before: $ stat include/uapi/linux/netdev.h File: include/uapi/linux/netdev.h Size: 2307 Blocks: 8 IO Block: 4096 regular file Access: 2023-10-27 15:19:56.347071940 -0700 Modify: 2023-10-27 15:19:45.089000900 -0700 Change: 2023-10-27 15:19:45.089000900 -0700 Birth: 2023-10-27 15:19:45.088000894 -0700 $ ./tools/net/ynl/ynl-regen.sh -f [...] $ stat include/uapi/linux/netdev.h File: include/uapi/linux/netdev.h Size: 2307 Blocks: 8 IO Block: 4096 regular file Access: 2023-10-27 15:19:56.347071940 -0700 Modify: 2023-10-27 15:22:18.417968446 -0700 Change: 2023-10-27 15:22:18.417968446 -0700 Birth: 2023-10-27 15:19:45.088000894 -0700 After: $ stat include/uapi/linux/netdev.h File: include/uapi/linux/netdev.h Size: 2307 Blocks: 8 IO Block: 4096 regular file Access: 2023-10-27 15:22:41.520114221 -0700 Modify: 2023-10-27 15:22:18.417968446 -0700 Change: 2023-10-27 15:22:18.417968446 -0700 Birth: 2023-10-27 15:19:45.088000894 -0700 $ ./tools/net/ynl/ynl-regen.sh -f [...] $ stat include/uapi/linux/netdev.h File: include/uapi/linux/netdev.h Size: 2307 Blocks: 8 IO Block: 4096 regular file Access: 2023-10-27 15:22:41.520114221 -0700 Modify: 2023-10-27 15:22:18.417968446 -0700 Change: 2023-10-27 15:22:18.417968446 -0700 Birth: 2023-10-27 15:19:45.088000894 -0700 Reviewed-by: Jiri Pirko <jiri@nvidia.com> Link: https://lore.kernel.org/r/20231027223408.1865704-1-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 05f0431 commit 2b7ac0c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

tools/net/ynl/ynl-gen-c.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import argparse
55
import collections
6+
import filecmp
67
import os
78
import re
89
import shutil
@@ -1168,7 +1169,7 @@ def __init__(self, nlib, out_file=None):
11681169
if out_file is None:
11691170
self._out = os.sys.stdout
11701171
else:
1171-
self._out = tempfile.TemporaryFile('w+')
1172+
self._out = tempfile.NamedTemporaryFile('w+')
11721173
self._out_file = out_file
11731174

11741175
def __del__(self):
@@ -1177,6 +1178,10 @@ def __del__(self):
11771178
def close_out_file(self):
11781179
if self._out == os.sys.stdout:
11791180
return
1181+
# Avoid modifying the file if contents didn't change
1182+
self._out.flush()
1183+
if os.path.isfile(self._out_file) and filecmp.cmp(self._out.name, self._out_file, shallow=False):
1184+
return
11801185
with open(self._out_file, 'w+') as out_file:
11811186
self._out.seek(0)
11821187
shutil.copyfileobj(self._out, out_file)

0 commit comments

Comments
 (0)