Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Multi-file BPF C builds (#2393)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay authored Jan 11, 2019
1 parent 79b334b commit 23c43ed
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 33 deletions.
File renamed without changes.
11 changes: 11 additions & 0 deletions programs/bpf/c/src/bench_alu/test_bench_alu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <criterion/criterion.h>
#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);
}
14 changes: 14 additions & 0 deletions programs/bpf/c/src/multiple_file/entrypoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @brief Example C-based BPF program that prints out the parameters
* passed to it
*/
#include <solana_sdk.h>

#include "helper.h"

extern bool entrypoint(const uint8_t *input) {
sol_log(__FILE__);
helper_function();
sol_log(__FILE__);
return true;
}
11 changes: 11 additions & 0 deletions programs/bpf/c/src/multiple_file/helper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @brief Example C-based BPF program that prints out the parameters
* passed to it
*/
#include <solana_sdk.h>

#include "helper.h"

void helper_function(void) {
sol_log(__FILE__);
}
7 changes: 7 additions & 0 deletions programs/bpf/c/src/multiple_file/helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @brief Example C-based BPF program that prints out the parameters
* passed to it
*/
#include <solana_sdk.h>

void helper_function(void);
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ extern bool entrypoint(const uint8_t *input) {
sol_log_params(ka, ka_len, data, data_len);
return true;
}

111 changes: 79 additions & 32 deletions sdk/bpf/bpf.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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, $<))
Expand Down
2 changes: 1 addition & 1 deletion tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 23c43ed

Please sign in to comment.