1# Copyright (c) 2024 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/templates/common/copy.gni")
16
17# Append something (files or lines) to a file and install it.
18#
19# Variables:
20#  source: file to be appended (Required)
21#  deps: some targets deps on (Optional)
22#  output: the final install file name (Optional);
23#       If not set, it will install with the source file name
24#  ranges: uid/gid value range (Required)
25#
26# Example:
27# ohos_passwd_appender("passwd") {
28#   sources = ["//base/startup/init/services/etc/passwd",
29#             "//base/startup/init/services/etc/passwd1",
30#             "//base/startup/init/services/etc/passwd2"
31#            ]
32#   ranges = ["0-6999", "7000-8000", "8001-9000"]
33#   output = "//base/startup/init/services/etc/passwd"
34# }
35# It will append files to source passwd file after deps targets
36#
37template("ohos_passwd_appender") {
38  assert(defined(invoker.sources), "source must be defined.")
39  assert(defined(invoker.ranges), "ranges must be defined.")
40  assert(defined(invoker.output), "output must be defined.")
41
42  _passwd_appender_target = "${target_name}_passwd_fixed"
43  _final_install_name = get_path_info(invoker.output, "file")
44  tmp_install_file = target_gen_dir + "/${target_name}.tmp"
45
46  passwd_file_list = []
47  foreach(file, invoker.sources) {
48    passwd_file_list += [ rebase_path(file, root_build_dir) ]
49  }
50  input_passwd_files = string_join(":", passwd_file_list)
51  input_ranges = string_join(":", invoker.ranges)
52
53  action_with_pydeps(_passwd_appender_target) {
54    script =
55        "//base/startup/init/services/etc/passwd_appender/passwd_appender.py"
56    depfile = "${target_gen_dir}/${target_name}.d"
57    if (defined(invoker.deps)) {
58      deps = invoker.deps
59    } else {
60      deps = []
61    }
62    args = [
63      "--output",
64      rebase_path(tmp_install_file, root_build_dir),
65      "--source-file",
66      input_passwd_files,
67      "--input-ranges",
68      input_ranges,
69      "--depfile",
70      rebase_path(depfile, root_build_dir),
71    ]
72    outputs = [ tmp_install_file ]
73  }
74
75  ohos_copy(target_name) {
76    deps = [ ":$_passwd_appender_target" ]
77    forward_variables_from(invoker,
78                           [
79                             "testonly",
80                             "visibility",
81
82                             "deps",
83                             "public_configs",
84                             "subsystem_name",
85                             "part_name",
86
87                             # For generate_module_info
88                             "install_images",
89                             "module_install_dir",
90                             "relative_install_dir",
91                             "symlink_target_name",
92
93                             # Open source license related
94                             "license_file",
95                             "license_as_sources",
96                           ])
97    sources = [ tmp_install_file ]
98    outputs = [ "${target_out_dir}/${target_name}/${_final_install_name}" ]
99    module_type = "etc"
100    install_enable = true
101    module_source_dir = "${target_out_dir}/${target_name}"
102    module_install_name = _final_install_name
103    if (defined(invoker.install_enable)) {
104      install_enable = invoker.install_enable
105    }
106  }
107}
108