#!/usr/bin/make -f

#################
#### GENERIC ####
#################

export DH_VERBOSE=1

# Force disable debug packages generation
ifeq (,$(filter noddebs,$(DEB_BUILD_OPTIONS)))
  DEB_BUILD_OPTIONS += noddebs
endif
export DEB_BUILD_OPTIONS

# Fails in cowbuilder too because this variable
# is not removed inside the chroot
unexport JAVA_HOME

TF_VERSION := $(shell dpkg-parsechangelog --count 1  --show-field Version | rev | cut -d'-' -f2- | rev)


################
#### PYTHON ####
################

PY2VERS	:= $(shell pyversions -rv)
PY3VERS	:= $(shell py3versions -rv)
# TensorFlow does NOT support Python 3.7 at the moment
PY3VERS	:= $(filter-out 3.7,$(PY3VERS))

PY2PACKAGES := $(shell dh_listpackages --arch | grep '^python-')
PY3PACKAGES := $(shell dh_listpackages --arch | grep '^python3-')
PY3PACKAGES += tensorflow-tools

# See ./tensorflow/tools/pip_package/setup.py for dependencies
# It's an absolute nightmare to deal with dh_pythonN, bazel shitty
# build system, python3.6, etc, so I'll just handle that by myself
#
# First I'll compute interpreter dependencies like python3 >= 3.5~, python3 << 3.7
# According to pyversions/py3versions
#
# Then I'll turn this into python2custom:Depends and python3custom:Depends substvars
#
# 
# Tensorflow 1.7.0 release:
# * OMG! Nothing new
# * Bump tensorboard (>= 1.7.0~) and (<< 1.8.0~)
#
# Tensorflow 1.6.0 release:
# * Add astor (>= 0.6.0~)  / Need package update
# * Add gast (>= 0.2.0~)   / Need packaging
# * Add grpcio (>= 1.8.6~) / Need packaging
# * Add termcolor (1.1.0~) / Available
#
# Tensorflow 1.5.0 release:
#
# * Add absl (>= 0.1.6~) new depencency
# * Bump protobuf required version anymore to (>= 3.4.0)
# * Bump tensorboard (>= 1.5.0~) and (<< 1.6.0~)
#
# Tensorflow 1.4.0 release:
# 
# * Not addind enum34 >= 1.1.6: It's not used anywhere in the src tree
#   https://github.com/tensorflow/tensorflow/commit/c98ecd18f6e294feaf9c9e059c12dee33fc2df84
#
# * Not bumping numpy to >= 1.12.1: It'll break Xenial compat and I cannot found in any commit referenced
#   below. I think TF guys just bump to latest version without giving a shit if it's required or not
#   https://github.com/tensorflow/tensorflow/commit/6e3e7d18f42cb4237ce6dbe2ffd0f9f158c36daf

PY2MINVER := $(shell pyversions -rv | rev | cut -d' ' -f1 | rev)
PY2MAXSUP := $(shell pyversions -rv | cut -d' ' -f1)
PY2MAXVER := $(shell python -c 'print(float('$(PY2MAXSUP)') + 0.1)')
PY3MINVER := $(shell py3versions -rv | rev | cut -d' ' -f1 | rev)
PY3MAXSUP := $(shell py3versions -rv | cut -d' ' -f1)
PY3MAXVER := $(shell python -c 'print(float('$(PY3MAXSUP)') + 0.1)')

# Mimic dh_numpy behavior to generate numpy dependencies
ifeq ($(shell test -f /usr/share/numpy3/versions && echo yes),yes)
    NUMPY3_ABI := $(shell grep '^abi' /usr/share/numpy3/versions | cut -d' ' -f2)
    NUMPY3_API_MIN_VERSION := $(shell grep '^api-min-version' /usr/share/numpy3/versions | cut -d' ' -f2)
    NUMPY2_ABI := $(shell grep '^abi' /usr/share/numpy/versions | cut -d' ' -f2)
    NUMPY2_API_MIN_VERSION := $(shell grep '^api-min-version' /usr/share/numpy/versions | cut -d' ' -f2)
else
    NUMPY3_ABI := $(shell grep '^abi' /usr/share/numpy/versions | cut -d' ' -f2)
    NUMPY3_API_MIN_VERSION := $(shell grep '^api-min-version' /usr/share/numpy/versions | cut -d' ' -f2)
    NUMPY2_ABI := $(shell grep '^abi' /usr/share/numpy/versions | cut -d' ' -f2)
    NUMPY2_API_MIN_VERSION := $(shell grep '^api-min-version' /usr/share/numpy/versions | cut -d' ' -f2)
endif

TENSORBOARD_MIN_VERSION = $(shell python -c 'print(str(float(".".join("$(TF_VERSION)".split(".")[:2]))))+".0~"')
TENSORBOARD_MAX_VERSION = $(shell python -c 'print(str(float(".".join("$(TF_VERSION)".split(".")[:2])) + 0.1))+".0~"')

SUBSTVARS_PY2DEPS := -Vpython2custom:Depends="python (>= $(PY2MINVER)~), python (<< $(PY2MAXVER)),
SUBSTVARS_PY2DEPS += python-numpy (>= $(NUMPY2_API_MIN_VERSION)), python-numpy-abi$(NUMPY2_ABI),
SUBSTVARS_PY2DEPS += python-absl (>= 0.1.6~),
SUBSTVARS_PY2DEPS += python-six (>= 1.10.0),
SUBSTVARS_PY2DEPS += python-protobuf (>= 3.4.0),
SUBSTVARS_PY2DEPS += python-astor (>= 0.6.0~),
SUBSTVARS_PY2DEPS += python-gast (>= 0.2.0~),
SUBSTVARS_PY2DEPS += python-grpcio (>= 1.8.6~),
SUBSTVARS_PY2DEPS += python-termcolor (>= 1.1.0~),
SUBSTVARS_PY2DEPS += python-tensorboard (>= $(TENSORBOARD_MIN_VERSION)),
SUBSTVARS_PY2DEPS += python-tensorboard (<< $(TENSORBOARD_MAX_VERSION)),
# These ones is for python2 only, see upstream setup.py
SUBSTVARS_PY2DEPS += python-mock (>= 2.0.0),
SUBSTVARS_PY2DEPS += python-enum34,
SUBSTVARS_PY2DEPS += python-backports.weakref (>= 1.0~rc1)"
SUBSTVARS_PY3DEPS := -Vpython3custom:Depends="python3 (>= $(PY3MINVER)~), python3 (<< $(PY3MAXVER)),
SUBSTVARS_PY3DEPS += python3-numpy (>= $(NUMPY3_API_MIN_VERSION)), python3-numpy-abi$(NUMPY3_ABI),
SUBSTVARS_PY3DEPS += python3-absl (>= 0.1.6~),
SUBSTVARS_PY3DEPS += python3-six (>= 1.10.0),
SUBSTVARS_PY3DEPS += python3-protobuf (>= 3.4.0),
SUBSTVARS_PY3DEPS += python3-astor (>= 0.6.0~),
SUBSTVARS_PY3DEPS += python3-gast (>= 0.2.0~),
SUBSTVARS_PY3DEPS += python3-grpcio (>= 1.8.6~),
SUBSTVARS_PY3DEPS += python3-termcolor (>= 1.1.0~),
SUBSTVARS_PY3DEPS += python3-tensorboard (>= $(TENSORBOARD_MIN_VERSION)),
SUBSTVARS_PY3DEPS += python3-tensorboard (<< $(TENSORBOARD_MAX_VERSION))"


###################
#### GCC FLAGS ####
###################

# Wow such crap much warnings :D
DISABLE_GCC_WARNINGS := -Wno-sign-compare -Wno-ignored-attributes -Wno-write-strings -Wno-strict-aliasing -Wno-maybe-uninitialized -Wno-unused-variable -Wno-uninitialized -Wno-comment
# Various set of CPU optimization
OPTFLAGS1 := -mavx -mavx2 -mfma -msse4.1 -msse4.2
OPTFLAGS2 := -mavx -msse4.1 -msse4.2
OPTFLAGS1 += $(DISABLE_GCC_WARNINGS)
OPTFLAGS2 += $(DISABLE_GCC_WARNINGS)
OPTFLAGS1_NO_CUDA := $(OPTFLAGS1)
OPTFLAGS2_NO_CUDA := $(OPTFLAGS2)
GCC_VERSION := $(shell gcc -dumpversion)
# FPBFS with GCC 7
# But obviously, it's using gcc when building without CUDA
# and gcc-4.9 (per request) when enabling CUDA. Older GCC fails to recognize
# this option
ifeq ($(shell dpkg --compare-versions $(GCC_VERSION) ge 7 && echo yes),yes)
  OPTFLAGS1_NO_CUDA += -Wno-error=stringop-overflow
  OPTFLAGS2_NO_CUDA += -Wno-error=stringop-overflow
endif


##################
#### OPEN MPI ####
##################

# OpenMPI install folder
# Can be either multiarch for recent distro, or /usr/lib for older ones
OPENMPI_VERSION := $(shell dpkg-query -W -f='$${Version}' libopenmpi-dev)
ifeq ($(shell dpkg --compare-versions $(OPENMPI_VERSION) ge 1.10 && echo yes),yes)
  OPENMPI_BASE := $(shell test -d /usr/lib/x86_64-linux-gnu/openmpi && echo /usr/lib/x86_64-linux-gnu/openmpi || echo /usr/lib/openmpi)
  OPENMPI_ENABLE := Yes
  # Workaround building with Debian package
  # https://github.com/tensorflow/tensorflow/issues/11903#issuecomment-332718012
  OPTFLAGS1 += -DOMPI_SKIP_MPICXX=1
  OPTFLAGS2 += -DOMPI_SKIP_MPICXX=1
  OPTFLAGS1_NO_CUDA += -DOMPI_SKIP_MPICXX=1
  OPTFLAGS2_NO_CUDA += -DOMPI_SKIP_MPICXX=1
else
  OPENMPI_ENABLE := No
endif


##############
#### CUDA ####
##############

# CUDA related configuration
CUDNN_PACKAGE := $(shell dpkg-query -W -f='$${Package}' libcudnn?)
CUDNN_VERSION := $(shell dpkg-query -W -f='$${Version}' libcudnn?)
CUDNN_MAJOR_VERSION := $(shell dpkg-query -W -f='$${Version}' libcudnn? | cut -d. -f1,2)
NCCL_MAJOR_VERSION := $(shell dpkg-query -W -f='$${Version}' libnccl-dev | cut -d. -f1,2)
CUDA_VERSION    := $(shell dpkg-query -W -f='$${Version}' nvidia-cuda-toolkit | cut -d. -f1,2)
ifeq ($(CUDA_VERSION),9.1)
  CUDA_CAP	:= 3.0,3.5,3.7,5.0,5.2,5.3,6.0,6.1,7.0
else ifeq ($(CUDA_VERSION),9.0)
  CUDA_CAP	:= 3.0,3.5,3.7,5.0,5.2,5.3,6.0,6.1,7.0
else ifeq ($(CUDA_VERSION),8.0)
  CUDA_CAP	:= 3.0,3.5,3.7,5.0,5.2,5.3,6.0,6.1
else ifeq ($(CUDA_VERSION),7.5)
  CUDA_CAP	:= 3.0,3.5,3.7,5.0,5.2,5.3
else
  $(error Unsupported CUDA $(CUDA_VERSION). Please fix debian/rules)
endif
SUBSTVARS_CUDNN := -Vcudnn:Depends="$(CUDNN_PACKAGE) (= $(CUDNN_VERSION))"

# NVIDIA TensorRT
ifeq ($(shell dpkg --compare-versions $(CUDA_VERSION) ge 8 && echo yes),yes)
  TENSORRT_ENABLE := Yes
else
  TENSORRT_ENABLE := No
endif


#####################
#### PKG VERSION ####
#####################

# Get version from changelog as well as lsb_release
# to compute unique build version (useful for repo mirroring systems doing file pooling)
PKG_VERSION  := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)
NULL  :=
SPACE := $(NULL) #
DOT   := .
COMA  := ,
CUDA_CAP_REMOVE_DOT := $(subst $(DOT),$(NULL),$(CUDA_CAP))
CUDA_CAP_DOT_SEP    := $(subst $(COMA),$(DOT),$(CUDA_CAP_REMOVE_DOT))
PKG_VERSION_TAG	    := $(PKG_VERSION)+$(DIST_NAME).$(DIST_RELEASE).$(DIST_VERSION)+Cuda.$(CUDA_VERSION).Levels.$(CUDA_CAP_DOT_SEP)

# Distrib with CUDA 7.5 or 8.0 will use 4.9
# Newer will use 6.4
ifeq ($(shell dpkg --compare-versions $(CUDA_VERSION) ge 9.0 && echo yes),yes)
  CUDA_CC	:= $(shell readlink -f /usr/bin/gcc-6)
else
  CUDA_CC	:= $(shell readlink -f /usr/bin/gcc-4.9)
endif


#################
#### MKL DNN ####
#################

# If enabled, set MKL DNN to the fake path we're going to build
export TF_MKL_ROOT=$(CURDIR)/mkldnn


#######################
#### BAZEL OPTIONS ####
#######################

# Verbose when failing
BAZEL_DEF_OPTS := --verbose_failures
# Concurrency setting in Bazel according to DEB_BUILD_OPTIONS
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    CONCURRENT_JOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    BAZEL_DEF_OPTS += --jobs $(CONCURRENT_JOBS)
endif
# Enable compile opt flags if set (like -mavx2)
BAZEL_DEF_OPTS += --config=opt
BAZEL_CUDA_OPTS := --config=cuda
BAZEL_MKL_OPTS := --config=mkl


###################################
#### CONFIGURE WRAPPER OPTIONS ####
###################################

CONF_DEF_OPTS := --silent
CONF_DEF_OPTS += --with-jemalloc
CONF_DEF_OPTS += --with-google-cloud-platform
CONF_DEF_OPTS += --with-hdfs
CONF_DEF_OPTS += --with-amazon-s3
CONF_DEF_OPTS += --with-kafka
CONF_DEF_OPTS += --with-xla-jit
CONF_DEF_OPTS += --with-gdr

CONF_CUDA_OPTS := --with-cuda
CONF_CUDA_OPTS += --cuda-version $(CUDA_VERSION)
CONF_CUDA_OPTS += --cuda-path $(CURDIR)/cuda
CONF_CUDA_OPTS += --cuda-cudnn-version $(CUDNN_MAJOR_VERSION)
CONF_CUDA_OPTS += --cuda-cudnn-path $(CURDIR)/cuda
CONF_CUDA_OPTS += --cuda-nccl-version $(NCCL_MAJOR_VERSION)
CONF_CUDA_OPTS += --cuda-capabilities $(CUDA_CAP)
CONF_CUDA_OPTS += --cuda-gcc-path $(CUDA_CC)
ifeq ($(TENSORRT_ENABLE),Yes)
CONF_CUDA_OPTS += --cuda-with-tensorrt
CONF_CUDA_OPTS += --cuda-tensorrt-path /usr/lib/x86_64-linux-gnu 
endif
# OpenMPI should work without CUDA but it does not
# https://github.com/tensorflow/tensorflow/issues/14811 
ifeq ($(OPENMPI_ENABLE),Yes)
CONF_CUDA_OPTS += --with-mpi
CONF_CUDA_OPTS += --mpi-path $(OPENMPI_BASE)
endif


build-info:
	@echo ""
	@echo "############################################################"
	@echo "# DEB_BUILD_OPTIONS:   $(DEB_BUILD_OPTIONS)"
	@echo "# BAZEL_DEF_OPTS:      $(BAZEL_DEF_OPTS)"
	@echo "# OPENMPI_ENABLE:      $(OPENMPI_ENABLE)"
	@echo "# CUDA_VERSION:        $(CUDA_VERSION)"
	@echo "# CUDA_CAP:            $(CUDA_CAP)"
	@echo "# CUDNN_MAJOR_VERSION: $(CUDNN_MAJOR_VERSION)"
	@echo "# TENSORBOARD:         >= $(TENSORBOARD_MIN_VERSION), << $(TENSORBOARD_MAX_VERSION)"
	@echo "############################################################"
	@echo ""
	@sleep 2

%:
	dh $@ --with python2,python3

cuda:
ifeq (,$(filter pkg.python-tensorflow-cuda.no-cuda,$(DEB_BUILD_PROFILES)))
	debian/create-cuda-tree.sh "$@"
endif


mkldnn:	
ifeq (,$(filter pkg.python-tensorflow-cuda.no-mkl-dnn,$(DEB_BUILD_PROFILES)))
	debian/create-mkldnn-tree.sh "$@"
endif

override_dh_auto_configure: build-info cuda mkldnn

### Build for supported Python 2/3 versions
override_dh_auto_install: $(PY3VERS:%=build-py%) $(PY2VERS:%=build-py%)

# So what's the hell is going here ?
#
# I'm going to build several time tensorflow with different
# GCC optimization flags.
#
# nVidia CUDA support variants will be built too.
#
# build_pip_package script has been patched to output classic Python
# package without the WHL container.
build-py%:
	# Will rename the generated .so file with proper suffix
	# So different python3 versions can live together
	# Also see "debian/patches/Patch_so_loader_to_use_soabi_version"
	# To see how tensorflow C extensions loader has been patched
	$(eval PYTHON_C_EXT := $(shell /usr/bin/python$* -c 'from imp import get_suffixes, C_EXTENSION; best_suffix = [ suffix for suffix in get_suffixes() if suffix[2] == C_EXTENSION ][0][0]; print(best_suffix)'))
	# PKG_PREFIX will contains python or python3 depending on interpreter version
	$(eval PKG_PREFIX := $(shell /usr/bin/python$* -c 'import sys; print("python3" if sys.version[0] == "3" else "python")'))
	
	$(eval VARIANT := avx-sse42)
	# Ok Bazel shit as usual.
	# If set to a sub directory of $(CURDIR) it fails with ERROR: infinite symlink expansion detected
	# If set to same folder among multiple variant it randomly fails with undeclared inclusion(s) in rule errors
	# Configure script also call bazel so it has to be set to same folder as bazel build call
	$(eval BAZEL_HOME := /tmp/tensorflow-build-$*-$(VARIANT))
	@echo ""
	@echo "####################################################################################"
	@echo "# BAZEL_OPTS:      $(BAZEL_DEF_OPTS)"
	@echo "# CONFIGURE_OPTS:  $(CONF_DEF_OPTS) --opts-flags \"$(OPTFLAGS2_NO_CUDA)\""
	@echo "# PYTHON:          python$*"
	@echo "# VARIANT:         $(VARIANT)"
	@echo "####################################################################################"
	@echo ""
	$(eval DESTDIR := $(CURDIR)/debian/$(PKG_PREFIX)-tensorflow-$(VARIANT))
	rm -rf "$(BAZEL_HOME)/cache" || true
	HOME="$(BAZEL_HOME)/cache" debian/configure-expect.py ./configure $(TF_VERSION) $* $(CONF_DEF_OPTS) --opts-flags "$(OPTFLAGS2_NO_CUDA)"
	HOME="$(BAZEL_HOME)/cache" bazel build $(BAZEL_DEF_OPTS) //tensorflow/tools/pip_package:build_pip_package
	bazel-bin/tensorflow/tools/pip_package/build_pip_package "$(DESTDIR)"
	
ifeq (,$(filter pkg.python-tensorflow-cuda.no-cuda,$(DEB_BUILD_PROFILES)))
	$(eval VARIANT := cuda-avx-sse42)
	$(eval BAZEL_HOME := /tmp/tensorflow-build-$*-$(VARIANT))
	@echo ""
	@echo "####################################################################################"
	@echo "# BAZEL_OPTS:      $(BAZEL_DEF_OPTS) $(BAZEL_CUDA_OPTS)"
	@echo "# CONFIGURE_OPTS:  $(CONF_DEF_OPTS) $(CONF_CUDA_OPTS) --opts-flags \"$(OPTFLAGS2)\""
	@echo "# PYTHON:          python$*"
	@echo "# VARIANT:         $(VARIANT)"
	@echo "####################################################################################"
	@echo ""
	$(eval DESTDIR := $(CURDIR)/debian/$(PKG_PREFIX)-tensorflow-$(VARIANT))
	rm -rf "$(BAZEL_HOME)/cache" || true
	HOME="$(BAZEL_HOME)/cache" debian/configure-expect.py ./configure $(TF_VERSION) $* $(CONF_DEF_OPTS) $(CONF_CUDA_OPTS) --opts-flags "$(OPTFLAGS2)"
	HOME="$(BAZEL_HOME)/cache" bazel build $(BAZEL_DEF_OPTS) $(BAZEL_CUDA_OPTS) //tensorflow/tools/pip_package:build_pip_package
	bazel-bin/tensorflow/tools/pip_package/build_pip_package "$(DESTDIR)"
endif
	
	$(eval VARIANT := avx2-sse42-fma)
	$(eval BAZEL_HOME := /tmp/tensorflow-build-$*-$(VARIANT))
	@echo ""
	@echo "####################################################################################"
	@echo "# BAZEL_OPTS:      $(BAZEL_DEF_OPTS)"
	@echo "# CONFIGURE_OPTS:  $(CONF_DEF_OPTS) --opts-flags \"$(OPTFLAGS1)\""
	@echo "# PYTHON:          python$*"
	@echo "# VARIANT:         $(VARIANT)"
	@echo "####################################################################################"
	@echo ""
	$(eval DESTDIR := $(CURDIR)/debian/$(PKG_PREFIX)-tensorflow-$(VARIANT))
	rm -rf "$(BAZEL_HOME)/cache" || true
	HOME="$(BAZEL_HOME)/cache" debian/configure-expect.py ./configure $(TF_VERSION) $* $(CONF_DEF_OPTS) --opts-flags "$(OPTFLAGS1_NO_CUDA)"
	HOME="$(BAZEL_HOME)/cache" bazel build $(BAZEL_DEF_OPTS) //tensorflow/tools/pip_package:build_pip_package $(BAZEL_DEF_OPTS)
	bazel-bin/tensorflow/tools/pip_package/build_pip_package "$(DESTDIR)"
	
ifeq (,$(filter pkg.python-tensorflow-cuda.no-mkl-dnn,$(DEB_BUILD_PROFILES)))
	$(eval VARIANT := avx2-sse42-fma-mkl-dnn)
	$(eval BAZEL_HOME := /tmp/tensorflow-build-$*-$(VARIANT))
	@echo ""
	@echo "####################################################################################"
	@echo "# BAZEL_OPTS:      $(BAZEL_DEF_OPTS) $(BAZEL_MKL_OPTS)"
	@echo "# CONFIGURE_OPTS:  $(CONF_DEF_OPTS) --opts-flags \"$(OPTFLAGS1_NO_CUDA)\""
	@echo "# PYTHON:          python$*"
	@echo "# VARIANT:         $(VARIANT)"
	@echo "####################################################################################"
	@echo ""
	$(eval DESTDIR := $(CURDIR)/debian/$(PKG_PREFIX)-tensorflow-$(VARIANT))
	rm -rf "$(BAZEL_HOME)/cache" || true
	HOME="$(BAZEL_HOME)/cache" debian/configure-expect.py ./configure $(TF_VERSION) $* $(CONF_DEF_OPTS) --opts-flags "$(OPTFLAGS1_NO_CUDA)"
	HOME="$(BAZEL_HOME)/cache" bazel build $(BAZEL_DEF_OPTS) $(BAZEL_MKL_OPTS) //tensorflow/tools/pip_package:build_pip_package
	bazel-bin/tensorflow/tools/pip_package/build_pip_package "$(DESTDIR)"
endif
	
ifeq (,$(filter pkg.python-tensorflow-cuda.no-cuda,$(DEB_BUILD_PROFILES)))
	$(eval VARIANT := cuda-avx2-sse42-fma)
	$(eval BAZEL_HOME := /tmp/tensorflow-build-$*-$(VARIANT))
	@echo ""
	@echo ""
	@echo "####################################################################################"
	@echo "# BAZEL_OPTS:      $(BAZEL_DEF_OPTS) $(BAZEL_CUDA_OPTS)"
	@echo "# CONFIGURE_OPTS:  $(CONF_DEF_OPTS) $(CONF_CUDA_OPTS) --opts-flags \"$(OPTFLAGS1)\""
	@echo "# PYTHON:          python$*"
	@echo "# VARIANT:         $(VARIANT)"
	@echo "####################################################################################"
	$(eval DESTDIR := $(CURDIR)/debian/$(PKG_PREFIX)-tensorflow-$(VARIANT))
	rm -rf "$(BAZEL_HOME)/cache" || true
	HOME="$(BAZEL_HOME)/cache" debian/configure-expect.py ./configure $(TF_VERSION) $* $(CONF_DEF_OPTS) $(CONF_CUDA_OPTS) --opts-flags "$(OPTFLAGS1)"
	HOME="$(BAZEL_HOME)/cache" bazel build $(BAZEL_DEF_OPTS) $(BAZEL_CUDA_OPTS) //tensorflow/tools/pip_package:build_pip_package
	bazel-bin/tensorflow/tools/pip_package/build_pip_package "$(DESTDIR)"
	
ifeq (,$(filter pkg.python-tensorflow-cuda.no-mkl-dnn,$(DEB_BUILD_PROFILES)))
	$(eval VARIANT := cuda-avx2-sse42-fma-mkl-dnn)
	$(eval BAZEL_HOME := /tmp/tensorflow-build-$*-$(VARIANT))
	@echo ""
	@echo ""
	@echo "####################################################################################"
	@echo "# BAZEL_OPTS:      $(BAZEL_DEF_OPTS) $(BAZEL_CUDA_OPTS) $(BAZEL_MKL_OPTS)"
	@echo "# CONFIGURE_OPTS:  $(CONF_DEF_OPTS) $(CONF_CUDA_OPTS) --opts-flags \"$(OPTFLAGS1)\""
	@echo "# PYTHON:          python$*"
	@echo "# VARIANT:         $(VARIANT)"
	@echo "####################################################################################"
	$(eval DESTDIR := $(CURDIR)/debian/$(PKG_PREFIX)-tensorflow-$(VARIANT))
	rm -rf "$(BAZEL_HOME)/cache" || true
	HOME="$(BAZEL_HOME)/cache" debian/configure-expect.py ./configure $(TF_VERSION) $* $(CONF_DEF_OPTS) $(CONF_CUDA_OPTS) --opts-flags "$(OPTFLAGS1)"
	HOME="$(BAZEL_HOME)/cache" bazel build $(BAZEL_DEF_OPTS) $(BAZEL_CUDA_OPTS) $(BAZEL_MKL_OPTS) //tensorflow/tools/pip_package:build_pip_package
	bazel-bin/tensorflow/tools/pip_package/build_pip_package "$(DESTDIR)"
endif
endif
	
	# Common operations
	# Rename python .so file to add version suffix (libtensorflow_framework.so is not a Python module)
	find "$(CURDIR)"/debian/python*-tensorflow-*/usr/lib/python*/dist-packages -name '*.so' -a ! -name '*-linux-gnu.so' -exec /usr/bin/rename.ul --verbose  '.so' '$(PYTHON_C_EXT)' {} \;
	
	# Move scripts to tensorflow-tools package if python3 and if it is not there already
	if [ "$(PKG_PREFIX)" = "python3" ]; then \
	  mkdir -p "$(CURDIR)/debian/tensorflow-tools/usr/"; \
 	  test -d "$(CURDIR)/debian/tensorflow-tools/usr/bin/" || mv -v "$(DESTDIR)/usr/bin/" "$(CURDIR)/debian/tensorflow-tools/usr/"; \
	  rm -f "$(CURDIR)/debian/tensorflow-tools/usr/bin/tensorboard"; \
        fi
	
	# Remove useless files
	rm -rf "$(CURDIR)"/debian/python*-tensorflow-*/usr/lib/python*/dist-packages/external
	rm -rf "$(CURDIR)"/debian/python*-tensorflow-*/usr/lib/python*/dist-packages/_solib_*
	rm -rf "$(CURDIR)"/debian/python*-tensorflow-*/usr/bin
	
	# Headers permissions are a mess
	find "$(CURDIR)"/debian/python*-tensorflow-*/usr/lib/python*/dist-packages/tensorflow/include -type d -exec chmod 0755 {} \;
	find "$(CURDIR)"/debian/python*-tensorflow-*/usr/lib/python*/dist-packages/tensorflow/include -type f -exec chmod 0644 {} \;
	
	touch $@

override_dh_auto_clean:
	dh_auto_clean
	rm -f .bazelrc .tf_configure.bazelrc tensorflow/tools/git/gen/spec.json tools/python_bin_path.sh
	rm -f build-py*
	rm -f bazel-*
	rm -rf cuda
	rm -rf mkldnn
	rm -rf third_party/eigen3/mkl_include third_party/mkl/include third_party/mkl/libdl.so.2 third_party/mkl/libmklml_intel.so third_party/mkl/libiomp5.so third_party/mkl/mkl.config
	rm -rf third_party/mpi/mpicxx.h third_party/mpi/mpi.h third_party/mpi/mpio.h third_party/mpi/libmpi.so third_party/mpi/mpi_portable_platform.h
	rm -rf tensorflow/tools/git/gen/branch_ref tensorflow/tools/git/gen/head
	rm -rf tensorflow/python/framework/fast_tensor_util.cpp

override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)" $(SUBSTVARS_CUDNN) $(SUBSTVARS_PY2DEPS) $(SUBSTVARS_PY3DEPS)

# Those are Python independant libraries, do not rename it
# Otherwise libs linked against will break
# --exclude is supposed to work but I see no code handling
# this option in dh-python, lol :D
override_dh_python2:
	for pkg in $(PY2PACKAGES); do \
	  dh_python2 --exclude=libtensorflow_framework.so --exclude=libforestprotos.so --verbose --package=$${pkg}; \
	  for file in `find $(CURDIR)/debian/$${pkg}/usr/lib/python*/dist-packages/tensorflow/ -name 'libtensorflow_framework.*.so'`; do \
	    dirname=`dirname $${file}`; \
	    mv -v "$${file}" "$${dirname}/libtensorflow_framework.so"; \
	    chrpath --delete "$${dirname}/libtensorflow_framework.so"; \
	  done; \
	  for file in `find $(CURDIR)/debian/$${pkg}/usr/lib/python*/dist-packages/tensorflow/ -name 'libforestprotos.*.so'`; do \
	    dirname=`dirname $${file}`; \
	    mv -v "$${file}" "$${dirname}/libforestprotos.so"; \
	  done; \
	done
override_dh_python3:
	for pkg in $(PY3PACKAGES); do \
	  dh_python3 --verbose --package=$${pkg}; \
	  for file in `find $(CURDIR)/debian/$${pkg}/usr/lib/python*/dist-packages/tensorflow/ -name 'libtensorflow_framework.*.so'`; do \
	    dirname=`dirname $${file}`; \
	    mv -v "$${file}" "$${dirname}/libtensorflow_framework.so"; \
	    chrpath --delete "$${dirname}/libtensorflow_framework.so"; \
	  done; \
	  for file in `find $(CURDIR)/debian/$${pkg}/usr/lib/python*/dist-packages/tensorflow/ -name 'libforestprotos.*.so'`; do \
	    dirname=`dirname $${file}`; \
	    mv -v "$${file}" "$${dirname}/libforestprotos.so"; \
	  done; \
	done
