FAKEROOT = fakeroot
PYTHON = python
PYTYPE = pytype
RSYNC = rsync

HGPATH =
ifneq ($(HGPATH),)
export PYTHONPATH := $(realpath $(HGPATH)):$(PYTHONPATH)
endif

# the following syntax for "define" requires GNU Make. See:
#     https://www.gnu.org/software/make/manual/make.html#Canned-Recipes
define check_hgpath =
	@[ -n "$(HGPATH)" ] || { echo "HGPATH not specified. Please run again with 'make $@ HGPATH=/path/to/hg'. For example, you could point to a checkout of the mercurial source code"; false; }
	@[ -e "$(HGPATH)" ] || { echo "HGPATH not found: $(HGPATH)"; false; }
	@[ -d "$(HGPATH)" ] || { echo "HGPATH must be a directory: $(HGPATH)"; false; }
endef

.PHONY: help
help:
	@echo 'Commonly used make targets:'
	@echo '  local        - build for inplace usage'
	@echo '  tests        - run all tests in the automatic test suite'
	@echo '  app          - create a py2app bundle on Mac OS X'
	@echo '  tarball      - create release tarball'
	@echo '  clean        - remove files created by other targets'
	@echo '                 (except installed files or dist source tarball)'
	@echo '  distclean    - remove all files created by other targets'
	@echo '  update-pot   - extract translatable strings'

.PHONY: local
local:
	$(PYTHON) setup.py \
		build_ui \
		build_py -c -d . \
		build_mo
	HGRCPATH= $(PYTHON) thg version

.PHONY: tests
tests:
	$(check_hgpath)
	$(PYTHON) tests/run-tests.py -m 'not largefiles' --doctest-modules \
		--ignore tortoisehg/hgqt/shellconf.py \
		--ignore tortoisehg/util/bugtraq.py \
		tests tortoisehg
	$(PYTHON) tests/run-tests.py -m largefiles tests
	$(PYTHON) tests/run-hgtests.py

.PHONY: pytype
pytype: PYTHON_VERSION = 3.8
pytype:
	$(check_hgpath)
	$(PYTYPE) -P ".:$(HGPATH)" -V "$(PYTHON_VERSION)" -j auto \
		--config pytype.cfg
	@echo 'pytype crashed while generating the following type stubs:'
	find .pytype/pyi -name '*.pyi' | xargs grep -l '# Caught error' | sort

.PHONY: app
app: DISTDIR = dist/app
app: SETUPCFG = contrib/setup-py2app.cfg
app: export MACOSX_DEPLOYMENT_TARGET=10.7
app:
	$(check_hgpath)
	[ -z "$(SETUPCFG)" ] || cp "$(SETUPCFG)" setup.cfg
	$(MAKE) -C "$(HGPATH)" local
	FORCE_SETUPTOOLS= $(PYTHON) setup.py py2app -d "$(DISTDIR)"

.PHONY: tarball
tarball:
	$(FAKEROOT) $(PYTHON) setup.py sdist
	@echo
	@echo '** Maybe you need to run "make upload-tarballs"'

.PHONY: upload-tarballs
upload-tarballs:
	$(RSYNC) -rtv --include 'tortoisehg-*.tar.gz' --exclude '*' \
		dist/ mercurial-scm.org:/var/www/release/tortoisehg/targz/

.PHONY: clean
clean:
	$(PYTHON) setup.py clean
	$(RM) -R .pytype

.PHONY: distclean
distclean: clean
	$(RM) -R build dist

.PHONY: update-pot
update-pot:
	$(PYTHON) setup.py update_pot

.PHONY: docker-rhel7
docker-rhel7:
	$(MAKE) -C contrib/docker build-thg-rpm PLATFORM=rhel7 EXTRA_RPM_OPTS_TOPLEVEL=--arch-dependent

.PHONY: docker-rhel8
docker-rhel8:
	$(MAKE) -C contrib/docker build-thg-rpm PLATFORM=rhel8 EXTRA_RPM_OPTS_TOPLEVEL=--arch-dependent

.PHONY: docker-rhel9
docker-rhel9:
	$(MAKE) -C contrib/docker build-thg-rpm PLATFORM=rhel9 EXTRA_RPM_OPTS_TOPLEVEL=--arch-dependent
