Skip to content

Commit

Permalink
fix(make): improve error handling and clean target logic in Makefile (#…
Browse files Browse the repository at this point in the history
…713)

1. Fix clean target to properly handle libpcap Makefile existence check
2. Use if statement instead of && operator for better control flow
3. Avoid executing unnecessary commands when libpcap is not present

This change makes the build process more robust and prevents false error
reports during cleanup operations.
  • Loading branch information
cfc4n authored Jan 1, 2025
1 parent 5868917 commit 91b8be1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
25 changes: 8 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ clean:
$(CMD_RM) -f assets/ebpf_probe.go
$(CMD_RM) -f bin/ecapture
$(CMD_RM) -f .check*
@if [ -e ./lib/libpcap/Makefile ] ; then \
cd ./lib/libpcap && make clean
fi
if test -e "./lib/libpcap/Makefile"; then $(MAKE) -C ./lib/libpcap clean; fi

.PHONY: $(KERN_OBJECTS)
$(KERN_OBJECTS): %.o: %.c \
Expand All @@ -126,14 +124,14 @@ $(KERN_OBJECTS): %.o: %.c \
$(BPFHEADER) \
-target bpfel -c $< -o $(subst kern/,user/bytecode/,$(subst .o,_core.o,$@)) \
-fno-ident -fdebug-compilation-dir . -g -D__BPF_TARGET_MISSING="GCC error \"The eBPF is using target specific macros, please provide -target\"" \
-MD -MP
-MD -MP || exit 1
$(CMD_CLANG) -D__TARGET_ARCH_$(LINUX_ARCH) \
$(EXTRA_CFLAGS) \
$(BPFHEADER) \
-DKERNEL_LESS_5_2 \
-target bpfel -c $< -o $(subst kern/,user/bytecode/,$(subst .c,_core$(KERNEL_LESS_5_2_PREFIX),$<)) \
-fno-ident -fdebug-compilation-dir . -g -D__BPF_TARGET_MISSING="GCC error \"The eBPF is using target specific macros, please provide -target\"" \
-MD -MP
-MD -MP || exit 1

.PHONY: autogen
autogen: .checkver_$(CMD_BPFTOOL)
Expand Down Expand Up @@ -164,7 +162,7 @@ $(KERN_OBJECTS_NOCORE): %.nocore: %.c \
-o - |$(CMD_LLC) \
-march=bpf \
-filetype=obj \
-o $(subst kern/,user/bytecode/,$(subst .c,_noncore.o,$<))
-o $(subst kern/,user/bytecode/,$(subst .c,_noncore.o,$<)) || exit 1
$(CMD_CLANG) \
$(EXTRA_CFLAGS_NOCORE) \
$(BPFHEADER) \
Expand All @@ -180,13 +178,10 @@ $(KERN_OBJECTS_NOCORE): %.nocore: %.c \
-o - |$(CMD_LLC) \
-march=bpf \
-filetype=obj \
-o $(subst kern/,user/bytecode/,$(subst .c,_noncore$(KERNEL_LESS_5_2_PREFIX),$<))
-o $(subst kern/,user/bytecode/,$(subst .c,_noncore$(KERNEL_LESS_5_2_PREFIX),$<)) || exit 1

.PHONY: assets
assets: \
.checkver_$(CMD_GO) \
ebpf \
ebpf_noncore
assets: .checkver_$(CMD_GO) ebpf ebpf_noncore
$(CMD_GO) run github.com/shuLhan/go-bindata/cmd/go-bindata $(IGNORE_LESS52) -pkg assets -o "assets/ebpf_probe.go" $(wildcard ./user/bytecode/*.o)

.PHONY: assets_noncore
Expand All @@ -205,14 +200,10 @@ $(TARGET_LIBPCAP):
--without-dpdk --without-dag --without-septel --without-snf \
--without-gcc --with-pcap=linux \
--without-turbocap --host=$(LIBPCAP_ARCH) && \
CC=$(CMD_CC_PREFIX)$(CMD_CC) AR=$(CMD_AR_PREFIX)$(CMD_AR) make
CC=$(CMD_CC_PREFIX)$(CMD_CC) AR=$(CMD_AR_PREFIX)$(CMD_AR) make || exit 1

.PHONY: build
build: \
.checkver_$(CMD_GO) \
$(TARGET_LIBPCAP) \
assets \
assets_noncore
build: .checkver_$(CMD_GO) $(TARGET_LIBPCAP) assets assets_noncore
$(call allow-override,VERSION_FLAG,$(UNAME_R))
$(call gobuild, $(ANDROID))

Expand Down
8 changes: 5 additions & 3 deletions builder/init_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ echo "GOBIN_ZIP:${GOBIN_ZIP}"
cd ~ || exit

uname -a
sudo apt-get update
# 环境安装
sudo apt-get install --yes build-essential pkgconf libelf-dev llvm${CLANG_NUM} clang${CLANG_NUM} linux-tools-common linux-tools-generic ${CROSS_COMPILE_DEB} libssl-dev flex bison bc linux-source
sudo apt-get update || { echo "apt-get update failed"; exit 1; }
# 环境安装,添加错误检查
sudo apt-get -y install build-essential pkgconf libelf-dev llvm${CLANG_NUM} \
clang${CLANG_NUM} linux-tools-common linux-tools-generic ${CROSS_COMPILE_DEB} \
libssl-dev flex bison bc linux-source || { echo "apt-get install failed"; exit 1; }
for tool in "clang" "llc" "llvm-strip"
do
sudo rm -f /usr/bin/$tool
Expand Down
8 changes: 4 additions & 4 deletions functions.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
$(CMD_TOUCH) $@ # avoid target rebuilds over and over due to inexistent file


# golang 版本检测 1.21 以上
# golang 版本检测 1.22 以上
.checkver_$(CMD_GO): \
| .check_$(CMD_GO)
@if [ ${GO_VERSION_MAJ} -eq 1 ]; then
if [ ${GO_VERSION_MIN} -lt 21 ]; then
echo -n "you MUST use golang 1.21 or newer, "
if [ ${GO_VERSION_MIN} -lt 22 ]; then
echo -n "you MUST use golang 1.22 or newer, "
echo "your current golang version is ${GO_VERSION}"
exit 1
fi
Expand All @@ -49,7 +49,7 @@ define gobuild
CGO_CFLAGS='-O2 -g -gdwarf-4 -I$(CURDIR)/lib/libpcap/' \
CGO_LDFLAGS='-O2 -g -L$(CURDIR)/lib/libpcap/ -lpcap -static' \
GOOS=linux GOARCH=$(GOARCH) CC=$(CMD_CC_PREFIX)$(CMD_CC) \
$(CMD_GO) build -tags '$(TARGET_TAG),netgo' -ldflags "-w -s -X 'github.com/gojue/ecapture/cli/cmd.GitVersion=$(TARGET_TAG)_$(GOARCH):$(VERSION_NUM):$(VERSION_FLAG)' -X 'github.com/gojue/ecapture/cli/cmd.ByteCodeFiles=$(BYTECODE_FILES)' -linkmode=external -extldflags -static " -o $(OUT_BIN)
$(CMD_GO) build -trimpath -buildmode=pie -mod=readonly -tags '$(TARGET_TAG),netgo' -ldflags "-w -s -X 'github.com/gojue/ecapture/cli/cmd.GitVersion=$(TARGET_TAG)_$(GOARCH):$(VERSION_NUM):$(VERSION_FLAG)' -X 'github.com/gojue/ecapture/cli/cmd.ByteCodeFiles=$(BYTECODE_FILES)' -linkmode=external -extldflags -static " -o $(OUT_BIN)
$(CMD_FILE) $(OUT_BIN)
endef

Expand Down

0 comments on commit 91b8be1

Please sign in to comment.