1#!/usr/bin/env python3 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 18import sys 19import shutil 20from utils.build_utils import CopyModule, output_root, root_path 21from enviroment import TargetEnvironment, BuildEnvironment, chip_copy_target 22 23def find_copy_target(target_name): 24 for chip in chip_copy_target: 25 if target_name in chip_copy_target[chip]: 26 print(target_name, chip) 27 return chip 28 return None 29 30class packTool: 31 def __init__(self, pack_name, target_name): 32 self.pack_name = pack_name 33 self.target_name = target_name 34 self.pack_suffix = ['bin', 'elf', 'lst', 'asm', 'nm', 'map', 'mem', 'hex', 'info', 'fwpkg', 'ko'] 35 self.pack_dir = ['parse_tool'] 36 self.pack_bin_suffix = ['bin'] 37 38 def __replace_copy_target_path(self, path): 39 path = path.replace("<root>", root_path) 40 path = path.replace("<out_root>", output_root) 41 path = path.replace("<pack_target>", self.pack_name) 42 return path 43 44 def __pack_copy_target__(self, chip, copy_target): 45 for item in copy_target: 46 src = self.__replace_copy_target_path(item['src']) 47 dst = self.__replace_copy_target_path(item['dst']) 48 if not dst and os.path.exists(src): 49 print("remove %s" % src) 50 if os.path.isfile(src): 51 os.remove(src) 52 elif os.path.isdir(src): 53 shutil.rmtree(src) 54 continue 55 print("copy %s -> %s" % (src, dst)) 56 c = CopyModule(copy_header = False) 57 c.copy(src, dst) 58 59 def pack(self): 60 chip = find_copy_target(self.target_name) 61 if chip is not None: 62 self.__pack_copy_target__(chip, chip_copy_target[chip][self.target_name]) 63 return 64 target_env = TargetEnvironment(self.target_name) 65 chip = target_env.get('chip') 66 output_path = target_env.get_output_path() 67 pack_path = os.path.join(output_root, 'package', chip, self.pack_name) 68 pack_target_path = os.path.join(pack_path, self.target_name) 69 pack_bin_path = pack_path 70 print("Start pack target %s" % self.target_name) 71 if not os.path.exists(pack_target_path): 72 os.makedirs(pack_target_path) 73 for file_name in os.listdir(output_path): 74 ext_name = os.path.splitext(file_name)[-1][1:] 75 if ext_name not in self.pack_suffix: 76 continue 77 src = os.path.join(output_path, file_name) 78 dst = os.path.join(pack_target_path, file_name) 79 shutil.copy(src, dst) 80 print("copy %s -> %s" % (src, dst)) 81 if ext_name not in self.pack_bin_suffix: 82 continue 83 bin_dst = os.path.join(pack_bin_path, file_name) 84 print("copy %s -> %s" % (src, bin_dst)) 85 shutil.copy(src, bin_dst) 86 87 for file_name in os.listdir(output_path): 88 if file_name not in self.pack_dir: 89 continue 90 src = os.path.join(output_path, file_name) 91 dst = os.path.join(pack_target_path, file_name) 92 shutil.copytree(src, dst) 93 print("copytree %s -> %s" % (src, dst))