1# Copyright (c) 2023 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/config/components/ets_frontend/es2abc_config.gni")
15import("//build/config/python.gni")
16import("//build/ohos/notice/notice.gni")
17import("//build/templates/common/collect_target.gni")
18import("//build/templates/metadata/module_info.gni")
19
20# Generate .abc files from .ts files
21#
22# Variables
23#   sources: Paths to .ts file to compile
24#
25# Example
26#   ohos_abc("foo_abc") {
27#     sources = [ "example.ts" ]
28#     subsystem_name = "example"
29#     part_name = "example"
30#   }
31template("ohos_abc") {
32  assert(defined(invoker.subsystem_name), "subsystem_name is required")
33  assert(defined(invoker.part_name), "part_name is required")
34
35  _test_target = defined(invoker.testonly) && invoker.testonly
36  subsystem_name = invoker.subsystem_name
37  part_name = invoker.part_name
38
39  if (is_use_check_deps && !_test_target) {
40    _check_target = "${target_name}__check"
41    target_path = get_label_info(":${target_name}", "label_no_toolchain")
42    check_target(_check_target) {
43      module_deps = []
44      if (defined(invoker.deps)) {
45        module_deps += invoker.deps
46      }
47      if (defined(invoker.public_deps)) {
48        module_deps += invoker.public_deps
49      }
50      if (defined(invoker.external_deps)) {
51        module_ex_deps = invoker.external_deps
52      }
53    }
54  }
55
56  if (is_standard_system) {
57    output_dir = "${root_out_dir}/${subsystem_name}/${part_name}"
58  } else {
59    output_dir = "${root_out_dir}"
60  }
61
62  target_label = get_label_info(":${target_name}", "label_with_toolchain")
63  target_toolchain = get_label_info(target_label, "toolchain")
64
65  if (target_toolchain == "${current_toolchain}") {
66    ohos_abc_target = target_name
67    module_info_target = "${target_name}_info"
68    generate_module_info(module_info_target) {
69      forward_variables_from(invoker, [ "testonly" ])
70      module_name = ohos_abc_target
71      if (!defined(invoker.module_type)) {
72        module_type = "unknown"
73      } else {
74        module_type = invoker.module_type
75      }
76      module_source_dir = "$root_out_dir"
77      if (defined(output_dir)) {
78        module_source_dir = output_dir
79      }
80
81      if (defined(invoker.symlink_target_name)) {
82        symlink_target_name = invoker.symlink_target_name
83      }
84
85      module_install_name = "${ohos_abc_target}.abc"
86      if (defined(invoker.output_name)) {
87        module_install_name = "${invoker.output_name}.abc"
88      }
89
90      module_install_images = [ "system" ]
91      if (defined(invoker.install_images)) {
92        module_install_images = []
93        module_install_images += invoker.install_images
94      }
95
96      module_install_dir = "etc/abc"
97      if (defined(invoker.module_install_dir)) {
98        module_install_dir = invoker.module_install_dir
99      }
100
101      install_enable = true
102      if (defined(invoker.install_enable)) {
103        install_enable = invoker.install_enable
104      }
105
106      if (defined(invoker.relative_install_dir)) {
107        relative_install_dir = invoker.relative_install_dir
108      }
109
110      notice = "$target_out_dir/$ohos_abc_target.notice.txt"
111    }
112  }
113
114  if (!_test_target) {
115    module_label = get_label_info(":${target_name}", "label_with_toolchain")
116    _collect_target = "${target_name}__collect"
117    collect_module_target(_collect_target) {
118      forward_variables_from(invoker, [ "install_images" ])
119    }
120
121    _notice_target = "${target_name}__notice"
122    ohos_abc_target = target_name
123    collect_notice(_notice_target) {
124      forward_variables_from(invoker, [ "testonly" ])
125      if (defined(invoker.license_as_sources)) {
126        license_as_sources = invoker.license_as_sources
127      }
128      if (defined(invoker.license_file)) {
129        license_file = invoker.license_file
130      }
131      module_name = ohos_abc_target
132      module_source_dir = get_label_info(":${ohos_abc_target}", "dir")
133    }
134  }
135
136  action_with_pydeps(target_name) {
137    forward_variables_from(invoker, [ "testonly" ])
138    if (!defined(deps)) {
139      deps = []
140    }
141    if (defined(invoker.deps)) {
142      deps += invoker.deps
143    }
144    deps += es2abc_build_deps
145    if (is_use_check_deps && !_test_target) {
146      deps += [ ":$_check_target" ]
147    }
148    if (target_toolchain == "${current_toolchain}") {
149      deps += [ ":$module_info_target" ]
150    }
151    if (!_test_target) {
152      deps += [
153        ":${_collect_target}",
154        ":${_notice_target}",
155      ]
156    }
157
158    if (defined(invoker.output_name)) {
159      output_file = "${output_dir}/${invoker.output_name}.abc"
160    } else {
161      output_file = "${output_dir}/${target_name}.abc"
162    }
163    script = "//build/scripts/ohos_abc.py"
164    sources = invoker.sources
165    args = [
166      "--outputs",
167      rebase_path(output_file),
168      "--es2abc",
169      rebase_path(es2abc_build_path),
170      "--sources",
171    ]
172    args += rebase_path(sources, root_build_dir)
173    if (defined(invoker.merge_abc) && invoker.merge_abc) {
174      args += [ "--merge-abc" ]
175    }
176    if (defined(invoker.disable_module) && invoker.disable_module) {
177      args += [ "--module" ]
178    }
179    outputs = [ output_file ]
180
181    install_module_info = {
182      module_def = target_label
183      module_info_file =
184          rebase_path(get_label_info(module_def, "target_out_dir"),
185                      root_build_dir) + "/${target_name}_module_info.json"
186      toolchain = current_toolchain
187      toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
188      subsystem_name = subsystem_name
189      part_name = part_name
190    }
191    metadata = {
192      install_modules = [ install_module_info ]
193    }
194  }
195}
196