.DEFAULT_GOAL := all

.PHONY: all
all: tidy gen lint build

ROOT_PACKAGE=github.com/pingcap-inc/pingcat
VERSION_PACKAGE=github.com/pingcap-inc/pchub/pkg/version

include script/make/init.mk
include script/make/golang.mk
include script/make/gen.mk
include script/make/tool.mk
include script/make/pack.mk

define USAGE_OPTIONS

Options:
  TARGETENV        The target environment to build. Default is prod.
                   Valid values are: prod, staging, dev.
                   This option is available when using: make gen
                   Example: make gen TARGETENV=staging
  PLATFORMS        The multiple platforms(separated by spaces) to build. Default is linux_amd64.
                   This option is available when using: make build.multiarch
                   Example: make build.multiarch PLATFORMS="linux_amd64 linux_arm64"
  VERSION          The version information compiled into binaries.
                   The default is obtained from gsemver or git.
  V                Set to 1 enable verbose build. Default is 0.
endef
export USAGE_OPTIONS


# ==============================================================================
# Targets

## tools: install dependent tools.
.PHONY: tools
tools:
	@$(MAKE) tools.install

## lint: Check syntax and styling of go sources.
.PHONY: lint
lint:
	@$(MAKE) go.lint

## build: Build source code for host platform.
.PHONY: build
build:
	@$(MAKE) go.build

## build: Pack executable programs into docker images and push.
.PHONY: pack
pack:
	@$(MAKE) pack.all

## clean: Remove all files that are created by building.
.PHONY: clean
clean:
	@echo "===========> Cleaning all build output"
	@-rm -vrf $(OUTPUT_DIR)

## gen: Generate all necessary files, such as error code files.
.PHONY: gen
gen:
	@$(MAKE) gen.run

## tidy: Run go mod tidy.
.PHONY: tidy
tidy:
	@$(GO) mod tidy

## help: Show this help info.
.PHONY: help
help: Makefile
	@echo -e "\nUsage: make <TARGETS> <OPTIONS> ...\n\nTargets:"
	@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
	@echo "$$USAGE_OPTIONS"
