aboutsummaryrefslogtreecommitdiffstats
path: root/tools/template_convert.py
blob: 8ff3d54c580cf4593e21e43d0ad3354716f548df (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3

import os
import re
from argparse import ArgumentParser

template_keywords = [
    "GR_EXPAND_X_H", "GR_EXPAND_CC_H", "GR_EXPAND_X_CC_H_IMPL", "GR_EXPAND_X_CC_H"
]
template_regex = re.compile(
    r"^(?P<template_type>" + "|".join(template_keywords) + r")\(" +
    r"(?P<category>\w+)\s+(?P<name>\w+_XX?X?)(_impl)?\s+(?P<types>[\w\s]+)" + r"\)$")

cpp_keywords = ["abs", "add", "and", "max", "min" "not" "xor"]
types = {"s": "std::int16_t", "i": "std::int32_t", "b": "std::uint8_t", "c": "gr_complex", "f": "float"}


def get_real_name(block_definition):
    """
    Return the base name of the template blocks (e.g. foo_XXX)
    """
    return "_".join(block_definition.get("name").split("_")[:-1])


def rewrite_cmakelists(block_definition):
    """
    Remove gengen template invokations from CMakeLists.txt
    """
    with open(
            os.path.join(block_definition.get("path"), "CMakeLists.txt"),
            "r") as f:
        cmakelists = f.readlines()
    cmakelists_new = []
    for line in cmakelists:
        if line.startswith(
                block_definition.get("template_type") + "(" + block_definition.
                get("category") + " " + block_definition.get("name")):
            continue
        cmakelists_new.append(line)
    with open(
            os.path.join(block_definition.get("path"), "CMakeLists.txt"),
            "w") as f:
        f.writelines(cmakelists_new)


def convert_public_header(block_definition):
    """
    Replace template arguments with the correct C++ template
    expressions
    """
    real_name = get_real_name(block_definition)
    original_name = real_name
    cpp_keyword = False
    if real_name in cpp_keywords:
        real_name += "_blk"
        cpp_keyword = True
    target_file = os.path.join(block_definition.get("path"), real_name + ".h")
    source_file = os.path.join(
        block_definition.get("path"),
        block_definition.get("name") + ".h.t")
    os.rename(source_file, target_file)

    with open(target_file, "r") as f:
        content = f.readlines()

    with open(target_file, "w") as f:
        new_content = []
        typedefs = False
        for line in content:
            line = line.replace("@GUARD_NAME@", "_".join([real_name,
                                                          "h"]).upper())
            if "typedef" in line:
                line = line.replace("@NAME@", " " + real_name + "<T> ")
                line = line.replace("@BASE_NAME@", " " + real_name + "<T> ")
            else:
                line = line.replace("@NAME@", real_name)
                line = line.replace("@BASE_NAME@", real_name)

            line = line.replace("@I_TYPE@", "T")
            line = line.replace("@O_TYPE@", "T")
            line = line.replace("@TYPE@", "T")
            if "@WARNING@" in line:
                continue
            if "class" in line:
                new_content.append("template<class T>\n")
            if not typedefs and "} /* namespace" in line:
                for t in block_definition.get("types"):
                    new_content.append("typedef " + real_name + "<" +
                                       types[t[0]] + "> " + original_name + "_" + t + ";\n")
                    typedefs = True
            new_content.append(line)
        f.writelines(new_content)


def convert_impl_header(block_definition):
    """
    Replace template arguments with the correct C++ template
    expressions
    """
    real_name = get_real_name(block_definition)
    cpp_keyword = False
    if real_name in cpp_keywords:
        real_name += "_blk"
        cpp_keyword = True
    target_header = os.path.join(
        block_definition.get("path"), real_name + "_impl.h")
    source_header = os.path.join(
        block_definition.get("path"),
        block_definition.get("name") + "_impl.h.t")
    os.rename(source_header, target_header)
    with open(target_header, "r") as f:
        content = f.readlines()

    with open(target_header, "w") as f:
        new_content = []
        for line in content:
            line = line.replace("@GUARD_NAME_IMPL@",
                                "_".join([real_name, "impl_h"]).upper())
            line = line.replace("@GUARD_NAME@",
                                "_".join([real_name, "impl_h"]).upper())
            if "typedef" in line or "class" in line:
                line = line.replace("@NAME@", " " + real_name + "<T> ")
                line = line.replace("@BASE_NAME@", " " + real_name + "<T> ")
                line = line.replace("@NAME_IMPL@", real_name + "_impl<T> ")
                line = line.replace("@IMPL_NAME@", real_name + "_impl<T> ")
            else:
                line = line.replace("@NAME@", real_name)
                line = line.replace("@BASE_NAME@", real_name)
                line = line.replace("@NAME_IMPL@", real_name + "_impl ")
                line = line.replace("@IMPL_NAME@", real_name + "_impl")

            line = line.replace("@I_TYPE@", "T")
            line = line.replace("@O_TYPE@", "T")
            line = line.replace("@TYPE@", "T")
            if "@WARNING@" in line:
                continue
            if "class" in line:
                new_content.append("template<class T>\n")
            new_content.append(line)
        f.writelines(new_content)


def convert_impl_impl(block_definition):
    """
    Replace template arguments with the correct C++ template
    expressions
    """
    real_name = get_real_name(block_definition)
    cpp_keyword = False
    if real_name in cpp_keywords:
        real_name += "_blk"
        cpp_keyword = True

    target_impl = os.path.join(
        block_definition.get("path"), real_name + "_impl.cc")
    source_impl = os.path.join(
        block_definition.get("path"),
        block_definition.get("name") + "_impl.cc.t")

    os.rename(source_impl, target_impl)
    with open(target_impl, "r") as f:
        content = f.readlines()

    with open(target_impl, "w") as f:
        new_content = []
        instantiated = False
        for line in content:
            line = line.replace("@GUARD_NAME_IMPL@",
                                "_".join([real_name, "impl_h"]).upper())
            if "typedef" in line or "class" in line:
                line = line.replace("@NAME@", " " + real_name + "<T> ")
                line = line.replace("@BASE_NAME@", " " + real_name + "<T> ")
            else:
                line = line.replace("@NAME@", real_name)
                line = line.replace("@BASE_NAME@", real_name)
            line = line.replace("@IMPL_NAME@", real_name + "_impl<T>")
            line = line.replace("@NAME_IMPL@", real_name + "_impl<T> ")
            line = line.replace("@I_TYPE@", "T")
            line = line.replace("@O_TYPE@", "T")
            line = line.replace("@TYPE@", "T")
            if "@WARNING@" in line:
                continue
            if "class" in line:
                new_content.append("template<class T>\n")
            if not instantiated and "} /* namespace" in line:
                for t in block_definition.get("types"):
                    new_content.append("template class " + real_name + "<" +
                                       types[t[0]] + ">;\n")
                    instantiated = True
            new_content.append(line)
        f.writelines(new_content)


def convert_impl(block_definition):
    """
    Convert the impl header and implementation
    """
    convert_impl_header(block_definition)
    convert_impl_impl(block_definition)


def handle_template_conversion(block_definition):
    """
    Convert gengen templates to C++ templates for simple cases
    which only have one type for input and output
    """
    if block_definition.get("single_type", None) == True:
        if block_definition.get("template_type") == "GR_EXPAND_X_H":
            convert_public_header(block_definition)
        elif block_definition.get(
                "template_type"
        ) == "GR_EXPAND_X_CC_H_IMPL" or block_definition.get(
                "template_type") == "GR_EXPAND_X_CC_H":
            convert_impl(block_definition)
        rewrite_cmakelists(block_definition)


def find_template_blocks(cmake_file):
    """
    Match every line in a CMakeLists.txt file with a template regex
    """
    blocks = []
    with open(cmake_file, "r") as f:
        for line in f:
            result = re.match(template_regex, line)
            if result is not None:
                r = result.groupdict()
                r["types"] = r.get("types", "").split(" ")
                if all([(t[1:] == t[:-1] or len(t) == 1) for t in r["types"]]):
                    r["single_type"] = True
                else:
                    r["single_type"] = False
                blocks.append(r)
    return blocks


def walk_and_find(root):
    """
    Identify templated blocks by looking in the CMakeLists
    """
    all_blocks = []
    for (dirpath, dirnames, filenames) in os.walk(root):
        if "CMakeLists.txt" in filenames:
            blocks = find_template_blocks(
                os.path.join(dirpath, "CMakeLists.txt"))
            all_blocks.extend([{**block, "path": dirpath} for block in blocks])
    return all_blocks


def parse_args():
    parser = ArgumentParser()
    parser.add_argument(
        '--directory_root',
        "-d",
        help="Root directory to start search",
        default=os.environ.get("PWD"))
    parser.add_argument(
        "--no_changes",
        "-n",
        dest="no_changes",
        default=False,
        action="store_true",
        help="Only show found templated blocks, don't apply changes")
    return parser.parse_args()


def main():
    """
    Run this if the program was invoked on the commandline
    """
    args = parse_args()
    all_blocks = walk_and_find(args.directory_root)
    for block in all_blocks:
        if not args.no_changes:
            handle_template_conversion(block)
        else:
            print(block)
    return True


if __name__ == "__main__":
    exit(not (main()))