@@ -1005,6 +1005,37 @@ let compile_source input_file output_dir _verbose generate_makefile btf_vmlinux_
10051005
10061006 let python_flags = if has_python_imports then " $(shell python3-config --cflags) $(shell python3-config --libs --embed 2>/dev/null || python3-config --libs)" else " " in
10071007
1008+ (* Check if kernel module was generated *)
1009+ let has_kernel_module = match kernel_module_code with | Some _ -> true | None -> false in
1010+
1011+ (* Kernel module variables and targets *)
1012+ let kernel_module_vars = if has_kernel_module then
1013+ sprintf {|
1014+ # Kernel module files
1015+ KERNEL_MODULE_SRC = % s.mod .c
1016+ KERNEL_MODULE_OBJ = % s.mod .ko| } base_name base_name
1017+ else " " in
1018+
1019+ let kernel_module_target = if has_kernel_module then " $(KERNEL_MODULE_OBJ)" else " " in
1020+
1021+ let kernel_module_rules = if has_kernel_module then
1022+ sprintf {|
1023+ # Build kernel module
1024+ $ (KERNEL_MODULE_OBJ ): $ (KERNEL_MODULE_SRC )
1025+ @ echo " Building kernel module..."
1026+ make - C / lib/ modules/$ (shell uname - r)/ build M =$ (PWD ) modules
1027+
1028+ # Install kernel module (requires root)
1029+ install- module : $ (KERNEL_MODULE_OBJ )
1030+ sudo insmod $ (KERNEL_MODULE_OBJ )
1031+
1032+ # Remove kernel module (requires root )
1033+ uninstall- module :
1034+ sudo rmmod % s || true |} base_name
1035+ else " " in
1036+
1037+ let kernel_module_clean = if has_kernel_module then " $(KERNEL_MODULE_OBJ) modules.order Module.symvers .*.cmd" else " " in
1038+
10081039 let makefile_content = Printf. sprintf {| # Multi - Program eBPF Makefile
10091040# Generated by KernelScript compiler
10101041
@@ -1018,7 +1049,7 @@ BPF_INCLUDES = -I/usr/include -I/usr/include/x86_64-linux-gnu
10181049
10191050# Userspace compilation flags
10201051CFLAGS = - Wall - Wextra - O2 - fPIC
1021- LIBS = - lbpf - lelf - lz
1052+ LIBS = - lbpf - lelf - lz% s
10221053
10231054# Object files
10241055BPF_OBJ = % s.ebpf.o
@@ -1030,7 +1061,7 @@ BPF_SRC = %s.ebpf.c
10301061USERSPACE_SRC = % s.c
10311062
10321063# Default target
1033- all :% s $ (BPF_OBJ ) $ (SKELETON_H ) $ (USERSPACE_BIN )
1064+ all :% s $ (BPF_OBJ ) $ (SKELETON_H ) $ (USERSPACE_BIN )% s
10341065
10351066# Compile eBPF C to object file
10361067$ (BPF_OBJ ): $ (BPF_SRC )
@@ -1044,11 +1075,11 @@ $(SKELETON_H): $(BPF_OBJ)
10441075# Compile userspace program (link with shared libraries )
10451076$ (USERSPACE_BIN ): $ (USERSPACE_SRC ) $ (SKELETON_H )% s
10461077 $ (CC ) $ (CFLAGS ) - o $@ $< $ (LIBS )% s% s
1047-
1078+ % s
10481079% s
10491080# Clean generated files
10501081clean:
1051- rm - f $ (BPF_OBJ ) $ (SKELETON_H ) $ (USERSPACE_BIN )% s
1082+ rm - f $ (BPF_OBJ ) $ (SKELETON_H ) $ (USERSPACE_BIN )% s% s
10521083
10531084# Build just the eBPF object without skeleton (for testing )
10541085ebpf- only: $ (BPF_OBJ )
@@ -1060,25 +1091,42 @@ run: $(USERSPACE_BIN)
10601091# Help target
10611092help:
10621093 @ echo "Available targets:"
1063- @ echo " all - Build eBPF program and userspace coordinator"
1094+ @ echo " all - Build eBPF program and userspace coordinator%s "
10641095 @ echo " ebpf-only - Build just the eBPF object file"
1065- @ echo " clean - Clean all generated files"
1096+ % s @ echo " clean - Clean all generated files"
10661097 @ echo " run - Run the userspace program (requires sudo)"
10671098
1068- .PHONY : all clean run ebpf - only help
1069- |} base_name base_name base_name base_name base_name
1070- shared_lib_deps shared_lib_deps
1099+ .PHONY : all clean run ebpf - only help % s
1100+ |} kernel_module_vars base_name base_name base_name base_name base_name
1101+ shared_lib_deps kernel_module_target shared_lib_deps
10711102 (if shared_lib_targets = " " then " " else (" " ^ String. concat " " (List. map (fun import -> " ./" ^ import.Import_resolver. module_name ^ " .so" ) ks_imports)))
10721103 python_flags
1104+ kernel_module_rules
10731105 shared_lib_rules
1074- (if shared_lib_targets = " " then " " else (" " ^ shared_lib_targets)) in
1106+ (if shared_lib_targets = " " then " " else (" " ^ shared_lib_targets))
1107+ kernel_module_clean
1108+ (if has_kernel_module then " and kernel module" else " " )
1109+ (if has_kernel_module then sprintf {| @ echo " install-module - Install kernel module (requires root)"
1110+ @ echo " uninstall-module - Remove kernel module (requires root)"
1111+ | } else " " )
1112+ (if has_kernel_module then " install-module uninstall-module" else " " ) in
10751113
10761114 let makefile_path = actual_output_dir ^ " /Makefile" in
10771115 let oc = open_out makefile_path in
10781116 output_string oc makefile_content;
10791117 close_out oc;
10801118
1081- Printf. printf " 📄 Generated Makefile: %s/Makefile\n " actual_output_dir
1119+ Printf. printf " 📄 Generated Makefile: %s/Makefile\n " actual_output_dir;
1120+
1121+ (* Generate Kbuild file if kernel module exists *)
1122+ if has_kernel_module then (
1123+ let kbuild_content = sprintf " obj-m += %s.mod.o\n " base_name in
1124+ let kbuild_path = actual_output_dir ^ " /Kbuild" in
1125+ let kbuild_oc = open_out kbuild_path in
1126+ output_string kbuild_oc kbuild_content;
1127+ close_out kbuild_oc;
1128+ Printf. printf " 📄 Generated Kbuild: %s/Kbuild\n " actual_output_dir
1129+ )
10821130 );
10831131
10841132 Printf. printf " \n ✨ Compilation completed successfully!\n " ;
0 commit comments