#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export LC_ALL=C.UTF-8
export PYBUILD_DISABLE_python2 = 1
export PYBUILD_NAME = torch
export SOVERSION = 1.13
export PATCH = 0

# [Maintainer Notes -- Mo Zhou <lumin@debian.org>]
# 1. Let's stick to the upstream LTS release. see https://pytorch.org
# 2. We may provide CUDA or ROCm variants of Pytorch in the future.
#    In that case we should create a bin:pytorch-src package,
#    and create new packages e.g. pytorch-cuda which Build-Dpends
#    on pytorch-src. This change is only necessary when we are
#    really ready to package the variants.

# [for debugging purpose]
#export CC=/usr/lib/ccache/gcc
#export CXX=/usr/lib/ccache/g++

# [special configurations]
# We use a run-time switching mechanism for BLAS/LAPACK.
# https://wiki.debian.org/DebianScience/LinearAlgebraLibraries
# Even if the torch package is built against the generic library, the backend
# libblas.so.3 is in fact automatically switched to the fastest implementation.
export BLAS = Generic
export GENERIC_BLAS_LIBRARIES = blas

# configurations
export CFLAGS = $(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CFLAGS) -gsplit-dwarf
export CXXFLAGS = $(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CXXFLAGS) -gsplit-dwarf
export NO_CUDA = 1 # Will never build CUDA version. Use Anaconda/Pip please.
export ONNX_NAMESPACE = onnx
export REL_WITH_DEB_INFO = ON
export USE_CUDA = OFF
export USE_CUDNN = OFF
ifneq (,$(filter $(DEB_HOST_ARCH),amd64 arm64 ppc64el mips64el s390x))
export USE_DISTRIBUTED = ON
else
export USE_DISTRIBUTED = OFF
endif
export USE_FBGEMM = OFF  # XXX: forget this; No need to enable.
export USE_FFMPEG = ON
export USE_GFLAGS= ON
export USE_GLOG = ON
export USE_LEVELDB = ON
export USE_LMDB = ON
export USE_MIOPEN = OFF
export USE_MPI = OFF
ifneq (,$(filter $(DEB_HOST_ARCH),amd64 arm64 ppc64el))
export USE_MKLDNN = ON  # requires ideep and onednn.
else
export USE_MKLDNN = OFF
endif
ifneq (,$(filter $(DEB_HOST_ARCH),amd64))
export USE_PYTORCH_QNNPACK = ON
else
export USE_PYTORCH_QNNPACK = OFF
endif
export USE_NNPACK = OFF  # XXX: forget this. No need to enable.
export USE_OPENCV = ON
export USE_QNNPACK = OFF  # XXX: forget this. No need to enable.
export USE_REDIS = OFF
export USE_ROCM = OFF
export USE_SYSTEM_LIBS = ON
export USE_SYSTEM_NCCL = OFF
export USE_TENSORPIPE = ON
ifneq (,$(filter $(DEB_HOST_ARCH),amd64 arm64))
export USE_XNNPACK = ON
else
export USE_XNNPACK = OFF
endif
export USE_ZMQ = ON
export USE_ZSTD = ON
export USE_KINETO = OFF
export USE_ITT= OFF

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
export MAX_JOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# Customization options (You may want to rebuild this package locally)
NATIVE :=
ifneq (,$(NATIVE))
export CFLAGS = $(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CFLAGS) -gsplit-dwarf -march=native
export CXXFLAGS = $(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CXXFLAGS) -gsplit-dwarf -march=native
endif


%:
	dh $@ -Spybuild --with python3

override_dh_auto_clean:
	dh_auto_clean
	-find . -type d -name __pycache__ -exec rm -rf '{}' +

override_dh_auto_configure:
	cd third_party/; rmdir googletest; ln -s /usr/src/googletest .
	# fix python and shell shebang -- no /usr/bin/env
	find . -type f -name '*.py' -exec sed -i -e 's@#!/usr/bin/env python.*@#!/usr/bin/python3@' '{}' \;
	find . -type f -name '*.sh' -exec sed -i -e 's@#!/usr/bin/env @#!/usr/bin/@g' '{}' \;
	# regenerate flatbuffers code -- upstream cmake does not regenerate.
	cd torch/csrc/jit/serialization/; flatc mobile_bytecode.fbs --cpp --gen-all --gen-mutable

override_dh_auto_build:
	: # In order to avoid building everything twice.

override_dh_auto_test:
	:

override_dh_auto_install:
	$(shell py3versions -d) setup.py install --install-layout=deb \
		--root=$(shell pwd)/debian/python3-torch/
	# [python3-torch::headers -> libtorch-dev] move the headers out of the python package
	mkdir -pv debian/libtorch-dev/usr/include
	mv -v debian/python3-torch/usr/lib/python3*/dist-packages/torch/include/* \
		debian/libtorch-dev/usr/include/
	cd debian/python3-torch/usr/lib/python3*/dist-packages/torch/include/; \
		ln -sv /usr/include/torch . ;\
		ln -sv /usr/include/caffe2 . ;\
		ln -sv /usr/include/ATen . ;\
		ln -sv /usr/include/c10 . ;\
		ln -sv /usr/include/TH .
	# [python3-torch::shlibs -> libtorch$(SOVERSION)] move the (public) shared libs out of the python package
	mkdir -pv debian/libtorch$(SOVERSION)/usr/lib/$(DEB_HOST_MULTIARCH)/
	mv -v debian/python3-torch/usr/lib/python3*/dist-packages/torch/lib/*.so.* \
		debian/libtorch$(SOVERSION)/usr/lib/$(DEB_HOST_MULTIARCH)/
	cd debian/libtorch$(SOVERSION)/usr/lib/$(DEB_HOST_MULTIARCH)/; \
		ln -sfv libc10.so.$(SOVERSION).$(PATCH) libc10.so.$(SOVERSION) ;\
		ln -sfv libcaffe2_detectron_ops.so.$(SOVERSION).$(PATCH) libcaffe2_detectron_ops.so.$(SOVERSION) ;\
		ln -sfv libcaffe2_module_test_dynamic.so.$(SOVERSION).$(PATCH) libcaffe2_module_test_dynamic.so.$(SOVERSION) ;\
		ln -sfv libcaffe2_observers.so.$(SOVERSION).$(PATCH) libcaffe2_observers.so.$(SOVERSION) ;\
		ln -sfv libshm.so.$(SOVERSION).$(PATCH) libshm.so.$(SOVERSION) ;\
		ln -sfv libtorch_cpu.so.$(SOVERSION).$(PATCH) libtorch_cpu.so.$(SOVERSION) ;\
		ln -sfv libtorch_python.so.$(SOVERSION).$(PATCH) libtorch_python.so.$(SOVERSION) ;\
		ln -sfv libtorch.so.$(SOVERSION).$(PATCH) libtorch.so.$(SOVERSION) ;\
		ln -sfv libtorch_global_deps.so.$(SOVERSION).$(PATCH) libtorch_global_deps.so.$(SOVERSION)
	# [python3-torch::symlinks -> libtorch-dev] move the links to shared libs out of the python package
	mkdir -pv debian/libtorch-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	cd debian/python3-torch/usr/lib/python3*/dist-packages/torch/lib ; $(RM) -v *.so;\
		ln -sv  ../../../../$(DEB_HOST_MULTIARCH)/libc10.so.$(SOVERSION) libc10.so ;\
		ln -sv  ../../../../$(DEB_HOST_MULTIARCH)/libcaffe2_detectron_ops.so.$(SOVERSION) libcaffe2_detectron_ops.so ;\
		ln -sv  ../../../../$(DEB_HOST_MULTIARCH)/libcaffe2_module_test_dynamic.so.$(SOVERSION) libcaffe2_module_test_dynamic.so ;\
		ln -sv  ../../../../$(DEB_HOST_MULTIARCH)/libcaffe2_observers.so.$(SOVERSION) libcaffe2_observers.so ;\
		ln -sv  ../../../../$(DEB_HOST_MULTIARCH)/libshm.so.$(SOVERSION) libshm.so ;\
		ln -sv  ../../../../$(DEB_HOST_MULTIARCH)/libtorch_cpu.so.$(SOVERSION) libtorch_cpu.so ;\
		ln -sv  ../../../../$(DEB_HOST_MULTIARCH)/libtorch_python.so.$(SOVERSION) libtorch_python.so ;\
		ln -sv  ../../../../$(DEB_HOST_MULTIARCH)/libtorch.so.$(SOVERSION) libtorch.so ;\
		ln -sv  ../../../../$(DEB_HOST_MULTIARCH)/libtorch_global_deps.so.$(SOVERSION) libtorch_global_deps.so
	cd debian/libtorch-dev/usr/lib/$(DEB_HOST_MULTIARCH)/; \
		ln -sfv libc10.so.$(SOVERSION) libc10.so ;\
		ln -sfv libcaffe2_detectron_ops.so.$(SOVERSION) libcaffe2_detectron_ops.so ;\
		ln -sfv libcaffe2_module_test_dynamic.so.$(SOVERSION) libcaffe2_module_test_dynamic.so ;\
		ln -sfv libcaffe2_observers.so.$(SOVERSION) libcaffe2_observers.so ;\
		ln -sfv libshm.so.$(SOVERSION) libshm.so ;\
		ln -sfv libtorch_cpu.so.$(SOVERSION) libtorch_cpu.so ;\
		ln -sfv libtorch_python.so.$(SOVERSION) libtorch_python.so ;\
		ln -sfv libtorch.so.$(SOVERSION) libtorch.so ;\
		ln -sfv libtorch_global_deps.so.$(SOVERSION) libtorch_global_deps.so
	# [python3-torch::cmake -> libtorch-dev] move the cmake files out of the python package, and fixup the cmake files accordingly
	mkdir -pv debian/libtorch-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	mv -v debian/python3-torch/usr/lib/python3*/dist-packages/torch/share/cmake \
		debian/libtorch-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	find debian/libtorch-dev/usr/lib/$(DEB_HOST_MULTIARCH) -type f -name '*.cmake' \
		-exec sed -i -e "s@\\(_IMPORT_PREFIX.\\)/lib/lib@\\1/$(DEB_HOST_MULTIARCH)/lib@g" '{}' \;
	find debian/libtorch-dev/usr/lib/$(DEB_HOST_MULTIARCH) -type f -name '*.cmake' \
		-exec sed -i -e "s@\\(_IMPORT_PREFIX.\\)/include@\\1/../include@g" '{}' \;
	find debian/libtorch-dev/usr/lib/$(DEB_HOST_MULTIARCH) -type f -name '*.cmake' \
	        -exec sed -i -e "s@\\(CMAKE_CURRENT_LIST_DIR.\\)/../../../@\\1/../../../../@g" '{}' \;
	find debian/libtorch-dev/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/ATen -type f -name '*.cmake' \
		-exec sed -i -e "s@/build/pytorch-.*/torch/include@/usr/include/ATen@g" '{}' \;
	# [python3-torch::testbin -> libtorch-test] move the test binaries out of the python packaage
	mkdir -pv debian/libtorch-test/usr/lib/libtorch-test/
	mv -v debian/python3-torch/usr/lib/python3*/dist-packages/torch/test/* \
		debian/libtorch-test/usr/lib/libtorch-test/
	rmdir -v debian/python3-torch/usr/lib/python3*/dist-packages/torch/test
	cd debian/python3-torch/usr/lib/python3*/dist-packages/torch/ ;\
		ln -s /usr/lib/libtorch-test test


override_dh_python3:
	dh_python3 --requires=requirements.txt --no-ext-rename
	dh_numpy3

override_dh_auto_clean:
	-$(RM) -r build/
	-$(RM) -f third_party/googletest

override_dh_dwz:
	: # FTBFS
