blob: 1d4ad0d648ab3544340fb9d862e44f00729fd87e [file] [log] [blame]
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This template uses the kernel vmlinux (from sysroot) to generate:
# 1. vmlinux.h source that BPFs skeletons build against. Placed in
# vmlinux_out_dir.
# 2. vmlinux.full.btf object that can optionally be used to generate tailored
# min CO-RE BTF objects. Placed in btf_out_dir.
template("generate_vmlinux") {
forward_variables_from(invoker,
[
"btf_out_dir",
"vmlinux_out_dir",
])
out_header = "${root_gen_dir}/${vmlinux_out_dir}/vmlinux.h"
out_btf = "${root_gen_dir}/${btf_out_dir}/vmlinux.full.btf"
action(target_name) {
outputs = [ out_header ]
script = "//common-mk/generate-ebpf.py"
args = [
"gen_vmlinux",
"--sysroot=${sysroot}",
"--out-header=${out_header}",
"--out-btf=${out_btf}",
]
}
}
# This template compiles a C program to target the Linux BPF VM.
# Also optionally generates a min CO-RE BTF iff called with a non-empty
# btf_inout_dir. The btf_inout_dir must contain a vmlinux.full.btf.
# Generated C code will be placed in bpf_skeleton_out_dir. Generated min CO-RE
# BTF (if requested) will be placed in btf_inout_dir.
template("generate_ebpf_object") {
forward_variables_from(invoker,
[
"obj_out_dir",
"btf_inout_dir",
"defines",
"deps",
])
obj_dir = "${root_gen_dir}/${obj_out_dir}"
arch = getenv("ARCH")
action_foreach(target_name) {
out_obj = "${obj_dir}/{{source_name_part}}.o"
sources = invoker.sources
outputs = [ out_obj ]
script = "//common-mk/generate-ebpf.py"
args = [
"compile_bpf",
"--out-obj=${out_obj}",
"--source={{source}}",
"--arch=${arch}",
"--sysroot=${sysroot}",
"--include",
"${platform2_root}",
"${root_gen_dir}",
]
if (defined(defines)) {
args += [ "--defines" ]
foreach(define, defines) {
args += [ define ]
}
}
if (btf_inout_dir != "") {
out_min_btf =
"${root_gen_dir}/${btf_inout_dir}/{{source_name_part}}.min.btf"
vmlinux_btf = "${root_gen_dir}/${btf_inout_dir}/vmlinux.full.btf"
args += [
"--out-min-btf=${out_min_btf}",
"--vmlinux-btf=${vmlinux_btf}",
]
outputs += [ out_min_btf ]
}
}
}
# This template compiles a C program to target the Linux BPF VM and then
# generates C code to load and interact with the BPF application.
# Also optionally generates a min CO-RE BTF iff called with a non-empty
# btf_inout_dir. The btf_inout_dir must contain a vmlinux.full.btf.
# Generated C code will be placed in bpf_skeleton_out_dir. Generated min CO-RE
# BTF (if requested) will be placed in btf_inout_dir.
template("generate_ebpf_skeletons") {
forward_variables_from(invoker,
[
"bpf_skeleton_out_dir",
"btf_inout_dir",
"defines",
"deps",
])
obj_dir = "${root_gen_dir}/obj"
bpf_skel_h_dir = "${root_gen_dir}/${bpf_skeleton_out_dir}"
arch = getenv("ARCH")
action_foreach(target_name) {
out_obj = "${obj_dir}/{{source_name_part}}.o"
out_bpf_skeleton_header =
"${bpf_skel_h_dir}/skeleton_{{source_name_part}}.h"
sources = invoker.sources
outputs = [ out_bpf_skeleton_header ]
script = "//common-mk/generate-ebpf.py"
args = [
"compile_bpf",
"--out-obj=${out_obj}",
"--out-bpf-skeleton-header=${out_bpf_skeleton_header}",
"--source={{source}}",
"--arch=${arch}",
"--sysroot=${sysroot}",
"--include",
"${platform2_root}",
"${root_gen_dir}",
]
if (defined(defines)) {
args += [ "--defines" ]
foreach(define, defines) {
args += [ define ]
}
}
if (btf_inout_dir != "") {
out_min_btf =
"${root_gen_dir}/${btf_inout_dir}/{{source_name_part}}.min.btf"
vmlinux_btf = "${root_gen_dir}/${btf_inout_dir}/vmlinux.full.btf"
args += [
"--out-min-btf=${out_min_btf}",
"--vmlinux-btf=${vmlinux_btf}",
]
outputs += [ out_min_btf ]
}
}
}