diff --git a/src/nouveau/headers/class_parser.py b/src/nouveau/headers/class_parser.py index e4f3e375da8..259b522ea57 100644 --- a/src/nouveau/headers/class_parser.py +++ b/src/nouveau/headers/class_parser.py @@ -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() diff --git a/src/nouveau/headers/lib_rs_gen.py b/src/nouveau/headers/lib_rs_gen.py index f622815702b..c17f220a773 100644 --- a/src/nouveau/headers/lib_rs_gen.py +++ b/src/nouveau/headers/lib_rs_gen.py @@ -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__': diff --git a/src/nouveau/headers/meson.build b/src/nouveau/headers/meson.build index a6c49258302..8d2433f7642 100644 --- a/src/nouveau/headers/meson.build +++ b/src/nouveau/headers/meson.build @@ -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] diff --git a/src/nouveau/headers/struct_parser.py b/src/nouveau/headers/struct_parser.py index 61faf6f9c99..a6fab670f6a 100644 --- a/src/nouveau/headers/struct_parser.py +++ b/src/nouveau/headers/struct_parser.py @@ -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() diff --git a/src/nouveau/headers/util.py b/src/nouveau/headers/util.py new file mode 100644 index 00000000000..f1deae30722 --- /dev/null +++ b/src/nouveau/headers/util.py @@ -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)