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