#!/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

# Disabled tests that has ModuleNotFoundError for import memray, need further investigation.
# 3rd from the last is timeout failure.
# Last two are 10s timeout failure in build i386.
export PYBUILD_TEST_ARGS = \
	-v -k 'not test_sys_manipulations_when_running_script \
	and not test_suppressing_sys_manipulations_when_running_script[-I] \
	and not test_suppressing_sys_manipulations_when_running_script[-P] \
	and not test_suppressing_sys_manipulations_when_running_module[-I] \
	and not test_suppressing_sys_manipulations_when_running_module[-P] \
	and not test_sys_manipulations_when_running_cmd \
	and not test_suppressing_sys_manipulations_when_running_cmd[-I] \
	and not test_suppressing_sys_manipulations_when_running_cmd[-P] \
	and not test_multithreaded_extension_with_native_tracking \
	and not test_simple_call_chain_with_native_tracking \
	and not test_inlined_call_chain_with_native_tracking \
	and not test_deep_call_chain_with_native_tracking \
	and not test_hybrid_stack_in_pure_python \
	and not test_hybrid_stack_in_pure_python_with_callbacks \
	and not test_hybrid_stack_of_allocations_inside_ceval \
	and not test_hybrid_stack_in_recursive_python_c_call \
	and not test_hybrid_stack_in_a_thread \
	and not test_hybrid_stack_of_python_thread_starts_with_native_frames \
	and not test_allocations_with_multiprocessing \
	and not test_allocations_with_multiprocessing_following_fork \
	and not test_pymalloc_allocations_after_fork \
	and not test_stack_cleanup_after_fork \
	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_reading_allocations_while_reading_stack_traces \
	and not test_live_tracking_waits_for_client \
	and not test_live_tracking'

%:
	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 {} +

override_dh_install:
	dh_install
	mkdir -p debian/python3-memray/usr/lib/python3/dist-packages/
	mv debian/python3-memray/usr/lib/python3.12/dist-packages/memray-*.egg-info debian/python3-memray/usr/lib/python3/dist-packages/
	rm -rf debian/python3-memray/usr/lib/python3.11/dist-packages/memray-*.egg-info
	rm -f debian/python3-memray/usr/bin/memray3.11
	rm -f debian/python3-memray/usr/bin/memray3.12

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
