ifeq (3.82,$(lastword $(sort $(MAKE_VERSION) 3.82)))
$(error requires gmake version at least 3.82)
endif

HAIL_HAIL_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

REVISION := $(shell git rev-parse HEAD)
ifndef REVISION
$(error "git rev-parse HEAD" failed to produce output)
endif

SHORT_REVISION := $(shell git rev-parse --short=12 HEAD)
ifndef SHORT_REVISION
$(error "git rev-parse --short=12 HEAD" failed to produce output)
endif

HAIL_MAJOR_MINOR_VERSION := 0.2
HAIL_PATCH_VERSION := 138
HAIL_PIP_VERSION := $(HAIL_MAJOR_MINOR_VERSION).$(HAIL_PATCH_VERSION)
HAIL_VERSION := $(HAIL_PIP_VERSION)-$(SHORT_REVISION)

SCALA_VERSION ?= 2.12
SPARK_VERSION ?= 3.5.3

include env_var.mk

.DEFAULT_GOAL := shadowJar
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:

$(eval $(call ENV_VAR,REVISION))
$(eval $(call ENV_VAR,SHORT_REVISION))
$(eval $(call ENV_VAR,HAIL_PIP_VERSION))
$(eval $(call ENV_VAR,HAIL_VERSION))

$(eval $(call ENV_VAR,SPARK_VERSION))

HAIL_PYTHON3 ?= python3
PIP ?= $(HAIL_PYTHON3) -m pip
ifdef JAVA_HOME
JAVAC ?= $(JAVA_HOME)/bin/javac
else
JAVAC ?= javac
endif

PYTEST_TARGET ?= test/hail test/hailtop
DOCTEST_TARGET ?= hail
PYTEST_INTER_CLOUD_TARGET ?= test/hailtop/inter_cloud

PY_FILES := $(shell git ls-files python)

INIT_SCRIPTS := python/hailtop/hailctl/deploy.yaml

PYTHON_JAR := python/hail/backend/hail-all-spark.jar
FAST_PYTHON_JAR := python/hail/backend/hail.jar
FAST_PYTHON_JAR_EXTRA_CLASSPATH := python/hail/backend/extra_classpath
WHEEL := build/deploy/dist/hail-$(HAIL_PIP_VERSION)-py3-none-any.whl

TEST_STORAGE_URI = $(shell kubectl get secret global-config --template={{.data.test_storage_uri}} | base64 --decode)
HAIL_TEST_GCS_BUCKET = $(shell kubectl get secret global-config --template={{.data.hail_test_gcs_bucket}} | base64 --decode)
CLOUD_HAIL_TEST_RESOURCES_PREFIX = $(TEST_STORAGE_URI)/$(shell whoami)/hail-test-resources
CLOUD_HAIL_TEST_RESOURCES_DIR = $(CLOUD_HAIL_TEST_RESOURCES_PREFIX)/test/resources/
CLOUD_HAIL_DOCTEST_DATA_DIR = $(CLOUD_HAIL_TEST_RESOURCES_PREFIX)/doctest/data/

# mill integration

MILL := bash mill
MILLOPTS ?=

.PHONY: mill-clean
mill-clean:
	$(MILL) $(MILLOPTS) clean

# Any target depending on this will always be recomputed. We use this for any target that
# runs mill, so that mill is always invoked and can decide if the target needs to be updated.
# If not, the target file's timestamp is unchanged, so downstream targets are not forced to
# be recomputed.
FORCE:

SHADOW_JAR := out/hail/$(SCALA_VERSION)/assembly.dest/out.jar
ifdef HAIL_COMPILE_NATIVES
$(SHADOW_JAR): native-lib-prebuilt
endif
$(SHADOW_JAR): FORCE
	$(MILL) $(MILLOPTS) hail[$(SCALA_VERSION)].assembly

.PHONY: shadowJar
shadowJar: $(SHADOW_JAR)

NON_SHADOW_JAR := out/hail/$(SCALA_VERSION)/jar.dest/out.jar
ifdef HAIL_COMPILE_NATIVES
$(NON_SHADOW_JAR): native-lib-prebuilt
endif
$(NON_SHADOW_JAR): FORCE
	$(MILL) $(MILLOPTS) hail[$(SCALA_VERSION)].jar

SHADOW_TEST_JAR := out/hail/$(SCALA_VERSION)/test/assembly.dest/out.jar
ifdef HAIL_COMPILE_NATIVES
$(SHADOW_TEST_JAR): native-lib-prebuilt
endif
$(SHADOW_TEST_JAR): FORCE
	$(MILL) $(MILLOPTS) hail[$(SCALA_VERSION)].test.assembly

.PHONY: shadowTestJar
shadowTestJar: $(SHADOW_TEST_JAR)

EXTRA_CLASSPATH := out/hail/$(SCALA_VERSION)/writeRunClasspath.dest/runClasspath
$(EXTRA_CLASSPATH): FORCE
	$(MILL) $(MILLOPTS) hail[$(SCALA_VERSION)].writeRunClasspath

.PHONY: jvm-test
ifdef HAIL_COMPILE_NATIVES
jvm-test: native-lib-prebuilt
endif
jvm-test:
	$(MILL) $(MILLOPTS) hail[$(SCALA_VERSION)].test testng.xml

.PHONY: services-jvm-test
ifdef HAIL_COMPILE_NATIVES
services-jvm-test: native-lib-prebuilt
endif
services-jvm-test:
	$(MILL) $(MILLOPTS) hail[$(SCALA_VERSION)].test testng-services.xml

.PHONY: fs-jvm-test
ifdef HAIL_COMPILE_NATIVES
fs-jvm-test: native-lib-prebuilt
endif
fs-jvm-test: upload-remote-test-resources
	! [ -z $(HAIL_CLOUD) ]  # call like make fs-jvm-test HAIL_CLOUD=gcp or azure
	! [ -z $(NAMESPACE) ]  # call like make fs-jvm-test NAMEPSPACE=default
	HAIL_CLOUD=$(HAIL_CLOUD) \
	HAIL_DEFAULT_NAMESPACE=$(NAMESPACE) \
	HAIL_FS_TEST_CLOUD_RESOURCES_URI=$(CLOUD_HAIL_TEST_RESOURCES_DIR)fs \
	HAIL_TEST_STORAGE_URI=$(TEST_STORAGE_URI) \
	$(MILL) $(MILLOPTS) hail[$(SCALA_VERSION)].test $(realpath testng-fs.xml)

# end mill integration

.PHONY: jars
jars: shadowJar shadowTestJar

PYTHON_VERSION_INFO := \
	python/hail/version.py \
	python/hail/docs/_static/hail_version.js \
	python/hailtop/version.py

$(PYTHON_VERSION_INFO): env/HAIL_VERSION env/HAIL_PIP_VERSION env/REVISION env/SHORT_REVISION

.PHONY: python-version-info
python-version-info: $(PYTHON_VERSION_INFO)

.ONESHELL: $(PYTHON_VERSION_INFO)
python/hail/version.py python/hailtop/version.py:
	cat <<- EOF > $@
		# Generated by make on $(shell date).
		__pip_version__ = "$(HAIL_PIP_VERSION)"
		__revision__ = "$(REVISION)"
		__version__ = "$(HAIL_VERSION)"
	EOF

python/hail/docs/_static/hail_version.js:
	cat <<- EOF > $@
		# Generated by make on $(shell date).
		hail_pip_version = "$(HAIL_PIP_VERSION)";
		hail_version = "$(HAIL_VERSION)";
	EOF

python/README.md: ../README.md
	cp ../README.md python/

$(PYTHON_JAR): $(SHADOW_JAR)
	cp -f $(SHADOW_JAR) $@

$(FAST_PYTHON_JAR): $(NON_SHADOW_JAR)
	cp -f $(NON_SHADOW_JAR) $@

.PHONY: python-jar
python-jar: $(PYTHON_JAR)

$(FAST_PYTHON_JAR_EXTRA_CLASSPATH): $(EXTRA_CLASSPATH)
	cp $(EXTRA_CLASSPATH) $@

.PHONY: pytest
pytest: install-editable
	cd python && \
          $(HAIL_PYTHON3) -m pytest \
            -Werror:::hail -Werror:::hailtop -Werror::ResourceWarning \
            --log-cli-level=INFO \
            -s \
            -vv \
            -r A \
            --instafail \
            --durations=50 \
            --self-contained-html \
            --html=../build/reports/pytest.html \
            --timeout=120 \
            --ignore $(PYTEST_INTER_CLOUD_TARGET) \
            $(PYTEST_TARGET) \
            $(PYTEST_ARGS)


# NOTE: Look at upload-remote-test-resources target if test resources are missing
.PHONY: pytest-inter-cloud
pytest-inter-cloud: upload-remote-test-resources install-editable
	cd python && \
          HAIL_TEST_STORAGE_URI=$(TEST_STORAGE_URI) \
          HAIL_TEST_GCS_BUCKET=$(HAIL_TEST_GCS_BUCKET) \
          HAIL_TEST_S3_BUCKET=hail-test-dy5rg \
          HAIL_TEST_AZURE_ACCOUNT=hailtest \
          HAIL_TEST_AZURE_CONTAINER=hail-test-4nxei \
          $(HAIL_PYTHON3) -m pytest \
            -Werror:::hail -Werror:::hailtop -Werror::ResourceWarning \
            --log-cli-level=INFO \
            -s \
            -vv \
            -r A \
            --instafail \
            --durations=50 \
            --self-contained-html \
            --html=../build/reports/pytest.html \
            --timeout=120 \
            $(PYTEST_INTER_CLOUD_TARGET) \
            $(PYTEST_ARGS)


# NOTE: Look at upload-remote-test-resources target if test resources are missing
.PHONY: pytest-qob
pytest-qob: upload-qob-jar upload-remote-test-resources install-editable
	! [ -z $(NAMESPACE) ]  # call this like: make pytest-qob NAMESPACE=default
	cd python && \
          HAIL_TEST_STORAGE_URI=$(TEST_STORAGE_URI) \
          HAIL_QUERY_BACKEND=batch \
          HAIL_DEFAULT_NAMESPACE=$(NAMESPACE) \
          HAIL_TEST_RESOURCES_DIR='$(CLOUD_HAIL_TEST_RESOURCES_DIR)' \
          HAIL_DOCTEST_DATA_DIR='$(HAIL_DOCTEST_DATA_DIR)' \
          $(HAIL_PYTHON3) -m pytest \
            -Werror:::hail -Werror:::hailtop -Werror::ResourceWarning \
            --log-cli-level=INFO \
            -s \
            -vv \
            -r A \
            --instafail \
            --durations=50 \
            --self-contained-html \
            --html=../build/reports/pytest.html \
            --timeout=120 \
            $(PYTEST_TARGET) \
            $(PYTEST_ARGS)


.PHONY: doctest-query
doctest-query: $(PYTHON_VERSION_INFO) $(INIT_SCRIPTS)
doctest-query: python/README.md $(FAST_PYTHON_JAR) $(FAST_PYTHON_JAR_EXTRA_CLASSPATH)
	[ $$(stty size | cut -f2 -d' ') -eq 80 ]  # doctests will fail unless you use an 80 character width tty
	cd python && \
          $(HAIL_PYTHON3) -m pytest \
            -Werror:::hail -Werror:::hailtop -Werror::ResourceWarning \
            --log-cli-level=INFO \
            -s \
            -vv \
            -r A \
            --instafail \
            --durations=50 \
            --self-contained-html \
            --html=../build/reports/pytest-doctest.html \
            --doctest-modules \
            --doctest-glob='*.rst' \
            --doctest-continue-on-failure \
            --ignore=setup.py \
            --ignore=setup-hailtop.py \
            --ignore=hail/docs/conf.py \
            --ignore=cluster-tests \
            --ignore=hailtop \
            --ignore=test \
            --ignore=hail/docs/doctest_write_data.py \
            --ignore=hail/docs/getting_started_developing.rst \
            --ignore=hail/docs/getting_started.rst \
            $(DOCTEST_TARGET) \
            $(PYTEST_ARGS)


.PHONY: copy-py-files
copy-py-files: $(PYTHON_VERSION_INFO) $(INIT_SCRIPTS) $(PY_FILES) $(PYTHON_JAR)
	rm -rf build/deploy
	mkdir -p build/deploy
	mkdir -p build/deploy/src
	cp ../README.md build/deploy/
	rsync -r \
	    --exclude '.eggs/' \
	    --exclude '.mypy_cache/' \
	    --exclude '.pytest_cache/' \
	    --exclude '__pycache__/' \
	    --exclude 'benchmark/' \
	    --exclude 'docs/' \
	    --exclude 'dist/' \
	    --exclude 'test/' \
	    --exclude '*.log' \
	    python/ build/deploy/

.PHONY: wheel
wheel: $(WHEEL)

$(WHEEL): copy-py-files
	# Clear the build cache before building the wheel
	cd build/deploy; rm -rf build; $(HAIL_PYTHON3) -m build -w

# if the DEPLOY_REMOTE flag is not set, then deploy init scripts into a dev-username location
ifndef DEPLOY_REMOTE
DEV_CLARIFIER ?= $(shell whoami)-dev
cloud_base ?= gs://hail-30-day/hailctl/dataproc/$(DEV_CLARIFIER)/$(HAIL_VERSION)
UPLOAD_RETENTION =
else
cloud_base := gs://hail-common/hailctl/dataproc/$(HAIL_PIP_VERSION)
UPLOAD_RETENTION = gcloud storage objects update "$(cloud_base)/*" --temporary-hold
endif

wheel_cloud_path := $(cloud_base)/hail-$(HAIL_PIP_VERSION)-py3-none-any.whl
resources := $(wildcard python/hailtop/hailctl/dataproc/resources/*)
$(eval $(call ENV_VAR,cloud_base))
$(eval $(call ENV_VAR,wheel_cloud_path))


.PHONY: check-pip-lockfile
check-pip-lockfile:
	$(PIP) install 'pip-tools==7.4.0' && bash ../check_pip_requirements.sh python


python/hailtop/hailctl/deploy.yaml: env/cloud_base env/wheel_cloud_path python/pinned-requirements.txt
python/hailtop/hailctl/deploy.yaml: $(resources)
	rm -f $@
	echo "dataproc:" >> $@
	for FILE in $(notdir $(resources)); do \
		echo "  $$FILE: $(cloud_base)/$$FILE" >> $@ || exit 1
	done
	echo "  wheel: $(wheel_cloud_path)" >> $@
	echo "  pip_dependencies:" >> $@
	sed '/^[[:blank:]]*#/d;s/#.*//;s/^/    - /' python/pinned-requirements.txt | grep -v pyspark >> $@

.PHONY: upload-artifacts
upload-artifacts: $(WHEEL)
	gcloud storage cp $(resources) $(WHEEL) $(cloud_base)
	gcloud storage objects update -r $(cloud_base) --add-acl-grant=entity=AllUsers,role=READER
	$(UPLOAD_RETENTION)


# NOTE: 1-day expiration of the test bucket means that this
# target must be run at least once a day. To trigger this target to re-run,
# > rm upload-remote-test-resources
upload-remote-test-resources: install-editable
upload-remote-test-resources: $(shell git ls-files hail/test/resources)
upload-remote-test-resources: $(shell git ls-files python/hail/docs/data)
	# # If hailtop.aiotools.copy gives you trouble:
	# gcloud storage cp -r src/test/resources/\* $(CLOUD_HAIL_TEST_RESOURCES_DIR)
	# gcloud storage cp -r python/hail/docs/data/\* $(CLOUD_HAIL_DOCTEST_DATA_DIR)
	python3 -m hailtop.aiotools.copy \
            -vvv \
            'null' \
            "[\
              {\"from\":\"hail/test/resources\",\"to\":\"$(CLOUD_HAIL_TEST_RESOURCES_DIR)\"}, \
              {\"from\":\"python/hail/docs/data\",\"to\":\"$(CLOUD_HAIL_DOCTEST_DATA_DIR)\"} \
             ]" \
            --timeout 600
	touch $@

# NOTE: 1-day expiration of the test bucket means that this
# target must be run at least once a day if using a dev NAMESPACE.
# To trigger this target to re-run,
# > rm upload-qob-jar
upload-qob-jar: $(SHADOW_JAR)
	! [ -z $(NAMESPACE) ]  # call this like: make upload-qob-jar NAMESPACE=default
	bash ./scripts/upload_qob_jar.sh $(NAMESPACE) $(REVISION) $(SHADOW_JAR) $@

# if the installation of Hail has changed, i.e. you pip-installed a non-development version of Hail,
# this file will be newer than "install-editable"
CURRENT_INSTALL = $(shell pip3 show -qq hail | grep Location: | sed 's/Location: //')

install-editable: $(if ($(CURRENT_INSTALL),),,$(CURRENT_INSTALL)/hail/__init__.py)
install-editable: python/setup.py
install-editable: python/requirements.txt
install-editable: python/README.md
install-editable: $(PYTHON_VERSION_INFO) $(INIT_SCRIPTS)
install-editable: $(FAST_PYTHON_JAR) $(FAST_PYTHON_JAR_EXTRA_CLASSPATH)
	-$(PIP) uninstall -y hail
	cd python && $(PIP) install -e .
	hailctl config set query/backend spark
	touch install-editable

.PHONY: install-for-qob
install-for-qob: upload-qob-jar install-editable
	! [ -z $(NAMESPACE) ]  # call this like: make install-for-qob NAMESPACE=default
	hailctl config set query/backend batch
	hailctl dev config set default_namespace $(NAMESPACE)

.PHONY: install
install: $(WHEEL)
	-$(PIP) uninstall -y hail
	$(PIP) install $(WHEEL)
	hailctl config set query/backend spark

.PHONY: install-on-cluster
install-on-cluster: $(WHEEL) check-pip-lockfile
	sed '/^pyspark/d' python/pinned-requirements.txt | grep -v -e '^[[:space:]]*#' -e '^$$' | tr '\n' '\0' | xargs -0 $(PIP) install -U
	-$(PIP) uninstall -y hail
	$(PIP) install $(WHEEL) --no-deps
	hailctl config set query/backend spark

.PHONY: install-hailctl
install-hailctl: install upload-artifacts

.PHONY: test-dataproc-37
test-dataproc-37: install
	bash scripts/test-dataproc.sh "GRCh37"

.PHONY: test-dataproc-38
test-dataproc-38: install
	bash scripts/test-dataproc.sh "GRCh38"

# install skopeo
# use curl version >=7.55.0
#
# set DEPLOY_REMOTE to the hail-is/hail remote
#
# create an access token with full repo privileges at https://github.com/settings/tokens
# create a file that contains "Authorization: token YOUR_ACCESS_TOKEN_HERE"
# set GITHUB_OAUTH_HEADER_FILE to that filename
#
# create the hailgenetics/hail image (see /docker) and place it somewhere skopeo can access it
# set HAIL_GENETICS_HAIL_IMAGE to that image's full name including the protocol, e.g. docker://us-docker.pkg.dev/hail-vdc/hailgenetics/hail:1234abcd
#
# create the hailgenetics/hail image using the Python 3.10 ubuntu base image (see /docker) and place it somewhere skopeo can access it
# set HAIL_GENETICS_HAIL_IMAGE_PY_3_10 to that image's full name including the protocol, e.g. docker://us-docker.pkg.dev/hail-vdc/hailgenetics/hail:1234abcd-py3.10
#
# create the hailgenetics/hail image using the Python 3.11 ubuntu base image (see /docker) and place it somewhere skopeo can access it
# set HAIL_GENETICS_HAIL_IMAGE_PY_3_11 to that image's full name including the protocol, e.g. docker://us-docker.pkg.dev/hail-vdc/hailgenetics/hail:1234abcd-py3.11
#
# create the hailgenetics/hailtop image (see /docker) and place it somewhere skopeo can access it
# set HAIL_GENETICS_HAILTOP_IMAGE to that image's full name including the protocol, e.g. docker://us-docker.pkg.dev/hail-vdc/hailgenetics/hailtop:1234abcd
#
# create the hailgenetics/vep-grch37-85 image (see /docker) and place it somewhere skopeo can access it
# set HAIL_GENETICS_VEP_GRCH37_85_IMAGE to that image's full name including the protocol, e.g. docker://us-docker.pkg.dev/hail-vdc/hailgenetics/vep-grch37-85:1234abcd
#
# create the hailgenetics/vep-grch38-95 image (see /docker) and place it somewhere skopeo can access it
# set HAIL_GENETICS_VEP_GRCH38_95_IMAGE to that image's full name including the protocol, e.g. docker://us-docker.pkg.dev/hail-vdc/hailgenetics/vep-grch38-95:1234abcd
#
# cluster to find the correct Scala and Spark versions because the version webpage does not include
# sufficient detail)
# set AZURE_WHEEL to the path to that wheel file
#
# Create a hail docs www.tar.gz file (see build.yaml "make_docs" rule) and set WEBSITE_TAR to the path to www.tar.gz
#
.PHONY: release
release: test-dataproc $(WHEEL)
	HAIL_PIP_VERSION=$(HAIL_PIP_VERSION) \
        HAIL_VERSION=$(HAIL_VERSION) \
        GIT_VERSION=$(REVISION) \
        REMOTE=$(DEPLOY_REMOTE) \
        WHEEL=$(WHEEL) \
        GITHUB_OAUTH_HEADER_FILE=$(GITHUB_OAUTH_HEADER_FILE) \
        HAIL_GENETICS_HAIL_IMAGE=$(HAIL_GENETICS_HAIL_IMAGE) \
        HAIL_GENETICS_HAIL_IMAGE_PY_3_10=$(HAIL_GENETICS_HAIL_IMAGE_PY_3_10) \
        HAIL_GENETICS_HAIL_IMAGE_PY_3_11=$(HAIL_GENETICS_HAIL_IMAGE_PY_3_11) \
        HAIL_GENETICS_HAILTOP_IMAGE=$(HAIL_GENETICS_HAILTOP_IMAGE) \
        HAIL_GENETICS_VEP_GRCH37_85_IMAGE=$(HAIL_GENETICS_VEP_GRCH37_85_IMAGE) \
        HAIL_GENETICS_VEP_GRCH38_95_IMAGE=$(HAIL_GENETICS_VEP_GRCH38_95_IMAGE) \
        AZURE_WHEEL=$(AZURE_WHEEL) \
        WEBSITE_TAR=$(WEBSITE_TAR) \
        bash scripts/release.sh

python/hail/docs/change_log.rst: python/hail/docs/change_log.md
	sed -E "s/\(hail\#([0-9]+)\)/(\[#\1](https:\/\/github.com\/hail-is\/hail\/pull\/\1))/g" \
	  < $< \
	  | pandoc -o $@

ifeq ($(shell uname -s),Darwin)
SED_INPLACE := sed -i ''
else
SED_INPLACE := sed -i
endif

.PHONY: batch-docs
batch-docs:
	$(MAKE) -C python/hailtop/batch/docs \
	      BUILDDIR=$(HAIL_HAIL_DIR)/build/docs/batch \
	      html
	rm -rf build/www/docs/batch
	mkdir -p build/www/docs
	cp -R build/docs/batch/html build/www/docs/batch
	find build/www/docs/batch \
	     -iname *.html \
	     -type f \
	     -exec $(SED_INPLACE) -e "s/\.css/\.css\?v\=$(HAIL_CACHE_VERSION)/" {} +;
	@echo Built Batch docs: build/www/docs/batch/index.html

HAIL_CACHE_VERSION = $(shell cat python/hail/hail_version)
.PHONY: hail-docs
hail-docs: $(PYTHON_VERSION_INFO) python/hail/docs/change_log.rst
	$(MAKE) -C python/hail/docs \
	        SPHINXOPTS='-tgenerate_notebook_outputs' \
	        BUILDDIR=$(HAIL_HAIL_DIR)/build/docs/hail \
	        html
	mkdir -p build/www/docs
	rm -rf build/www/docs/$(HAIL_MAJOR_MINOR_VERSION)
	cp -R build/docs/hail/html build/www/docs/$(HAIL_MAJOR_MINOR_VERSION)
	find build/www/docs/$(HAIL_MAJOR_MINOR_VERSION) \
	     -iname *.html \
	     -type f \
	     -exec $(SED_INPLACE) -e "s/\.css/\.css\?v\=$(HAIL_CACHE_VERSION)/" {} +;
	@echo Built Hail docs: build/www/docs/$(HAIL_MAJOR_MINOR_VERSION)/index.html

.PHONY: hail-docs-do-not-render-notebooks
hail-docs-do-not-render-notebooks: install-editable
hail-docs-do-not-render-notebooks: $(PYTHON_VERSION_INFO) python/hail/docs/change_log.rst
	$(MAKE) -C python/hail/docs \
	        BUILDDIR=$(HAIL_HAIL_DIR)/build/docs/hail \
	        html
	mkdir -p build/www/docs
	rm -rf build/www/docs/$(HAIL_MAJOR_MINOR_VERSION)
	cp -R build/docs/hail/html build/www/docs/$(HAIL_MAJOR_MINOR_VERSION)
	find build/www/docs/$(HAIL_MAJOR_MINOR_VERSION) \
	     -iname *.html \
	     -type f \
	     -exec $(SED_INPLACE) -e "s/\.css/\.css\?v\=$(HAIL_CACHE_VERSION)/" {} +;
	@echo Built docs: file://$(HAIL_HAIL_DIR)/build/www/docs/$(HAIL_MAJOR_MINOR_VERSION)/index.html

.PHONY: test
test: pytest jvm-test doctest

.PHONY: native-lib native-lib-test native-lib-clean native-lib-prebuilt native-lib-reset-prebuilt
native-lib:
	$(MAKE) -C c
native-lib-test:
	$(MAKE) -C c test
native-lib-clean:
	$(MAKE) -C c clean
native-lib-prebuilt:
	$(MAKE) -C c prebuilt
native-lib-reset-prebuilt:
	$(MAKE) -C c reset-prebuilt

clean-libs:
	rm -rf libs

.PHONY: clean
clean: clean-env clean-libs native-lib-clean mill-clean
	$(MAKE) -C python/hail/docs clean
	$(MAKE) -C python/hailtop/batch/docs clean
	rm -rf build/
	rm -rf $(PYTHON_JAR)
	rm -rf $(FAST_PYTHON_JAR)
	rm -rf $(FAST_PYTHON_JAR_EXTRA_CLASSPATH)
	rm -rf python/README.md
	rm -rf $(SCALA_BUILD_INFO)
	rm -rf $(PYTHON_VERSION_INFO)
	rm -rf python/hail/docs/change_log.rst

.PHONY: update-hail-repl
update-hail-repl: NAMESPACE ?= default
update-hail-repl: wheel
	kubectl -n $(NAMESPACE) cp $(WHEEL) $$(kubectl get pods -n $(NAMESPACE) -l app=hail-repl | tail -n +2 | awk '{print $$1}'):.
	kubectl -n $(NAMESPACE) exec -it $$(kubectl get pods -n $(NAMESPACE) -l app=hail-repl | tail -n +2 | awk '{print $$1}') -- pip3 install -U hail-$(HAIL_PIP_VERSION)-py3-none-any.whl

HAIL_BENCHMARK_RESULTS_PREFIX ?= "gs://hail-benchmarks-2/$(USER)/$(HAIL_VERSION)"
HAIL_DEV_IMAGE ?= ""
.PHONY: benchmark
benchmark:
	$(HAIL_PYTHON3) scripts/benchmark_in_batch.py submit \
		$(HAIL_DEV_IMAGE) \
		$(HAIL_BENCHMARK_RESULTS_PREFIX) \
		$(HAIL_VERSION)
