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

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

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

SKIP_TESTS=long
#internal compiler not working yet
SKIP_TESTS+=compile
#poco feature unsuported in current debian release
SKIP_TESTS+=00416
#base64
SKIP_TESTS+=00732
#flaky test
SKIP_TESTS+=00417

#tests affected by SSE disable
SKIP_TESTS+=00120 00161 00212 00232 00273 00284 00506 00647 00704 00720 00800

#fails on buildd O_DIRECT?
SKIP_TESTS+=00750

ifeq ($(DEB_HOST_ARCH),ppc64el)
	SKIP_TESTS+=00047
endif

#likely rounding problem. See Bug#1023141
SKIP_TESTS+=00700_decimal_math

# another rounding problem, apparently… but only on arm64. See Bug#1023141
ifeq ($(DEB_HOST_ARCH),arm64)
	SKIP_TESTS+=00047_stored_aggregates_complex
endif

#
# LP: #1840511 cleanup
#

# libm precision on exp() related functions
SKIP_TESTS+=00536 00534

# long_http_bufferization ?
SKIP_TESTS+=00429

# system processes port ? (bind: error already in use)
SKIP_TESTS+=00379

# clickhouse-clang not found
SKIP_TESTS+=00281

# end of LP: #1840511 cleanup

# Python3 porting, skip failing tests (we're gonna have to work on them at some point, just not now)
SKIP_TESTS+=00730 00646 00612 00309

export TEST_OPT=--skip ${SKIP_TESTS}

ifdef DH_VERBOSE
	TEST_ARGS=--verbose
endif

#exclude with_server test on archs other than listed ones
# test_unit_tests_dbms hangs. See Bug#1023145
ifeq (,$(filter $(DEB_HOST_ARCH),amd64 ppc64el))
	TEST_ARGS+=-E '(with_server|test_unit_tests_dbms)'
else
	TEST_ARGS+=-E 'test_unit_tests_dbms'
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    TEST_ARGS+=--parallel $(NUMJOBS) -VV
else
    TEST_ARGS+=--parallel $(nproc) -VV
endif

CMAKE_FLAGS = -DUNBUNDLED=1 -DUSE_STATIC_LIBRARIES=0 USE_UNWIND=0 -DCLICKHOUSE_SPLIT_BINARY=1 -DVERSION_DESCRIBE=$(shell dpkg-parsechangelog -S Version)

ifeq ($(DEB_HOST_ARCH),amd64)
	CMAKE_FLAGS+=-DENABLE_IPO=1
endif

%:
	dh $@  --buildsystem=cmake

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_FLAGS)

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	(cd obj-* ; ctest ARGS+=$(TEST_ARGS) )
endif
