nouveau/headers: Factor out write_template

This code was copied across each of the scripts.

Acked-by: Dave Airlie <airlied@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36121>
This commit is contained in:
Mel Henning
2025-07-14 16:06:31 -04:00
committed by Marge Bot
parent ad7a5ddfb0
commit 5e2eea81b9
5 changed files with 41 additions and 54 deletions
+12 -23
View File
@@ -11,6 +11,9 @@ import subprocess
from mako.template import Template
import util
METHOD_ARRAY_SIZES = {
'BIND_GROUP_CONSTANT_BUFFER' : 16,
'CALL_MME_DATA' : 256,
@@ -565,30 +568,16 @@ def main():
'bs': '\\'
}
try:
if args.out_h is not None:
environment['header'] = os.path.basename(args.out_h)
with open(args.out_h, 'w', encoding='utf-8') as f:
f.write(TEMPLATE_H.render(**environment))
if args.out_c is not None:
with open(args.out_c, 'w', encoding='utf-8') as f:
f.write(TEMPLATE_C.render(**environment))
if args.out_rs is not None:
with open(args.out_rs, 'w', encoding='utf-8') as f:
f.write(TEMPLATE_RS.render(**environment))
if args.out_rs_mthd is not None:
with open(args.out_rs_mthd, 'w', encoding='utf-8') as f:
f.write(TEMPLATE_RS_MTHD.render(**environment))
if args.out_h is not None:
environment['header'] = os.path.basename(args.out_h)
util.write_template(args.out_h, TEMPLATE_H, environment)
if args.out_c is not None:
util.write_template(args.out_c, TEMPLATE_C, environment)
if args.out_rs is not None:
util.write_template(args.out_rs, TEMPLATE_RS, environment)
if args.out_rs_mthd is not None:
util.write_template(args.out_rs_mthd, TEMPLATE_RS_MTHD, environment)
except Exception:
# In the event there's an error, this imports some helpers from mako
# to print a useful stack trace and prints it, then exits with
# status 1, if python is run with debug; otherwise it just raises
# the exception
import sys
from mako import exceptions
print(exceptions.text_error_template().render(), file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()
+4 -12
View File
@@ -14,6 +14,9 @@ import sys
from mako.template import Template
import util
TEMPLATE_RS = Template("""\
// Copyright © 2024 Collabora Ltd. and Red Hat Inc.
// SPDX-License-Identifier: MIT
@@ -106,18 +109,7 @@ def main():
assert mod_path[0] == 'nvh'
root.add_child(mod_path[1:])
try:
with open(args.out_rs, 'w', encoding='utf-8') as f:
f.write(TEMPLATE_RS.render(root=root))
except Exception:
# In the event there's an error, this imports some helpers from mako
# to print a useful stack trace and prints it, then exits with
# status 1, if python is run with debug; otherwise it just raises
# the exception
import sys
from mako import exceptions
print(exceptions.text_error_template().render(), file=sys.stderr)
sys.exit(1)
util.write_template(args.out_rs, TEMPLATE_RS, dict(root=root))
if __name__ == '__main__':
+7 -7
View File
@@ -48,7 +48,7 @@ cl_generated = []
foreach cl : nv_classes
cl_generated += custom_target(
cl + '.h',
input : ['class_parser.py', 'nvidia/classes/'+cl+'.h'],
input : ['class_parser.py', 'nvidia/classes/'+cl+'.h', 'util.py'],
output : ['nv_push_'+cl+'.h', 'nv_push_'+cl+'.c'],
command : [prog_python, '@INPUT0@', '--in-h', '@INPUT1@',
'--out-h', '@OUTPUT0@', '--out-c', '@OUTPUT1@'],
@@ -83,7 +83,7 @@ if with_nouveau_vk
foreach cl : nv_classes
cl_rs_generated += custom_target(
'nvh_classes_' + cl + '.rs',
input : ['class_parser.py', 'nvidia/classes/'+cl+'.h'],
input : ['class_parser.py', 'nvidia/classes/'+cl+'.h', 'util.py'],
output : ['nvh_classes_'+cl+'.rs'],
command : [prog_python, '@INPUT0@', '--in-h', '@INPUT1@',
'--out-rs', '@OUTPUT0@'],
@@ -104,7 +104,7 @@ if with_nouveau_vk
cl_rs_generated += custom_target(
'nvh_' + cl + '_mthd.rs',
input : ['class_parser.py', 'nvidia/classes/'+cl+'.h', prev_cl_h],
input : ['class_parser.py', 'nvidia/classes/'+cl+'.h', prev_cl_h, 'util.py'],
output : ['nvh_classes_'+cl+'_mthd.rs'],
command : [prog_python, '@INPUT0@', '--in-h', '@INPUT1@',
prev_cl_args, '--out-rs-mthd', '@OUTPUT0@'],
@@ -113,7 +113,7 @@ if with_nouveau_vk
if cl.endswith('c0') and fs.is_file('nvidia/classes/'+cl+'qmd.h')
cl_rs_generated += custom_target(
'nvh_classes_' + cl + '_qmd.rs',
input : ['struct_parser.py', 'nvidia/classes/'+cl+'qmd.h'],
input : ['struct_parser.py', 'nvidia/classes/'+cl+'qmd.h', 'util.py'],
output : ['nvh_classes_'+cl+'_qmd.rs'],
command : [prog_python, '@INPUT0@', '--in-h', '@INPUT1@',
'--out-rs', '@OUTPUT0@']
@@ -123,7 +123,7 @@ if with_nouveau_vk
if cl.endswith('97')
cl_rs_generated += custom_target(
'nvh_classes_' + cl + '_tex.rs',
input : ['struct_parser.py', 'nvidia/classes/'+cl+'tex.h'],
input : ['struct_parser.py', 'nvidia/classes/'+cl+'tex.h', 'util.py'],
output : ['nvh_classes_'+cl+'_tex.rs'],
command : [prog_python, '@INPUT0@', '--in-h', '@INPUT1@',
'--out-rs', '@OUTPUT0@']
@@ -132,7 +132,7 @@ if with_nouveau_vk
if fs.is_file(f'nvidia/classes/@cl@sph.h')
cl_rs_generated += custom_target(
input : ['struct_parser.py', f'nvidia/classes/@cl@sph.h'],
input : ['struct_parser.py', f'nvidia/classes/@cl@sph.h', 'util.py'],
output : [f'nvh_classes_@cl@_sph.rs'],
command : [prog_python, '@INPUT0@', '--in-h', '@INPUT1@',
'--out-rs', '@OUTPUT0@']
@@ -152,7 +152,7 @@ if with_nouveau_vk
_nvidia_headers_lib_rs = custom_target(
'lib.rs',
input : ['lib_rs_gen.py'],
input : ['lib_rs_gen.py', 'util.py'],
output : ['lib.rs'],
command : [prog_python, '@INPUT0@', '--out-rs', '@OUTPUT0@',
cl_rs_generated]
+4 -12
View File
@@ -11,6 +11,9 @@ import sys
from collections import namedtuple
from mako.template import Template
import util
TEMPLATE_RS = Template("""\
// Copyright © 2024 Collabora Ltd. and Red Hat Inc.
// SPDX-License-Identifier: MIT
@@ -161,19 +164,8 @@ def main():
with open(args.in_h, 'r', encoding='utf-8') as f:
structs = parse_header(nvcl, f)
try:
with open(args.out_rs, 'w', encoding='utf-8') as f:
f.write(TEMPLATE_RS.render(structs=structs))
util.write_template(args.out_rs, TEMPLATE_RS, dict(structs=structs))
except Exception:
# In the event there's an error, this imports some helpers from mako
# to print a useful stack trace and prints it, then exits with
# status 1, if python is run with debug; otherwise it just raises
# the exception
import sys
from mako import exceptions
print(exceptions.text_error_template().render(), file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()
+14
View File
@@ -0,0 +1,14 @@
def write_template(out_file, template, environment):
try:
with open(out_file, "w", encoding="utf-8") as f:
f.write(template.render(**environment))
except Exception:
# In the event there's an error, this imports some helpers from mako
# to print a useful stack trace and prints it, then exits with
# status 1, if python is run with debug; otherwise it just raises
# the exception
import sys
from mako import exceptions
print(exceptions.text_error_template().render(), file=sys.stderr)
sys.exit(1)