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/ohos/build_var.gni")
16import("//build/ohos_var.gni")
17import("${build_configs_path}/platforms_list.gni")
18
19declare_args() {
20  sdk_notice_dir = "$root_build_dir/NOTICE_FILES/sdk"
21  sdk_notice_archive_dir = "$root_build_dir/NOTICE_FILES/sdk_archives"
22  ndk_notice_dir = "$root_build_dir/NOTICE_FILES/ndk"
23  static_libraries_notice_dir = "$root_build_dir/NOTICE_FILES/static"
24}
25
26declare_args() {
27  ndk_notice_txt = "$root_build_dir/NOTICE_FILES/ndk-final-notice/NOTICE.txt"
28  ndk_notice_gz = "$root_build_dir/NOTICE_FILES/ndk-final-notice/NOTICE.xml.gz"
29  sdk_notice_txt = "$root_build_dir/NOTICE_FILES/sdk-final-notice/NOTICE.txt"
30  sdk_notice_gz = "$root_build_dir/NOTICE_FILES/sdk-final-notice/NOTICE.xml.gz"
31}
32
33# Gen notice file
34# private template
35#
36template("collect_notice") {
37  assert(defined(invoker.module_source_dir), "module_source_dir is required.")
38  if (!enable_notice_collection) {
39    group(target_name) {
40      not_needed(invoker, "*")
41    }
42  } else {
43    action_with_pydeps(target_name) {
44      forward_variables_from(invoker,
45                             [
46                               "module_name",
47                               "module_source_dir",
48                               "deps",
49                               "license_file",
50                               "testonly",
51                               "module_type",
52                               "outputs",
53                               "source_list",
54
55                               # Some license file are generated in gn gen.
56                               # Such notices should not be used as sources.
57                               "license_as_sources",
58                             ])
59      script = rebase_path("//build/ohos/notice/collect_module_notice_file.py")
60      depfile = "${target_gen_dir}/$target_name.d"
61
62      if (!defined(outputs)) {
63        outputs = []
64        if (defined(module_type) &&
65            (module_type == "static_library" || module_type == "source_set" ||
66             module_type == "rust_library")) {
67          _current_toolchain = get_label_info(current_toolchain, "name")
68          _notice_subdir = "$_current_toolchain/${invoker.subsystem_name}/${invoker.part_name}"
69
70          # Although static library and source set are not installed, their
71          # notice files still needs to be collected.
72          # We may collect a little more notice files than needed.
73          outputs += [
74            "${static_libraries_notice_dir}/$_notice_subdir/$module_name.a.txt",
75          ]
76        } else {
77          if (defined(module_type) && module_type == "java_library" &&
78              defined(license_file) &&
79              get_path_info(license_file, "extension") == "zip") {
80            outputs = [ "$target_out_dir/$module_name.notice.zip" ]
81          } else {
82            outputs += [ "$target_out_dir/$module_name.notice.txt" ]
83          }
84        }
85      }
86
87      args = [
88        "--module-source-dir",
89        rebase_path(module_source_dir, root_build_dir),
90        "--depfile",
91        rebase_path(depfile, root_build_dir),
92      ]
93      foreach(o, outputs) {
94        args += [
95          "--output",
96          rebase_path(o, root_build_dir),
97        ]
98      }
99
100      if (build_ohos_sdk && defined(module_name)) {
101        import("//build/ohos/sdk/sdk.gni")
102        if (defined(source_list)) {
103          foreach(s, source_list) {
104            args += [
105              "--sources",
106              rebase_path(s, root_build_dir),
107            ]
108          }
109        }
110        args += [
111          "--sdk-install-info-file",
112          rebase_path(generated_sdk_module_install_paths, root_out_dir),
113          "--label",
114          get_label_info(":${module_name}", "label_no_toolchain"),
115          "--sdk-notice-dir",
116          rebase_path(sdk_notice_dir, root_build_dir),
117        ]
118      } else {
119        not_needed([ "source_list" ])
120      }
121
122      if (defined(license_file)) {
123        _license_as_sources = true
124        if (defined(license_as_sources)) {
125          _license_as_sources = license_as_sources
126        }
127        if (_license_as_sources) {
128          inputs = [ license_file ]
129        }
130        args += [
131          "--license-file",
132          rebase_path(license_file, root_build_dir),
133        ]
134      }
135    }
136  }
137}
138