#!/usr/bin/make -f

export PYBUILD_NAME=memray
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
	dh $@ --with sphinxdoc --buildsystem=pybuild

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--warnings=9

override_dh_auto_install:
	dh_auto_install
	# Strip unneeded symbols and notes from shared libraries
	find debian/python3-memray -name "*.so*" -exec strip --strip-unneeded --remove-section=.note.gnu.build-id {} +

# Skip failing tests in each ARCH
export FILES_ARGS=--ignore=tests/integration/test_main.py \
--ignore=tests/integration/test_native_tracking.py

export TIMEOUT_ARGS=and not test_reading_allocations_while_reading_stack_traces \
and not test_live_tracking_waits_for_client \
and not test_live_tracking \
and not test_live_client_exits_properly_on_sigint_before_connecting \
and not test_multithreaded_extension \
and not test_misbehaving_extension \
and not test_extension_that_uses_pygilstate_ensure \
and not test_native_dlopen \
and not test_valloc_at_thread_exit \
and not test_valloc_at_thread_exit_in_subprocess \
and not test_dlopen_with_rpath

export ARM64_ARGS="${FILES_ARGS} \
-v -k 'not test_aggregation_same_python_stack_but_different_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_but_different_native_stack[AGGREGATED_ALLOCATIONS] \
and not test_allocations_with_multiprocessing \
and not test_pymalloc_allocations_after_fork \
and not test_stack_cleanup_after_fork \
${TIMEOUT_ARGS}'"

export ARMHF_ARGS="${FILES_ARGS} \
--ignore=tests/integration/test_tracing.py \
--ignore=tests/integration/test_tracking.py \
-v -k 'not test_thread_name \
and not test_setting_python_thread_name \
and not test_successful_parse_of_aggregated_capture_file \
${TIMEOUT_ARGS}'"

export ARMEL_ARGS="${FILES_ARGS} \
--ignore=tests/integration/test_tracing.py \
-v -k 'not test_thread_name \
and not test_setting_python_thread_name \
and not test_aggregation_same_python_stack_and_same_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_and_same_native_stack[AGGREGATED_ALLOCATIONS] \
and not test_aggregation_same_python_stack_but_different_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_but_different_native_stack[AGGREGATED_ALLOCATIONS] \
${TIMEOUT_ARGS}'"

export S390X_ARGS="${FILES_ARGS} \
-v -k 'not test_aggregation_same_python_stack_and_same_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_and_same_native_stack[AGGREGATED_ALLOCATIONS] \
and not test_large_number_of_frame_pops_between_subsequent_allocations \
and not test_aggregation_same_python_stack_but_different_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_but_different_native_stack[AGGREGATED_ALLOCATIONS] \
and not test_inverted_very_deep_call_is_limited \
and not test_rejects_different_header_version \
and not test_allocations_with_multiprocessing \
${TIMEOUT_ARGS}'"

export AMD64_OTHERS_ARGS="${FILES_ARGS} \
-v -k 'not test_reading_allocations_while_reading_stack_traces \
and not test_live_tracking_waits_for_client \
and not test_live_tracking \
${TIMEOUT_ARGS}'"

override_dh_auto_test:
ifneq (,$(filter arm64,$(DEB_BUILD_ARCH)))
	PYBUILD_TEST_ARGS=$(ARM64_ARGS) dh_auto_test
else ifneq (,$(filter armhf,$(DEB_BUILD_ARCH)))
	PYBUILD_TEST_ARGS=$(ARMHF_ARGS) dh_auto_test
else ifneq (,$(filter armel,$(DEB_BUILD_ARCH)))
	PYBUILD_TEST_ARGS=$(ARMEL_ARGS) dh_auto_test
else ifneq (,$(filter s390x,$(DEB_BUILD_ARCH)))
	PYBUILD_TEST_ARGS=$(S390X_ARGS) dh_auto_test
else ifneq (,$(filter amd64 i386 ppc64el riscv64,$(DEB_BUILD_ARCH)))
	PYBUILD_TEST_ARGS=$(AMD64_OTHERS_ARGS) dh_auto_test
else ifneq (,$(filter mips64el ppc64 loong64 sh4,$(DEB_BUILD_ARCH)))
	@echo "Skip tests in $(DEB_BUILD_ARCH)"
else
	dh_auto_test
endif

override_dh_install:
	dh_install
	rm -f debian/python3-memray/usr/bin/memray3.*

override_dh_installdocs:
	https_proxy='127.0.0.1:9' sphinx-build -b html docs build/sphinx/html

	# Remove duplicate files
	rm -f build/sphinx/html/_static/images/codespaces_testing_tab.png
	rm -f build/sphinx/html/_static/images/complex_example.png
	rm -f build/sphinx/html/_static/images/exercise1_flamegraph.png
	rm -f build/sphinx/html/_static/images/exercise2_flamegraph.png
	rm -f build/sphinx/html/_static/images/exercise3_flamegraph_basic.png
	rm -f build/sphinx/html/_static/images/exercise3_flamegraph_native.png
	rm -f build/sphinx/html/_static/images/filter_thread_dropdown.png
	rm -f build/sphinx/html/_static/images/gprof2dot_example.png
	rm -f build/sphinx/html/_static/images/icicle_flame_toggle.png
	rm -f build/sphinx/html/_static/images/inverted_flame_graph_for_inverted_example.png
	rm -f build/sphinx/html/_static/images/live_different_thread.png
	rm -f build/sphinx/html/_static/images/live_disconnected.png
	rm -f build/sphinx/html/_static/images/live_running.png
	rm -f build/sphinx/html/_static/images/logo.png
	rm -f build/sphinx/html/_static/logo.png
	rm -f build/sphinx/html/_static/images/mandelbrot_operation_native.png
	rm -f build/sphinx/html/_static/images/mandelbrot_operation_non_native.png
	rm -f build/sphinx/html/_static/images/native_mode_debug.png
	rm -f build/sphinx/html/_static/images/native_mode_no_debug.png
	rm -f build/sphinx/html/_static/images/non_relevant_checkbox.png
	rm -f build/sphinx/html/_static/images/normal_flamegraph_for_inverted_example.png
	rm -f build/sphinx/html/_static/images/output.png
	rm -f build/sphinx/html/_static/images/plot_default_options.png
	rm -f build/sphinx/html/_static/images/plot_native.png
	rm -f build/sphinx/html/_static/images/plot_python_allocators.png
	rm -f build/sphinx/html/_static/images/plot_python_allocators_native.png
	rm -f build/sphinx/html/_static/images/ports_tab.png
	rm -f build/sphinx/html/_static/images/pymalloc.png
	rm -f build/sphinx/html/_static/images/pytest_cli_output.png
	rm -f build/sphinx/html/_static/images/quotes.png
	rm -f build/sphinx/html/_static/images/rss_vs_heap.png
	rm -f build/sphinx/html/_static/images/rss_vs_heap_no_free.png
	rm -f build/sphinx/html/_static/images/simple_example.png
	rm -f build/sphinx/html/_static/images/stats_example.png
	rm -f build/sphinx/html/_static/images/summary_example.png
	rm -f build/sphinx/html/_static/images/table_example.png
	rm -f build/sphinx/html/_static/images/temporal_high_water_mark_controls.png
	rm -f build/sphinx/html/_static/images/tree_example.png

	# Create symlinks
	ln -sf ../../../_images/codespaces_testing_tab.png build/sphinx/html/_static/images/codespaces_testing_tab.png
	ln -sf ../../../_images/complex_example.png build/sphinx/html/_static/images/complex_example.png
	ln -sf ../../../_images/exercise1_flamegraph.png build/sphinx/html/_static/images/exercise1_flamegraph.png
	ln -sf ../../../_images/exercise2_flamegraph.png build/sphinx/html/_static/images/exercise2_flamegraph.png
	ln -sf ../../../_images/exercise3_flamegraph_basic.png build/sphinx/html/_static/images/exercise3_flamegraph_basic.png
	ln -sf ../../../_images/exercise3_flamegraph_native.png build/sphinx/html/_static/images/exercise3_flamegraph_native.png
	ln -sf ../../../_images/filter_thread_dropdown.png build/sphinx/html/_static/images/filter_thread_dropdown.png
	ln -sf ../../../_images/gprof2dot_example.png build/sphinx/html/_static/images/gprof2dot_example.png
	ln -sf ../../../_images/icicle_flame_toggle.png build/sphinx/html/_static/images/icicle_flame_toggle.png
	ln -sf ../../../_images/inverted_flame_graph_for_inverted_example.png build/sphinx/html/_static/images/inverted_flame_graph_for_inverted_example.png
	ln -sf ../../../_images/live_different_thread.png build/sphinx/html/_static/images/live_different_thread.png
	ln -sf ../../../_images/live_disconnected.png build/sphinx/html/_static/images/live_disconnected.png
	ln -sf ../../../_images/live_running.png build/sphinx/html/_static/images/live_running.png
	ln -sf ../../../_images/logo.png build/sphinx/html/_static/images/logo.png
	ln -sf ../../../logo.png build/sphinx/html/_static/logo.png
	ln -sf ../../../_images/mandelbrot_operation_native.png build/sphinx/html/_static/images/mandelbrot_operation_native.png
	ln -sf ../../../_images/mandelbrot_operation_non_native.png build/sphinx/html/_static/images/mandelbrot_operation_non_native.png
	ln -sf ../../../_images/native_mode_debug.png build/sphinx/html/_static/images/native_mode_debug.png
	ln -sf ../../../_images/native_mode_no_debug.png build/sphinx/html/_static/images/native_mode_no_debug.png
	ln -sf ../../../_images/non_relevant_checkbox.png build/sphinx/html/_static/images/non_relevant_checkbox.png
	ln -sf ../../../_images/normal_flamegraph_for_inverted_example.png build/sphinx/html/_static/images/normal_flamegraph_for_inverted_example.png
	ln -sf ../../../_images/output.png build/sphinx/html/_static/images/output.png
	ln -sf ../../../_images/plot_default_options.png

	dh_installdocs -ppython-memray-doc --doc-main-package=python-memray-doc build/sphinx/html
	dh_installdocs
