summaryrefslogtreecommitdiff
path: root/util/create_dlt_version_h.py
blob: 2b685b76e27f1d9e92dde53c0867094081730764 (plain)
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
#!/usr/bin/python3
# This software has been developed by Advanced Driver Information Technology.
# Copyright(c) 2019 Advanced Driver Information Technology GmbH,
# Advanced Driver Information Technology Corporation, Robert Bosch GmbH,
# Robert Bosch Car Multimedia GmbH and DENSO Corporation.
#
# This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
#
# This Source Code Form is subject to the terms of the
# Mozilla Public License (MPL), v. 2.0.
# If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# For further information see http://www.genivi.org/.
import pathlib
import argparse
import re


# CMake variables without references to other vars
determined_vars = {}


# Resolves variable reverences from CMakeLists e.g.:
#       ${DLT_MAJOR_VERSION}.${DLT_MINOR_VERSION}
#    -> 2.${DLT_MINOR_VERSION}
def resolve_variables(undetermined_vars):
    for key, val in undetermined_vars.items():
        m = re.search('\$\{(?P<var_name>\w+)\}', val)
        if m:
            if m.group('var_name') in determined_vars:
                yield key, val.replace(m.group(0), determined_vars[m.group('var_name')])
                continue
            yield key, val
        else:
            determined_vars[key] = val


def main(cmake_file, header_in_file, header_out_file):
    cmakelists = pathlib.Path(cmake_file)
    header_in = pathlib.Path(header_in_file)
    header_out = pathlib.Path(header_out_file)

    src = cmakelists.open().read()
    undetermined_vars = {}

    # Find all cmake variable assignments
    for match in re.finditer('set\s*\(\s*(?P<key>\w+)\s*(?P<value>\S+)\s*\)', src):
        key = match.group('key')
        val = match.group('value')
        if '${' not in val:
            determined_vars[key] = val
        else:
            undetermined_vars[key] = val

    # Try to resolve all variables referencing other vars e.g.:
    # set(DLT_VERSION ${DLT_MAJOR_VERSION}.${DLT_MINOR_VERSION}.${DLT_PATCH_LEVEL})
    no_changes = 5
    def_len = len(undetermined_vars)
    while no_changes > 0:
        undetermined_vars = dict(resolve_variables(undetermined_vars))
        if len(undetermined_vars) == def_len:
            no_changes -= 1
        else:
            def_len = len(undetermined_vars)
            no_changes = 5

    header_out.parent.mkdir(parents=True, exist_ok=True)
    with header_in.open() as hi, header_out.open('w') as ho:
        for line in hi:
            text, _ = re.subn('@(?P<var_name>\w+)@', lambda x: determined_vars.get(x.group('var_name'), "NONE"), line)
            ho.write(text)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('cmakelists')
    parser.add_argument('header_in')
    parser.add_argument('header_out')
    args = parser.parse_args()
    main(args.cmakelists, args.header_in, args.header_out)