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