• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding: utf-8
3
4"""
5Copyright (c) 2021 Huawei Device Co., Ltd.
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18Description: Generate javascript byte code using es2abc
19"""
20
21import os
22import subprocess
23import platform
24import argparse
25
26
27def parse_args():
28    parser = argparse.ArgumentParser()
29    parser.add_argument('--src-js',
30                        help='js source file')
31    parser.add_argument('--dst-file',
32                        help='the converted target file')
33    parser.add_argument('--frontend-tool-path',
34                        help='path of the frontend conversion tool')
35    parser.add_argument('--extension',
36                        help='source file extension')
37    parser.add_argument("--debug", action='store_true',
38                        help='whether add debuginfo')
39    parser.add_argument("--module", action='store_true',
40                        help='whether is module')
41    parser.add_argument("--commonjs", action='store_true',
42                        help='whether is commonjs')
43    parser.add_argument("--merge-abc", action='store_true',
44                        help='whether is merge abc')
45    parser.add_argument("--generate-patch", action='store_true',
46                        help='generate patch abc')
47    parser.add_argument("--dump-symbol-table",
48                        help='dump symbol table of base abc')
49    parser.add_argument("--input-symbol-table",
50                        help='input symbol table for patch abc')
51    parser.add_argument("--target-api-sub-version",
52                        help='input symbol table for patch abc')
53    parser.add_argument("--module-record-field-name",
54                        help='specify the field name of module record in unmerged abc. This argument is optional, ' +
55                             'its value will be the path of input file if not specified')
56    parser.add_argument("--source-file",
57                        help='specify the file path info recorded in generated abc. This argument is optional, ' +
58                             'its value will be the path of input file if not specified')
59    parser.add_argument("--enable-annotations", action='store_true',
60                        help='whether annotations are enabled or not')
61    parser.add_argument("--enable-release-column", action='store_true',
62                        help='enable column number information for bytecode instructions in non-debug mode.')
63
64    parser.add_argument("--enable-ets-implements", action='store_true',
65                        help='whether ets implements are enabled or not'),
66    arguments = parser.parse_args()
67    return arguments
68
69
70def run_command(cmd, execution_path):
71    print(" ".join(cmd) + " | execution_path: " + execution_path)
72    proc = subprocess.Popen(cmd, cwd=execution_path)
73    proc.wait()
74    if proc.returncode != 0:
75        raise subprocess.CalledProcessError(proc.returncode, cmd)
76
77
78def gen_abc_info(input_arguments):
79    frontend_tool_path = input_arguments.frontend_tool_path
80
81    (path, name) = os.path.split(frontend_tool_path)
82
83    cmd = [os.path.join("./", name, "es2abc"),
84           '--output', input_arguments.dst_file,
85           input_arguments.src_js]
86
87    if input_arguments.extension:
88        cmd += ['--extension', input_arguments.extension]
89    if input_arguments.dump_symbol_table:
90        cmd += ['--dump-symbol-table', input_arguments.dump_symbol_table]
91    if input_arguments.input_symbol_table:
92        cmd += ['--input-symbol-table', input_arguments.input_symbol_table]
93    if input_arguments.debug:
94        src_index = cmd.index(input_arguments.src_js)
95        cmd.insert(src_index, '--debug-info')
96    if input_arguments.module:
97        src_index = cmd.index(input_arguments.src_js)
98        cmd.insert(src_index, '--module')
99    if input_arguments.commonjs:
100        src_index = cmd.index(input_arguments.src_js)
101        cmd.insert(src_index, '--commonjs')
102    if input_arguments.merge_abc:
103        src_index = cmd.index(input_arguments.src_js)
104        cmd.insert(src_index, '--merge-abc')
105    if input_arguments.generate_patch:
106        src_index = cmd.index(input_arguments.src_js)
107        cmd.insert(src_index, '--generate-patch')
108    if input_arguments.module_record_field_name:
109        cmd += ["--module-record-field-name", input_arguments.module_record_field_name]
110    if input_arguments.source_file:
111        cmd += ["--source-file", input_arguments.source_file]
112    if input_arguments.enable_annotations:
113        src_index = cmd.index(input_arguments.src_js)
114        cmd.insert(src_index, '--enable-annotations')
115    if input_arguments.enable_release_column:
116        src_index = cmd.index(input_arguments.src_js)
117        cmd.insert(src_index, '--enable-release-column')
118    if input_arguments.enable_ets_implements:
119        src_index = cmd.index(input_arguments.src_js)
120        cmd.insert(src_index, '--enable-ets-implements')
121        # insert d.ts option to cmd later
122    cmd.append("--target-api-sub-version=beta3")
123
124    try:
125        run_command(cmd, path)
126    except subprocess.CalledProcessError as e:
127        exit(e.returncode)
128
129
130if __name__ == '__main__':
131    gen_abc_info(parse_args())
132