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 21 22KCONFIG_STR = 'config {}\n\ 23 bool "{}"\n\ 24 default n\n' 25PROPERTIES_STR = 'config {}\n\ 26 string "{}"\n\ 27 default ""\n' 28KMENU_STR = 'menu "{}"\n' 29 30FEATURE_STR = 'config feature$$%s\n\ 31 string "feature"\n\ 32 default ""\n\ 33 depends on %s\n' 34 35 36def create_config(name: str, comment: str): 37 return KCONFIG_STR.format(name, comment) 38 39 40def create_property(name: str, comment: str): 41 return PROPERTIES_STR.format(name, comment) 42 43 44def create_menu(name: str): 45 return KMENU_STR.format(name) 46 47 48def end_menu(): 49 return "endmenu\n" 50 51 52def create_feature(name: str): 53 return FEATURE_STR % (name, name) 54 55 56def read_json(file: str): 57 data = {} 58 with open(file, "rb") as f: 59 data = json.load(f) 60 return data 61 62 63def write_kconfig(result: str, outdir: str): 64 outpath = os.path.join(outdir, "kconfig") 65 with open(outpath, "w") as f: 66 f.writelines(result) 67 print("output file in: ", os.path.abspath(outpath)) 68 69 70def gen_kconfig(config_path: str, outdir: str): 71 data = read_json(config_path) 72 subsystems = data.get("subsystems") 73 result = 'mainmenu "Subsystem Component Kconfig Configuration"\n' 74 for prop, _ in data.items(): 75 if prop == "subsystems": 76 continue 77 result += create_property("property$${}".format(prop), prop) 78 79 for subsys_dict in subsystems: 80 subsys_name = subsys_dict.get("subsystem") 81 result += create_menu(subsys_name) 82 for component_dict in subsys_dict.get("components"): 83 component_name = component_dict.get("component") 84 result += create_config("{}$${}".format(subsys_name, component_name), component_name) 85 result += create_feature("{}$${}".format(subsys_name, component_name)) 86 result += end_menu() 87 write_kconfig(result, outdir) 88 89 90if __name__ == "__main__": 91 INTRO = 'Genenrate newly kconfig input file.\n\ 92 For example: python3 generate_kconfig.py\n\ 93 or python3 generate_kconfig.py --base_product={$repo}/productdefine/common/base/base_product.json --outdir=./' 94 parser = argparse.ArgumentParser( 95 formatter_class=argparse.RawDescriptionHelpFormatter, 96 description=INTRO 97 ) 98 parser.add_argument('--base_product', type=str, default="./../../../productdefine/common/base/base_product.json", 99 help='Basic config path in repo prodcutdefine,\ 100 default={$repo}/productdefine/common/base/base_product.json') 101 parser.add_argument('--outdir', type=str, default="./", 102 help="define output file path dir, default={$current_dir}") 103 args = parser.parse_args() 104 print("read base_product file: ", os.path.abspath(args.base_product)) 105 gen_kconfig(args.base_product, args.outdir) 106