1# Copyright (c) 2021 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14import("//build/ohos.gni") 15import("//build/test.gni") 16 17declare_args() { 18 buildtool_linux = "//build/toolchain/linux:clang_x64" 19 buildtool_mac = "//build/toolchain/mac:clang_x64" 20 buildtool_win = "//build/toolchain/mingw:mingw_x86_64" 21 22 ts2abc_root = "//ark/ts2abc/ts2panda" 23 nodejs_dir = "" 24 node_path = "" 25 node_modules = "" 26 27 ts2abc_build_deps = "" 28 ts2abc_build_path = "" 29} 30 31if (build_public_version) { 32 nodejs_dir = "//prebuilts/build-tools/common/nodejs" 33 node_modules = "//prebuilts/build-tools/common/ts2abc/node_modules" 34} else { 35 nodejs_dir = "//prebuilts/ace-toolkit/nodejs" 36 node_modules = "//prebuilts/ace-toolkit/ace-loader/panda/node_modules" 37} 38 39if (host_toolchain == buildtool_mac) { 40 ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build_mac($buildtool_mac)" ] 41 ts2abc_build_path = 42 get_label_info("//ark/ts2abc/ts2panda:ts2abc_build_mac($buildtool_mac)", 43 "root_out_dir") + "/obj/ark/ts2abc/ts2panda/build-mac" 44 node_path = "${nodejs_dir}/node-v12.18.4-darwin-x64/bin/" 45} else if (host_toolchain == buildtool_win) { 46 ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build_win($buildtool_win)" ] 47 ts2abc_build_path = 48 get_label_info("//ark/ts2abc/ts2panda:ts2abc_build_win($buildtool_win)", 49 "root_out_dir") + "/obj/ark/ts2abc/ts2panda/build_win" 50} else { 51 ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build($buildtool_linux)" ] 52 ts2abc_build_path = 53 get_label_info("//ark/ts2abc/ts2panda:ts2abc_build($buildtool_linux)", 54 "root_out_dir") + "/obj/ark/ts2abc/ts2panda/build" 55 node_path = "${nodejs_dir}/node-v12.18.4-linux-x64/bin/" 56} 57 58# Generate js plugin. 59# 60# Mandatory arguments: 61# plugin_path -- plugin js file path 62# plugin_name -- name of js file, ex: BatteryPlugin.js 63# generat_file -- name of generated file 64# package_name -- name of generated file's package 65# extra_dependencies -- a list of files that should be considered as dependencies, must be lable 66# out_puts 67template("ts2abc_gen_file") { 68 assert(defined(invoker.plugin_path), "plugin_path is required!") 69 assert(defined(invoker.plugin_name), "plugin_name is required!") 70 assert(defined(invoker.generat_file), "generat_file is required!") 71 assert(defined(invoker.package_name), "package_name is required!") 72 assert(defined(invoker.out_puts), "out_puts is required!") 73 74 extra_dependencies = [] 75 if (defined(invoker.extra_dependencies)) { 76 extra_dependencies += invoker.extra_dependencies 77 } 78 79 action("$target_name") { 80 script = "${ts2abc_root}/scripts/generate_plugin.py" 81 82 deps = extra_dependencies 83 deps += ts2abc_build_deps 84 args = [ 85 "--node", 86 rebase_path("${node_path}"), 87 "--frontend-tool-path", 88 rebase_path("${ts2abc_build_path}"), 89 "--node-modules", 90 rebase_path("${node_modules}"), 91 "--plugin-path", 92 invoker.plugin_path, 93 "--plugin-name", 94 invoker.plugin_name, 95 "--generated-file", 96 invoker.generat_file, 97 "--package-name", 98 invoker.package_name, 99 ] 100 101 outputs = invoker.out_puts 102 } 103} 104 105# Generate abc 106# 107# Mandatory arguments: 108# src_js -- name of js file, ex: BatteryPlugin.js 109# dst_file -- ex: BatteryPlugin.abc 110# out_puts 111template("ts2abc_gen_abc") { 112 assert(defined(invoker.src_js), "src_js is required!") 113 assert(defined(invoker.dst_file), "dst_file is required!") 114 assert(defined(invoker.out_puts), "out_puts is required!") 115 116 extra_dependencies = [] 117 if (defined(invoker.extra_dependencies)) { 118 extra_dependencies += invoker.extra_dependencies 119 } 120 121 action("$target_name") { 122 if (defined(invoker.extra_visibility)) { 123 visibility = invoker.extra_visibility 124 } 125 126 script = "${ts2abc_root}/scripts/generate_js_bytecode.py" 127 128 deps = extra_dependencies 129 deps += ts2abc_build_deps 130 131 args = [ 132 "--src-js", 133 invoker.src_js, 134 "--dst-file", 135 invoker.dst_file, 136 "--node", 137 rebase_path("${node_path}"), 138 "--frontend-tool-path", 139 rebase_path("${ts2abc_build_path}"), 140 "--node-modules", 141 rebase_path("${node_modules}"), 142 ] 143 144 if (defined(invoker.extra_args)) { 145 args += invoker.extra_args 146 } 147 148 if (defined(invoker.in_puts)) { 149 inputs = invoker.in_puts 150 } 151 152 outputs = invoker.out_puts 153 } 154} 155 156template("ts2abc_unittest_action") { 157 _target_name_ = "${target_name}" 158 invoker.module_out_path = "ark/ts2abc" 159 160 # unittest for phone running 161 ohos_unittest(_target_name_) { 162 configs = [ "${ts2abc_root}/ts2abc:ts2abc_config" ] 163 deps = [ "${ts2abc_root}/ts2abc:ts2abc_static" ] 164 forward_variables_from(invoker, "*") 165 } 166 167 _module_out_path_ = invoker.module_out_path 168 169 # unittest for host running 170 action("${_target_name_}Action") { 171 testonly = true 172 _host_test_target_ = ":${_target_name_}(${host_toolchain})" 173 _root_out_dir_ = get_label_info(_host_test_target_, "root_out_dir") 174 175 deps = [ _host_test_target_ ] 176 177 script = "${ts2abc_root}/scripts/run_tests_executable.sh" 178 179 args = [ rebase_path(_root_out_dir_) + 180 "/tests/unittest/${_module_out_path_}/${_target_name_}" ] 181 182 inputs = [ 183 "$_root_out_dir_/tests/unittest/${_module_out_path_}/${_target_name_}", 184 ] 185 outputs = [ "$target_out_dir/${_target_name_}/" ] 186 } 187} 188 189# ts2abc performs the ut test 190# 191# Mandatory arguments: 192# js_file: The name of the test use case file to execute , ex expression/TemplateExpression.test.js 193template("ts2abc_unittest") { 194 assert(defined(invoker.js_file), "js_file is required!") 195 196 action("$target_name") { 197 script = "${ts2abc_root}/scripts/run_tests.py" 198 deps = [ "${ts2abc_root}:ts2abc_tests" ] 199 200 args = [ 201 "--src-dir", 202 rebase_path("${ts2abc_root}"), 203 "--dist-dir", 204 rebase_path(target_out_dir + "/.."), 205 "--node-modules", 206 rebase_path("${node_modules}"), 207 "--js-file", 208 invoker.js_file, 209 "--gn-build", 210 ] 211 212 if (host_toolchain == buildtool_linux) { 213 args += [ 214 "--platform", 215 "linux", 216 ] 217 } else if (host_toolchain == buildtool_mac) { 218 args += [ 219 "--platform", 220 "mac", 221 ] 222 } else { 223 args += [ 224 "--platform", 225 "win", 226 ] 227 } 228 outputs = [ "$target_out_dir/${target_name}/" ] 229 } 230} 231