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