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