1#!/usr/bin/env python 2# -*- coding: UTF-8 -*- 3 4# Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import os, sys, shutil 18import argparse 19from kconfiglib import Kconfig 20from menuconfig import menuconfig 21 22root_path = os.path.join(os.path.dirname(os.path.abspath(__file__)) + os.path.sep + ".", "..", "..") 23 24def mconf_set_env(style, conf, config_header, autoheader_header, output_path = None): 25 """ 26 Set Kconfig Env 27 """ 28 os.environ["MENUCONFIG_STYLE"] = style 29 os.environ["KCONFIG_CONFIG"] = conf 30 os.environ["KCONFIG_CONFIG_HEADER"] = config_header 31 os.environ["KCONFIG_AUTOHEADER"] = os.path.join("." if output_path == None else output_path, "mconfig.h") 32 os.environ["KCONFIG_AUTOHEADER_HEADER"] = autoheader_header 33 os.environ["CONFIG_"] = "CONFIG_" 34 35def mconfig(command, chip, core, target, output, assignments = None, root=""): 36 if not root: 37 root = root_path 38 39 kconfig = os.path.join(root, "config.in") 40 print(kconfig) 41 print(os.getcwd()) 42 43 display_style = "default selection=fg:white,bg:blue" 44 target = target.replace('-', "_") 45 target_conf = os.path.join(root, "build", "config", "target_config", chip, "menuconfig", core, "%s.config" % target) 46 config_header = '''# Generated by Kconfig Tool. 47# Note: !!!This file can not be modify manually!!! 48''' 49 autoheader_header = '''/* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. */ 50''' 51 mconf_set_env(display_style, target_conf, config_header, autoheader_header, output) 52 kconf = Kconfig(filename=kconfig) 53 54 if command == 'savemenuconfig': 55 kconf.load_config() 56 print(kconf.write_autoconf()) # save menu config 57 elif command == 'defconfig': 58 kconf.load_allconfig("alldef.config") 59 print(kconf.write_config()) # default config 60 elif command == 'allyesconfig': 61 kconf.warn = False 62 for sym in kconf.unique_defined_syms: 63 sym.set_value(1 if sym.choice else 2) 64 for choice in kconf.unique_choices: 65 choice.set_value(2) 66 kconf.warn = True 67 kconf.load_allconfig("allyes.config") 68 print(kconf.write_config()) # all yes config 69 elif command == 'allnoconfig': 70 kconf.warn = False 71 for sym in kconf.unique_defined_syms: 72 sym.set_value(2 if sym.is_allnoconfig_y else 0) 73 kconf.warn = True 74 kconf.load_allconfig("allno.config") 75 print(kconf.write_config()) # all no config 76 elif command == 'setconfig': 77 kconf.load_config() 78 for arg in args.assignments: 79 if "=" not in arg: 80 sys.exit("error: no '=' in assignment: '{}'".format(arg)) 81 name, value = arg.split("=", 1) 82 83 sym = kconf.syms[name] 84 85 if not sym.set_value(value): 86 sys.exit("error: '{}' is an invalid value for the {} symbol {}" 87 .format(value, kconfiglib.TYPE_TO_STR[sym.orig_type], 88 name)) 89 90 print(kconf.write_config()) 91 elif command == 'reloadconfig': 92 kconf.load_config() 93 print(kconf.write_config()) 94 else: 95 menuconfig(kconf) # menu config 96 97if __name__ == "__main__": 98 parser = argparse.ArgumentParser( 99 formatter_class=argparse.RawDescriptionHelpFormatter, 100 description=__doc__) 101 102 parser.add_argument( 103 "--command", 104 default="menuconfig", 105 choices=["menuconfig", "savemenuconfig", "defconfig", "allyesconfig", "allnoconfig", "setconfig", "openproject"], 106 help="command to be used") 107 108 parser.add_argument( 109 "--chip", 110 default="bs25", 111 help="chips: bs25/bs21/ws63/ws53 ...") 112 113 parser.add_argument( 114 "--core", 115 default="acore", 116 help="cores: acore/bcore/ccore ...") 117 118 parser.add_argument( 119 "--target", 120 default="standard-bs25-app-evb", 121 help="targets: standard-bs25-app-evb ...") 122 123 parser.add_argument( 124 "--output", 125 help="The path of the mconfig.h file will be stored.") 126 127 parser.add_argument( 128 "assignments", 129 metavar="ASSIGNMENT", 130 nargs="*", 131 help="A 'NAME=value' assignment") 132 133 args = parser.parse_args() 134 135 if args.command == "openproject": 136 if os.path.exists("board.json"): 137 if os.path.exists(root_path + "/board.json"): 138 root_path + "/board.json" 139 pass 140 shutil.copyfile("board.json", root_path + "/board.json") 141 142 current_path = os.getcwd() 143 current_sample = "SAMPLE_SUPPORT_" + os.path.basename(current_path).upper() 144 os.chdir(root_path) 145 args.command = "setconfig" 146 args.assignments.clear() 147 args.assignments.append("SAMPLE_ENABLE=n") 148 mconfig(args.command, args.chip, args.core, args.target, args.output, args.assignments, root_path) 149 args.assignments.clear() 150 args.assignments.append("SAMPLE_ENABLE=y") 151 if "peripheral" in current_path: 152 args.assignments.append("ENABLE_PERIPHERAL_SAMPLE=y") 153 elif "products" in current_path: 154 args.assignments.append("ENABLE_PRODUCTS_SAMPLE=y") 155 else: 156 print("Not a valid sample directory") 157 exit(1) 158 args.assignments.append(current_sample + "=y") 159 160 161 162 mconfig(args.command, args.chip, args.core, args.target, args.output, args.assignments, root_path)