#!/usr/bin/make -f

export DH_VERBOSE=1
.SHELLFLAGS := -uec

include /usr/share/dpkg/pkg-info.mk

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=+lto

%:
	PREFIX=/usr dh $@ --with=python3

execute_after_dh_auto_clean:
	$(RM) debian/man/*.1

        # Remove gitignore'd files
	git --git-dir=debian/git-repo init
	git --git-dir=debian/git-repo --work-tree=. clean -Xdf \
		-e '!/abc' \
		-e '!/tests/simple_abc9/abc9.v'
	rm -rf debian/git-repo

	rm -f abc/abc-*

	# Help with transition from earlier versions of this package
	rm -rf debian/yosys-src.substvars debian/yosys-src/
	rm -rf debian/yosys-tests.substvars debian/yosys-tests/


override_dh_auto_configure:
	printf '%s\n' \
	  'CONFIG := gcc' \
	  'STRIP=:' \
	  > Makefile.conf

execute_after_dh_auto_install:
	chmod a-x debian/tmp/usr/share/yosys/achronix/speedster22i/cells*.v

execute_before_dh_installman:
	cd debian/man ; ./genmanpages.sh

override_dh_compress:
	dh_compress --exclude=.pdf

override_dh_auto_build-arch:
	dh_auto_build -- all

override_dh_auto_build-indep: export LC_ALL=C.UTF-8
override_dh_auto_build-indep: export TZ=UTC
override_dh_auto_build-indep: export FORCE_SOURCE_DATE=1
override_dh_auto_build-indep:
	dh_auto_build -- docs DOC_TARGET=latexpdf

override_dh_auto_test-indep: # no tests for docs

override_dh_auto_test-arch:
ifeq (big,$(shell dpkg-architecture -qDEB_TARGET_ARCH_ENDIAN))
	dh_auto_test -- PATH="$$PWD:$$PATH" && { \
	  echo 'Big-endian arches are considered "unusably broken" by upstream and tests are'; \
	  echo 'expected to be broken. Despite this the tests seem to have succeeded, is it time'; \
	  echo 'to revise this expectation?'; \
	  echo "See https://github.com/YosysHQ/yosys/issues/2645"; \
	  echo "Failing build artificially."; \
	  exit 1; \
	}
else
	dh_auto_test -- PATH="$$PWD:$$PATH"
endif