util/glsl2spirv: fixup the generated depfile when copying sources

So that the depfile contains a reference to the original source rather
than the copied one. This is necessary to avoid ninja not finding the
copy and causing spurious rebuilds when the copy has been removed, as
well as correctly tracking changes to the input files.

fixes: 46644ba371

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30132>
(cherry picked from commit 36160c967c)
This commit is contained in:
Dylan Baker 2024-07-11 10:10:38 -07:00 committed by Eric Engestrom
parent 3b61e8f004
commit f6ba6a5205
2 changed files with 30 additions and 5 deletions

View file

@ -254,7 +254,7 @@
"description": "util/glsl2spirv: fixup the generated depfile when copying sources",
"nominated": true,
"nomination_type": 1,
"resolution": 0,
"resolution": 1,
"main_sha": null,
"because_sha": "46644ba371e817d8f33ad7b46ce2ba7775e6d2cc",
"notes": null

View file

@ -145,7 +145,7 @@ def postprocess_file(args: 'Arguments') -> None:
w.writelines(lines)
def preprocess_file(args: 'Arguments', origin_file: T.TextIO, directory: os.PathLike) -> str:
def preprocess_file(args: 'Arguments', origin_file: T.TextIO, directory: os.PathLike, filemap: T.Dict[str, str]) -> str:
with open(os.path.join(directory, os.path.basename(origin_file.name)), "w") as copy_file:
lines = origin_file.readlines()
@ -157,13 +157,36 @@ def preprocess_file(args: 'Arguments', origin_file: T.TextIO, directory: os.Path
copy_file.writelines(lines)
filemap[copy_file.name] = origin_file.name
return copy_file.name
def fixup_depfile(depfile: str, filemap: T.Mapping[str, str]) -> None:
"""Replace generated file references in the depfile with their source.
"""
depends: T.List[str] = []
out: T.Optional[str] = None
with open(depfile, 'r') as f:
for line in f.readlines():
if ':' in line:
out, line = line.split(':', 1)
depends.extend(l.strip() for l in line.split())
with open(depfile, 'w') as f:
f.write(out)
f.write(': ')
for dep in depends:
f.write(filemap.get(dep, dep))
f.write(' ')
f.write('\n')
def process_file(args: 'Arguments') -> None:
filemap: T.Dict[str, str] = {}
with open(args.input, "r") as infile:
copy_file = preprocess_file(args, infile,
os.path.dirname(args.output))
copy_file = preprocess_file(
args, infile, os.path.dirname(args.output), filemap)
cmd_list = [args.glslang]
@ -184,7 +207,6 @@ def process_file(args: 'Arguments') -> None:
for f in args.includes:
cmd_list.append('-I' + f)
for d in args.defines:
cmd_list.append('-D' + d)
@ -207,6 +229,9 @@ def process_file(args: 'Arguments') -> None:
if args.create_entry is not None:
os.remove(copy_file)
if args.depfile is not None:
fixup_depfile(args.depfile, filemap)
def main() -> None:
args = get_args()