# Copyright 2020 PingCAP, Inc.
#
# 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.

PROJECT=observability
P=8

# Get system info, include OS and ARCH
OS ?= $(shell uname -s)
ifeq ($(OS), Darwin)
    OS := osx
else ifeq ($(OS), darwin)
    OS := osx
else ifeq ($(OS), Linux)
    OS := linux
endif

ARCH ?= $(shell uname -m)
ifeq ($(ARCH), arm64)
    ARCH := x86_64
else ifeq ($(ARCH), amd64)
    ARCH := x86_64
else ifeq ($(ARCH), aarch64)
    ARCH := aarch_64
endif

FAIL_ON_STDOUT := awk '{ print } END { if (NR > 0) { exit 1 } }'

CURDIR := $(shell pwd)
TEST_DIR := /tmp/observability
CODECOV_BASH := /tmp/codecov_bash

GO              := GO111MODULE=on go
GOBUILD         := $(GO) build $(BUILD_FLAGS) -tags codes
GOTEST          := $(GO) test -p $(P)
OVERALLS        := GO111MODULE=on overalls
STATICCHECK     := GO111MODULE=on staticcheck
EDITION    ?= Enterprise

# Ensure EDITION is set to Community or Enterprise before running build process.
ifneq "$(EDITION)" "Enterprise"
  $(error Please set the correct environment variable EDITION before running `make`)
endif

# use 'make <target> PREFIX=/path/for/prefix' to overwrite the PREFIX value
ifeq (${PREFIX},)
    PREFIX := /usr/local
endif

LINUX     := "Linux"
MAC       := "Darwin"

ifneq ($(wildcard go.work),)
GO_LIST_ENV :=
GO_LINT_ENV :=
else
GO_LIST_ENV := GOWORK=off
GO_LINT_ENV := GOWORK=off
endif

GO_MODULE_ENV := $(GO_LIST_ENV)
GO_LIST_CMD := $(GO_LIST_ENV) go list ./...

PACKAGE_LIST  := $(GO_LIST_CMD)| grep -vE "test|docs|proto|message|swagger|structs|fake|github.com/tidbcloud/observability/api|.*observability/cmd|.*observability/pkg/common/constants|.*observability/pkg/common/recordingrule|.*observability/pkg/common/errors|.*observability/pkg/resourceworker/controllers/fake|.*observability/pkg/resourceworker/worker|.*observability/pkg/metamanager/s3|github.com/tidbcloud/observability/pkg/resourceworker/providers/gcp/giam/fake|github.com/tidbcloud/observability/pkg/resourceworker/providers/aws|github.com/tidbcloud/observability/pkg/resourceworker/providers/gcp|.*observability/pkg/resourceworker/controllers/clusterupgradeoperation/providers|github.com/tidbcloud/observability/sli-reporter/cmd|github.com/tidbcloud/observability/sli-reporter/slprober|dataapi|globalapi|github.com/tidbcloud/observability/sli-reporter/sqs|github.com/tidbcloud/observability/pkg/diagcontroller|github.com/tidbcloud/observability/pkg/diagcontroller/tasks|github.com/tidbcloud/observability/pkg/diagcontroller/resources|github.com/tidbcloud/observability/pkg/diagcontroller/helper$$|o11ydiagnosis|regionalapi|vmoperation|ops|integrationtask"
PACKAGES  ?= $$($(PACKAGE_LIST))
LINT_PACKAGE_LIST  := $(GO_LIST_CMD)| grep -vE "docs|proto|test|message|swagger|fake|mock|scripts|sli-reporter" | sed 's|github.com/tidbcloud/observability/||g' | sed 's|github.com/tidbcloud/observability$$|.|g' | tr '\n' ' '
LINT_PACKAGES  ?= $$($(LINT_PACKAGE_LIST))
#PACKAGE_DIRECTORIES := $(PACKAGE_LIST) | sed 's|github.com/tidbcloud/$(PROJECT)||'
PACKAGE_DIRECTORIES := $(PACKAGE_LIST)
FILES     := $$(find $$($(PACKAGE_DIRECTORIES)) -name "*.go")

FAILPOINT_ENABLE  := $$(find $$PWD/ -type d | grep -vE "(\.git)" | xargs bin/failpoint-ctl enable)
FAILPOINT_DISABLE := $$(find $$PWD/ -type d | grep -vE "(\.git)" | xargs bin/failpoint-ctl disable)

LDFLAGS += -X "github.com/tidbcloud/observability/utils/versioninfo.ReleaseVersion=$(shell git describe --tags --dirty --always)"
LDFLAGS += -X "github.com/tidbcloud/observability/utils/versioninfo.GitHash=$(shell git rev-parse HEAD)"
LDFLAGS += -X "github.com/tidbcloud/observability/utils/versioninfo.GitBranch=$(shell git rev-parse --abbrev-ref HEAD)"
LDFLAGS += -X "github.com/tidbcloud/observability/utils/versioninfo.Edition=$(EDITION)"
LDFLAGS += -buildid $(shell git rev-parse HEAD)

RACE_FLAG =
ifeq ("$(WITH_RACE)", "1")
	RACE_FLAG = -race
	GOBUILD   = $(GO) build
endif

CHECK_FLAG =
ifeq ("$(WITH_CHECK)", "1")
	CHECK_FLAG = $(TEST_LDFLAGS)
endif
