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/clang/clang.gni")
15import("//build/config/python.gni")
16import("//build/ohos/kernel/kernel.gni")
17import("//build/ohos/notice/notice.gni")
18import("//build/templates/bpf/ohos_bpf_config.gni")
19import("//build/templates/common/collect_target.gni")
20import("//build/templates/metadata/module_info.gni")
21
22# Generate .o files from .c files
23#
24# Variables
25#   sources: Paths to .c file to compile, one bpf target can only handle
26#     one .c source file.
27#
28# Example
29#   ohos_bpf("foo_bpf") {
30#     sources = [
31#       "xxx.c",
32#     ]
33#     subsystem_name = "xxx"
34#     part_name = "xxx"
35#   }
36template("ohos_bpf") {
37  forward_variables_from(invoker, [ "testonly" ])
38  assert(defined(invoker.sources), "sources are necessary")
39
40  subsystem_name = invoker.subsystem_name
41  part_name = invoker.part_name
42  assert(subsystem_name != "")
43  assert(part_name != "")
44  _clang = "${clang_base_path}/bin/clang"
45  _base_dir =
46      get_label_info("//build/templates/bpf:gen_bpf_uapi", "target_out_dir")
47  _include_dirs = [
48    "${_base_dir}/${bpf_inc_out_dir}/usr/include",
49    "${_base_dir}/${bpf_inc_out_dir}",
50  ]
51  if (defined(invoker.include_dirs)) {
52    _include_dirs += invoker.include_dirs
53  }
54  _src_name = get_path_info(invoker.sources, "name")
55  _output_file = "${target_out_dir}/${_src_name[0]}.o"
56
57  ohos_module_name = target_name
58  _module_info_target = "${target_name}_info"
59  generate_module_info(_module_info_target) {
60    forward_variables_from(invoker,
61                           [
62                             "module_install_dir",
63                             "relative_install_dir",
64                             "module_source_dir",
65                             "module_install_name",
66                             "module_type",
67                             "install_enable",
68                           ])
69    module_name = ohos_module_name
70    if (!defined(module_type)) {
71      module_type = "unknown"
72    }
73
74    if (!defined(module_source_dir)) {
75      module_source_dir = "${target_out_dir}"
76    }
77
78    module_install_images = [ "system" ]
79    if (defined(invoker.install_images)) {
80      module_install_images = []
81      module_install_images += invoker.install_images
82    }
83
84    module_install_name = "${_src_name[0]}.o"
85    if (defined(invoker.output_name)) {
86      module_install_name = invoker.output_name
87    }
88
89    if (defined(invoker.install_enable)) {
90      install_enable = invoker.install_enable
91    }
92
93    module_install_dir = "etc/bpf"
94    if (defined(invoker.module_install_dir)) {
95      module_install_dir = invoker.module_install_dir
96    }
97
98    if (defined(invoker.relative_install_dir)) {
99      relative_install_dir = invoker.relative_install_dir
100    }
101
102    if (defined(invoker.symlink_target_name)) {
103      symlink_target_name = invoker.symlink_target_name
104    }
105    notice = "$target_out_dir/$ohos_module_name.notice.txt"
106  }
107
108  module_label = get_label_info(":${target_name}", "label_with_toolchain")
109  _collect_target = "${target_name}__collect"
110  collect_module_target(_collect_target) {
111    forward_variables_from(invoker, [ "install_images" ])
112  }
113
114  _notice_target = "${target_name}__notice"
115  _main_target_name = target_name
116  collect_notice(_notice_target) {
117    forward_variables_from(invoker,
118                           [
119                             "testonly",
120                             "license_as_sources",
121                             "license_file",
122                           ])
123
124    module_name = _main_target_name
125    module_source_dir = get_label_info(":${_main_target_name}", "dir")
126  }
127
128  target_label = get_label_info(":${target_name}", "label_with_toolchain")
129  action_with_pydeps(target_name) {
130    script = "//build/scripts/bpf.py"
131    sources = invoker.sources
132    args = [
133      "--clang-path",
134      rebase_path(_clang, root_build_dir),
135      "--output-file",
136      rebase_path(_output_file, root_build_dir),
137      "--include-dirs",
138    ]
139    args += rebase_path(_include_dirs, root_build_dir)
140    args += [ "--input-file" ]
141    args += rebase_path(sources, root_build_dir)
142    if (defined(invoker.defines)) {
143      args += [ "--defines" ]
144      args += invoker.defines
145    }
146    deps = [
147      ":$_module_info_target",
148      ":$_notice_target",
149      ":${_collect_target}",
150      "//build/templates/bpf:gen_bpf_uapi",
151    ]
152    if (defined(invoker.deps)) {
153      deps += invoker.deps
154    }
155    outputs = [ _output_file ]
156
157    install_module_info = {
158      module_def = target_label
159      part_name = part_name
160      module_info_file =
161          rebase_path(get_label_info(module_def, "target_out_dir"),
162                      root_build_dir) + "/${target_name}_module_info.json"
163      subsystem_name = subsystem_name
164      part_name = part_name
165      toolchain = current_toolchain
166      toolchain_out_dir = rebase_path(root_out_dir, root_build_dir)
167    }
168    metadata = {
169      install_modules = [ install_module_info ]
170    }
171  }
172}
173