1#!/usr/bin/env python3
2
3# The format of the kernel configs in the framework compatibility matrix
4# has a couple properties that would make it confusing or cumbersome to
5# maintain by hand:
6#
7#  - Conditions apply to all configs within the same <kernel> section.
8#    The <kernel> tag also specifies the LTS version. Since the entire
9#    file in the kernel/configs repo is for a single kernel version,
10#    the section is renamed as a "group", and the LTS version is
11#    specified once at the top of the file with a tag of the form
12#    <kernel minlts="x.y.z" />.
13#  - The compatibility matrix understands all kernel config options as
14#    tristate values. In reality however some kernel config options are
15#    boolean. This script simply converts booleans to tristates so we
16#    can avoid describing boolean values as tristates in hand-maintained
17#    files.
18#
19
20from __future__ import print_function
21import argparse
22import os
23import re
24import sys
25
26def fixup(args):
27    with open(args.input) as source_f:
28        # The first line of the conditional xml has the tag containing
29        # the kernel min LTS version.
30        line = source_f.readline()
31        exp_re = re.compile(r"^<kernel minlts=\"(\d+).(\d+).(\d+)\"\s+/>")
32        exp_match = re.match(exp_re, line)
33        assert exp_match, "Malformatted kernel conditional config file.\n"
34
35        major = exp_match.group(1)
36        minor = exp_match.group(2)
37        tiny = exp_match.group(3)
38
39        if args.output_version:
40            with open(args.output_version, "w+") as version_f:
41                version_f.write("{}.{}.{}".format(major, minor, tiny))
42
43        if args.output_matrix:
44            with open(args.output_matrix, "w+") as dest_f:
45                dest_f.write("<compatibility-matrix version=\"1.0\" type=\"framework\">\n")
46
47                # First <kernel> must not have <condition> for libvintf backwards compatibility.
48                dest_f.write("<kernel version=\"{}.{}.{}\" />".format(major, minor, tiny))
49
50                line = source_f.readline()
51                while line:
52                    line = line.replace("<value type=\"bool\">",
53                            "<value type=\"tristate\">")
54                    line = line.replace("<group>",
55                            "<kernel version=\"{}.{}.{}\">".format(major, minor, tiny))
56                    line = line.replace("</group>", "</kernel>")
57                    dest_f.write(line)
58                    line = source_f.readline()
59
60                dest_f.write("</compatibility-matrix>")
61
62if __name__ == '__main__':
63    parser = argparse.ArgumentParser(description=__doc__)
64    parser.add_argument('--input', help='Input file', required=True)
65    parser.add_argument('--output-matrix', help='Output compatibility matrix file')
66    parser.add_argument('--output-version', help='Output version file')
67
68    args = parser.parse_args()
69
70    fixup(args)
71
72    sys.exit(0)
73