# 统一的 Makefile - 支持 x86 和 ARM 架构
# 使用方法：
#   make ARCH=x86 all          - 构建 x86 版本
#   make ARCH=arm image        - 构建并推送 ARM 镜像
#   make ARCH=arm only-build   - 构建 ARM 版本

arch ?= x86

# 根据架构设置变量
ifeq ($(arch),arm)
	DOCKER_CONTAINER = euspace-arm
	IMAGE_TAG_SUFFIX = arm64
	PLATFORM = linux/arm64
	DOCKERFILE = Dockerfile.arm64
else
	DOCKER_CONTAINER = euspace
	IMAGE_TAG_SUFFIX = amd64
	PLATFORM = linux/amd64
	DOCKERFILE = Dockerfile
endif

VERSION=0.0.1
FILTER=
PARAMS=

# Set debug
ifeq ($(debug),1)
PARAMS+=debug=1
endif

# Set filter pid
ifdef pid
	ifneq ($(pid),0)
		PARAMS+=pid=$(pid)
		FILTER=FILTER_PID=${pid}
	endif
endif

# Set send data
ifeq ($(send),1)
FILTER+= SEND=1
endif

# Set eBPF build kernel
ifdef kernel
	PARAMS+=kernel=${kernel}
endif

ifdef version
	VERSION=${version}
	PARAMS+=version=${version}
endif

GIT_COMMIT=$(shell git rev-parse --short HEAD)
BUILD_DATE=$(shell date '+%Y-%m-%d-%H:%M:%S')
INNER_ARCH=$(shell uname -m)

# Set Version
TARGET_FILE=dist/${INNER_ARCH}/package_dir/bin/euspace
GO_LD_FLAGS_VERSION=-X github.com/coroot/coroot-node-agent/flags.AgentVersion=${VERSION}
GO_LD_FLAGS_GIT_COMMIT=-X github.com/coroot/coroot-node-agent/flags.GitCommit=${GIT_COMMIT}
GO_LD_FLAGS_BUILD_DATE=-X github.com/coroot/coroot-node-agent/flags.BuildDate=${BUILD_DATE}
GO_LD_FLAGS=-ldflags="${GO_LD_FLAGS_VERSION} ${GO_LD_FLAGS_GIT_COMMIT} ${GO_LD_FLAGS_BUILD_DATE}"

# 帮助信息
help:
	@echo "Usage: make arch=<x86|arm> <target>"
	@echo ""
	@echo "Architecture options (case-insensitive):"
	@echo "  arch=x86     - Build for x86_64 architecture (default)"
	@echo "  arch=arm     - Build for ARM64 architecture"
	@echo ""
	@echo "Build targets:"
	@echo "  all          - Build (ebpf-build + go-build)"
	@echo "  ebpf-build   - Build eBPF components"
	@echo "  go-build     - Build Go application"
	@echo "  only-build   - Build only (ebpf-build + go-build)"
	@echo "  docker-build - Build in containerized environment (for CI/CD)"
	@echo ""
	@echo "eBPF kernel parameters (only for ebpf-build):"
	@echo "  kernel=all   - Build for all supported kernels (512, 506, 420, 416)"
	@echo "  kernel=512   - Build for kernel 5.12 only"
	@echo "  kernel=506   - Build for kernel 5.06 only"
	@echo "  kernel=420   - Build for kernel 4.20 only"
	@echo "  kernel=416   - Build for kernel 4.16 only"
	@echo ""
	@echo "Version parameters (only for go-build / image-build):"
	@echo "  version=<ver> - Set version number for Go application"
	@echo "                  version=1.1.2, version=2.0.0, etc."
	@echo ""
	@echo "Docker targets:"
	@echo "  image-build  - Build Docker image"
	@echo "  image-push   - Push Docker image"
	@echo "  image        - Build and push image"
	@echo ""
	@echo "Examples:"
	@echo "  make arch=x86 all          - Build for x86"
	@echo "  make arch=arm image        - Build and push ARM image"
	@echo "  make arch=arm only-build   - Build ARM version only"
	@echo "  make ebpf-build kernel=all - Build eBPF for all kernels"
	@echo "  make ebpf-build kernel=512 - Build eBPF for kernel 5.12 only"
	@echo "  make go-build version=1.1.2 - Build Go with version 1.1.2"
	@echo "  make docker-build kernel=all version=1.0 - CI/CD build with all kernels"

ebpf-build:
	docker exec -it $(DOCKER_CONTAINER) sh -c 'cd /opt/github/euspace/ebpftracer && make all ${PARAMS}'

go-build:
	docker exec -it $(DOCKER_CONTAINER) bash -c 'cd /opt/github/euspace && source ~/.g/env && make build ${PARAMS}'

only-build: ebpf-build go-build

all: only-build

build:
	CGO_ENABLED=1 go build -gcflags="all=-N -l" ${GO_LD_FLAGS} -buildvcs=false -o ${TARGET_FILE}
	${TARGET_FILE} -v

run:
	ssh root@10.211.55.27 'cd /opt/github/euspace && CONFIG_ENDPOINT=10.0.16.250:18080 && TRACES_ENDPOINT=http://10.0.16.250:18080/docp/api/v2/data/receive ${FILTER} ./euspace --listen="0.0.0.0:8123"'

docker-build-c:
	cd /opt/github/euspace/ebpftracer && make all ${PARAMS}
docker-build-go:
	source ~/.g/env; CGO_ENABLED=1 go build -a -gcflags="all=-N -l" ${GO_LD_FLAGS} -buildvcs=false -o ${TARGET_FILE}
docker-build: docker-build-c docker-build-go
	@echo Target file: ${TARGET_FILE}
	@echo Euspaces build success!

# Image configuration
AGENT_IMAGE_NAME=euspace-agent
IMAGE_TAG=${VERSION}-dev-$(IMAGE_TAG_SUFFIX)
IMAGE_FULL=${AGENT_IMAGE_NAME}:${IMAGE_TAG}

image-build:
	docker build --platform=$(PLATFORM) -t harbor.cloudwise.com/apm/${IMAGE_FULL} -f $(DOCKERFILE) .

image-push:
	docker push harbor.cloudwise.com/apm/${IMAGE_FULL}

image: only-build image-build image-push

debug:
	exit

.PHONY: help ebpf-build go-build only-build all docker-build image-build image-push image debug