# as go build already parallelize things, running make in parallel
# just slow down the whole process and we also requires to respect
# a specific order
.NOTPARALLEL:

all: $(ALL)

.PHONY: collect
collect:
	@printf " DEPENDS\n"
	$(V)mkdir -p $(BUILDDIR)
	$(V):>$(BUILDDIR)/mergeddeps
	$(V)for i in `find $(BUILDDIR) -name '*.o.d'`; do		\
		(awk -v path="$${i%/*.*}" '/^.+:/ {			\
			print path "/" $$0; next }{ print }' < $$i	\
			>> $(BUILDDIR)/mergeddeps)			\
	done

.PHONY: check
check: codegen
	@echo " CHECK golangci-lint"
	$(V) cd $(SOURCEDIR) && \
		golangci-lint run --verbose
	@echo "       PASS"

.PHONY: dist
dist: conmon_CLEAN squashfuse_CLEAN
	$(V) if test -e '$(SOURCEDIR)/vendor' ; then \
		echo 'E: There is a vendor directory in $(SOURCEDIR).' ; \
		echo 'E: This is unexpected. Abort.' ; \
		exit 1 ; \
	fi
	$(V) cd $(SOURCEDIR) && $(GO) mod vendor
	$(V) cd $(SOURCEDIR) && $(SOURCEDIR)/scripts/make-dist.sh $(VERSION)
	$(V) rm -rf '$(SOURCEDIR)/vendor'

.PHONY: unit-test
unit-test: EXTRA_FLAGS := $(if $(filter yes,$(strip $(JUNIT_OUTPUT))),-junit $(BUILDDIR_ABSPATH)/unit-test.xml)
unit-test:
	@echo " TEST sudo go test [unit]"
	$(V)cd $(SOURCEDIR) && \
		scripts/go-test -sudo -v $(GO_RACE) $(EXTRA_FLAGS) \
		./...
	@echo "       PASS"

.PHONY: short-unit-test
short-unit-test: EXTRA_FLAGS := $(if $(filter yes,$(strip $(JUNIT_OUTPUT))),-junit $(BUILDDIR_ABSPATH)/unit-test.xml)
short-unit-test:
	@echo " TEST sudo go test [short-unit]"
	$(V)cd $(SOURCEDIR) && \
		scripts/go-test -sudo -v -short $(GO_RACE) $(EXTRA_FLAGS) \
		./...
	@echo "       PASS"

.PHONY: integration-test
integration-test:
	@echo " TEST sudo go test [integration]"
	$(V)cd $(SOURCEDIR) && \
		scripts/go-test -sudo -v -tags 'integration_test' $(GO_RACE) \
		./pkg/network
	@echo "       PASS"

.PHONY: short-integration-test
short-integration-test:
	@echo " TEST sudo go test [short-integration]"
	$(V)cd $(SOURCEDIR) && \
		scripts/go-test -sudo -v -short -tags 'integration_test' $(GO_RACE) \
		./pkg/network
	@echo "       PASS"


.PHONY: e2e-test

e2e-test: GROUPS_FLAG := $(if $(E2E_GROUPS),-e2e_groups $(E2E_GROUPS))
e2e-test: TESTS_FLAG := $(if $(E2E_TESTS),-e2e_tests $(E2E_TESTS))
e2e-test: EXTRA_FLAGS := $(if $(filter yes,$(strip $(JUNIT_OUTPUT))),-junit $(BUILDDIR_ABSPATH)/e2e-test.xml)
e2e-test:
	@echo " TEST sudo go test [e2e]"
	$(V)cd $(SOURCEDIR) && \
		scripts/e2e-test -v $(GO_RACE) $(EXTRA_FLAGS) $(GROUPS_FLAG) $(TESTS_FLAG)
	@echo "       PASS"
	
# test runs only those tests that do not need any docker auth setup etc.
.PHONY: test
test: short-unit-test short-integration-test

# testall will require docker auth to complete as the e2e-tests will hit dockerhub limits
# We use short-unit-test here, until the non-short unit tests respect dockerhub auth, or
# the requirement is eliminated by de-dpulication vs e2e tests.
.PHONY: testall
testall: check short-unit-test integration-test e2e-test

.PHONY: rpm
rpm: dist
	@echo " BUILD RPM"
	$(V)(set -x; cd $(SOURCEDIR) && \
	  if [ -n "$(RPMPREFIX)" ]; then \
	    rpmbuild $(RPMCLEAN) -ta \
	      --define '_prefix $(RPMPREFIX)' \
	      --define '_sysconfdir $(RPMPREFIX)/etc' \
	      --define '_localstatedir $(RPMPREFIX)/var' \
	      --define '_mandir $(RPMPREFIX)/share/man' \
	      $(SOURCEDIR)/singularity-ce-$(VERSION).tar.gz; \
	  else \
	    rpmbuild $(RPMCLEAN) -ta \
	      $(SOURCEDIR)/singularity-ce-$(VERSION).tar.gz; \
	  fi)

.PHONY: cscope
cscope:
	@printf " CSCOPE\n"
	$(V)(cscope -k -R -q -u -b -v `find $(SOURCEDIR) -name '*.[chS]'` \
		>/dev/null 2>&1 || true)

.PHONY: clean
clean: $(CLEANTARGETS)
	@printf " CLEAN\n"
	$(V)rm -rf $(BUILDDIR)/mergeddeps cscope.* $(CLEANFILES)

.PHONY: install
install: $(INSTALLFILES)
	@echo " DONE"

-include $(BUILDDIR)/mergeddeps
