• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2022 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")
15
16# Generate .ts or .cpp files from .idl files.
17#
18# Variables
19#   sources: Paths to .idl file to compile, one idl target can only handle
20#     one idl source file.
21#   gen_type: Only support ts and cpp type currently
22#       "ts": generate ts interface files
23#       "cpp": generate cpp interface files
24#
25# Example
26#   ohos_idl("foo_idl") {
27#       gen_type = "cpp"
28#       sources = [
29#         "cpp/bar/FooBarServiceCallback.idl",
30#       ]
31#   }
32template("ohos_idl") {
33  forward_variables_from(invoker, [ "testonly" ])
34  assert(defined(invoker.sources), "sources are necessary")
35  _idl_target = "//foundation/ability/idl_tool:idl($host_toolchain)"
36  _idl_module_info_target =
37      get_label_info("$_idl_target", "label_no_toolchain") +
38      "_info($host_toolchain)"
39  _idl_module_info =
40      get_label_info("$_idl_module_info_target", "target_out_dir") + "/" +
41      get_label_info("$_idl_target", "name") + "_module_info.json"
42  _rebased_idl_module_info = rebase_path("$_idl_module_info", root_build_dir)
43
44  _libcxx_target = "//build/common/musl:musl-libcxx.so($host_toolchain)"
45  _libcxx_module_info_target =
46      get_label_info("$_libcxx_target", "label_no_toolchain") +
47      "_info($host_toolchain)"
48  _libcxx_module_info =
49      get_label_info("$_libcxx_module_info_target", "target_out_dir") + "/" +
50      get_label_info("$_libcxx_target", "name") + "_module_info.json"
51  _rebased_libcxx_module_info =
52      rebase_path("$_libcxx_module_info", root_build_dir)
53  forward_variables_from(invoker, [ "gen_type" ])
54  assert(defined(gen_type),
55         "need define gen_type to point which type files should be generated")
56
57  if (gen_type == "cpp") {
58    _idl_include_target_name = "${target_name}__inculde"
59    config(_idl_include_target_name) {
60      include_dirs = [ target_gen_dir ]
61    }
62  }
63
64  action_with_pydeps(target_name) {
65    script = "//build/scripts/idl.py"
66    sources = invoker.sources
67    args = [
68      "--idl-path",
69      "@FileArg($_rebased_idl_module_info:source)",
70      "--libcxx-path",
71      "@FileArg($_rebased_libcxx_module_info:source)",
72    ]
73    deps = [
74      "${_idl_module_info_target}",
75      "${_idl_target}",
76      "${_libcxx_module_info_target}",
77      "${_libcxx_target}",
78    ]
79    if (defined(invoker.deps)) {
80      deps += invoker.deps
81    }
82    _output_archive = "${target_gen_dir}/${target_name}.zip"
83    if (gen_type == "cpp") {
84      public_configs = [ ":$_idl_include_target_name" ]
85    }
86    args += [
87      "--gen-type",
88      gen_type,
89      "--output-archive-path",
90      rebase_path(_output_archive, root_build_dir),
91      "--generated-src-directory",
92      rebase_path(target_gen_dir + "/$target_name", root_build_dir),
93    ]
94    args += rebase_path(sources, root_build_dir)
95    outputs = [ _output_archive ]
96  }
97}
98