include ../metadata.mk

PACKAGE_NAME = github.com/projectcalico/calico/pod2daemon

# The node-driver-registrar binary is the only standalone binary produced by
# pod2daemon. It is cloned from upstream and bundled into the calico/calico
# image by cmd/calico/Makefile. The CSI driver and flex volume driver are
# compiled into the main calico binary as subcommands.
REGISTRAR_IMAGE ?= node-driver-registrar

###############################################################################
# Download and include ../lib.Makefile before anything else
###############################################################################

include ../lib.Makefile

###############################################################################

## SRC_FILES is auto-populated from deps.txt by lib.Makefile.

.PHONY: clean
## Clean enough that a new release build will be clean
clean:
	find . -name '.*.created*' -type f -delete
	find . -name '.*.published*' -type f -delete
	rm -rf report/
	rm -rf bin
	$(MAKE) clean-registrar

###############################################################################
# Building the binary
###############################################################################
BINDIR=bin

.PHONY: build
## Build the node-driver-registrar binary for the current architecture.
build: $(BINDIR)/node-driver-registrar-$(ARCH)

protobuf proto/udsver.pb.go: proto/udsver.proto
	$(DOCKER_GO_BUILD) sh -c 'protoc --proto_path=proto --go_out=proto --go-grpc_out=. --go_opt=paths=source_relative udsver.proto'
	# Make sure the generated code won't cause a static-checks failure.
	$(MAKE) fix-changed

UPSTREAM_REGISTRAR_PROJECT ?= kubernetes-csi/$(REGISTRAR_IMAGE)
UPSTREAM_REGISTRAR_REPO    ?= https://github.com/$(UPSTREAM_REGISTRAR_PROJECT).git
# https://github.com/kubernetes-csi/node-driver-registrar/commit/2d18e12bc5077c36cbd564be7eab9ea94c0c85fb
UPSTREAM_REGISTRAR_TAG     ?= 2d18e12bc5077c36cbd564be7eab9ea94c0c85fb

# Where the build sees the upstream checkout: mounted at its GOPATH location in the
# build container, or in place when building natively.
ifeq ($(NATIVE_GO_BUILD),true)
REGISTRAR_SRC_DIR = $(CURDIR)/$(REGISTRAR_IMAGE)
else
REGISTRAR_SRC_DIR = /go/src/github.com/$(UPSTREAM_REGISTRAR_PROJECT)
endif

REGISTRAR_TIGERA_BUILD_CMD="cd $(REGISTRAR_SRC_DIR) && \
	go build -tags $(TAGS) -buildvcs=false -v -o $(BINDIR)/csi-node-driver-registrar cmd/csi-node-driver-registrar/*.go"
REGISTRAR_UPSTREAM_BUILD_CMD="cd $(REGISTRAR_SRC_DIR) && make build BUILD_PLATFORMS=$(BUILD_PLATFORMS)"

ifeq ($(ARCH), $(filter $(ARCH),amd64 arm64))
CGO_ENABLED=0
REGISTRAR_BUILD_CMD=$(REGISTRAR_TIGERA_BUILD_CMD)
else ifeq ($(ARCH), $(filter $(ARCH),ppc64le))
# Note: We must use single quotes on BUILD_PLATFORMS since it is to be nested within another double-quoted string.
BUILD_PLATFORMS='linux ppc64le ppc64le'
REGISTRAR_BUILD_CMD=$(REGISTRAR_UPSTREAM_BUILD_CMD)
else ifeq ($(ARCH), $(filter $(ARCH),s390x))
# Note: We must use single quotes on BUILD_PLATFORMS since it is to be nested within another double-quoted string.
BUILD_PLATFORMS='linux s390x s390x'
REGISTRAR_BUILD_CMD=$(REGISTRAR_UPSTREAM_BUILD_CMD)
else ifeq ($(ARCH), $(filter $(ARCH),win64))
# Note: We must use single quotes on BUILD_PLATFORMS since it is to be nested within another double-quoted string.
BUILD_PLATFORMS='windows amd64 amd64'
REGISTRAR_BUILD_CMD=$(REGISTRAR_UPSTREAM_BUILD_CMD)
endif

ifeq ($(NATIVE_GO_BUILD),true)
REGISTRAR_RUN = env CGO_ENABLED=$(CGO_ENABLED)
else
REGISTRAR_RUN = $(DOCKER_RUN) -e CGO_ENABLED=$(CGO_ENABLED) \
	-v $(REPO_ROOT)/pod2daemon/$(REGISTRAR_IMAGE):$(REGISTRAR_SRC_DIR):rw \
	$(CALICO_BUILD)
endif

$(BINDIR)/node-driver-registrar-%: clone-registrar-upstream
	$(REGISTRAR_RUN) /bin/bash -c $(REGISTRAR_BUILD_CMD)
	mkdir -p $(dir $@)
	mv $(REGISTRAR_IMAGE)/$(BINDIR)/csi-node-driver-registrar $@

clone-registrar-upstream:
	$(call fetch_repo,$(UPSTREAM_REGISTRAR_REPO),$(UPSTREAM_REGISTRAR_TAG),$(REGISTRAR_IMAGE))
	rm -rf ./$(REGISTRAR_IMAGE)/vendor
	# Reset first so re-applying is idempotent.
	cd $(REGISTRAR_IMAGE) && git checkout -- go.mod go.sum
	# One directory per class, applied in name order. No dep-pins.txt here, so
	# the dependency patch stays hand-maintained -- the generator would read the
	# missing pin file as "no pins" and delete it.
	found=; \
	for patch in node-driver-registrar-patches/*/*.patch; do \
		[ -f "$$patch" ] || continue; found=1; \
		patch -d $(REGISTRAR_IMAGE) -p1 -F0 < "$$patch" || exit 1; \
	done; \
	[ -n "$$found" ] || { echo "error: no patches in node-driver-registrar-patches/" >&2; exit 1; }

clean-registrar:
	rm -rf ./$(REGISTRAR_IMAGE)

###############################################################################
# UTs
###############################################################################
.PHONY: ut
## Run the tests in a container. Useful for CI, Mac dev
ut: $(SRC_FILES)
	mkdir -p report
	$(DOCKER_RUN) --privileged $(CALICO_BUILD) sh -c 'set -o pipefail && cd /go/src/$(PACKAGE_NAME) && go test -v ./... | go-junit-report > ./report/tests.xml'

fv st:
	@echo "No FVs or STs available"

###############################################################################
# CI
###############################################################################
.PHONY: ci
ci: clean mod-download build clean-registrar static-checks ut
