
SHELL:=$(shell sh -c "command -v bash")

MAKE_MAJORVER:=$(shell echo "$(MAKE_VERSION)" | cut -d. -f1)
ifeq ($(MAKE_MAJORVER),3)
$(error GNU Make version is too old. Aborting.)
endif

ifeq ($(ROOT),)
$(error ROOT is undefined)
endif
override ROOT:=$(shell cd $(ROOT) && pwd)

ifneq ($(filter help,$(MAKECMDGOALS)),)
override HELP:=1
else ifeq ($(HELP),1)
MK_ALL_TARGETS:=help
endif

ifeq ($(READIES),)
ifneq ($(wildcard $(ROOT)/deps/readies),)
READIES:=$(ROOT)/deps/readies
else ifneq ($(wildcard $(ROOT)/opt/readies),)
READIES:=$(ROOT)/opt/readies
else ifneq ($(wildcard $(ROOT)/readies),)
READIES:=$(ROOT)/readies
else
$(error Cannot find readies root)
endif
endif

ifeq ($(wildcard $(READIES)/*),)
___:=$(shell git submodule update --init --recursive &> /dev/null)
endif

export MK:=$(READIES)/mk

MK.pyver ?= 3
MK.getpy=getpy$(MK.pyver)

ifneq ($(shell { CHECK=1 $(READIES)/bin/$(MK.getpy); echo -n $$?; }),0)
ifneq ($(filter setup,$(MAKECMDGOALS)),)
MK.nobindir:=1
else
define PYERROR
Cannot find python$(MK.pyver) interpreter.
You can install one by running 'make setup'.
endef
$(error $(PYERROR))
endif
__NO_PYTHON=1
endif

ifneq ($(HELP),1)
MK_ALL_TARGETS:=bindirs build
DEFAULT_TARGETS:=bindirs
else
MK_ALL_TARGETS:=help
DEFAULT_TARGETS:=help
endif

SUDO:=$(shell [ $$( command -v sudo >/dev/null 2>&1; echo $$? ) = 0 ] && echo sudo)
USER:=$(shell whoami)

include $(MK)/functions
include $(MK)/common.defs
include $(MK)/cfg

ifneq ($(__NO_PYTHON),1)

include $(MK)/platform.defs

ifeq ($(OS),macos)
include $(MK)/macos.defs
endif

include $(MK)/git.defs
include $(MK)/variant.defs
include $(MK)/bindirs.defs
include $(MK)/nproc.defs

endif # __NO_PYTHON
