Skip to content

Commit 28e6f97

Browse files
add support for compilation with pylibbpf
Signed-off-by: varun-r-mallya <varunrmallya@gmail.com>
1 parent a1bc813 commit 28e6f97

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ requires-python = ">=3.8"
1616

1717
dependencies = [
1818
"llvmlite",
19-
"astpretty"
19+
"astpretty",
20+
"pylibbpf"
2021
]
2122

2223
[tool.setuptools.packages.find]

pythonbpf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .decorators import bpf, map, section, bpfglobal, struct
2-
from .codegen import compile_to_ir, compile
2+
from .codegen import compile_to_ir, compile, BPF

pythonbpf/codegen.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import subprocess
1010
import inspect
1111
from pathlib import Path
12+
from pylibbpf import BpfProgram
1213

1314

1415
def find_bpf_chunks(tree):
@@ -116,3 +117,17 @@ def compile():
116117
], check=True)
117118

118119
print(f"Object written to {o_file}, {ll_file} can be removed")
120+
121+
def BPF() -> BpfProgram:
122+
caller_frame = inspect.stack()[1]
123+
caller_file = Path(caller_frame.filename).resolve()
124+
ll_file = Path("/tmp") / caller_file.with_suffix(".ll").name
125+
o_file = Path("/tmp") / caller_file.with_suffix(".o").name
126+
compile_to_ir(str(caller_file), str(ll_file))
127+
128+
subprocess.run([
129+
"llc", "-march=bpf", "-filetype=obj", "-O2",
130+
str(ll_file), "-o", str(o_file)
131+
], check=True)
132+
133+
return BpfProgram(str(o_file))

0 commit comments

Comments
 (0)