1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2023 Huawei Device Co., Ltd. 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 19import argparse 20import os 21import subprocess 22import sys 23 24OUTPUT_TARGET = { 25 'x86': 'elf32-i386', 26 'x86_64': 'elf64-x86-64', 27 'arm': 'elf32-littlearm', 28 'arm64': 'elf64-littleaarch64', 29} 30 31BUILD_ID_LINK_OUTPUT = { 32 'x86': 'i386', 33 'x86_64': 'i386:x86-64', 34 'arm': 'arm', 35 'arm64': 'aarch64', 36} 37 38def main(): 39 parser = argparse.ArgumentParser(description='Translate and copy data file to object file') 40 parser.add_argument('-e', '--objcopy', type=str, required=True, help='The path of objcopy') 41 parser.add_argument('-a', '--arch', type=str, required=True, help='The architecture of target') 42 parser.add_argument('-i', '--input', type=str, required=True, help='The path of input file') 43 parser.add_argument('-o', '--output', type=str, required=True, help='The path of output target') 44 45 args = parser.parse_args() 46 input_dir, input_file = os.path.split(args.input) 47 48 cmd = [ args.objcopy, '-I', 'binary', '-B', BUILD_ID_LINK_OUTPUT[args.arch], '-O', OUTPUT_TARGET[args.arch], 49 input_file, args.output] 50 51 process = subprocess.Popen(cmd, 52 stdout=subprocess.PIPE, 53 stderr=subprocess.STDOUT, 54 universal_newlines=True, 55 cwd=input_dir) 56 for line in iter(process.stdout.readline, ''): 57 sys.stdout.write(line) 58 sys.stdout.flush() 59 60 process.wait() 61 ret_code = process.returncode 62 63 return ret_code 64 65if __name__ == '__main__': 66 sys.exit(main()) 67