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 'x64': 'elf64-x86-64', 27 'x86_64': 'pe-x86-64', 28 'arm': 'elf32-littlearm', 29 'arm64': 'elf64-littleaarch64', 30} 31 32BUILD_ID_LINK_OUTPUT = { 33 'x86': 'i386', 34 'x64': 'i386:x86-64', 35 'x86_64': 'i386:x86-64', 36 'arm': 'arm', 37 'arm64': 'aarch64', 38} 39 40def main(): 41 parser = argparse.ArgumentParser(description='Translate and copy data file to object file') 42 parser.add_argument('-e', '--objcopy', type=str, required=True, help='The path of objcopy') 43 parser.add_argument('-a', '--arch', type=str, required=True, help='The architecture of target') 44 parser.add_argument('-i', '--input', type=str, required=True, help='The path of input file') 45 parser.add_argument('-o', '--output', type=str, required=True, help='The path of output target') 46 47 args = parser.parse_args() 48 input_dir, input_file = os.path.split(args.input) 49 50 cmd = [ args.objcopy, '-I', 'binary', '-B', BUILD_ID_LINK_OUTPUT[args.arch], '-O', OUTPUT_TARGET[args.arch], 51 input_file, args.output] 52 53 process = subprocess.Popen(cmd, 54 stdout=subprocess.PIPE, 55 stderr=subprocess.STDOUT, 56 universal_newlines=True, 57 cwd=input_dir) 58 for line in iter(process.stdout.readline, ''): 59 sys.stdout.write(line) 60 sys.stdout.flush() 61 62 process.wait() 63 ret_code = process.returncode 64 65 return ret_code 66 67if __name__ == '__main__': 68 sys.exit(main()) 69