15f9996aaSopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
25f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
35f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
45f9996aaSopenharmony_ci# You may obtain a copy of the License at
55f9996aaSopenharmony_ci#
65f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
95f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
105f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
115f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
125f9996aaSopenharmony_ci# limitations under the License.
135f9996aaSopenharmony_ci
145f9996aaSopenharmony_ciimport("//build/config/python.gni")
155f9996aaSopenharmony_ciimport("//build/templates/common/collect_target.gni")
165f9996aaSopenharmony_ciimport("//build/templates/metadata/module_info.gni")
175f9996aaSopenharmony_ci
185f9996aaSopenharmony_cideclare_args() {
195f9996aaSopenharmony_ci  native_stub = "native-stub"
205f9996aaSopenharmony_ci  stub_version_script_suffix = ".map.txt"
215f9996aaSopenharmony_ci  native_stub_signature_save_dir = "//interface/native-stub"
225f9996aaSopenharmony_ci}
235f9996aaSopenharmony_ci
245f9996aaSopenharmony_cistub_signature_out_dir = "$root_out_dir/${native_stub}/signature"
255f9996aaSopenharmony_ci
265f9996aaSopenharmony_ci# Generate native stub library from native stub description file for system components.
275f9996aaSopenharmony_ci#
285f9996aaSopenharmony_ci# Variables:
295f9996aaSopenharmony_ci#  stub_description_file: stub description file, json format with stub function names
305f9996aaSopenharmony_ci#  install_enable: default is false, if you want install, assign with true
315f9996aaSopenharmony_ci#
325f9996aaSopenharmony_ci# Example:
335f9996aaSopenharmony_ci# ohos_native_stub_library("libtest_stub") {
345f9996aaSopenharmony_ci#   output_extension = "so"
355f9996aaSopenharmony_ci#   stub_description_file = "./libtest.stub.json"
365f9996aaSopenharmony_ci# }
375f9996aaSopenharmony_ci# It will generate libtest_stub.so with symbols defined in libtest.stub.json.
385f9996aaSopenharmony_ci# The stub library will not be installed by default.
395f9996aaSopenharmony_ci#
405f9996aaSopenharmony_ci#
415f9996aaSopenharmony_citemplate("ohos_native_stub_library") {
425f9996aaSopenharmony_ci  forward_variables_from(invoker, [ "testonly" ])
435f9996aaSopenharmony_ci  assert(defined(invoker.stub_description_file),
445f9996aaSopenharmony_ci         "stub description file is necessary ")
455f9996aaSopenharmony_ci
465f9996aaSopenharmony_ci  _stub_description_file = invoker.stub_description_file
475f9996aaSopenharmony_ci
485f9996aaSopenharmony_ci  _system_capability = ""
495f9996aaSopenharmony_ci  if (defined(invoker.system_capability)) {
505f9996aaSopenharmony_ci    _system_capability = invoker.system_capability
515f9996aaSopenharmony_ci  }
525f9996aaSopenharmony_ci
535f9996aaSopenharmony_ci  _deps = []
545f9996aaSopenharmony_ci  if (defined(invoker.deps)) {
555f9996aaSopenharmony_ci    _deps += invoker.deps
565f9996aaSopenharmony_ci  }
575f9996aaSopenharmony_ci
585f9996aaSopenharmony_ci  _output_name = target_name
595f9996aaSopenharmony_ci  if (defined(invoker.output_name)) {
605f9996aaSopenharmony_ci    _output_name = invoker.output_name
615f9996aaSopenharmony_ci  }
625f9996aaSopenharmony_ci
635f9996aaSopenharmony_ci  _output_extension = "z.so"
645f9996aaSopenharmony_ci  if (defined(invoker.output_extension)) {
655f9996aaSopenharmony_ci    _output_extension = invoker.output_extension
665f9996aaSopenharmony_ci  }
675f9996aaSopenharmony_ci
685f9996aaSopenharmony_ci  _native_stub_target = "${target_name}__native_stub"
695f9996aaSopenharmony_ci  _generated_native_stub_file =
705f9996aaSopenharmony_ci      target_gen_dir + "/${target_name}.stub/" +
715f9996aaSopenharmony_ci      get_path_info(_stub_description_file, "name") + ".c"
725f9996aaSopenharmony_ci
735f9996aaSopenharmony_ci  _current_label = get_label_info(":${target_name}", "label_with_toolchain")
745f9996aaSopenharmony_ci  action_with_pydeps(_native_stub_target) {
755f9996aaSopenharmony_ci    deps = _deps
765f9996aaSopenharmony_ci    script = "//build/ohos/ndk/generate_ndk_stub_file.py"
775f9996aaSopenharmony_ci    depfile = "${target_gen_dir}/${target_name}.d"
785f9996aaSopenharmony_ci    args = [
795f9996aaSopenharmony_ci      "--output",
805f9996aaSopenharmony_ci      rebase_path(_generated_native_stub_file, root_build_dir),
815f9996aaSopenharmony_ci      "--ndk-description-file",
825f9996aaSopenharmony_ci      rebase_path(_stub_description_file, root_build_dir),
835f9996aaSopenharmony_ci      "--depfile",
845f9996aaSopenharmony_ci      rebase_path(depfile, root_build_dir),
855f9996aaSopenharmony_ci    ]
865f9996aaSopenharmony_ci    inputs = [ _stub_description_file ]
875f9996aaSopenharmony_ci    outputs = [ _generated_native_stub_file ]
885f9996aaSopenharmony_ci
895f9996aaSopenharmony_ci    _stub_config_info = {
905f9996aaSopenharmony_ci      label = _current_label
915f9996aaSopenharmony_ci      lib_name = _output_name
925f9996aaSopenharmony_ci      system_capability = _system_capability
935f9996aaSopenharmony_ci    }
945f9996aaSopenharmony_ci    metadata = {
955f9996aaSopenharmony_ci      ndk_config = [ _stub_config_info ]
965f9996aaSopenharmony_ci    }
975f9996aaSopenharmony_ci  }
985f9996aaSopenharmony_ci
995f9996aaSopenharmony_ci  _stub_shlib_target = "${target_name}"
1005f9996aaSopenharmony_ci
1015f9996aaSopenharmony_ci  target_label = get_label_info(":${target_name}", "label_with_toolchain")
1025f9996aaSopenharmony_ci  if (defined(invoker.subsystem_name) && defined(invoker.part_name)) {
1035f9996aaSopenharmony_ci    subsystem_name = invoker.subsystem_name
1045f9996aaSopenharmony_ci    part_name = invoker.part_name
1055f9996aaSopenharmony_ci  } else if (defined(invoker.part_name)) {
1065f9996aaSopenharmony_ci    part_name = invoker.part_name
1075f9996aaSopenharmony_ci    _part_subsystem_info_file =
1085f9996aaSopenharmony_ci        "$root_build_dir/build_configs/parts_info/part_subsystem.json"
1095f9996aaSopenharmony_ci    _arguments = [
1105f9996aaSopenharmony_ci      "--part-name",
1115f9996aaSopenharmony_ci      part_name,
1125f9996aaSopenharmony_ci      "--part-subsystem-info-file",
1135f9996aaSopenharmony_ci      rebase_path(_part_subsystem_info_file, root_build_dir),
1145f9996aaSopenharmony_ci    ]
1155f9996aaSopenharmony_ci    get_subsystem_script = "//build/templates/common/get_subsystem_name.py"
1165f9996aaSopenharmony_ci    subsystem_name =
1175f9996aaSopenharmony_ci        exec_script(get_subsystem_script, _arguments, "trim string")
1185f9996aaSopenharmony_ci  } else if (defined(invoker.subsystem_name)) {
1195f9996aaSopenharmony_ci    subsystem_name = invoker.subsystem_name
1205f9996aaSopenharmony_ci    part_name = subsystem_name
1215f9996aaSopenharmony_ci  } else {
1225f9996aaSopenharmony_ci    subsystem_name = "build"
1235f9996aaSopenharmony_ci    part_name = "build_framework"
1245f9996aaSopenharmony_ci  }
1255f9996aaSopenharmony_ci
1265f9996aaSopenharmony_ci  module_label = get_label_info(":${target_name}", "label_with_toolchain")
1275f9996aaSopenharmony_ci  _collect_target = "${target_name}__collect"
1285f9996aaSopenharmony_ci  collect_module_target(_collect_target) {
1295f9996aaSopenharmony_ci    forward_variables_from(invoker, [ "install_images" ])
1305f9996aaSopenharmony_ci  }
1315f9996aaSopenharmony_ci
1325f9996aaSopenharmony_ci  ohos_module_name = target_name
1335f9996aaSopenharmony_ci  _module_info_target = "${target_name}_info"
1345f9996aaSopenharmony_ci  generate_module_info(_module_info_target) {
1355f9996aaSopenharmony_ci    module_name = ohos_module_name
1365f9996aaSopenharmony_ci    module_type = "lib"
1375f9996aaSopenharmony_ci    module_source_dir = target_out_dir
1385f9996aaSopenharmony_ci
1395f9996aaSopenharmony_ci    module_install_name = ohos_module_name
1405f9996aaSopenharmony_ci    if (defined(invoker.output_name)) {
1415f9996aaSopenharmony_ci      module_install_name = invoker.output_name
1425f9996aaSopenharmony_ci    }
1435f9996aaSopenharmony_ci
1445f9996aaSopenharmony_ci    module_install_images = [ "system" ]
1455f9996aaSopenharmony_ci    if (defined(invoker.install_images)) {
1465f9996aaSopenharmony_ci      module_install_images = []
1475f9996aaSopenharmony_ci      module_install_images += invoker.install_images
1485f9996aaSopenharmony_ci    }
1495f9996aaSopenharmony_ci
1505f9996aaSopenharmony_ci    module_output_extension = shlib_extension
1515f9996aaSopenharmony_ci    if (defined(invoker.output_extension)) {
1525f9996aaSopenharmony_ci      module_output_extension = "." + invoker.output_extension
1535f9996aaSopenharmony_ci    }
1545f9996aaSopenharmony_ci
1555f9996aaSopenharmony_ci    install_enable = false
1565f9996aaSopenharmony_ci    if (defined(invoker.install_enable)) {
1575f9996aaSopenharmony_ci      install_enable = invoker.install_enable
1585f9996aaSopenharmony_ci    }
1595f9996aaSopenharmony_ci
1605f9996aaSopenharmony_ci    if (defined(invoker.module_install_dir)) {
1615f9996aaSopenharmony_ci      module_install_dir = invoker.module_install_dir
1625f9996aaSopenharmony_ci    }
1635f9996aaSopenharmony_ci
1645f9996aaSopenharmony_ci    if (defined(invoker.relative_install_dir)) {
1655f9996aaSopenharmony_ci      relative_install_dir = invoker.relative_install_dir
1665f9996aaSopenharmony_ci    }
1675f9996aaSopenharmony_ci
1685f9996aaSopenharmony_ci    if (defined(invoker.symlink_target_name)) {
1695f9996aaSopenharmony_ci      symlink_target_name = invoker.symlink_target_name
1705f9996aaSopenharmony_ci    }
1715f9996aaSopenharmony_ci
1725f9996aaSopenharmony_ci    if (defined(invoker.output_prefix_override)) {
1735f9996aaSopenharmony_ci      output_prefix_override = invoker.output_prefix_override
1745f9996aaSopenharmony_ci    }
1755f9996aaSopenharmony_ci    notice = "$target_out_dir/$ohos_module_name.notice.txt"
1765f9996aaSopenharmony_ci  }
1775f9996aaSopenharmony_ci
1785f9996aaSopenharmony_ci  shared_library(_stub_shlib_target) {
1795f9996aaSopenharmony_ci    forward_variables_from(invoker,
1805f9996aaSopenharmony_ci                           [
1815f9996aaSopenharmony_ci                             "cflags",
1825f9996aaSopenharmony_ci                             "ldflags",
1835f9996aaSopenharmony_ci                             "configs",
1845f9996aaSopenharmony_ci                             "public_configs",
1855f9996aaSopenharmony_ci                             "libs",
1865f9996aaSopenharmony_ci                             "include_dirs",
1875f9996aaSopenharmony_ci                           ])
1885f9996aaSopenharmony_ci    deps = [
1895f9996aaSopenharmony_ci      ":$_native_stub_target",
1905f9996aaSopenharmony_ci      ":${_collect_target}",
1915f9996aaSopenharmony_ci    ]
1925f9996aaSopenharmony_ci    if (!skip_gen_module_info) {
1935f9996aaSopenharmony_ci      deps += [ ":$_module_info_target" ]
1945f9996aaSopenharmony_ci    }
1955f9996aaSopenharmony_ci    sources = [ _generated_native_stub_file ]
1965f9996aaSopenharmony_ci    output_dir = target_out_dir
1975f9996aaSopenharmony_ci    output_name = _output_name
1985f9996aaSopenharmony_ci    output_extension = _output_extension
1995f9996aaSopenharmony_ci
2005f9996aaSopenharmony_ci    if (defined(visibility) && visibility != []) {
2015f9996aaSopenharmony_ci      visibility += [ "//build/*" ]
2025f9996aaSopenharmony_ci      if (defined(build_ext_path)) {
2035f9996aaSopenharmony_ci        visibility += [ "${build_ext_path}/*" ]
2045f9996aaSopenharmony_ci      }
2055f9996aaSopenharmony_ci    }
2065f9996aaSopenharmony_ci
2075f9996aaSopenharmony_ci    install_module_info = {
2085f9996aaSopenharmony_ci      module_def = target_label
2095f9996aaSopenharmony_ci      module_info_file =
2105f9996aaSopenharmony_ci          rebase_path(get_label_info(module_def, "target_out_dir"),
2115f9996aaSopenharmony_ci                      root_build_dir) + "/${target_name}_module_info.json"
2125f9996aaSopenharmony_ci      subsystem_name = subsystem_name
2135f9996aaSopenharmony_ci      part_name = part_name
2145f9996aaSopenharmony_ci      toolchain = current_toolchain
2155f9996aaSopenharmony_ci      toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
2165f9996aaSopenharmony_ci    }
2175f9996aaSopenharmony_ci    metadata = {
2185f9996aaSopenharmony_ci      install_modules = [ install_module_info ]
2195f9996aaSopenharmony_ci    }
2205f9996aaSopenharmony_ci  }
2215f9996aaSopenharmony_ci}
2225f9996aaSopenharmony_ci
2235f9996aaSopenharmony_ci# Generate native stub library version script from native stub description file for system components.
2245f9996aaSopenharmony_ci#
2255f9996aaSopenharmony_ci# Variables:
2265f9996aaSopenharmony_ci#  stub_description_file: stub description file, json format with stub function names
2275f9996aaSopenharmony_ci#
2285f9996aaSopenharmony_ci# Example:
2295f9996aaSopenharmony_ci# ohos_native_stub_versionscript("libtest_stub_versionscript") {
2305f9996aaSopenharmony_ci#   stub_description_file = "./libtest.stub.json"
2315f9996aaSopenharmony_ci# }
2325f9996aaSopenharmony_ci# It will generate version script with symbols defined in libtest.stub.json.
2335f9996aaSopenharmony_ci# The generated version script location is:
2345f9996aaSopenharmony_ci#    get_label_info(":libtest_stub_versionscript", "target_gen_dir") + "/" +
2355f9996aaSopenharmony_ci#      get_label_info(":libtest_stub_versionscript", "name") + stub_version_script_suffix
2365f9996aaSopenharmony_ci#
2375f9996aaSopenharmony_ci# ohos_executable() or ohos_shared_library() can use version scripit as follow:
2385f9996aaSopenharmony_ci# ohos_shared_library("libtest") {
2395f9996aaSopenharmony_ci#   ...
2405f9996aaSopenharmony_ci#   deps += [ ":libtest_stub_versionscript"]
2415f9996aaSopenharmony_ci#   version_script = get_label_info(":libtest_stub_versionscript", "target_gen_dir") + "/" +
2425f9996aaSopenharmony_ci#         get_label_info(":libtest_stub_versionscript", "name") + stub_version_script_suffix
2435f9996aaSopenharmony_ci#   ...
2445f9996aaSopenharmony_ci# }
2455f9996aaSopenharmony_ci#  In this way, libtest.z.so will only export symbols specified in libtest.stub.json.
2465f9996aaSopenharmony_ci#
2475f9996aaSopenharmony_ci#
2485f9996aaSopenharmony_citemplate("ohos_native_stub_versionscript") {
2495f9996aaSopenharmony_ci  assert(defined(invoker.stub_description_file),
2505f9996aaSopenharmony_ci         "stub description file is necessary ")
2515f9996aaSopenharmony_ci
2525f9996aaSopenharmony_ci  _stub_description_file = invoker.stub_description_file
2535f9996aaSopenharmony_ci
2545f9996aaSopenharmony_ci  _deps = []
2555f9996aaSopenharmony_ci  if (defined(invoker.deps)) {
2565f9996aaSopenharmony_ci    _deps += invoker.deps
2575f9996aaSopenharmony_ci  }
2585f9996aaSopenharmony_ci
2595f9996aaSopenharmony_ci  _output_name = target_name
2605f9996aaSopenharmony_ci  if (defined(invoker.output_name)) {
2615f9996aaSopenharmony_ci    _output_name = invoker.output_name
2625f9996aaSopenharmony_ci  }
2635f9996aaSopenharmony_ci
2645f9996aaSopenharmony_ci  _ndk_version_script_target = target_name
2655f9996aaSopenharmony_ci  _generated_version_script =
2665f9996aaSopenharmony_ci      target_gen_dir + "/$target_name" + stub_version_script_suffix
2675f9996aaSopenharmony_ci  action_with_pydeps(_ndk_version_script_target) {
2685f9996aaSopenharmony_ci    deps = _deps
2695f9996aaSopenharmony_ci    script = "//build/ohos/ndk/generate_version_script.py"
2705f9996aaSopenharmony_ci    depfile = "${target_gen_dir}/${target_name}.d"
2715f9996aaSopenharmony_ci    args = [
2725f9996aaSopenharmony_ci      "--output",
2735f9996aaSopenharmony_ci      rebase_path(_generated_version_script, root_build_dir),
2745f9996aaSopenharmony_ci      "--ndk-description-file",
2755f9996aaSopenharmony_ci      rebase_path(_stub_description_file, root_build_dir),
2765f9996aaSopenharmony_ci      "--shlib-name",
2775f9996aaSopenharmony_ci      _output_name,
2785f9996aaSopenharmony_ci      "--depfile",
2795f9996aaSopenharmony_ci      rebase_path(depfile, root_build_dir),
2805f9996aaSopenharmony_ci    ]
2815f9996aaSopenharmony_ci    outputs = [ _generated_version_script ]
2825f9996aaSopenharmony_ci  }
2835f9996aaSopenharmony_ci}
2845f9996aaSopenharmony_ci
2855f9996aaSopenharmony_ci# Specify native-stub header files
2865f9996aaSopenharmony_ci#
2875f9996aaSopenharmony_ci# Input variables:
2885f9996aaSopenharmony_ci#   sources: List of files.
2895f9996aaSopenharmony_ci#
2905f9996aaSopenharmony_citemplate("ohos_native_stub_headers") {
2915f9996aaSopenharmony_ci  assert(defined(invoker.sources), "sources are necessary ")
2925f9996aaSopenharmony_ci
2935f9996aaSopenharmony_ci  _stub_header_signature_target = "${target_name}__stub_signature_check"
2945f9996aaSopenharmony_ci  _target_name = target_name
2955f9996aaSopenharmony_ci  action_with_pydeps(_stub_header_signature_target) {
2965f9996aaSopenharmony_ci    if (defined(invoker.deps)) {
2975f9996aaSopenharmony_ci      deps = invoker.deps
2985f9996aaSopenharmony_ci    }
2995f9996aaSopenharmony_ci
3005f9996aaSopenharmony_ci    script = "//build/ohos/ndk/check_ndk_header_signature.py"
3015f9996aaSopenharmony_ci    depfile = "${target_gen_dir}/${target_name}.d"
3025f9996aaSopenharmony_ci
3035f9996aaSopenharmony_ci    inputs = []
3045f9996aaSopenharmony_ci    foreach(src, invoker.sources) {
3055f9996aaSopenharmony_ci      _all_files = []
3065f9996aaSopenharmony_ci      _all_files = exec_script("//build/scripts/find.py",
3075f9996aaSopenharmony_ci                               [ rebase_path(src) ],
3085f9996aaSopenharmony_ci                               "list lines")
3095f9996aaSopenharmony_ci
3105f9996aaSopenharmony_ci      inputs += _all_files
3115f9996aaSopenharmony_ci    }
3125f9996aaSopenharmony_ci
3135f9996aaSopenharmony_ci    _output = "$target_gen_dir/$target_name.stamp"
3145f9996aaSopenharmony_ci
3155f9996aaSopenharmony_ci    args = [
3165f9996aaSopenharmony_ci      "--depfile",
3175f9996aaSopenharmony_ci      rebase_path(depfile, root_build_dir),
3185f9996aaSopenharmony_ci      "--generated-signature",
3195f9996aaSopenharmony_ci      rebase_path("$stub_signature_out_dir/$_target_name/signature.txt",
3205f9996aaSopenharmony_ci                  root_build_dir),
3215f9996aaSopenharmony_ci      "--saved-signature",
3225f9996aaSopenharmony_ci      rebase_path("$native_stub_signature_save_dir/$_target_name/signature.txt",
3235f9996aaSopenharmony_ci                  root_build_dir),
3245f9996aaSopenharmony_ci      "--output",
3255f9996aaSopenharmony_ci      rebase_path(_output, root_build_dir),
3265f9996aaSopenharmony_ci    ]
3275f9996aaSopenharmony_ci    foreach(f, inputs) {
3285f9996aaSopenharmony_ci      args += [
3295f9996aaSopenharmony_ci        "--headers",
3305f9996aaSopenharmony_ci        rebase_path(f, root_build_dir),
3315f9996aaSopenharmony_ci        "--root-build-dir",
3325f9996aaSopenharmony_ci        rebase_path("//", root_build_dir),
3335f9996aaSopenharmony_ci      ]
3345f9996aaSopenharmony_ci    }
3355f9996aaSopenharmony_ci
3365f9996aaSopenharmony_ci    outputs = [ _output ]
3375f9996aaSopenharmony_ci  }
3385f9996aaSopenharmony_ci}
339