• 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("//build/config/python.gni")
15import("//build/templates/metadata/module_info.gni")
16
17declare_args() {
18  native_stub = "native-stub"
19  stub_version_script_suffix = ".map.txt"
20  native_stub_signature_save_dir = "//interface/native-stub"
21}
22
23stub_signature_out_dir = "$root_out_dir/${native_stub}/signature"
24
25# Generate native stub library from native stub description file for system components.
26#
27# Variables:
28#  stub_description_file: stub description file, json format with stub function names
29#  install_enable: default is false, if you want install, assign with true
30#
31# Example:
32# ohos_native_stub_library("libtest_stub") {
33#   output_extension = "so"
34#   stub_description_file = "./libtest.stub.json"
35# }
36# It will generate libtest_stub.so with symbols defined in libtest.stub.json.
37# The stub library will not be installed by default.
38#
39#
40template("ohos_native_stub_library") {
41  forward_variables_from(invoker, [ "testonly" ])
42  assert(defined(invoker.stub_description_file),
43         "stub description file is necessary ")
44
45  _stub_description_file = invoker.stub_description_file
46
47  _system_capability = ""
48  if (defined(invoker.system_capability)) {
49    _system_capability = invoker.system_capability
50  }
51
52  _deps = []
53  if (defined(invoker.deps)) {
54    _deps += invoker.deps
55  }
56
57  _output_name = target_name
58  if (defined(invoker.output_name)) {
59    _output_name = invoker.output_name
60  }
61
62  _output_extension = "z.so"
63  if (defined(invoker.output_extension)) {
64    _output_extension = invoker.output_extension
65  }
66
67  _native_stub_target = "${target_name}__native_stub"
68  _generated_native_stub_file =
69      target_gen_dir + "/${target_name}.stub/" +
70      get_path_info(_stub_description_file, "name") + ".c"
71
72  _current_label = get_label_info(":${target_name}", "label_with_toolchain")
73  action_with_pydeps(_native_stub_target) {
74    deps = _deps
75    script = "//build/ohos/ndk/generate_ndk_stub_file.py"
76    depfile = "${target_gen_dir}/${target_name}.d"
77    args = [
78      "--output",
79      rebase_path(_generated_native_stub_file, root_build_dir),
80      "--ndk-description-file",
81      rebase_path(_stub_description_file, root_build_dir),
82      "--depfile",
83      rebase_path(depfile, root_build_dir),
84    ]
85    inputs = [ _stub_description_file ]
86    outputs = [ _generated_native_stub_file ]
87
88    _stub_config_info = {
89      label = _current_label
90      lib_name = _output_name
91      system_capability = _system_capability
92    }
93    metadata = {
94      ndk_config = [ _stub_config_info ]
95    }
96  }
97
98  _stub_shlib_target = "${target_name}"
99
100  target_label = get_label_info(":${target_name}", "label_with_toolchain")
101  if (defined(invoker.subsystem_name) && defined(invoker.part_name)) {
102    subsystem_name = invoker.subsystem_name
103    part_name = invoker.part_name
104  } else if (defined(invoker.part_name)) {
105    part_name = invoker.part_name
106    _part_subsystem_info_file =
107        "$root_build_dir/build_configs/parts_info/part_subsystem.json"
108    _arguments = [
109      "--part-name",
110      part_name,
111      "--part-subsystem-info-file",
112      rebase_path(_part_subsystem_info_file, root_build_dir),
113    ]
114    get_subsystem_script = "//build/templates/common/get_subsystem_name.py"
115    subsystem_name =
116        exec_script(get_subsystem_script, _arguments, "trim string")
117  } else if (defined(invoker.subsystem_name)) {
118    subsystem_name = invoker.subsystem_name
119    part_name = subsystem_name
120  } else {
121    subsystem_name = "build"
122    part_name = "build_framework"
123  }
124
125  ohos_module_name = target_name
126  _module_info_target = "${target_name}_info"
127  generate_module_info(_module_info_target) {
128    module_name = ohos_module_name
129    module_type = "lib"
130    module_source_dir = target_out_dir
131
132    module_install_name = ohos_module_name
133    if (defined(invoker.output_name)) {
134      module_install_name = invoker.output_name
135    }
136
137    module_install_images = [ "system" ]
138    if (defined(invoker.install_images)) {
139      module_install_images = []
140      module_install_images += invoker.install_images
141    }
142
143    module_output_extension = shlib_extension
144    if (defined(invoker.output_extension)) {
145      module_output_extension = "." + invoker.output_extension
146    }
147
148    install_enable = false
149    if (defined(invoker.install_enable)) {
150      install_enable = invoker.install_enable
151    }
152
153    if (defined(invoker.module_install_dir)) {
154      module_install_dir = invoker.module_install_dir
155    }
156
157    if (defined(invoker.relative_install_dir)) {
158      relative_install_dir = invoker.relative_install_dir
159    }
160
161    if (defined(invoker.symlink_target_name)) {
162      symlink_target_name = invoker.symlink_target_name
163    }
164
165    if (defined(invoker.output_prefix_override)) {
166      output_prefix_override = invoker.output_prefix_override
167    }
168    notice = "$target_out_dir/$ohos_module_name.notice.txt"
169  }
170
171  shared_library(_stub_shlib_target) {
172    forward_variables_from(invoker,
173                           [
174                             "cflags",
175                             "ldflags",
176                             "configs",
177                             "public_configs",
178                             "libs",
179                             "include_dirs",
180                           ])
181    deps = [ ":$_native_stub_target" ]
182    if (!skip_gen_module_info) {
183      deps += [ ":$_module_info_target" ]
184    }
185    sources = [ _generated_native_stub_file ]
186    output_dir = target_out_dir
187    output_name = _output_name
188    output_extension = _output_extension
189
190    install_module_info = {
191      module_def = target_label
192      module_info_file =
193          rebase_path(get_label_info(module_def, "target_out_dir"),
194                      root_build_dir) + "/${target_name}_module_info.json"
195      subsystem_name = subsystem_name
196      part_name = part_name
197      toolchain = current_toolchain
198      toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
199    }
200    metadata = {
201      install_modules = [ install_module_info ]
202    }
203  }
204}
205
206# Generate native stub library version script from native stub description file for system components.
207#
208# Variables:
209#  stub_description_file: stub description file, json format with stub function names
210#
211# Example:
212# ohos_native_stub_versionscript("libtest_stub_versionscript") {
213#   stub_description_file = "./libtest.stub.json"
214# }
215# It will generate version script with symbols defined in libtest.stub.json.
216# The generated version script location is:
217#    get_label_info(":libtest_stub_versionscript", "target_gen_dir") + "/" +
218#      get_label_info(":libtest_stub_versionscript", "name") + stub_version_script_suffix
219#
220# ohos_executable() or ohos_shared_library() can use version scripit as follow:
221# ohos_shared_library("libtest") {
222#   ...
223#   deps += [ ":libtest_stub_versionscript"]
224#   version_script = get_label_info(":libtest_stub_versionscript", "target_gen_dir") + "/" +
225#         get_label_info(":libtest_stub_versionscript", "name") + stub_version_script_suffix
226#   ...
227# }
228#  In this way, libtest.z.so will only export symbols specified in libtest.stub.json.
229#
230#
231template("ohos_native_stub_versionscript") {
232  assert(defined(invoker.stub_description_file),
233         "stub description file is necessary ")
234
235  _stub_description_file = invoker.stub_description_file
236
237  _deps = []
238  if (defined(invoker.deps)) {
239    _deps += invoker.deps
240  }
241
242  _output_name = target_name
243  if (defined(invoker.output_name)) {
244    _output_name = invoker.output_name
245  }
246
247  _ndk_version_script_target = target_name
248  _generated_version_script =
249      target_gen_dir + "/$target_name" + stub_version_script_suffix
250  action_with_pydeps(_ndk_version_script_target) {
251    deps = _deps
252    script = "//build/ohos/ndk/generate_version_script.py"
253    depfile = "${target_gen_dir}/${target_name}.d"
254    args = [
255      "--output",
256      rebase_path(_generated_version_script, root_build_dir),
257      "--ndk-description-file",
258      rebase_path(_stub_description_file, root_build_dir),
259      "--shlib-name",
260      _output_name,
261      "--depfile",
262      rebase_path(depfile, root_build_dir),
263    ]
264    outputs = [ _generated_version_script ]
265  }
266}
267
268# Specify native-stub header files
269#
270# Input variables:
271#   sources: List of files.
272#
273template("ohos_native_stub_headers") {
274  assert(defined(invoker.sources), "sources are necessary ")
275
276  _stub_header_signature_target = "${target_name}__stub_signature_check"
277  _target_name = target_name
278  action_with_pydeps(_stub_header_signature_target) {
279    if (defined(invoker.deps)) {
280      deps = invoker.deps
281    }
282
283    script = "//build/ohos/ndk/check_ndk_header_signature.py"
284    depfile = "${target_gen_dir}/${target_name}.d"
285
286    inputs = []
287    foreach(src, invoker.sources) {
288      _all_files = []
289      _all_files = exec_script("//build/scripts/find.py",
290                               [ rebase_path(src) ],
291                               "list lines")
292
293      inputs += _all_files
294    }
295
296    _output = "$target_gen_dir/$target_name.stamp"
297
298    args = [
299      "--depfile",
300      rebase_path(depfile, root_build_dir),
301      "--generated-signature",
302      rebase_path("$stub_signature_out_dir/$_target_name/signature.txt",
303                  root_build_dir),
304      "--saved-signature",
305      rebase_path("$native_stub_signature_save_dir/$_target_name/signature.txt",
306                  root_build_dir),
307      "--output",
308      rebase_path(_output, root_build_dir),
309    ]
310    foreach(f, inputs) {
311      args += [
312        "--headers",
313        rebase_path(f, root_build_dir),
314        "--root-build-dir",
315        rebase_path("//", root_build_dir),
316      ]
317    }
318
319    outputs = [ _output ]
320  }
321}
322