13af6ab5fSopenharmony_ci#!/usr/bin/env python3
23af6ab5fSopenharmony_ci# coding: utf-8
33af6ab5fSopenharmony_ci
43af6ab5fSopenharmony_ci"""
53af6ab5fSopenharmony_ciCopyright (c) 2021 Huawei Device Co., Ltd.
63af6ab5fSopenharmony_ciLicensed under the Apache License, Version 2.0 (the "License");
73af6ab5fSopenharmony_ciyou may not use this file except in compliance with the License.
83af6ab5fSopenharmony_ciYou may obtain a copy of the License at
93af6ab5fSopenharmony_ci
103af6ab5fSopenharmony_ci    http://www.apache.org/licenses/LICENSE-2.0
113af6ab5fSopenharmony_ci
123af6ab5fSopenharmony_ciUnless required by applicable law or agreed to in writing, software
133af6ab5fSopenharmony_cidistributed under the License is distributed on an "AS IS" BASIS,
143af6ab5fSopenharmony_ciWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
153af6ab5fSopenharmony_ciSee the License for the specific language governing permissions and
163af6ab5fSopenharmony_cilimitations under the License.
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ciDescription: Generate javascript byte code using es2abc
193af6ab5fSopenharmony_ci"""
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_ciimport os
223af6ab5fSopenharmony_ciimport subprocess
233af6ab5fSopenharmony_ciimport platform
243af6ab5fSopenharmony_ciimport argparse
253af6ab5fSopenharmony_ci
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_cidef parse_args():
283af6ab5fSopenharmony_ci    parser = argparse.ArgumentParser()
293af6ab5fSopenharmony_ci    parser.add_argument('--src-js',
303af6ab5fSopenharmony_ci                        help='js source file')
313af6ab5fSopenharmony_ci    parser.add_argument('--dst-file',
323af6ab5fSopenharmony_ci                        help='the converted target file')
333af6ab5fSopenharmony_ci    parser.add_argument('--frontend-tool-path',
343af6ab5fSopenharmony_ci                        help='path of the frontend conversion tool')
353af6ab5fSopenharmony_ci    parser.add_argument('--extension',
363af6ab5fSopenharmony_ci                        help='source file extension')
373af6ab5fSopenharmony_ci    parser.add_argument("--debug", action='store_true',
383af6ab5fSopenharmony_ci                        help='whether add debuginfo')
393af6ab5fSopenharmony_ci    parser.add_argument("--module", action='store_true',
403af6ab5fSopenharmony_ci                        help='whether is module')
413af6ab5fSopenharmony_ci    parser.add_argument("--commonjs", action='store_true',
423af6ab5fSopenharmony_ci                        help='whether is commonjs')
433af6ab5fSopenharmony_ci    parser.add_argument("--merge-abc", action='store_true',
443af6ab5fSopenharmony_ci                        help='whether is merge abc')
453af6ab5fSopenharmony_ci    parser.add_argument("--generate-patch", action='store_true',
463af6ab5fSopenharmony_ci                        help='generate patch abc')
473af6ab5fSopenharmony_ci    parser.add_argument("--dump-symbol-table",
483af6ab5fSopenharmony_ci                        help='dump symbol table of base abc')
493af6ab5fSopenharmony_ci    parser.add_argument("--input-symbol-table",
503af6ab5fSopenharmony_ci                        help='input symbol table for patch abc')
513af6ab5fSopenharmony_ci    parser.add_argument("--target-api-sub-version",
523af6ab5fSopenharmony_ci                        help='input symbol table for patch abc')
533af6ab5fSopenharmony_ci    parser.add_argument("--module-record-field-name",
543af6ab5fSopenharmony_ci                        help='specify the field name of module record in unmerged abc. This argument is optional, ' +
553af6ab5fSopenharmony_ci                             'its value will be the path of input file if not specified')
563af6ab5fSopenharmony_ci    parser.add_argument("--source-file",
573af6ab5fSopenharmony_ci                        help='specify the file path info recorded in generated abc. This argument is optional, ' +
583af6ab5fSopenharmony_ci                             'its value will be the path of input file if not specified')
593af6ab5fSopenharmony_ci    parser.add_argument("--enable-annotations", action='store_true',
603af6ab5fSopenharmony_ci                        help='whether annotations are enabled or not')
613af6ab5fSopenharmony_ci    arguments = parser.parse_args()
623af6ab5fSopenharmony_ci    return arguments
633af6ab5fSopenharmony_ci
643af6ab5fSopenharmony_ci
653af6ab5fSopenharmony_cidef run_command(cmd, execution_path):
663af6ab5fSopenharmony_ci    print(" ".join(cmd) + " | execution_path: " + execution_path)
673af6ab5fSopenharmony_ci    proc = subprocess.Popen(cmd, cwd=execution_path)
683af6ab5fSopenharmony_ci    proc.wait()
693af6ab5fSopenharmony_ci    if proc.returncode != 0:
703af6ab5fSopenharmony_ci        raise subprocess.CalledProcessError(proc.returncode, cmd)
713af6ab5fSopenharmony_ci
723af6ab5fSopenharmony_ci
733af6ab5fSopenharmony_cidef gen_abc_info(input_arguments):
743af6ab5fSopenharmony_ci    frontend_tool_path = input_arguments.frontend_tool_path
753af6ab5fSopenharmony_ci
763af6ab5fSopenharmony_ci    (path, name) = os.path.split(frontend_tool_path)
773af6ab5fSopenharmony_ci
783af6ab5fSopenharmony_ci    cmd = [os.path.join("./", name, "es2abc"),
793af6ab5fSopenharmony_ci           '--output', input_arguments.dst_file,
803af6ab5fSopenharmony_ci           input_arguments.src_js]
813af6ab5fSopenharmony_ci
823af6ab5fSopenharmony_ci    if input_arguments.extension:
833af6ab5fSopenharmony_ci        cmd += ['--extension', input_arguments.extension]
843af6ab5fSopenharmony_ci    if input_arguments.dump_symbol_table:
853af6ab5fSopenharmony_ci        cmd += ['--dump-symbol-table', input_arguments.dump_symbol_table]
863af6ab5fSopenharmony_ci    if input_arguments.input_symbol_table:
873af6ab5fSopenharmony_ci        cmd += ['--input-symbol-table', input_arguments.input_symbol_table]
883af6ab5fSopenharmony_ci    if input_arguments.debug:
893af6ab5fSopenharmony_ci        src_index = cmd.index(input_arguments.src_js)
903af6ab5fSopenharmony_ci        cmd.insert(src_index, '--debug-info')
913af6ab5fSopenharmony_ci    if input_arguments.module:
923af6ab5fSopenharmony_ci        src_index = cmd.index(input_arguments.src_js)
933af6ab5fSopenharmony_ci        cmd.insert(src_index, '--module')
943af6ab5fSopenharmony_ci    if input_arguments.commonjs:
953af6ab5fSopenharmony_ci        src_index = cmd.index(input_arguments.src_js)
963af6ab5fSopenharmony_ci        cmd.insert(src_index, '--commonjs')
973af6ab5fSopenharmony_ci    if input_arguments.merge_abc:
983af6ab5fSopenharmony_ci        src_index = cmd.index(input_arguments.src_js)
993af6ab5fSopenharmony_ci        cmd.insert(src_index, '--merge-abc')
1003af6ab5fSopenharmony_ci    if input_arguments.generate_patch:
1013af6ab5fSopenharmony_ci        src_index = cmd.index(input_arguments.src_js)
1023af6ab5fSopenharmony_ci        cmd.insert(src_index, '--generate-patch')
1033af6ab5fSopenharmony_ci    if input_arguments.module_record_field_name:
1043af6ab5fSopenharmony_ci        cmd += ["--module-record-field-name", input_arguments.module_record_field_name]
1053af6ab5fSopenharmony_ci    if input_arguments.source_file:
1063af6ab5fSopenharmony_ci        cmd += ["--source-file", input_arguments.source_file]
1073af6ab5fSopenharmony_ci    if input_arguments.enable_annotations:
1083af6ab5fSopenharmony_ci        src_index = cmd.index(input_arguments.src_js)
1093af6ab5fSopenharmony_ci        cmd.insert(src_index, '--enable-annotations')
1103af6ab5fSopenharmony_ci        # insert d.ts option to cmd later
1113af6ab5fSopenharmony_ci    cmd.append("--target-api-sub-version=beta3")
1123af6ab5fSopenharmony_ci
1133af6ab5fSopenharmony_ci    try:
1143af6ab5fSopenharmony_ci        run_command(cmd, path)
1153af6ab5fSopenharmony_ci    except subprocess.CalledProcessError as e:
1163af6ab5fSopenharmony_ci        exit(e.returncode)
1173af6ab5fSopenharmony_ci
1183af6ab5fSopenharmony_ci
1193af6ab5fSopenharmony_ciif __name__ == '__main__':
1203af6ab5fSopenharmony_ci    gen_abc_info(parse_args())
121