123b3eb3cSopenharmony_ci#!/usr/bin/env python3
223b3eb3cSopenharmony_ci# -*- coding: utf-8 -*-
323b3eb3cSopenharmony_ci
423b3eb3cSopenharmony_ci#
523b3eb3cSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
623b3eb3cSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
723b3eb3cSopenharmony_ci# you may not use this file except in compliance with the License.
823b3eb3cSopenharmony_ci# You may obtain a copy of the License at
923b3eb3cSopenharmony_ci#
1023b3eb3cSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
1123b3eb3cSopenharmony_ci#
1223b3eb3cSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1323b3eb3cSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1423b3eb3cSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1523b3eb3cSopenharmony_ci# See the License for the specific language governing permissions and
1623b3eb3cSopenharmony_ci# limitations under the License.
1723b3eb3cSopenharmony_ci#
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ciimport argparse
2023b3eb3cSopenharmony_ciimport os
2123b3eb3cSopenharmony_ciimport subprocess
2223b3eb3cSopenharmony_ciimport sys
2323b3eb3cSopenharmony_ci
2423b3eb3cSopenharmony_ciOUTPUT_TARGET = {
2523b3eb3cSopenharmony_ci    "x86": "elf32-i386",
2623b3eb3cSopenharmony_ci    "x64": "elf64-x86-64",
2723b3eb3cSopenharmony_ci    "x86_64": "pe-x86-64",
2823b3eb3cSopenharmony_ci    "arm": "elf32-littlearm",
2923b3eb3cSopenharmony_ci    "arm64": "elf64-littleaarch64",
3023b3eb3cSopenharmony_ci}
3123b3eb3cSopenharmony_ci
3223b3eb3cSopenharmony_ciBUILD_ID_LINK_OUTPUT = {
3323b3eb3cSopenharmony_ci    "x86": "i386",
3423b3eb3cSopenharmony_ci    "x64": "i386:x86-64",
3523b3eb3cSopenharmony_ci    "x86_64": "i386:x86-64",
3623b3eb3cSopenharmony_ci    "arm": "arm",
3723b3eb3cSopenharmony_ci    "arm64": "aarch64",
3823b3eb3cSopenharmony_ci}
3923b3eb3cSopenharmony_ci
4023b3eb3cSopenharmony_ci
4123b3eb3cSopenharmony_cidef main():
4223b3eb3cSopenharmony_ci    parser = argparse.ArgumentParser(
4323b3eb3cSopenharmony_ci        description="Translate and copy data file to object file"
4423b3eb3cSopenharmony_ci    )
4523b3eb3cSopenharmony_ci    parser.add_argument(
4623b3eb3cSopenharmony_ci        "-e", "--objcopy", type=str, required=True, help="The path of objcopy"
4723b3eb3cSopenharmony_ci    )
4823b3eb3cSopenharmony_ci    parser.add_argument(
4923b3eb3cSopenharmony_ci        "-a", "--arch", type=str, required=True, help="The architecture of target"
5023b3eb3cSopenharmony_ci    )
5123b3eb3cSopenharmony_ci    parser.add_argument(
5223b3eb3cSopenharmony_ci        "-i", "--input", type=str, required=True, help="The path of input file"
5323b3eb3cSopenharmony_ci    )
5423b3eb3cSopenharmony_ci    parser.add_argument(
5523b3eb3cSopenharmony_ci        "-o", "--output", type=str, required=True, help="The path of output target"
5623b3eb3cSopenharmony_ci    )
5723b3eb3cSopenharmony_ci
5823b3eb3cSopenharmony_ci    args = parser.parse_args()
5923b3eb3cSopenharmony_ci    input_dir, input_file = os.path.split(args.input)
6023b3eb3cSopenharmony_ci
6123b3eb3cSopenharmony_ci    cmd = [
6223b3eb3cSopenharmony_ci        args.objcopy,
6323b3eb3cSopenharmony_ci        "-I",
6423b3eb3cSopenharmony_ci        "binary",
6523b3eb3cSopenharmony_ci        "-B",
6623b3eb3cSopenharmony_ci        BUILD_ID_LINK_OUTPUT[args.arch],
6723b3eb3cSopenharmony_ci        "-O",
6823b3eb3cSopenharmony_ci        OUTPUT_TARGET[args.arch],
6923b3eb3cSopenharmony_ci        input_file,
7023b3eb3cSopenharmony_ci        args.output,
7123b3eb3cSopenharmony_ci    ]
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_ci    process = subprocess.Popen(
7423b3eb3cSopenharmony_ci        cmd,
7523b3eb3cSopenharmony_ci        stdout=subprocess.PIPE,
7623b3eb3cSopenharmony_ci        stderr=subprocess.STDOUT,
7723b3eb3cSopenharmony_ci        universal_newlines=True,
7823b3eb3cSopenharmony_ci        cwd=input_dir,
7923b3eb3cSopenharmony_ci    )
8023b3eb3cSopenharmony_ci    for line in iter(process.stdout.readline, ""):
8123b3eb3cSopenharmony_ci        sys.stdout.write(line)
8223b3eb3cSopenharmony_ci        sys.stdout.flush()
8323b3eb3cSopenharmony_ci
8423b3eb3cSopenharmony_ci    process.wait()
8523b3eb3cSopenharmony_ci    ret_code = process.returncode
8623b3eb3cSopenharmony_ci
8723b3eb3cSopenharmony_ci    return ret_code
8823b3eb3cSopenharmony_ci
8923b3eb3cSopenharmony_ci
9023b3eb3cSopenharmony_ciif __name__ == "__main__":
9123b3eb3cSopenharmony_ci    sys.exit(main())
92