From 51da40d0db9b8dcf8178e6d1dea088b0cd1b8680 Mon Sep 17 00:00:00 2001 From: Faith Ekstrand Date: Sun, 23 Mar 2025 00:32:12 -0500 Subject: [PATCH] nouveau/headers: Drop unused Rust constants The only users of the Rust crate use the structs and Mthd impls. This reduces the size of libnouveau_headers.rlib from 99 MiB to 84 MiB. Part-of: --- src/nouveau/headers/class_parser.py | 64 ----------------------------- 1 file changed, 64 deletions(-) diff --git a/src/nouveau/headers/class_parser.py b/src/nouveau/headers/class_parser.py index 618dc2b239b..64b3c9b7712 100644 --- a/src/nouveau/headers/class_parser.py +++ b/src/nouveau/headers/class_parser.py @@ -476,65 +476,6 @@ def parse_header(nvcl, f): return (version, methods) -def convert_to_rust_constants(filename): - with open(filename, 'r') as file: - lines = file.readlines() - - rust_items = [] - processed_constants = {} - file_prefix = "NV" + os.path.splitext(os.path.basename(filename))[0].upper() + "_" - file_prefix = file_prefix.replace('CL', '') - for line in lines: - match = re.match(r'#define\s+(\w+)\((\w+)\)\s+(.+)', line.strip()) - if match: - name, arg, expr = match.groups() - if name in processed_constants: - processed_constants[name] += 1 - name += f"_{processed_constants[name]}" - else: - processed_constants[name] = 0 - name = name.replace(file_prefix, '') - # convert to snake case - name = re.sub(r'(?<=[a-z])(?=[A-Z])', '_', name).lower() - rust_items.append(f"#[inline]\npub fn {name} ({arg}: u32) -> u32 {{ {expr.replace('(', '').replace(')', '')} }} ") - else: - match = re.match(r'#define\s+(\w+)\s+(?:MW\()?(\d+):(\d+)\)?', line.strip()) - if match: - name, high, low = match.groups() - high = int(high) + 1 # Convert to exclusive range - if name in processed_constants: - processed_constants[name] += 1 - name += f"_{processed_constants[name]}" - else: - processed_constants[name] = 0 - # name = name.replace('__', '_').replace(file_prefix, '') - name = name.replace(file_prefix, '') - rust_items.append(f"pub const {name}: Range = {low}..{high};") - else: - match = re.match(r'#define\s+(\w+)\s+\(?0x(\w+)\)?', line.strip()) - if match: - name, value = match.groups() - if name in processed_constants: - processed_constants[name] += 1 - name += f"_{processed_constants[name]}" - else: - processed_constants[name] = 0 - name = name.replace(file_prefix, '') - rust_items.append(f"pub const {name}: u32 = 0x{value};") - else: - match = re.match(r'#define\s+(\w+)\s+\(?(\d+)\)?', line.strip()) - if match: - name, value = match.groups() - if name in processed_constants: - processed_constants[name] += 1 - name += f"_{processed_constants[name]}" - else: - processed_constants[name] = 0 - name = name.replace(file_prefix, '') - rust_items.append(f"pub const {name}: u32 = {value};") - - return '\n'.join(rust_items) - def main(): parser = argparse.ArgumentParser() parser.add_argument('--out-h', required=False, help='Output C header.') @@ -579,15 +520,10 @@ def main(): 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("#![allow(non_camel_case_types)]\n") f.write("#![allow(non_snake_case)]\n") - f.write("#![allow(non_upper_case_globals)]\n\n") - f.write("use std::ops::Range;\n") f.write("use crate::Mthd;\n") f.write("use crate::ArrayMthd;\n") f.write("\n") - f.write(convert_to_rust_constants(args.in_h)) - f.write("\n") f.write(TEMPLATE_RS_MTHD.render(**environment)) except Exception: