util/glsl2spirv: close resources as soon as possible

Reviewed-by: Luis Felipe Strano Moraes <luis.strano@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19449>
This commit is contained in:
Dylan Baker
2022-11-01 13:00:26 -07:00
committed by Marge Bot
parent 949c3b55db
commit 87c83c041a

View File

@@ -115,14 +115,14 @@ def postprocess_file(args: Arguments) -> None:
with open(args.output, "r") as r: with open(args.output, "r") as r:
lines = r.readlines() lines = r.readlines()
# glslangValidator creates a variable without the static modifier. # glslangValidator creates a variable without the static modifier.
lines = convert_to_static_variable(lines, args.vn) lines = convert_to_static_variable(lines, args.vn)
# '#pragma once' is unstandardised. # '#pragma once' is unstandardised.
lines = create_include_guard(lines, os.path.basename(r.name)) lines = create_include_guard(lines, os.path.basename(r.name))
with open(args.output, "w") as w: with open(args.output, "w") as w:
w.writelines(lines) w.writelines(lines)
def preprocess_file(args: Arguments, origin_file: T.TextIO) -> str: def preprocess_file(args: Arguments, origin_file: T.TextIO) -> str:
@@ -137,50 +137,50 @@ def preprocess_file(args: Arguments, origin_file: T.TextIO) -> str:
copy_file.writelines(lines) copy_file.writelines(lines)
return copy_file.name return copy_file.name
def process_file(args: Arguments) -> None: def process_file(args: Arguments) -> None:
with open(args.input, "r") as infile: with open(args.input, "r") as infile:
copy_file = preprocess_file(args, infile) copy_file = preprocess_file(args, infile)
cmd_list = ["glslangValidator"] cmd_list = ["glslangValidator"]
if args.Olib: if args.Olib:
cmd_list += ["--keep-uncalled"] cmd_list += ["--keep-uncalled"]
if args.vn is not None: if args.vn is not None:
cmd_list += ["--variable-name", args.vn] cmd_list += ["--variable-name", args.vn]
if args.extra is not None: if args.extra is not None:
cmd_list.append(args.extra) cmd_list.append(args.extra)
if args.create_entry is not None: if args.create_entry is not None:
cmd_list += ["--entry-point", args.create_entry] cmd_list += ["--entry-point", args.create_entry]
cmd_list.append("-V") cmd_list.append("-V")
cmd_list += ["-o", args.output] cmd_list += ["-o", args.output]
cmd_list += ["-S", args.stage] cmd_list += ["-S", args.stage]
cmd_list.append(copy_file) cmd_list.append(copy_file)
with subprocess.Popen(" ".join(cmd_list), with subprocess.Popen(" ".join(cmd_list),
shell = True, shell = True,
stdout = subprocess.PIPE, stdout = subprocess.PIPE,
stderr = subprocess.PIPE, stderr = subprocess.PIPE,
stdin = subprocess.PIPE) as proc: stdin = subprocess.PIPE) as proc:
out, err = proc.communicate(timeout=30) out, err = proc.communicate(timeout=30)
if proc.returncode != 0: if proc.returncode != 0:
message = out.decode('utf-8') + '\n' + err.decode('utf-8') message = out.decode('utf-8') + '\n' + err.decode('utf-8')
raise ShaderCompileError(message.strip()) raise ShaderCompileError(message.strip())
if args.vn is not None: if args.vn is not None:
postprocess_file(args) postprocess_file(args)
if args.create_entry is not None: if args.create_entry is not None:
os.remove(copy_file) os.remove(copy_file)
def main() -> None: def main() -> None: