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