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