# Define the Go source files and binary name
SRC := main.go
BIN := rfswift

# Define the output directories
OUT_DIR := bin

# Reproducible release-oriented defaults: omit local source paths and Go's
# symbol/debug tables. This materially reduces every distributed binary while
# retaining module provenance (`go version -m`).
GO_BUILD := go build -trimpath -ldflags="-s -w"

# Define a static build tag to ensure static linking
STATIC_BUILD_TAG := netgo

# Default target (Build all targets)
.PHONY: all
all: linux windows darwin

# Targets for Linux
.PHONY: linux
linux: linux_amd64 linux_arm64 linux_riscv64

.PHONY: linux_amd64
linux_amd64:
	GOOS=linux GOARCH=amd64 CGO_ENABLED=0 $(GO_BUILD) -tags '$(STATIC_BUILD_TAG)' -o $(OUT_DIR)/$(BIN)_linux_amd64 $(SRC)

.PHONY: linux_arm64
linux_arm64:
	GOOS=linux GOARCH=arm64 CGO_ENABLED=0 $(GO_BUILD) -tags '$(STATIC_BUILD_TAG)' -o $(OUT_DIR)/$(BIN)_linux_arm64 $(SRC)

.PHONY: linux_riscv64
linux_riscv64:
	GOOS=linux GOARCH=riscv64 CGO_ENABLED=0 $(GO_BUILD) -tags '$(STATIC_BUILD_TAG)' -o $(OUT_DIR)/$(BIN)_linux_riscv64 $(SRC)

# Targets for Windows
.PHONY: windows
windows: windows_amd64 windows_arm64

.PHONY: windows_amd64
windows_amd64:
	GOOS=windows GOARCH=amd64 CGO_ENABLED=0 $(GO_BUILD) -tags '$(STATIC_BUILD_TAG)' -o $(OUT_DIR)/$(BIN)_windows_amd64.exe $(SRC)

.PHONY: windows_arm64
windows_arm64:
	GOOS=windows GOARCH=arm64 CGO_ENABLED=0 $(GO_BUILD) -tags '$(STATIC_BUILD_TAG)' -o $(OUT_DIR)/$(BIN)_windows_arm64.exe $(SRC)

# Targets for macOS (Darwin)
.PHONY: darwin
darwin: darwin_amd64 darwin_arm64

.PHONY: darwin_amd64
darwin_amd64:
	GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 $(GO_BUILD) -tags '$(STATIC_BUILD_TAG)' -o $(OUT_DIR)/$(BIN)_darwin_amd64 $(SRC)

.PHONY: darwin_arm64
darwin_arm64:
	GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 $(GO_BUILD) -tags '$(STATIC_BUILD_TAG)' -o $(OUT_DIR)/$(BIN)_darwin_arm64 $(SRC)

# Clean target
.PHONY: clean
clean:
	rm -rf $(OUT_DIR)

# Create the output directory if it doesn't exist
$(shell mkdir -p $(OUT_DIR))
