#!/usr/bin/make -f


#### CUDA version

CUDA_MAJOR_VER	:= $(shell dpkg-query -W -f='$${Version}' nvidia-cuda-toolkit | cut -d. -f1,2)
CUDA_MAJOR_VER_NEXT	:= $(shell python3 -c 'print(round(float('$(CUDA_MAJOR_VER)') + 0.1, 1))')

# Distrib with CUDA 7.5 or 8.0 will use 4.9
# CUDA 9.0/9.1 will use 5.x because 6.x is completely broken
# CUDA 9.2+ wants 7.x
ifeq ($(shell dpkg --compare-versions $(CUDA_MAJOR_VER) ge 10~ && echo yes),yes)
  CUDA_CC_PKG	:= gcc-7
  CUDA_CXX_PKG	:= g++-7
  LIBTHRUST_MIN_VER := 1.9.4~
else ifeq ($(shell dpkg --compare-versions $(CUDA_MAJOR_VER) ge 9.2~ && echo yes),yes)
  CUDA_CC_PKG	:= gcc-7
  CUDA_CXX_PKG	:= g++-7
  LIBTHRUST_MIN_VER := 1.9.2~
else ifeq ($(shell dpkg --compare-versions $(CUDA_MAJOR_VER) ge 9.0~ && echo yes),yes)
  CUDA_CC_PKG	:= gcc-5
  CUDA_CXX_PKG	:= g++-5
  LIBTHRUST_MIN_VER := 1.9.0~
else ifeq ($(shell dpkg --compare-versions $(CUDA_MAJOR_VER) ge 7.5~ && echo yes),yes)
  CUDA_CC_PKG	:= gcc-4.9
  CUDA_CXX_PKG	:= g++-4.9
  LIBTHRUST_MIN_VER := 1.8.1~
else
  $(error ERROR: Unsupported CUDA $(CUDA_MAJOR_VER). Please fix debian/rules)
endif

CUDA_CC		:= $(shell readlink -f /usr/bin/$(CUDA_CC_PKG))
CUDA_CXX	:= $(shell readlink -f /usr/bin/$(CUDA_CXX_PKG))

ifeq ($(CUDA_CC),)
  $(error ERROR: Path to proper CC not found, something went wrong)
endif
ifeq ($(CUDA_CXX),)
  $(error ERROR: Path to proper CXX not found, something went wrong)
endif

GCC_FULL_VER	:= $(shell dpkg-query -W -f='$${Version}' $(CUDA_CC_PKG))



#### CUDA Compute levels

ifeq ($(CUDA_MAJOR_VER),10.1)
  CUDA_CAP_SPACE_SEP	:= 3.0 3.5 3.7 5.0 5.2 5.3 6.0 6.1 7.0 7.5
else ifeq ($(CUDA_MAJOR_VER),10.0)
  CUDA_CAP_SPACE_SEP	:= 3.0 3.5 3.7 5.0 5.2 5.3 6.0 6.1 7.0 7.5
else ifeq ($(CUDA_MAJOR_VER),9.2)
  CUDA_CAP_SPACE_SEP	:= 3.0 3.5 3.7 5.0 5.2 5.3 6.0 6.1 7.0
else ifeq ($(CUDA_MAJOR_VER),9.1)
  CUDA_CAP_SPACE_SEP	:= 3.0 3.5 3.7 5.0 5.2 5.3 6.0 6.1 7.0
else ifeq ($(CUDA_MAJOR_VER),9.0)
  CUDA_CAP_SPACE_SEP	:= 3.0 3.5 3.7 5.0 5.2 5.3 6.0 6.1 7.0
else ifeq ($(CUDA_MAJOR_VER),8.0)
  CUDA_CAP_SPACE_SEP	:= 3.0 3.5 3.7 5.0 5.2 5.3 6.0 6.1
else ifeq ($(CUDA_MAJOR_VER),7.5)
  CUDA_CAP_SPACE_SEP	:= 3.0 3.5 3.7 5.0 5.2 5.3
else
  $(error Unsupported CUDA $(CUDA_MAJOR_VER). Please fix debian/rules)
endif
NULL  :=
SPACE := $(NULL) #
DOT := .
COMA := ,
SEMICOLON := ;
CUDA_CAP_NO_DOT_SPACE_SEP := $(subst $(DOT),$(NULL),$(CUDA_CAP_SPACE_SEP))
CUDA_CAP_NO_DOT_DOT_SEP := $(subst $(SPACE),$(DOT),$(CUDA_CAP_NO_DOT_SPACE_SEP))
CUDA_CAP_COMA_SEP := $(subst $(SPACE),$(COMA),$(CUDA_CAP_SPACE_SEP))
CUDA_CAP_NO_DOT_COMA_SEP := $(subst $(SPACE),$(COMA),$(CUDA_CAP_NO_DOT_SPACE_SEP))
CUDA_CAP_SEMICOLON_SEP := $(subst $(SPACE),$(SEMICOLON),$(CUDA_CAP_SPACE_SEP))
CUDA_CAP_NO_DOT_SEMICOLON_SEP := $(subst $(SPACE),$(SEMICOLON),$(CUDA_CAP_NO_DOT_SPACE_SEP))


#### Distribution version

PKG_VERSION 	:= $(shell dpkg-parsechangelog | egrep '^Version:' | cut -d' ' -f2)
DIST_NAME	:= $(shell lsb_release -si)
DIST_RELEASE	:= $(shell lsb_release -sc)
DIST_VERSION	:= $(shell lsb_release -sr)


#### Define useful variables

SVARS_CUDTLK	:= -Vcudatlk:Depends="nvidia-cuda-toolkit (>= $(CUDA_MAJOR_VER)~), nvidia-cuda-toolkit (<< $(CUDA_MAJOR_VER_NEXT)~),"
ifeq ($(shell dpkg --compare-versions $(GCC_FULL_VER) ge 5.5~ && echo yes),yes)  # Buggy GCC 5.5
  ifeq ($(shell dpkg --compare-versions $(GCC_FULL_VER) lt 5.6~ && echo yes),yes)
    GCC_WORKAROUND_PKG := , gcc-5.5-avx512-workaround
  endif
endif
SVARS_CUDACC	:= -Vcudacc:Depends="$(CUDA_CC_PKG), $(CUDA_CXX_PKG) $(GCC_WORKAROUND_PKG)"

SVARS_LIBTHRUST	:= -Vlibthrust:Depends="libthrust-dev (>= $(LIBTHRUST_MIN_VER))"  # No logic for this so...
SVARS_LIBCUPTI	:= -Vlibcupti:Depends="libcupti-dev (>= $(CUDA_MAJOR_VER)~)"

PKG_VERSION_TAG	:= $(PKG_VERSION)+$(DIST_NAME).$(DIST_VERSION).$(DIST_RELEASE)


%:
	dh $@

debian/control: debian/control.in
	sed 's/@@CUDA_MAJOR_VER@@/$(CUDA_MAJOR_VER)/g' debian/control.in > debian/control

override_dh_auto_clean: debian/control
	dh_auto_clean

override_dh_auto_build:
	$(info )
	$(info INFO: CUDA $(CUDA_MAJOR_VER) detected)
	$(info INFO: CUDA_CC set to $(CUDA_CC))
	$(info INFO: CUDA_CXX set to $(CUDA_CXX))
	$(info )

override_dh_auto_install:
	mkdir -p debian/cuda-toolchain-$(CUDA_MAJOR_VER)/usr/share/cuda-toolchain
	sed -e 's!@@CUDA_MAJOR_VER@@!$(CUDA_MAJOR_VER)!' \
	    -e 's!@@CUDA_CC@@!$(CUDA_CC)!' \
	    -e 's!@@CUDA_CXX@@!$(CUDA_CXX)!' \
	    -e 's!@@CUDA_CAP_SPACE_SEP@@!$(CUDA_CAP_SPACE_SEP)!' \
	    -e 's!@@CUDA_CAP_COMA_SEP@@!$(CUDA_CAP_COMA_SEP)!' \
	    -e 's!@@CUDA_CAP_SEMICOLON_SEP@@!$(CUDA_CAP_SEMICOLON_SEP)!' \
	    -e 's!@@CUDA_CAP_NO_DOT_SPACE_SEP@@!$(CUDA_CAP_NO_DOT_SPACE_SEP)!' \
	    -e 's!@@CUDA_CAP_NO_DOT_DOT_SEP@@!$(CUDA_CAP_NO_DOT_DOT_SEP)!' \
	    -e 's!@@CUDA_CAP_NO_DOT_COMA_SEP@@!$(CUDA_CAP_NO_DOT_COMA_SEP)!' \
	    -e 's!@@CUDA_CAP_NO_DOT_SEMICOLON_SEP@@!$(CUDA_CAP_NO_DOT_SEMICOLON_SEP)!' \
	    cuda-toolchain.mk.in > debian/cuda-toolchain-$(CUDA_MAJOR_VER)/usr/share/cuda-toolchain/cuda-toolchain.mk

override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)" $(SVARS_CUDTLK) $(SVARS_CUDACC) $(SVARS_LIBTHRUST) $(SVARS_LIBCUPTI)

.PHONY: debian/control
