#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#export DH_BUILD_MAINT_OPTIONS  = nocheck
export DEB_CFLAGS_MAINT_APPEND  = -I./TH.build/lib/TH/
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

TMP=$(shell pwd)/debian/tmp
LUA51=/usr/bin/lua5.1
LUAJIT=/usr/bin/luajit
ROOTDIR=$(shell pwd)

# [Maintainer Note]
# According to lib/TH/cmake/FindBLAS.cmake, if you want to use this package
# with MKL, you need to recompile it after changing -DWITH_BLAS=generic to
# -DWITH_BLAS=mkl . (See #897894)

TORCH_FLAGS_5_1= \
 -DLUA=/usr/bin/lua5.1 \
 -DLUA_INCDIR=/usr/include/lua5.1 \
 -DLUA_LIBDIR=/usr/lib \
 -DLUA_BINDIR=/usr/bin \
 -DLIBDIR=/usr/lib \
 -DLUALIB=lua5.1 \
 -DTorch_INSTALL_BIN_SUBDIR=bin/ \
 -DTorch_INSTALL_LIB_SUBDIR=lib/$(DEB_HOST_MULTIARCH)/ \
 -DTorch_INSTALL_INCLUDE_SUBDIR=include/ \
 -DCMAKE_VERBOSE_MAKEFILE=ON \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_INSTALL_PREFIX=/usr \
 -DWITH_BLAS=generic

ifeq (amd64,$(DEB_HOST_ARCH))
ISA_FLAGS= -DDEBIAN_AMD64=ON
endif
ifeq (i386,$(DEB_HOST_ARCH))
ISA_FLAGS= -DDEBIAN_I386=ON
endif
ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),arm64 armel armhf))
ISA_FLAGS= -DDEBIAN_ARM=ON
endif

%:
	dh $@ --buildsystem=lua --with=lua

override_dh_auto_configure:
	ln -s . torch
	# configure libluaT.so
	dh_auto_configure --buildsystem=cmake --builddirectory=luaT.build -- \
		../lib/luaT $(TORCH_FLAGS_5_1) \
		-DTorch_INSTALL_CMAKE_SUBDIR=/usr/share/libtorch-luat-dev/
	# configure libTH.so
	dh_auto_configure --buildsystem=cmake --builddirectory=TH.build -- \
		../lib/TH $(TORCH_FLAGS_5_1) $(ISA_FLAGS) \
		-DTorch_INSTALL_CMAKE_SUBDIR=/usr/share/libtorch-th-dev/
	# configure the rest lua stuff
	dh_auto_configure --buildsystem=lua
	# generate random.c and TensorMath.c
	$(LUA51) random.lua random.c
	$(LUA51) TensorMath.lua TensorMath.c
	# generate torch7.h
	printf "/* Generated by Debian Package */\nint luaopen_libtorch (lua_State *L);\n" > torch.h
	# generate install template
	sed -i -e 's/#DEB_HOST_MULTIARCH#/$(DEB_HOST_MULTIARCH)/g' debian/libtorch-th.install
	sed -i -e 's/#DEB_HOST_MULTIARCH#/$(DEB_HOST_MULTIARCH)/g' debian/libtorch-th-dev.install
	sed -i -e 's/#DEB_HOST_MULTIARCH#/$(DEB_HOST_MULTIARCH)/g' debian/libtorch-luat.install
	sed -i -e 's/#DEB_HOST_MULTIARCH#/$(DEB_HOST_MULTIARCH)/g' debian/libtorch-luat-dev.install

override_dh_auto_build:
	# build libTH.so
	dh_auto_build --buildsystem=cmake -B TH.build/ --parallel
	# build libluaT.so
	dh_auto_build --buildsystem=cmake -B luaT.build/ --parallel
	# build the rest lua stuff with dh-lua
	dh_auto_build --buildsystem=lua

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	-$(RM) -rf deb.test
	# prepare lua files to launch a "torch.test()"
	set -e; if ! test -d deb.test/torch; then mkdir -p deb.test/torch; fi
	set -e; find . -name '*.so*' -exec cp -v '{}' deb.test/ \;
	set -e; find . -path './.pc' -prune -o \( -name '*.lua' -exec cp -v '{}' deb.test/torch/ \; \)
	set -e; cd deb.test; ln -s torch/init.lua torch.lua;
	set -e; cd deb.test; \
		LD_LIBRARY_PATH=$(ROOTDIR)/deb.test \
		$(LUAJIT) -e "require 'torch'; torch.test();"
	dh_auto_test
endif

override_dh_auto_install:
	# install libTH.so
	dh_auto_install --buildsystem=cmake -B TH.build
	# install libluaT.so
	dh_auto_install --buildsystem=cmake -B luaT.build
	# install the rest lua stuff
	dh_auto_install --buildsystem=lua

override_dh_compress:
	# don't compress documents used by lua-torch-dok
	dh_compress -X.md

override_dh_shlibdeps:
	dh_shlibdeps -Llibtorch-th -Llibtorch-luat

override_dh_auto_clean:
	-$(RM) -rf TH.build/ luaT.build/ torch .pc/
	-$(RM) -rf 5.1-torch-torch7/ 5.2-torch-torch7/
	-$(RM) libtorch.c random.c TensorMath.c torch.h
	-$(RM) -rf deb.test
	-$(RM) test/*.bin
	dh_auto_clean

# Note, sed command used to mark SIMD symbols as optional:
# sed -i -e 's/^ \(.*\)AVX\(.*\)/ (optional)\1AVX\2/g' libtorch-th.symbols
# sed -i -e 's/^ \(.*\)avx\(.*\)/ (optional)\1avx\2/g' libtorch-th.symbols
# sed -i -e 's/^ \(.*\)sse\(.*\)/ (optional)\1sse\2/g' libtorch-th.symbols
