Update Makefile: default Zig linker, UPX compression options, version info injection

This commit is contained in:
2025-12-04 00:33:45 +08:00
parent 7fff13e48c
commit 6eeb4bb494

View File

@@ -8,57 +8,85 @@ VERSION := 1.0.1
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo none) GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo none)
BUILD_TIME := $(shell date -u +%Y-%m-%dT%H:%M:%SZ) BUILD_TIME := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
# Detect tools # ----------------------------------------------------
DETECTED_ZIG := $(shell command -v zig 2>/dev/null) # Detect tools (crossplatform compatible)
DETECTED_UPX := $(shell command -v upx 2>/dev/null) # ----------------------------------------------------
# User config # Detect Zig
ZIG ?= 1 # auto enable zig linker if exists ifeq ($(OS),Windows_NT)
BRUTE ?= 0 # UPX brute mode disabled by default ZIG_BIN := $(shell where zig 2>nul)
else
ZIG_BIN := $(shell which zig 2>/dev/null)
endif
# Detect UPX
ifeq ($(OS),Windows_NT)
UPX_BIN := $(shell where upx 2>nul)
else
UPX_BIN := $(shell which upx 2>/dev/null)
endif
# ----------------------------------------------------
# User Options
# ----------------------------------------------------
BRUTE ?= 0 # UPX ultra-brute disabled by default
.PHONY: all build run clean .PHONY: all build run clean
all: build all: build
# ----------------------------------------------------
# Build
# ----------------------------------------------------
build: build:
@mkdir -p ./bin @mkdir -p ./bin
@if [ "$(ZIG)" = "1" ] && [ -n "$(DETECTED_ZIG)" ]; then \ @echo "Version: $(VERSION)"
echo "Zig detected -> Using Zig as Go external linker"; \ @echo "Commit: $(GIT_COMMIT)"
export CGO_ENABLED=0; \ @echo "Time: $(BUILD_TIME)"
export CC="zig cc"; \
export CXX="zig c++"; \ # ---------- Zig External Linker ----------
@if [ -n "$(ZIG_BIN)" ]; then \
echo "Zig found -> Using Zig as external Go linker"; \
CGO_ENABLED=0 CC="zig cc" CXX="zig c++" \
$(GO) build -trimpath -ldflags "-s -w \ $(GO) build -trimpath -ldflags "-s -w \
-X 'netcli/cmd.Version=$(VERSION)' \ -X netcli/cmd.Version=$(VERSION) \
-X 'netcli/cmd.Commit=$(GIT_COMMIT)' \ -X netcli/cmd.Commit=$(GIT_COMMIT) \
-X 'netcli/cmd.BuildTime=$(BUILD_TIME)'" \ -X netcli/cmd.BuildTime=$(BUILD_TIME)" \
-o $(OUTPUT) $(SRC); \ -o $(OUTPUT) $(SRC); \
else \ else \
echo "Zig not used Using Go native linker"; \ echo "Zig not used -> Using Go native linker"; \
$(GO) build -trimpath -ldflags "-s -w \ $(GO) build -trimpath -ldflags "-s -w \
-X 'netcli/cmd.Version=$(VERSION)' \ -X netcli/cmd.Version=$(VERSION) \
-X 'netcli/cmd.Commit=$(GIT_COMMIT)' \ -X netcli/cmd.Commit=$(GIT_COMMIT) \
-X 'netcli/cmd.BuildTime=$(BUILD_TIME)'" \ -X netcli/cmd.BuildTime=$(BUILD_TIME)" \
-o $(OUTPUT) $(SRC); \ -o $(OUTPUT) $(SRC); \
fi fi
@if [ -n "$(DETECTED_UPX)" ]; then \ # ---------- UPX Compression ----------
@if [ -n "$(UPX_BIN)" ]; then \
if [ "$(BRUTE)" = "1" ]; then \ if [ "$(BRUTE)" = "1" ]; then \
echo "UPX brute mode enabled (--ultra-brute)..."; \ echo "UPX found -> Using ultra-brute compression..."; \
upx --ultra-brute $(OUTPUT); \ upx --ultra-brute $(OUTPUT); \
else \ else \
echo "UPX detected → Applying normal compression (--best --lzma)..."; \ echo "UPX found -> Using normal compression (--best --lzma)..."; \
upx --best --lzma $(OUTPUT); \ upx --best --lzma $(OUTPUT); \
fi \ fi; \
else \ else \
echo "UPX not found Skipping compression"; \ echo "UPX not found -> Skipping compression"; \
fi fi
# ----------------------------------------------------
# Run without building binary
# ----------------------------------------------------
run: run:
$(GO) run -ldflags "-s -w \ $(GO) run -ldflags "-s -w \
-X 'netcli/cmd.Version=$(VERSION)' \ -X netcli/cmd.Version=$(VERSION) \
-X 'netcli/cmd.Commit=$(GIT_COMMIT)' \ -X netcli/cmd.Commit=$(GIT_COMMIT) \
-X 'netcli/cmd.BuildTime=$(BUILD_TIME)'" $(SRC) -X netcli/cmd.BuildTime=$(BUILD_TIME)" $(SRC)
# ----------------------------------------------------
# Clean
# ----------------------------------------------------
clean: clean:
rm -f $(OUTPUT) rm -f $(OUTPUT)