• 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  if (ohos_indep_compiler_enable) {
36    _idl_target = "//binarys/foundation/ability/idl_tool/innerapis/idl/clang_x64:idl($host_toolchain)"
37  } else {
38    _idl_target = "//foundation/ability/idl_tool:idl($host_toolchain)"
39  }
40  _idl_module_info_target =
41      get_label_info("$_idl_target", "label_no_toolchain") +
42      "_info($host_toolchain)"
43  _idl_module_info =
44      get_label_info("$_idl_module_info_target", "target_out_dir") + "/" +
45      get_label_info("$_idl_target", "name") + "_module_info.json"
46  _rebased_idl_module_info = rebase_path("$_idl_module_info", root_build_dir)
47
48  _libcxx_target = "//build/common/musl:musl-libcxx.so($host_toolchain)"
49  _libcxx_module_info_target =
50      get_label_info("$_libcxx_target", "label_no_toolchain") +
51      "_info($host_toolchain)"
52  _libcxx_module_info =
53      get_label_info("$_libcxx_module_info_target", "target_out_dir") + "/" +
54      get_label_info("$_libcxx_target", "name") + "_module_info.json"
55  _rebased_libcxx_module_info =
56      rebase_path("$_libcxx_module_info", root_build_dir)
57  forward_variables_from(invoker, [ "gen_type" ])
58  assert(defined(gen_type),
59         "need define gen_type to point which type files should be generated")
60
61  if (gen_type == "cpp") {
62    _idl_include_target_name = "${target_name}__inculde"
63    config(_idl_include_target_name) {
64      include_dirs = [ target_gen_dir ]
65    }
66  }
67
68  action_with_pydeps(target_name) {
69    script = "//build/scripts/idl.py"
70    sources = invoker.sources
71    args = [
72      "--idl-path",
73      "@FileArg($_rebased_idl_module_info:source)",
74      "--libcxx-path",
75      "@FileArg($_rebased_libcxx_module_info:source)",
76    ]
77    deps = [
78      "${_idl_module_info_target}",
79      "${_idl_target}",
80      "${_libcxx_module_info_target}",
81      "${_libcxx_target}",
82    ]
83    if (defined(invoker.deps)) {
84      deps += invoker.deps
85    }
86    _output_archive = "${target_gen_dir}/${target_name}.zip"
87    if (gen_type == "cpp") {
88      public_configs = [ ":$_idl_include_target_name" ]
89    }
90    args += [
91      "--gen-type",
92      gen_type,
93      "--output-archive-path",
94      rebase_path(_output_archive, root_build_dir),
95      "--generated-src-directory",
96      rebase_path(target_gen_dir + "/$target_name", root_build_dir),
97    ]
98    args += rebase_path(sources, root_build_dir)
99    outputs = [ _output_archive ]
100  }
101}
102