Skip to content

Commit 05c398d

Browse files
add compile command
1 parent b474dfc commit 05c398d

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

examples/execve3.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
from pythonbpf.decorators import bpf, map, section, bpfglobal
2-
from ctypes import c_void_p, c_int64, c_int32, c_uint64
1+
import pythonbpf as pb
32
from pythonbpf.helpers import bpf_ktime_get_ns
43
from pythonbpf.maps import HashMap
54

5+
from ctypes import c_void_p, c_int64, c_int32, c_uint64
66

7-
@bpf
8-
@map
7+
@pb.bpf
8+
@pb.map
99
def last() -> HashMap:
1010
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
1111

1212

13-
@bpf
14-
@section("tracepoint/syscalls/sys_enter_execve")
13+
@pb.bpf
14+
@pb.section("tracepoint/syscalls/sys_enter_execve")
1515
def hello(ctx: c_void_p) -> c_int32:
1616
print("entered")
1717
print("multi constant support")
1818
return c_int32(0)
1919

2020

21-
@bpf
22-
@section("tracepoint/syscalls/sys_exit_execve")
21+
@pb.bpf
22+
@pb.section("tracepoint/syscalls/sys_exit_execve")
2323
def hello_again(ctx: c_void_p) -> c_int64:
2424
print("exited")
2525
key = 0
@@ -34,7 +34,9 @@ def hello_again(ctx: c_void_p) -> c_int64:
3434
return c_int64(0)
3535

3636

37-
@bpf
38-
@bpfglobal
37+
@pb.bpf
38+
@pb.bpfglobal
3939
def LICENSE() -> str:
4040
return "GPL"
41+
42+
pb.compile()

pythonbpf/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .decorators import bpf, map, section, bpfglobal
2+
from .codegen import compile_to_ir, compile

pythonbpf/codegen.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from .maps_pass import maps_proc
66
from .globals_pass import globals_processing
77
import os
8-
8+
import subprocess
9+
import pathlib
10+
import __main__
911

1012
def find_bpf_chunks(tree):
1113
"""Find all functions decorated with @bpf in the AST."""
@@ -92,3 +94,18 @@ def compile_to_ir(filename: str, output: str):
9294
f.write("\n")
9395

9496
return output
97+
98+
def compile():
99+
main_file = pathlib.Path(__main__.__file__).resolve()
100+
101+
ll_file = pathlib.Path("/tmp") / main_file.with_suffix(".ll").name
102+
o_file = main_file.with_suffix(".o")
103+
104+
compile_to_ir(str(main_file), str(ll_file))
105+
106+
subprocess.run([
107+
"llc", "-march=bpf", "-filetype=obj", "-O2",
108+
str(ll_file), "-o", str(o_file)
109+
], check=True)
110+
111+
print(f"Object written to {o_file}")

0 commit comments

Comments
 (0)