123b3eb3cSopenharmony_ci#!/usr/bin/env python
223b3eb3cSopenharmony_ci# -*- coding: utf-8 -*-
323b3eb3cSopenharmony_ci# Copyright (c) 2020 Huawei Device Co., Ltd.
423b3eb3cSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
523b3eb3cSopenharmony_ci# you may not use this file except in compliance with the License.
623b3eb3cSopenharmony_ci# You may obtain a copy of the License at
723b3eb3cSopenharmony_ci#
823b3eb3cSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
923b3eb3cSopenharmony_ci#
1023b3eb3cSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1123b3eb3cSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1223b3eb3cSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1323b3eb3cSopenharmony_ci# See the License for the specific language governing permissions and
1423b3eb3cSopenharmony_ci# limitations under the License.
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ciimport argparse
1723b3eb3cSopenharmony_ciimport os
1823b3eb3cSopenharmony_ciimport sys
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_cidef resource_file_to_bytecode(input_dir, input_file, output_path):
2223b3eb3cSopenharmony_ci    with open(os.path.join(input_dir, input_file), 'rb')\
2323b3eb3cSopenharmony_ci            as resource_file_object:
2423b3eb3cSopenharmony_ci        with open(output_path, 'a') as cpp_file_object:
2523b3eb3cSopenharmony_ci            length = 0;
2623b3eb3cSopenharmony_ci            all_the_content = resource_file_object.read();
2723b3eb3cSopenharmony_ci            template0 = "#include <stdint.h>\n";
2823b3eb3cSopenharmony_ci            template1 = "const uint8_t  _binary_$1_start[$2] = {$3};\n";
2923b3eb3cSopenharmony_ci            template2 = \
3023b3eb3cSopenharmony_ci                "const uint8_t* _binary_$1_end = _binary_$1_start + $2;";
3123b3eb3cSopenharmony_ci
3223b3eb3cSopenharmony_ci            formats = ","
3323b3eb3cSopenharmony_ci            seq = []
3423b3eb3cSopenharmony_ci            for content in all_the_content:
3523b3eb3cSopenharmony_ci                seq.append(str(hex(content)))
3623b3eb3cSopenharmony_ci                length = length + 1
3723b3eb3cSopenharmony_ci            byte_code = formats.join(seq);
3823b3eb3cSopenharmony_ci            input_file = input_file.replace(".", "_")
3923b3eb3cSopenharmony_ci            template1 = template1.replace("$1", str(input_file)) \
4023b3eb3cSopenharmony_ci                .replace("$2", str(length)) \
4123b3eb3cSopenharmony_ci                .replace("$3", str(byte_code))
4223b3eb3cSopenharmony_ci            template2 = template2.replace("$1", str(input_file)) \
4323b3eb3cSopenharmony_ci                .replace("$2", str(length))
4423b3eb3cSopenharmony_ci            cpp_file_object.seek(0)
4523b3eb3cSopenharmony_ci            cpp_file_object.truncate();
4623b3eb3cSopenharmony_ci            cpp_file_object.write(template0 + template1 + template2);
4723b3eb3cSopenharmony_ci
4823b3eb3cSopenharmony_ci
4923b3eb3cSopenharmony_cidef main():
5023b3eb3cSopenharmony_ci    parser = argparse.ArgumentParser()
5123b3eb3cSopenharmony_ci    parser.add_argument('--objcopy', type=str, required=False)
5223b3eb3cSopenharmony_ci    parser.add_argument('--input', type=str, required=True)
5323b3eb3cSopenharmony_ci    parser.add_argument('--output', type=str, required=True)
5423b3eb3cSopenharmony_ci    parser.add_argument('--arch', type=str, required=False)
5523b3eb3cSopenharmony_ci
5623b3eb3cSopenharmony_ci    args = parser.parse_args()
5723b3eb3cSopenharmony_ci
5823b3eb3cSopenharmony_ci    input_dir, input_file = os.path.split(args.input)
5923b3eb3cSopenharmony_ci    output_path = os.path.abspath(args.output)
6023b3eb3cSopenharmony_ci    resource_file_to_bytecode(input_dir, input_file, output_path)
6123b3eb3cSopenharmony_ci
6223b3eb3cSopenharmony_ci
6323b3eb3cSopenharmony_ciif __name__ == '__main__':
6423b3eb3cSopenharmony_ci    sys.exit(main())
65