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 38 39def main(): 40 parser = argparse.ArgumentParser( 41 description="Translate and copy data file to object file" 42 ) 43 parser.add_argument( 44 "-e", "--objcopy", type=str, required=True, help="The path of objcopy" 45 ) 46 parser.add_argument( 47 "-a", "--arch", type=str, required=True, help="The architecture of target" 48 ) 49 parser.add_argument( 50 "-i", "--input", type=str, required=True, help="The path of input file" 51 ) 52 parser.add_argument( 53 "-o", "--output", type=str, required=True, help="The path of output target" 54 ) 55 56 args = parser.parse_args() 57 input_dir, input_file = os.path.split(args.input) 58 59 cmd = [ 60 args.objcopy, 61 "-I", 62 "binary", 63 "-B", 64 BUILD_ID_LINK_OUTPUT[args.arch], 65 "-O", 66 OUTPUT_TARGET[args.arch], 67 input_file, 68 args.output, 69 ] 70 71 process = subprocess.Popen( 72 cmd, 73 stdout=subprocess.PIPE, 74 stderr=subprocess.STDOUT, 75 universal_newlines=True, 76 cwd=input_dir, 77 ) 78 for line in iter(process.stdout.readline, ""): 79 sys.stdout.write(line) 80 sys.stdout.flush() 81 82 process.wait() 83 ret_code = process.returncode 84 85 return ret_code 86 87 88if __name__ == "__main__": 89 sys.exit(main()) 90