• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("//arkcompiler/ets_frontend/ets_frontend_config.gni")
15if (!ark_standalone_build) {
16  import("//build/ohos.gni")
17  import("//build/test.gni")
18  build_root = "//build"
19  ts2abc_root = "//arkcompiler/ets_frontend/ts2panda"
20} else {
21  import("$build_root/ark.gni")
22  ts2abc_root = "//arkcompiler/ets_frontend/ts2panda"
23}
24
25declare_args() {
26  nodejs_dir = ""
27  node_path = ""
28  node_modules = ""
29
30  ts2abc_build_deps = ""
31  ts2abc_build_path = ""
32}
33
34node_modules = "//prebuilts/build-tools/common/ts2abc/node_modules"
35if (build_public_version) {
36  nodejs_dir = "//prebuilts/build-tools/common/nodejs"
37} else {
38  nodejs_dir = "//prebuilts/ace-toolkit/nodejs"
39}
40
41if (host_toolchain == toolchain_mac) {
42  ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build_mac($toolchain_mac)" ]
43  ts2abc_build_path =
44      get_label_info("$ts2abc_root:ts2abc_build_mac($toolchain_mac)",
45                     "root_out_dir") +
46      "/obj/arkcompiler/ets_frontend/ts2panda/build-mac"
47  node_path = "${nodejs_dir}/node-v12.18.4-darwin-x64/bin/"
48} else if (host_toolchain == toolchain_win) {
49  ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build_win($toolchain_win)" ]
50  ts2abc_build_path =
51      get_label_info("$ts2abc_root:ts2abc_build_win($toolchain_win)",
52                     "root_out_dir") +
53      "/obj/arkcompiler/ets_frontend/ts2panda/build_win"
54} else {
55  ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build($toolchain_linux)" ]
56  ts2abc_build_path =
57      get_label_info("$ts2abc_root:ts2abc_build($toolchain_linux)",
58                     "root_out_dir") +
59      "/obj/arkcompiler/ets_frontend/ts2panda/build"
60  node_path = "${nodejs_dir}/node-v12.18.4-linux-x64/bin/"
61}
62
63# Generate js plugin.
64#
65# Mandatory arguments:
66# plugin_path -- plugin js file path
67# plugin_name -- name of js file, ex: BatteryPlugin.js
68# generat_file -- name of generated file
69# package_name -- name of generated file's package
70# extra_dependencies -- a list of files that should be considered as dependencies, must be label
71# out_puts
72template("ts2abc_gen_file") {
73  assert(defined(invoker.plugin_path), "plugin_path is required!")
74  assert(defined(invoker.plugin_name), "plugin_name is required!")
75  assert(defined(invoker.generat_file), "generat_file is required!")
76  assert(defined(invoker.package_name), "package_name is required!")
77  assert(defined(invoker.out_puts), "out_puts is required!")
78
79  extra_dependencies = []
80  if (defined(invoker.extra_dependencies)) {
81    extra_dependencies += invoker.extra_dependencies
82  }
83
84  action("$target_name") {
85    script = "${ts2abc_root}/scripts/generate_plugin.py"
86
87    deps = extra_dependencies
88    deps += ts2abc_build_deps
89    args = [
90      "--node",
91      rebase_path("${node_path}"),
92      "--frontend-tool-path",
93      rebase_path("${ts2abc_build_path}"),
94      "--node-modules",
95      rebase_path("${node_modules}"),
96      "--plugin-path",
97      invoker.plugin_path,
98      "--plugin-name",
99      invoker.plugin_name,
100      "--generated-file",
101      invoker.generat_file,
102      "--package-name",
103      invoker.package_name,
104    ]
105
106    outputs = invoker.out_puts
107  }
108}
109
110# Generate abc
111#
112# Mandatory arguments:
113# src_js -- name of js file, ex: BatteryPlugin.js
114# dst_file -- ex: BatteryPlugin.abc
115# out_puts
116template("ts2abc_gen_abc") {
117  assert(defined(invoker.src_js), "src_js is required!")
118  assert(defined(invoker.dst_file), "dst_file is required!")
119  assert(defined(invoker.out_puts), "out_puts is required!")
120
121  extra_dependencies = []
122  if (defined(invoker.extra_dependencies)) {
123    extra_dependencies += invoker.extra_dependencies
124  }
125
126  action("$target_name") {
127    if (defined(invoker.extra_visibility)) {
128      visibility = invoker.extra_visibility
129    }
130
131    script = "${ts2abc_root}/scripts/generate_js_bytecode.py"
132
133    deps = extra_dependencies
134    deps += ts2abc_build_deps
135
136    args = [
137      "--src-js",
138      invoker.src_js,
139      "--dst-file",
140      invoker.dst_file,
141      "--node",
142      rebase_path("${node_path}"),
143      "--frontend-tool-path",
144      rebase_path("${ts2abc_build_path}"),
145      "--node-modules",
146      rebase_path("${node_modules}"),
147    ]
148
149    if (defined(invoker.extra_args)) {
150      args += invoker.extra_args
151    }
152
153    if (defined(invoker.in_puts)) {
154      inputs = invoker.in_puts
155    }
156
157    outputs = invoker.out_puts
158  }
159}
160
161template("ts2abc_unittest_action") {
162  _target_name_ = "${target_name}"
163  invoker.module_out_path = "ark/ts2abc"
164
165  # unittest for phone running
166  ohos_unittest(_target_name_) {
167    configs = [ "${ts2abc_root}/ts2abc:ts2abc_config" ]
168    deps = [ "${ts2abc_root}/ts2abc:ts2abc_static" ]
169    forward_variables_from(invoker, "*")
170  }
171
172  _module_out_path_ = invoker.module_out_path
173
174  # unittest for host running
175  action("${_target_name_}Action") {
176    testonly = true
177    _host_test_target_ = ":${_target_name_}(${host_toolchain})"
178    _root_out_dir_ = get_label_info(_host_test_target_, "root_out_dir")
179
180    deps = [ _host_test_target_ ]
181
182    script = "${ts2abc_root}/scripts/run_tests_executable.sh"
183
184    args = [ rebase_path(_root_out_dir_) +
185             "/tests/unittest/${_module_out_path_}/${_target_name_}" ]
186
187    inputs = [
188      "$_root_out_dir_/tests/unittest/${_module_out_path_}/${_target_name_}",
189    ]
190    outputs = [ "$target_out_dir/${_target_name_}/" ]
191  }
192}
193
194# ts2abc performs the ut test
195#
196# Mandatory arguments:
197# js_file: The name of the test use case file to execute , ex expression/TemplateExpression.test.js
198template("ts2abc_unittest") {
199  assert(defined(invoker.js_file), "js_file is required!")
200
201  action("$target_name") {
202    script = "${ts2abc_root}/scripts/run_tests.py"
203    deps = [ "${ts2abc_root}:ts2abc_tests" ]
204
205    args = [
206      "--src-dir",
207      rebase_path("${ts2abc_root}"),
208      "--dist-dir",
209      rebase_path(target_out_dir + "/.."),
210      "--node-modules",
211      rebase_path("${node_modules}"),
212      "--js-file",
213      invoker.js_file,
214      "--gn-build",
215    ]
216
217    if (host_toolchain == toolchain_linux) {
218      args += [
219        "--platform",
220        "linux",
221      ]
222    } else if (host_toolchain == toolchain_mac) {
223      args += [
224        "--platform",
225        "mac",
226      ]
227    } else {
228      args += [
229        "--platform",
230        "win",
231      ]
232    }
233    outputs = [ "$target_out_dir/${target_name}/" ]
234  }
235}
236