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/ohos.gni")
15
16idl_root = "//foundation/ability/idl_tool"
17idl_build_deps = ""
18idl_out_root = ""
19
20build_root = "//build"
21toolchain_linux = "$build_root/toolchain/linux:clang_x64"
22if (host_cpu == "arm64") {
23  toolchain_mac = "$build_root/toolchain/mac:clang_arm64"
24} else {
25  toolchain_mac = "$build_root/toolchain/mac:clang_x64"
26}
27toolchain_win = "$build_root/toolchain/mingw:mingw_x86_64"
28
29if (host_toolchain == toolchain_mac) {
30  idl_out_root = get_label_info("$idl_root:idl($toolchain_mac)", "root_out_dir")
31  idl_build_deps = [ "$idl_root:idl($toolchain_mac)" ]
32} else if (host_toolchain == toolchain_win) {
33  idl_out_root = get_label_info("$idl_root:idl($toolchain_win)", "root_out_dir")
34  idl_build_deps = [ "$idl_root:idl($toolchain_win)" ]
35} else {
36  idl_out_root =
37      get_label_info("$idl_root:idl($toolchain_linux)", "root_out_dir")
38  idl_build_deps = [ "$idl_root:idl($toolchain_linux)" ]
39}
40
41idl_build_path = idl_out_root + "/ability/idl_tool"
42
43template("idl_gen_interface") {
44  # idl sources
45  idl_list = []
46  src_idl_fullpath = []
47  if (defined(invoker.sources)) {
48    idl_list += invoker.sources
49    not_needed(invoker,
50               [
51                 "src_idl",
52                 "dst_file",
53               ])
54  } else {
55    assert(defined(invoker.src_idl), "src-idl is required!")
56    not_needed(invoker, [ "dst_file" ])
57    idl_list += [ get_path_info(invoker.src_idl, "file") ]
58    src_idl_fullpath += [ invoker.src_idl ]
59  }
60
61  # language, default cpp, support c/cpp/rust
62  language = "cpp"
63  if (defined(invoker.language)) {
64    assert(invoker.language == "c" || invoker.language == "cpp" ||
65               invoker.language == "rust",
66           "the language must be set to 'c' or 'cpp' or 'rust', default 'cpp'")
67    language = invoker.language
68  }
69
70  # idl name transform
71  str_upper = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
72  str_lower = "a b c d e f g h i j k l m n o p q r s t u v w x y z"
73  str_upper_list = string_split(str_upper, " ")
74  str_lower_list = string_split(str_lower, " ")
75  store = []
76  dst_file_list = []
77  print("idl config idl_list: ", idl_list)
78  foreach(idl_name, idl_list) {
79    i = 0
80    if (defined(invoker.sources)) {
81      src_idl_fullpath += [ rebase_path(idl_name) ]
82    }
83    name = string_replace(idl_name, ".idl", "")
84    foreach(s, str_upper_list) {
85      name = string_replace(name, s, "_" + str_lower_list[i])
86      i = i + 1
87    }
88
89    # first letter
90    name_split = []
91    name_split = string_split(name, "_i_")
92    if (name_split[0] == "") {
93      name = string_replace(name, "_i_", "", 1)
94    }
95    name_split = []
96    name_split = string_split(name, "_")
97    if (name_split[0] == "") {
98      name = string_replace(name, "_", "", 1)
99    }
100    dst_file_list += [ name ]
101    store += [
102      "${target_gen_dir}/" + name + "_proxy.cpp",
103      "${target_gen_dir}/" + name + "_stub.cpp",
104    ]
105  }
106  arg_src_idl = string_join(",", src_idl_fullpath)
107  arg_dst_file = string_join(",", dst_file_list)
108  print("idl config store: ", store, dst_file_list)
109
110  action("$target_name") {
111    inputs = src_idl_fullpath
112    deps = idl_build_deps
113    script = "${idl_root}/scripts/idl.py"
114    args = [
115      "--src-idl",
116      arg_src_idl,
117      "--dst-path",
118      rebase_path("${target_gen_dir}"),
119      "--idl-tool-path",
120      rebase_path("${idl_build_path}"),
121      "--dst-file",
122      arg_dst_file,
123      "--language",
124      language,
125    ]
126    if (defined(invoker.log_domainid)) {
127      args += [
128        "--log-domainid",
129        invoker.log_domainid,
130      ]
131    }
132    if (defined(invoker.log_tag)) {
133      args += [
134        "--log-tag",
135        invoker.log_tag,
136      ]
137    }
138    if (defined(invoker.hitrace)) {
139      args += [
140        "--hitrace",
141        invoker.hitrace,
142      ]
143    }
144    outputs = store
145  }
146
147  # multip cpp, build as so
148  if ((language == "c" || language == "cpp") && defined(invoker.sources)) {
149    idl_headers_config = target_name + "_idl_headers_config"
150    config("$idl_headers_config") {
151      include_dirs = [ "${target_gen_dir}" ]
152      if (defined(invoker.sub_include)) {
153        include_dirs += invoker.sub_include
154      }
155    }
156    lib_client = "lib" + target_name + "_proxy"
157    action_target_name = ":" + target_name
158    ohos_shared_library(lib_client) {
159      sources = []
160      output_values = get_target_outputs(action_target_name)
161      sources += filter_include(output_values, [ "*_proxy.cpp" ])
162      if (defined(invoker.sources_cpp)) {
163        sources += invoker.sources_cpp
164      }
165      if (defined(invoker.configs)) {
166        configs = invoker.configs
167      }
168      public_configs = [ ":$idl_headers_config" ]
169      deps = [ action_target_name ]
170      if (is_standard_system) {
171        public_deps = []
172        if (defined(invoker.sequenceable_pub_deps)) {
173          public_deps += invoker.sequenceable_pub_deps
174        }
175        external_deps = [ "c_utils:utils" ]
176        if (defined(invoker.hitrace)) {
177          external_deps += [ "hitrace:hitrace_meter" ]
178        }
179        if (defined(invoker.log_domainid)) {
180          external_deps += [ "hilog:libhilog" ]
181        }
182        if (defined(invoker.sequenceable_ext_deps)) {
183          external_deps += invoker.sequenceable_ext_deps
184        }
185        if (language == "c") {
186          external_deps += [ "hdf_core:libhdf_ipc_adapter" ]
187        } else if (language == "cpp") {
188          external_deps += [ "ipc:ipc_single" ]
189        }
190      } else {
191        external_deps = [ "hilog:libhilog" ]
192      }
193      if (defined(invoker.subsystem_name)) {
194        subsystem_name = invoker.subsystem_name
195      }
196      if (defined(invoker.part_name)) {
197        part_name = invoker.part_name
198      }
199      if (defined(invoker.innerapi_tags)) {
200        innerapi_tags = invoker.innerapi_tags
201      }
202      if (defined(invoker.sanitize)) {
203        sanitize = invoker.sanitize
204      } else {
205        sanitize = {
206          cfi = true
207          cfi_cross_dso = true
208          debug = false
209        }
210      }
211    }
212    lib_server = "lib" + target_name + "_stub"
213    ohos_shared_library(lib_server) {
214      sources = []
215      output_values = get_target_outputs(action_target_name)
216      sources += filter_include(output_values, [ "*_stub.cpp" ])
217      if (defined(invoker.sources_cpp)) {
218        sources += invoker.sources_cpp
219      }
220      if (defined(invoker.configs)) {
221        configs = invoker.configs
222      }
223      public_configs = [ ":$idl_headers_config" ]
224      deps = [ action_target_name ]
225      if (is_standard_system) {
226        public_deps = []
227        if (defined(invoker.sequenceable_pub_deps)) {
228          public_deps += invoker.sequenceable_pub_deps
229        }
230        external_deps = [ "c_utils:utils" ]
231        if (defined(invoker.hitrace)) {
232          external_deps += [ "hitrace:hitrace_meter" ]
233        }
234        if (defined(invoker.log_domainid)) {
235          external_deps += [ "hilog:libhilog" ]
236        }
237        if (defined(invoker.sequenceable_ext_deps)) {
238          external_deps += invoker.sequenceable_ext_deps
239        }
240        if (language == "c") {
241          external_deps += [ "hdf_core:libhdf_ipc_adapter" ]
242        } else if (language == "cpp") {
243          external_deps += [ "ipc:ipc_single" ]
244        }
245      } else {
246        external_deps = [ "hilog:libhilog" ]
247      }
248      if (defined(invoker.subsystem_name)) {
249        subsystem_name = invoker.subsystem_name
250      }
251      if (defined(invoker.part_name)) {
252        part_name = invoker.part_name
253      }
254      if (defined(invoker.sanitize)) {
255        sanitize = invoker.sanitize
256      } else {
257        sanitize = {
258          cfi = true
259          cfi_cross_dso = true
260          debug = false
261        }
262      }
263    }
264
265    # generate code and shared library
266    group("$target_name" + "_idl_target") {
267      deps = [
268        ":$lib_client",
269        ":$lib_server",
270      ]
271    }
272  }
273}
274