1#!/usr/bin/env python3 2# coding=utf-8 3 4''' 5* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 6* Licensed under the Apache License, Version 2.0 (the "License"); 7* you may not use this file except in compliance with the License. 8* You may obtain a copy of the License at 9* 10* http://www.apache.org/licenses/LICENSE-2.0 11* 12* Unless required by applicable law or agreed to in writing, software 13* distributed under the License is distributed on an "AS IS" BASIS, 14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15* See the License for the specific language governing permissions and 16* limitations under the License. 17* 18* Description: .config parser, bases on kconfig format. 19''' 20 21import os 22import sys 23import shutil 24proj_top_dir = os.path.realpath(os.path.join(__file__, '..', '..', '..')) 25print('proj_top_dir:',proj_top_dir) 26sys.path.append(os.path.join(proj_top_dir, 'build')) 27from scripts.scons_env_cfg import SconsEnvCfg as EnvCfg 28from scripts.scons_utils import flag 29from scripts.scons_utils import flag_r 30from scripts.common_env import get_fixed_config 31from scripts.common_env import get_make_str 32from scripts import scons_app 33 34def mkdir(path): 35 isExists=os.path.exists(path) 36 if not isExists: 37 os.makedirs(path) 38 print ('%s create success'% path) 39 return True 40 else: 41 return False 42 43def create_output_dirs(env_cfg): 44 dir_list = [env_cfg.bin_path, env_cfg.log_path, env_cfg.cache_path, env_cfg.link_path] 45 #env_cfg.obj_path, env_cfg.lib_path, 46 [mkdir(x) for x in dir_list] 47 48def transform_env_to_make(env_cfg, module = 'common'): 49 fixed_env_cfg = get_fixed_config('env_cfg') 50 line = '' 51 for flg in flag: 52 flg_cfg = None 53 if flg != 'CPPPATH': 54 flg_cfg = fixed_env_cfg[flag[flg]][module] if module in fixed_env_cfg[flag[flg]] else None 55 else: 56 cfg_list = [] 57 common_inc = fixed_env_cfg['common_inc_path'] 58 inc_path = common_inc[module] if module in common_inc else None 59 if inc_path is not None: 60 for x in inc_path: 61 if x.startswith('#'): 62 cfg_list.append(x[2:]) 63 else: 64 cfg_list.append(x) 65 os_inc = fixed_env_cfg['liteos_inc_path'] 66 inc_path = os_inc[module] if module in os_inc else None 67 if inc_path is not None: 68 cfg_list.extend([os.path.join(os.path.relpath(env_cfg.os_root, env_cfg.root), y) for y in inc_path]) 69 flg_cfg = cfg_list 70 71 if flg_cfg is None: 72 continue 73 if flg == 'CPPDEFINES': 74 defines = ['-D%s'%x for x in flg_cfg if isinstance(x, str)] 75 defines.extend(['-D%s=%s'%x for x in flg_cfg if isinstance(x, tuple)]) 76 line = '%sDEFINES := %s\n'%(line, ' \\\n\t'.join(defines)) 77 elif flg == 'CPPPATH': 78 line = '%sINCLUDE := -I$(MAIN_TOPDIR)/%s\n'%(line, ' \\\n\t-I$(MAIN_TOPDIR)/'.join(flg_cfg)) 79 elif flg == 'LINK_SCRIPTS_FLAG': 80 line = '\n'.join([ 81 line, 82 '%s := -I$(MAIN_TOPDIR)/%s'%(flg, ' -I$(MAIN_TOPDIR)/'.join([x[2:] for x in flg_cfg if x.startswith('-I')])), 83 '%s += %s'%(flg, ' '.join([x for x in flg_cfg if not x.startswith('-I')])) 84 ]) 85 else: 86 line = '%s%s := %s\n'%(line, flg, ' '.join(flg_cfg)) 87 return line 88 89def prepare_config_file(env_cfg): 90 config_file = os.path.join(proj_top_dir, 'build', 'make_scripts', 'config.mk') 91 with open(config_file, 'w+') as fp: 92 environs = env_cfg.get_makefile_environs() 93 var = environs['var'] 94 append_var = environs['append_var'] 95 mod_dir = environs['mod_dir'] 96 modules = get_fixed_config('module') 97 lib_cfg = get_fixed_config('lib_cfg') 98 #env_cfg = get_fixed_config('env_cfg') 99 line = '\n'.join(['include $(MAIN_TOPDIR)/build/config/sdk.mk', 100 'include $(MAIN_TOPDIR)/build/make_scripts/usr.mk', 101 'SHELL = /bin/bash', 102 'COMPILE_MODULE += %s\n'%(' '.join(modules))]) 103 for item in mod_dir: 104 line = '%s%s := %s\n'%(line, item, mod_dir[item]) 105 106 for module_name in lib_cfg: 107 libs = list(lib_cfg[module_name].keys()) 108 line = '%s%s_libs := %s\n'%(line, module_name, ' '.join(libs)) 109 110 for item in var: 111 line = '%s%s := %s\n'%(line, item, var[item]) 112 113 #for item in append_var: 114 #line = '%s%s += %s\n'%(line, item, append_var[item]) 115 116 line = '\n'.join([line, 117 'LIBS += $(patsubst lib%.a, -l%, $(notdir $(wildcard $(MAIN_TOPDIR)/build/libs/*.a)))', 118 '\n'.join(['%s += %s'%(item, append_var[item]) for item in append_var]), 119 'ifeq ($(USR_APP_ON), n)', 120 'COMPILE_MODULE += $(APP_NAME)', 121 '$(APP_NAME)_dir := app/$(APP_NAME)', 122 '$(APP_NAME)_libs := $(APP_NAME)', 123 'endif', 124 'LIBS += $(USR_LIBS)\n']) 125 #line = '\n'.join([line, 126 #'LIBS += $(patsubst lib%.a, -l%, $(notdir $(wildcard $(MAIN_TOPDIR)/build/libs/*.a)))']) 127 line = '\n'.join([line, 128 'PYTHON_SCRIPTS = y', 129 'ifeq ($(PYTHON_SCRIPTS),y)', 130 'NV_TOOL := python3 $(MAIN_TOPDIR)/tools/nvtool/build_nv.py', 131 'OTA_TOOL := python3 $(MAIN_TOPDIR)/build/scripts/pkt_builder.py', 132 'else', 133 'NV_TOOL := cd $(MAIN_TOPDIR)/tools/nvtool;\\', 134 ' $(MAIN_TOPDIR)/tools/nvtool/nv_builder', 135 'OTA_TOOL := cd $(MAIN_TOPDIR)/build/scripts;\\', 136 ' $(MAIN_TOPDIR)/build/scripts/ota_builder', 137 'endif\n\n']) 138 139 line = '\n'.join([line, 140 transform_env_to_make(env_cfg = env_cfg), 141 get_make_str(), 142 'ifeq ($(FACTORY_MODE), y)', 143 'DEFINES += -DCONFIG_FACTORY_TEST_MODE', 144 'endif', 145 'LIBS += $(addprefix -l, $(foreach module, $(COMPILE_MODULE), $($(module)_libs)))', 146 'ifeq ($(CONFIG_CE_SUPPORT), y)', 147 '\tNV_CFG_NAME = mss_nvi_db.xml', 148 'endif', 149 'ifeq ($(CONFIG_FCC_SUPPORT), y)', 150 '\tNV_CFG_NAME = mss_nvi_db_fcc.xml', 151 'endif', 152 'ifeq ($(CONFIG_MAX_POWER_SUPPORT), y)', 153 '\tNV_CFG_NAME = mss_nvi_db_max.xml', 154 'endif', 155 'CCFLAGS += $(DEFINES) $(INCLUDE)\n']) 156 print('+'*20,'config.mk','+'*20,'\n',line) 157 fp.write(line) 158# 159def prepare_module_makefile(env_cfg): 160 modules = env_cfg.get_all_modules() 161 for x in modules: 162 dst_dir = os.path.join(proj_top_dir, env_cfg.get_module_dir(x)) 163 dst_makefile = os.path.join(dst_dir, 'Makefile') 164 print("---------dst_dir: %s proj_top_dir :%s -"%(dst_dir, proj_top_dir)) 165 module_mk = os.path.join(dst_dir, 'module.mk') 166 if not os.path.exists(module_mk): 167 src_makefile = os.path.join(proj_top_dir, 'build', 'make_scripts', 'module.mk') 168 shutil.copyfile(src_makefile, dst_makefile) 169 else: 170 shutil.move(module_mk, dst_makefile) 171# 172def prepare_module_lib_cfg(env_cfg, module): 173 src_makefile = os.path.join(proj_top_dir, env_cfg.get_module_dir(module), 'module_config.mk') 174 print('module:',module) 175 if os.path.exists(src_makefile): 176 return 177 178 with open(src_makefile, 'w+') as fp: 179 lib_cfg = get_fixed_config('lib_cfg')[module] 180 line = '' 181 for lib_name in lib_cfg: 182 line = '%s%s_srcs := %s\n'%(line, lib_name, ' '.join(lib_cfg[lib_name])) 183 184 #transform_env_to_make(env_cfg, module) 185 fixed_env_cfg = get_fixed_config('env_cfg') 186 for flg in flag: 187 #flg_cfg = env_cfg.get_module_cfg(module, flg, fixed=True) 188 flg_cfg = None 189 if flg != 'CPPPATH': 190 flg_cfg = fixed_env_cfg[flag[flg]][module] if module in fixed_env_cfg[flag[flg]] else None 191 else: 192 cfg_list = [] 193 common_inc = fixed_env_cfg['common_inc_path'] 194 inc_path = common_inc[module] if module in common_inc else None 195 if inc_path is not None: 196 for x in inc_path: 197 if x.startswith('#'): 198 cfg_list.append(x[2:]) 199 else: 200 cfg_list.append(x) 201 os_inc = fixed_env_cfg['liteos_inc_path'] 202 inc_path = os_inc[module] if module in os_inc else None 203 if inc_path is not None: 204 cfg_list.extend([os.path.join(os.path.relpath(env_cfg.os_root, env_cfg.root), y) for y in inc_path]) 205 flg_cfg = cfg_list 206 207 if flg_cfg is None: 208 continue 209 if flg == 'CPPDEFINES': 210 defines = ['-D%s'%x for x in flg_cfg if isinstance(x, str)] 211 defines.extend(['-D%s=%s'%x for x in flg_cfg if isinstance(x, tuple)]) 212 line = '%sCCFLAGS += %s\n'%(line, ' '.join(defines)) 213 elif flg == 'CPPPATH': 214 line = '%sCCFLAGS += -I$(MAIN_TOPDIR)/%s\n'%(line, ' \\\n\t-I$(MAIN_TOPDIR)/'.join(flg_cfg)) 215 else: 216 line = '%s%s += %s\n'%(line, flg, ' '.join(flg_cfg)) 217 218 module_cfg = get_make_str(module) 219 line = '%s%s\n'%(line, module_cfg) if module_cfg is not None else line 220 print('+'*10, ' %s module config'%module, '+'*10, '\n', line) 221 fp.write(line) 222# 223def do_prepare(env_cfg): 224 create_output_dirs(env_cfg) 225 print("------------------------------------env_cfg1: %s"%(env_cfg)) 226 prepare_config_file(env_cfg) 227 print("------------------------------------env_cfg2: %s"%(env_cfg)) 228 prepare_module_makefile(env_cfg) 229 print("------------------------------------env_cfg3: %s"%(env_cfg)) 230 [prepare_module_lib_cfg(env_cfg, module) for module in env_cfg.get_all_modules()] 231 232 233 234if __name__ == "__main__": 235 args = len(sys.argv) 236 print('+'*40, 'PREPARE', '+'*40) 237 env_cfg = EnvCfg() 238 #app_builder = scons_app.AppTarget('demo') 239 #env_cfg.set_app_builder(app_builder) 240 do_prepare(env_cfg) 241 242 #print('common str','+'*50) 243 #print(get_make_str()) 244 #print('+'*50) 245