• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2020-2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import getopt
18import sys
19
20HMF_ACE_BASE_PATH = os.path.join("..", "..")
21FRAMEWORK_SNAPSHOT_FILE_PATH = os.path.join(
22    HMF_ACE_BASE_PATH, "packages", "runtime-core",
23    "build", "framework.min.bc")
24SNAPSHOT_OUTPUT_C_FILE_PATH = os.path.join(
25    HMF_ACE_BASE_PATH, "src", "core", "base", "framework_min_bc.h")
26
27FRAMEWORK_JS_FILE_PATH = os.path.join(
28    HMF_ACE_BASE_PATH, "packages", "runtime-core",
29    "build", "framework.min.js")
30JS_OUTPUT_C_FILE_PATH = os.path.join(
31    HMF_ACE_BASE_PATH, "src", "core", "base", "framework_min_js.h")
32
33
34def output_copyright(output):
35    output.write("/*\n")
36    output.write(" * Copyright (c) 2020-2023 Huawei Device Co., Ltd.\n")
37    output.write(" * Licensed under the Apache License, Version 2.0")
38    output.write(" (the \"License\");\n")
39    output.write(" * you may not use this file except in compliance ")
40    output.write("with the License.\n")
41    output.write(" * You may obtain a copy of the License at\n")
42    output.write(" *\n")
43    output.write(" *     http://www.apache.org/licenses/LICENSE-2.0\n")
44    output.write(" *\n")
45    output.write(" * Unless required by applicable law or agreed to in ")
46    output.write("writing, software\n")
47    output.write(" * distributed under the License is distributed on an ")
48    output.write("\"AS IS\" BASIS,\n")
49    output.write(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either ")
50    output.write("express or implied.\n")
51    output.write(" * See the License for the specific language governing ")
52    output.write("permissions and\n")
53    output.write(" * limitations under the License.\n")
54    output.write(" */\n")
55    output.write("\n")
56
57
58def output_check_notes(output):
59    output.write("// AUTO GENERATED, PLEASE DO NOT EDIT DIRECTLY\n")
60    output.write("#include <stdint.h>\n\n")
61    output.write("#ifndef ACELITE_FRAMEWORK_RAW_BUFFER\n")
62    output.write("#error THIS FILE CAN ONLY BE INCLUDED BY RAW BUFFER CPP\n")
63    output.write("#endif\n\n")
64
65
66def convert_bc():
67    with open(FRAMEWORK_SNAPSHOT_FILE_PATH, 'rb') as input_file:
68        byte_code_buffer = input_file.read()
69        with open(SNAPSHOT_OUTPUT_C_FILE_PATH, 'w') as output:
70            output_copyright(output)
71            output.write("#ifndef OHOS_ACELITE_FRAMEWORK_MIN_BC_H\n")
72            output.write("#define OHOS_ACELITE_FRAMEWORK_MIN_BC_H\n")
73            output.write("\n")
74            output_check_notes(output)
75            output.write(
76                "#ifndef OHOS_ACELITE_FRAMEWORK_MIN_SNAPSHOT_BUFFER\n")
77            output.write(
78                "#define OHOS_ACELITE_FRAMEWORK_MIN_SNAPSHOT_BUFFER\n")
79            output.write("const uint8_t g_frameworkBCBuffer[] =\n{\n    ")
80            index = 1
81            max_count = len(byte_code_buffer)
82            for data in byte_code_buffer:
83                hex_string = '0x%02x' % data
84                final_hex_string = hex_string
85                if index != max_count:
86                    if index % 16 == 0:
87                        final_hex_string = '%s,' % hex_string
88                    else:
89                        final_hex_string = '%s, ' % hex_string
90                    output.write(final_hex_string)
91                    if index % 16 == 0:
92                        output.write("\n    ")
93                else:
94                    output_final_string = '%s\n' % final_hex_string
95                    output.write(output_final_string)
96                index = index + 1
97            output.write("};\n")
98            output.write("#endif\n")
99            output.write("#endif // OHOS_ACELITE_FRAMEWORK_MIN_BC_H")
100
101
102def convert_js():
103    with open(FRAMEWORK_JS_FILE_PATH, 'r') as input_file:
104        javascript_buffer = input_file.read()
105        with open(JS_OUTPUT_C_FILE_PATH, 'w') as output:
106            output_copyright(output)
107            output.write("#ifndef OHOS_ACELITE_FRAMEWORK_MIN_JS_H\n")
108            output.write("#define OHOS_ACELITE_FRAMEWORK_MIN_JS_H\n")
109            output.write("\n")
110            output_check_notes(output)
111            output.write(
112                "#ifndef OHOS_ACELITE_FRAMEWORK_MIN_JS_BUFFER\n")
113            output.write(
114                "#define OHOS_ACELITE_FRAMEWORK_MIN_JS_BUFFER\n")
115            output.write(
116                "const char * const g_frameworkJSBuffer =\n    \"")
117            max_count = len(javascript_buffer)
118            index = 0
119            for data in javascript_buffer:
120                cha = data
121                if cha == '\"':
122                    cha = '\''
123                if cha == '\n':
124                    continue
125                final_string = '%c' % cha
126                if index != (max_count - 1):
127                    if (index != 0 and index % 90 == 0):
128                        final_string = '%c\"' % cha
129                    output.write(final_string)
130                    if (index != 0 and index % 90 == 0):
131                        output.write("\n    \"")
132                else:
133                    output.write(final_string)
134                index = index + 1
135            output.write("\";\n")
136            output.write("#endif\n")
137            output.write("#endif // OHOS_ACELITE_FRAMEWORK_MIN_JS_H")
138
139
140def usage():
141    print("  > use default input path: python framework2char.py")
142    print("  > use specific input path: "
143          "python framework2char.py -b framework.min.bc -j framework.min.js")
144    print("    > -b : the input snapshot file")
145    print("    > -j : the input javascript file")
146
147
148if __name__ == '__main__':
149    options, arguments = getopt.getopt(
150        sys.argv[1:], '-h-b-j:', ['help', 'bc=', 'js='])
151    for option, value in options:
152        if option in ("-h", "--help"):
153            usage()
154            sys.exit()
155        if option in ("-b", "--bc"):
156            FRAMEWORK_SNAPSHOT_FILE_PATH = value
157        if option in ("-j", "--js"):
158            FRAMEWORK_JS_FILE_PATH = value
159    if (os.path.exists(os.path.abspath(FRAMEWORK_SNAPSHOT_FILE_PATH))
160        and os.path.exists(os.path.abspath(FRAMEWORK_JS_FILE_PATH))):
161        convert_js()
162        convert_bc()
163    else:
164        print("[Error]: framework.min.bc/.js must be prepared")
165