diff --git a/programs/bpf/c/src/bench_alu.c b/programs/bpf/c/src/bench_alu/bench_alu.c similarity index 100% rename from programs/bpf/c/src/bench_alu.c rename to programs/bpf/c/src/bench_alu/bench_alu.c diff --git a/programs/bpf/c/src/bench_alu/test_bench_alu.c b/programs/bpf/c/src/bench_alu/test_bench_alu.c new file mode 100644 index 00000000000000..87b4ae8b0cc4dc --- /dev/null +++ b/programs/bpf/c/src/bench_alu/test_bench_alu.c @@ -0,0 +1,11 @@ +#include +#include "bench_alu.c" + +Test(bench_alu, sanity) { + uint64_t input[] = {500, 0}; + + cr_assert(entrypoint((uint8_t *) input)); + + cr_assert_eq(input[0], 500); + cr_assert_eq(input[1], 5); +} diff --git a/programs/bpf/c/src/move_funds.c b/programs/bpf/c/src/move_funds/move_funds.c similarity index 100% rename from programs/bpf/c/src/move_funds.c rename to programs/bpf/c/src/move_funds/move_funds.c diff --git a/programs/bpf/c/src/multiple_file/entrypoint.c b/programs/bpf/c/src/multiple_file/entrypoint.c new file mode 100644 index 00000000000000..8689ed3be89729 --- /dev/null +++ b/programs/bpf/c/src/multiple_file/entrypoint.c @@ -0,0 +1,14 @@ +/** + * @brief Example C-based BPF program that prints out the parameters + * passed to it + */ +#include + +#include "helper.h" + +extern bool entrypoint(const uint8_t *input) { + sol_log(__FILE__); + helper_function(); + sol_log(__FILE__); + return true; +} diff --git a/programs/bpf/c/src/multiple_file/helper.c b/programs/bpf/c/src/multiple_file/helper.c new file mode 100644 index 00000000000000..9067cf3d3d43f3 --- /dev/null +++ b/programs/bpf/c/src/multiple_file/helper.c @@ -0,0 +1,11 @@ +/** + * @brief Example C-based BPF program that prints out the parameters + * passed to it + */ +#include + +#include "helper.h" + +void helper_function(void) { + sol_log(__FILE__); +} diff --git a/programs/bpf/c/src/multiple_file/helper.h b/programs/bpf/c/src/multiple_file/helper.h new file mode 100644 index 00000000000000..bdb840e1aa5286 --- /dev/null +++ b/programs/bpf/c/src/multiple_file/helper.h @@ -0,0 +1,7 @@ +/** + * @brief Example C-based BPF program that prints out the parameters + * passed to it + */ +#include + +void helper_function(void); diff --git a/programs/bpf/c/src/multiple_static.c b/programs/bpf/c/src/multiple_static/multiple_static.c similarity index 100% rename from programs/bpf/c/src/multiple_static.c rename to programs/bpf/c/src/multiple_static/multiple_static.c diff --git a/programs/bpf/c/src/noop++.cc b/programs/bpf/c/src/noop++/noop++.cc similarity index 100% rename from programs/bpf/c/src/noop++.cc rename to programs/bpf/c/src/noop++/noop++.cc diff --git a/programs/bpf/c/src/noop.c b/programs/bpf/c/src/noop/noop.c similarity index 99% rename from programs/bpf/c/src/noop.c rename to programs/bpf/c/src/noop/noop.c index 499ab5b88abd03..66c487f4be78f4 100644 --- a/programs/bpf/c/src/noop.c +++ b/programs/bpf/c/src/noop/noop.c @@ -28,3 +28,4 @@ extern bool entrypoint(const uint8_t *input) { sol_log_params(ka, ka_len, data, data_len); return true; } + diff --git a/programs/bpf/c/src/struct_pass.c b/programs/bpf/c/src/struct_pass/struct_pass.c similarity index 100% rename from programs/bpf/c/src/struct_pass.c rename to programs/bpf/c/src/struct_pass/struct_pass.c diff --git a/programs/bpf/c/src/struct_ret.c b/programs/bpf/c/src/struct_ret/struct_ret.c similarity index 100% rename from programs/bpf/c/src/struct_ret.c rename to programs/bpf/c/src/struct_ret/struct_ret.c diff --git a/sdk/bpf/bpf.mk b/sdk/bpf/bpf.mk index a66ef23635d8c5..728e06d2479be0 100644 --- a/sdk/bpf/bpf.mk +++ b/sdk/bpf/bpf.mk @@ -145,42 +145,56 @@ help: @echo ' - make dump_foo' @echo '' -.PHONY: $(INSTALL_SH) -$(INSTALL_SH): - $(INSTALL_SH) +define C_RULE +$1: $2 + @echo "[cc] $1 ($2)" + $(_@)mkdir -p $(dir $1) + $(_@)$(CC) $(BPF_C_FLAGS) -o $1 -c $2 -MD -MF $(1:.ll=.d) +endef -.PRECIOUS: $(OUT_DIR)/%.o -$(OUT_DIR)/%.o: $(SRC_DIR)/%.c $(INSTALL_SH) - @echo "[cc] $@ ($<)" - $(_@)mkdir -p $(OUT_DIR) - $(_@)$(CC) $(BPF_C_FLAGS) -o $@ -c $< -MD -MF $(@:.o=.d) +define CC_RULE +$1: $2 + @echo "[cxx] $1 ($2)" + $(_@)mkdir -p $(dir $1) + $(_@)$(CXX) $(BPF_CXX_FLAGS) -o $1 -c $2 -MD -MF $(1:.ll=.d) +endef -$(OUT_DIR)/%.o: $(SRC_DIR)/%.cc $(INSTALL_SH) - @echo "[cxx] $@ ($<)" - $(_@)mkdir -p $(OUT_DIR) - $(_@)$(CXX) $(BPF_CXX_FLAGS) -o $@ -c $< -MD -MF $(@:.o=.d) +define O_RULE +$1: $2 + @echo "[llc] $1 ($2)" + $(_@)mkdir -p $(dir $1) + $(_@)$(LLC) $(BPF_LLC_FLAGS) -o $1 $2 +endef -.PRECIOUS: $(OUT_DIR)/%.so -$(OUT_DIR)/%.so: $(OUT_DIR)/%.o $(INSTALL_SH) - @echo "[lld] $@ ($<)" - $(_@)$(LLD) $(BPF_LLD_FLAGS) -o $@ $< +define SO_RULE +$1: $2 + @echo "[lld] $1 ($2)" + $(_@)mkdir -p $(dir $1) + $(_@)$(LLD) $(BPF_LLD_FLAGS) --entry entrypoint -o $1 $2 +endef -$(OUT_DIR)/test_%: $(TEST_DIR)/%.c $(INSTALL_SH) - @echo "[test cc] $@ ($<)" - $(_@)mkdir -p $(OUT_DIR) - $(_@)$(CC) $(TEST_C_FLAGS) -o $@ $< -MD -MF $(@:=.d) - $(_@)$(MACOS_ADJUST_TEST_DYLIB) $@ +define TEST_C_RULE +$1: $2 + @echo "[test cc] $1 ($2)" + $(_@)mkdir -p $(dir $1) + $(_@)$(CC) $(TEST_C_FLAGS) -o $1 $2 -MD -MF $(1:.o=.d) + $(_@)$(MACOS_ADJUST_TEST_DYLIB) $1 +endef -$(OUT_DIR)/test_%: $(TEST_DIR)/%.cc $(INSTALL_SH) - @echo "[test cxx] $@ ($<)" - $(_@)mkdir -p $(OUT_DIR) - $(_@)$(CXX) $(TEST_CXX_FLAGS) -o $@ $< -MD -MF $(@:=.d) - $(_@)$(MACOS_ADJUST_TEST_DYLIB) $@ +define TEST_CC_RULE +$1: $2 + @echo "[test cxx] $1 ($2)" + $(_@)mkdir -p $(dir $1) + $(_@)$(CXX) $(TEST_CXX_FLAGS) -o $1 $2 -MD -MF $(1:.o=.d) + $(_@)$(MACOS_ADJUST_TEST_DYLIB) $1 +endef --include $(wildcard $(OUT_DIR)/*.d) +.PHONY: $(INSTALL_SH) +$(INSTALL_SH): + $(_@)$(INSTALL_SH) -PROGRAM_NAMES := $(notdir $(basename $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/*.cc))) -TEST_NAMES := $(addprefix test_,$(notdir $(basename $(wildcard $(TEST_DIR)/*.c)))) +PROGRAM_NAMES := $(notdir $(basename $(wildcard $(SRC_DIR)/*))) +# TEST_NAMES := $(addprefix test_,$(notdir $(basename $(wildcard $(TEST_DIR)/*.c)))) define \n @@ -189,11 +203,44 @@ endef all: $(PROGRAM_NAMES) +.PHONY: $(PROGRAM_NAMES) +$(PROGRAM_NAMES): $(INSTALL_SH) +$(PROGRAM_NAMES): %: $(addprefix $(OUT_DIR)/, %.so) + +$(foreach PROGRAM, $(PROGRAM_NAMES), \ + $(eval -include $(wildcard $(OUT_DIR)/$(PROGRAM)/*.d)) \ + \ + $(eval $(PROGRAM)_SRCS := \ + $(addprefix $(SRC_DIR)/$(PROGRAM)/, \ + $(filter-out test_%,$(notdir $(wildcard $(SRC_DIR)/$(PROGRAM)/*.c $(SRC_DIR)/$(PROGRAM)/*.cc))))) \ + $(eval $(PROGRAM)_OBJS := $(subst $(SRC_DIR), $(OUT_DIR), \ + $(patsubst %.c,%.o, \ + $(patsubst %.cc,%.o,$($(PROGRAM)_SRCS))))) \ + $(eval $(call SO_RULE,$(OUT_DIR)/$(PROGRAM).so,$($(PROGRAM)_OBJS))) \ + $(foreach _,$(filter %.c,$($(PROGRAM)_SRCS)), \ + $(eval $(call C_RULE,$(subst $(SRC_DIR),$(OUT_DIR),$(_:%.c=%.o)),$_))) \ + $(foreach _,$(filter %.cc,$($(PROGRAM)_SRCS)), \ + $(eval $(call CC_RULE,$(subst $(SRC_DIR),$(OUT_DIR),$(_:%.cc=%.o)),$_))) \ + \ + $(eval TESTS := $(notdir $(basename $(wildcard $(SRC_DIR)/$(PROGRAM)/test_*.c)))) \ + $(eval $(TESTS): %: $(addprefix $(OUT_DIR)/$(PROGRAM)/, %)) \ + $(eval TEST_NAMES := $(TEST_NAMES) $(TESTS)) \ + $(foreach TEST, $(TESTS), \ + $(eval $(TEST)_SRCS := \ + $(addprefix $(SRC_DIR)/$(PROGRAM)/, \ + $(notdir $(wildcard $(SRC_DIR)/$(PROGRAM)/test_*.c $(SRC_DIR)/$(PROGRAM)/test_*.cc)))) \ + $(foreach _,$(filter %.c,$($(TEST)_SRCS)), \ + $(eval $(call TEST_C_RULE,$(subst $(SRC_DIR),$(OUT_DIR),$(_:%.c=%)),$_))) \ + $(foreach _,$(filter %.cc, $($(TEST)_SRCS)), \ + $(eval $(call TEST_CC_RULE,$(subst $(SRC_DIR),$(OUT_DIR),$(_:%.cc=%)),$_))) \ + ) \ +) + test: $(TEST_NAMES) - $(foreach test, $(TEST_NAMES), $(OUT_DIR)/$(test)$(\n)) + $(foreach test, $(TEST_NAMES), $(OUT_DIR)/bench_alu/$(test)$(\n)) -$(PROGRAM_NAMES): %: $(addprefix $(OUT_DIR)/, %.so) ; -$(TEST_NAMES): %: $(addprefix $(OUT_DIR)/, %) ; +.PHONY: $(TEST_NAMES) +$(TEST_NAMES): $(INSTALL_SH) dump_%: % $(_@)$(OBJ_DUMP) $(OBJ_DUMP_FLAGS) $(addprefix $(OUT_DIR)/, $(addsuffix .so, $<)) diff --git a/tests/programs.rs b/tests/programs.rs index 09fec36bc52c7e..9d00cdfca6e692 100644 --- a/tests/programs.rs +++ b/tests/programs.rs @@ -300,11 +300,11 @@ fn test_program_bpf_c() { solana_logger::setup(); let programs = [ + "multiple_static", "noop", "noop++", "struct_pass", "struct_ret", - "multiple_static", ]; for program in programs.iter() { println!("Test program: {:?}", program);