# Copyright © 2022 sealos.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# ==============================================================================
# Build options

ROOT_PACKAGE=github.com/labring/sealos
VERSION_PACKAGE=github.com/labring/sealos/pkg/version

# ==============================================================================
# Includes

include scripts/make-rules/common.mk # must be the first to include
include scripts/make-rules/golang.mk
include scripts/make-rules/gen.mk
include scripts/make-rules/license.mk
include scripts/make-rules/tools.mk

# ==============================================================================
# Usage

define USAGE_OPTIONS

Options:
  DEBUG            Whether or not to generate debug symbols. Default is 0.

  BINS             Binaries to build. Default is all binaries under cmd.
                   This option is available when using: make {build}(.multiarch)
                   Example: make build BINS="sealos sealctl"

  PLATFORMS        Platform to build for. Default is linux_arm64 and linux_amd64.
                   This option is available when using: make {build}.multiarch
                   Example: make build.multiarch PLATFORMS="linux_arm64 linux_amd64"

  CC               Override the C compiler for CGO-enabled binaries in single-platform builds.
                   Example: make build BINS="sealos" CC=gcc

  CC_<platform>    Override the C compiler for a specific platform in multi-platform builds.
                   Example: make build.multiarch BINS="sealos" CC_linux_arm64=aarch64-linux-gnu-gcc

  V                Set to 1 enable verbose build. Default is 0.
endef
export USAGE_OPTIONS

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

.DEFAULT_GOAL = build

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

## build.multiarch: Build source code for multiple platforms. See option PLATFORMS.
.PHONY: build.multiarch
build.multiarch:
	@$(MAKE) go.build.multiarch

# ## image: Build docker images for host platform.
# .PHONY: image
# image:
# 	@$(MAKE) image.build

# ## image.multiarch: Build docker images for multiple platforms. See option PLATFORMS.
# .PHONY: image.multiarch
# image.multiarch:
# 	@$(MAKE) image.build.multiarch

# ## push: Push docker images for host platform to registry.
# .PHONY: push
# push:
# 	@$(MAKE) image.push

# ## push.multiarch: Push docker images for multiple platforms to registry. See option PLATFORMS.
# .PHONY: push.multiarch
# push.multiarch:
# 	@$(MAKE) image.push.multiarch

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

## format: Gofmt (reformat) package sources.
.PHONY: format
format:
	@$(MAKE) go.format

## coverage: Run unit tests and output test coverage.
.PHONY: coverage
coverage:
	@$(MAKE) go.coverage

## verify-license: Verify the license headers for all files.
.PHONY: verify-license
verify-license:
	@$(MAKE) license.verify

## add-license: Ensure source code files have license headers.
.PHONY: add-license
add-license:
	@$(MAKE) license.add

##license: Add license header to all files.
.PHONY: license
license:
	@$(MAKE) license.add

.PHONY: license.controller
license.controller:
	@$(MAKE) license.controller.add

## gen: Generate all necessary files.
.PHONY: gen
gen:
	@$(MAKE) gen.run

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

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

## help: Show this help info.
.PHONY: help
help: Makefile
	@echo -e "\nUsage: make <TARGETS> <OPTIONS> ...\n\nTargets:"
	@sed -n 's/^##//p' $< | awk -F':' '{printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' | sed -e 's/^/ /'
	@echo "$$USAGE_OPTIONS"
