#!/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- |  cut -d'~' -f2- | rev)


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

PY3VERS	:= $(shell py3versions -rv)
# 1.13 release brought 3.7
ifeq ($(shell dpkg --compare-versions $(TF_VERSION) lt 1.13~ && echo yes),yes)
    PY3VERS	:= $(filter-out 3.7,$(PY3VERS))
endif

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 python3custom:Depends substvars
#
# Tensorflow 1.13.1 release:
#  * Add tensorflow_estimator >= 1.13.0, < 1.14.0rc0'
#
# Tensorflow 1.12.0 release:
# * Add keras_applications >= 1.0.6
# * Add keras_preprocessing >= 1.0.5
#
# Tensorflow 1.11.0 release:
# * Bump protobuf to >= 3.6.0, it is failing with older
# 
# Tensorflow 1.9.0 release:
# * Still nothing new
#
# 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

PY3MINVER := $(shell py3versions -rv | rev | cut -d' ' -f1 | rev)
PY3MAXSUP := $(shell py3versions -rv | cut -d' ' -f1)
PY3MAXVER := $(shell python3 -c 'print(round(float('$(PY3MAXSUP)') + 0.1, 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)
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)
endif

TENSORBOARD_MIN_VERSION = $(shell python3 -c 'split=[int(x) for x in "$(TF_VERSION)".split(".")]; print("%d.%d.%d~" % (split[0], split[1], 0))')
TENSORBOARD_MAX_VERSION = $(shell python3 -c 'split=[int(x) for x in "$(TF_VERSION)".split(".")]; print("%d.%d.%d~" % (split[0], split[1]+1, 0))')
# For rc version needing previous tensorboard
#TENSORBOARD_MIN_VERSION = $(shell python3 -c 'split=[int(x) for x in "$(TF_VERSION)".split(".")]; print("%d.%d.%d~" % (split[0], split[1]-1, 0))')
#TENSORBOARD_MAX_VERSION = $(shell python3 -c 'split=[int(x) for x in "$(TF_VERSION)".split(".")]; print("%d.%d.%d~" % (split[0], split[1]+1, 0))')

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.6.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-keras-applications (>= 1.0.6~),
SUBSTVARS_PY3DEPS += python3-keras-preprocessing (>= 1.0.5~),
SUBSTVARS_PY3DEPS += python3-tensorboard (>= $(TENSORBOARD_MIN_VERSION)),
SUBSTVARS_PY3DEPS += python3-tensorboard (<< $(TENSORBOARD_MAX_VERSION)),
SUBSTVARS_PY3DEPS += python3-tensorflow-estimator (>= $(TENSORBOARD_MIN_VERSION)),
SUBSTVARS_PY3DEPS += python3-tensorflow-estimator (<< $(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
ifeq (,$(filter pkg.python-tensorflow-cuda.no-cuda,$(DEB_BUILD_PROFILES)))
  # cuda-toolchain helper !
  include /usr/share/cuda-toolchain/cuda-toolchain.mk

  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)
  SUBSTVARS_CUDNN := -Vcudnn:Depends="$(CUDNN_PACKAGE) (= $(CUDNN_VERSION))"
  SUBSTVARS_CUPTI := -Vcupti:Depends="libcupti$(CUDA_MAJOR_VER)"

  # NVIDIA TensorRT
  ifeq ($(shell dpkg --compare-versions $(CUDA_MAJOR_VER) ge 9.2 && echo yes),yes)
    TENSORRT_ENABLE := Yes
  else
    TENSORRT_ENABLE := No
  endif
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 | grep -E '^Version:' | cut -d' ' -f2)
PKG_UPSTREAM_VERSION := $(shell echo $(PKG_VERSION) | cut -d'-' -f1)
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)
ifeq (,$(filter pkg.python-tensorflow-cuda.no-cuda,$(DEB_BUILD_PROFILES)))
  PKG_VERSION_TAG     := $(PKG_VERSION)+$(DIST_NAME).$(DIST_VERSION).$(DIST_RELEASE)+Cuda.$(CUDA_MAJOR_VER).Levels.$(CUDA_CAP_NO_DOT_DOT_SEP)
else
  PKG_VERSION_TAG := $(PKG_VERSION)+$(DIST_NAME).$(DIST_VERSION).$(DIST_RELEASE)
endif
SUBSTVARS_BINARY_VERSION := -Vbinary:Version="$(PKG_VERSION_TAG)"
SUBSTVARS_SOURCE_VERSION := -Vsource:Version="$(PKG_VERSION_TAG)"


#################
#### 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 --define grpc_no_ares=true
# 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
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 :=
CONF_DEF_OPTS += --with-apache-ignite
#CONF_DEF_OPTS += --with-jemalloc
CONF_DEF_OPTS += --with-google-cloud-platform
CONF_DEF_OPTS += --with-hdfs
CONF_DEF_OPTS += --with-amazon-aws
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_MAJOR_VER)
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-nccl-path /usr
CONF_CUDA_OPTS += --cuda-capabilities $(CUDA_CAP_COMA_SEP)
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_MAJOR_VER)"
	@echo "# CUDA_CAP:            $(CUDA_CAP_COMA_SEP)"
	@echo "# CUDNN_MAJOR_VERSION: $(CUDNN_MAJOR_VERSION)"
	@echo "# TENSORBOARD:         >= $(TENSORBOARD_MIN_VERSION), << $(TENSORBOARD_MAX_VERSION)"
	@echo "############################################################"
	@echo ""
	@sleep 2

%:
	dh $@ --with 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 3 versions
override_dh_auto_install: $(PY3VERS:%=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 .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:
	# Because shlibsdeps is not picking libmkl dependencies, not sure why
	$(eval SUBSTVARS_MKL := -Vmklcustom:Depends="$(shell debian/generate-mkl-dependencies.py debian)")
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)" $(SUBSTVARS_BINARY_VERSION) $(SUBSTVARS_SOURCE_VERSION) $(SUBSTVARS_CUDNN) $(SUBSTVARS_CUPTI) $(SUBSTVARS_PY3DEPS) $(SUBSTVARS_MKL)

# 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_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

.PHONY: cuda mkldnn
