| 1 |
#! /usr/bin/env python3 |
| 2 |
# |
| 3 |
# Copyright © 2024 Collabora Ltd. and Red Hat Inc. |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
|
| 6 |
# This script takes a list of Rust files, each of the form nvh_path_to_mod.rs |
| 7 |
# and constructs a lib.rs which puts each of them in ::path::to::mod. |
| 8 |
|
| 9 |
import argparse |
| 10 |
import os.path |
| 11 |
import re |
| 12 |
import subprocess |
| 13 |
import sys |
| 14 |
|
| 15 |
from mako.template import Template |
| 16 |
|
| 17 |
import util |
| 18 |
|
| 19 |
|
| 20 |
TEMPLATE_RS = Template("""\ |
| 21 |
// Copyright © 2024 Collabora Ltd. and Red Hat Inc. |
| 22 |
// SPDX-License-Identifier: MIT |
| 23 |
|
| 24 |
// This file is generated by lib_rs_gen.py. DO NOT EDIT! |
| 25 |
|
| 26 |
#![allow(unused_imports)] |
| 27 |
|
| 28 |
<%def name="import_mod(m, path)"> |
| 29 |
% if m.present: |
| 30 |
mod nvh_${'_'.join(path)}; |
| 31 |
% endif |
| 32 |
% for name in sorted(m.children): |
| 33 |
${import_mod(m.children[name], path + [name])} |
| 34 |
% endfor |
| 35 |
</%def> |
| 36 |
${import_mod(root, [])} |
| 37 |
|
| 38 |
<%def name="decl_mod(m, path)"> |
| 39 |
% if path: |
| 40 |
pub mod ${path[-1]} { |
| 41 |
% endif |
| 42 |
|
| 43 |
% if m.present: |
| 44 |
pub use crate::nvh_${'_'.join(path)}::*; |
| 45 |
% endif |
| 46 |
|
| 47 |
% for name in sorted(m.children): |
| 48 |
${decl_mod(m.children[name], path + [name])} |
| 49 |
% endfor |
| 50 |
|
| 51 |
% if path: |
| 52 |
} |
| 53 |
% endif |
| 54 |
</%def> |
| 55 |
|
| 56 |
/// Converts a method to its raw representation. |
| 57 |
pub trait Mthd { |
| 58 |
/// The hardware address of the method. |
| 59 |
const ADDR: u16; |
| 60 |
/// The class of the method. |
| 61 |
const CLASS: u16; |
| 62 |
/// Converts the method to its raw representation. |
| 63 |
fn to_bits(self) -> u32; |
| 64 |
} |
| 65 |
|
| 66 |
pub trait ArrayMthd { |
| 67 |
/// The class of the method. |
| 68 |
const CLASS: u16; |
| 69 |
/// The hardware address of the method for the given index. |
| 70 |
fn addr(i: usize) -> u16; |
| 71 |
/// Converts the method to its raw representation. |
| 72 |
fn to_bits(self) -> u32; |
| 73 |
} |
| 74 |
|
| 75 |
${decl_mod(root, [])} |
| 76 |
""") |
| 77 |
|
| 78 |
class Mod(object): |
| 79 |
def __init__(self): |
| 80 |
self.present = False; |
| 81 |
self.children = {} |
| 82 |
|
| 83 |
def add_child(self, path): |
| 84 |
mod = self |
| 85 |
for p in path: |
| 86 |
if p not in mod.children: |
| 87 |
mod.children[p] = Mod() |
| 88 |
mod = mod.children[p] |
| 89 |
|
| 90 |
# Once we've found the child, mark it present |
| 91 |
assert not mod.present |
| 92 |
mod.present = True |
| 93 |
|
| 94 |
def main(): |
| 95 |
parser = argparse.ArgumentParser() |
| 96 |
parser.add_argument('--out-rs', required=True, help='Output Rust file.') |
| 97 |
parser.add_argument('class_files', metavar='FILE', nargs='*', |
| 98 |
action='append', |
| 99 |
help='Input class Rust filename') |
| 100 |
args = parser.parse_args() |
| 101 |
|
| 102 |
root = Mod() |
| 103 |
for f in args.class_files[0]: |
| 104 |
f = os.path.basename(f) |
| 105 |
assert f.endswith('.rs') |
| 106 |
f = f.removesuffix('.rs') |
| 107 |
|
| 108 |
mod_path = f.split('_') |
| 109 |
assert mod_path[0] == 'nvh' |
| 110 |
root.add_child(mod_path[1:]) |
| 111 |
|
| 112 |
util.write_template_rs(args.out_rs, TEMPLATE_RS, dict(root=root)) |
| 113 |
|
| 114 |
|
| 115 |
if __name__ == '__main__': |
| 116 |
main() |