From 91b8be1a1fec9122eb025ad510d9626182797615 Mon Sep 17 00:00:00 2001 From: CFC4N Date: Wed, 1 Jan 2025 20:59:39 +0800 Subject: [PATCH] fix(make): improve error handling and clean target logic in Makefile (#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. --- Makefile | 25 ++++++++----------------- builder/init_env.sh | 8 +++++--- functions.mk | 8 ++++---- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 22eef9b0c..259370c91 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -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) @@ -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) \ @@ -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 @@ -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)) diff --git a/builder/init_env.sh b/builder/init_env.sh index 49fc13409..f7f470bdb 100755 --- a/builder/init_env.sh +++ b/builder/init_env.sh @@ -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 diff --git a/functions.mk b/functions.mk index 67ac96cdf..54b5cf3f4 100644 --- a/functions.mk +++ b/functions.mk @@ -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 @@ -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