Commit 2b7ac0c8 authored by Jakub Kicinski's avatar Jakub Kicinski

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: default avatarJiri Pirko <jiri@nvidia.com>
Link: https://lore.kernel.org/r/20231027223408.1865704-1-kuba@kernel.orgSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 05f0431b
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import argparse import argparse
import collections import collections
import filecmp
import os import os
import re import re
import shutil import shutil
...@@ -1168,7 +1169,7 @@ class CodeWriter: ...@@ -1168,7 +1169,7 @@ class CodeWriter:
if out_file is None: if out_file is None:
self._out = os.sys.stdout self._out = os.sys.stdout
else: else:
self._out = tempfile.TemporaryFile('w+') self._out = tempfile.NamedTemporaryFile('w+')
self._out_file = out_file self._out_file = out_file
def __del__(self): def __del__(self):
...@@ -1177,6 +1178,10 @@ class CodeWriter: ...@@ -1177,6 +1178,10 @@ class CodeWriter:
def close_out_file(self): def close_out_file(self):
if self._out == os.sys.stdout: if self._out == os.sys.stdout:
return return
# Avoid modifying the file if contents didn't change
self._out.flush()
if os.path.isfile(self._out_file) and filecmp.cmp(self._out.name, self._out_file, shallow=False):
return
with open(self._out_file, 'w+') as out_file: with open(self._out_file, 'w+') as out_file:
self._out.seek(0) self._out.seek(0)
shutil.copyfileobj(self._out, out_file) shutil.copyfileobj(self._out, out_file)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment