File: make_abi_structs.py

package info (click to toggle)
rdma-core 33.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,844 kB
  • sloc: ansic: 145,804; python: 5,688; sh: 2,761; perl: 1,465; makefile: 73
file content (56 lines) | stat: -rw-r--r-- 1,730 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#/usr/bin/env python
"""This script transforms the structs inside the kernel ABI headers into a define
of an anonymous struct.

eg
  struct abc {int foo;};
becomes
  #define _STRUCT_abc struct {int foo;};

This allows the exact same struct to be included in the provider wrapper struct:

struct abc_resp {
   struct ibv_abc ibv_resp;
   _STRUCT_abc;
};

Which duplicates the struct layout and naming we have historically used, but
sources the data directly from the kernel headers instead of manually copying."""
import re;
import functools;
import sys;

def in_struct(ln,FO,nesting=0):
    """Copy a top level structure over to the #define output, keeping track of
    nested structures."""
    if nesting == 0:
        if re.match(r"(}.*);",ln):
            FO.write(ln[:-1] + "\n\n");
            return find_struct;

    FO.write(ln + " \\\n");

    if ln == "struct {" or ln == "union {":
        return functools.partial(in_struct,nesting=nesting+1);

    if re.match(r"}.*;",ln):
        return functools.partial(in_struct,nesting=nesting-1);
    return functools.partial(in_struct,nesting=nesting);

def find_struct(ln,FO):
    """Look for the start of a top level structure"""
    if ln.startswith("struct ") or ln.startswith("union "):
        g = re.match(r"(struct|union)\s+(\S+)\s+{",ln);
        FO.write("#define _STRUCT_%s %s { \\\n"%(g.group(2),g.group(1)));
        return in_struct;
    return find_struct;

with open(sys.argv[1]) as FI:
    with open(sys.argv[2],"w") as FO:
        state = find_struct;
        for ln in FI:
            # Drop obvious comments
            ln = ln.strip();
            ln = re.sub(r"/\*.*\*/","",ln);
            ln = re.sub(r"//.*$","",ln);
            state = state(ln,FO);