#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

BLDDIR = debian/cmake
TMPDIR = debian/tmp
DIST := $(shell dpkg-parsechangelog | awk '/^Distribution: / {print $$2}')

# Additional compiler/linker flags to avoid FTBFS on Ubuntu 12.04 and 14.04.
# ("-O2" is not essential for building, just avoids too aggressive optimizations.)
ifeq ($(DIST),precise)
EXTRA_FLAGS = -DSPECIAL_COMPILE_FLAGS:STRING="-Wno-error=strict-overflow -O2"
endif
ifeq ($(DIST),trusty)
EXTRA_FLAGS = -DEXTRA_OSLEXEC_LIBRARIES:STRING="tinfo"
endif

%:
	dh $@ -Scmake -B$(BLDDIR) --parallel

override_dh_auto_configure:
	# Note: currently all tests pass on OS X but a few tests fail on Linux
	#  strictly for floating point precision reasons, not because anything
	#  is really broken.
	# Note2: graphics driver based on Gallium3D also uses LLVM, so we needs
	#  to use LLVM static library to avoid symbol conflicts.
	dh_auto_configure -- \
		-DBUILD_TESTING:BOOL=OFF \
		-DLLVM_STATIC:BOOL=ON \
		$(EXTRA_FLAGS)
	# A patch 020_fix_implicit_include changes the implicit include path to
	# stdosl.h from DESTDIR/shaders to DESTDIR/share/OSL-1.4/shaders.
	mkdir -p $(BLDDIR)/src/share/OSL-1.4
	ln -sf ../../shaders $(BLDDIR)/src/share/OSL-1.4

override_dh_auto_install:
	dh_auto_install
	# CMAKE_SKIP_RPATH cannot be used because it causes build failure.
	chrpath -d $(TMPDIR)/usr/bin/* $(TMPDIR)/usr/lib/lib*.so

override_dh_installchangelogs:
	dh_installchangelogs CHANGES
