• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Copyright (c) 2021 Huawei Device Co., Ltd.
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9    http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17"""
18import argparse
19import json
20import os
21import stat
22
23KCONFIG_STR = 'config {}\n\
24    bool "{}"\n\
25    default n\n'
26PROPERTIES_STR = 'config {}\n\
27        string "{}"\n\
28        default ""\n'
29KMENU_STR = 'menu "{}"\n'
30
31FEATURE_STR = 'config feature$$%s\n\
32        string "feature"\n\
33        default ""\n\
34        depends on %s\n'
35
36
37def create_config(name: str, comment: str):
38    return KCONFIG_STR.format(name, comment)
39
40
41def create_property(name: str, comment: str):
42    return PROPERTIES_STR.format(name, comment)
43
44
45def create_menu(name: str):
46    return KMENU_STR.format(name)
47
48
49def end_menu():
50    return "endmenu\n"
51
52
53def create_feature(name: str):
54    return FEATURE_STR % (name, name)
55
56
57def read_json(file: str):
58    data = {}
59    with open(file, "rb") as f:
60        data = json.load(f)
61    return data
62
63
64def write_kconfig(result: str, outdir: str):
65    outpath = os.path.join(outdir, "kconfig")
66    with os.fdopen(os.open(outpath,
67                               os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 'w') as f:
68        f.writelines(result)
69    print("output file in: ", os.path.abspath(outpath))
70
71
72def gen_kconfig(config_path: str, outdir: str):
73    data = read_json(config_path)
74    subsystems = data.get("subsystems")
75    result = 'mainmenu "Subsystem Component Kconfig Configuration"\n'
76    for prop, _ in data.items():
77        if prop == "subsystems":
78            continue
79        result += create_property("property$${}".format(prop), prop)
80
81    for subsys_dict in subsystems:
82        subsys_name = subsys_dict.get("subsystem")
83        result += create_menu(subsys_name)
84        for component_dict in subsys_dict.get("components"):
85            component_name = component_dict.get("component")
86            result += create_config("{}$${}".format(subsys_name, component_name), component_name)
87            result += create_feature("{}$${}".format(subsys_name, component_name))
88        result += end_menu()
89    write_kconfig(result, outdir)
90
91
92if __name__ == "__main__":
93    INTRO = 'Genenrate newly kconfig input file.\n\
94        For example: python3 generate_kconfig.py\n\
95        or           python3 generate_kconfig.py --base_product={$repo}/productdefine/common/base/base_product.json --outdir=./'
96    parser = argparse.ArgumentParser(
97        formatter_class=argparse.RawDescriptionHelpFormatter,
98        description=INTRO
99    )
100    parser.add_argument('--base_product', type=str, default="./../../../productdefine/common/base/base_product.json",
101                        help='Basic config path in repo prodcutdefine,\
102                                default={$repo}/productdefine/common/base/base_product.json')
103    parser.add_argument('--outdir', type=str, default="./",
104                        help="define output file path dir, default={$current_dir}")
105    args = parser.parse_args()
106    print("read base_product file: ", os.path.abspath(args.base_product))
107    gen_kconfig(args.base_product, args.outdir)
108